-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStorageEloquent.php
201 lines (180 loc) · 4.8 KB
/
StorageEloquent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @link https://github.com/illuminatech
* @copyright Copyright (c) 2019 Illuminatech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace Illuminatech\Config;
use Illuminate\Database\Eloquent\Model;
/**
* StorageEloquent uses Eloquent model for the config values storage.
*
* This storage has degraded performance in comparison to `StorageDb`, but provides more flexibility, allowing usage
* Eloquent features like events.
*
* Database migration example:
*
* ```php
* Schema::create('configs', function (Blueprint $table) {
* $table->bigIncrements('id');
* $table->string('key')->index();
* $table->string('value')->nullable();
* // ...
* });
* ```
*
* Model example:
*
* ```php
* class Config extends \Illuminate\Database\Eloquent\Model
* {
* protected $fillable = [
* 'key',
* 'value',
* ];
*
* // ...
* }
* ```
*
* Instantiation example:
*
* ```php
* use App\Models\Config;
* use Illuminatech\Config\StorageEloquent;
*
* $storage = (new StorageEloquent(Config::class)
* ->setKeyAttribute('key')
* ->setValueAttribute('value')
* ->setFilter(['category_id' => 'global']);
* ```
*
* > Note: this storage requires "illuminate/database" package installed.
*
* @see \Illuminate\Database\Eloquent\Model
* @see \Illuminatech\Config\StorageDb
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class StorageEloquent implements StorageContract
{
/**
* @var string|\Illuminate\Database\Eloquent\Model name of the Eloquent model class, which should store the config values.
*/
public $model;
/**
* @var string name of the model attribute, which should store config item key.
*/
public $keyAttribute = 'key';
/**
* @var string name of the model attribute, which should store config item value.
*/
public $valueAttribute = 'value';
/**
* @var array filter condition for records query restriction.
* @see \Illuminate\Database\Eloquent\Builder::where()
*/
public $filter = [];
/**
* Constructor.
*
* @param string|null $model name of the Eloquent model class, which should store the config values.
*/
public function __construct(string $model = null)
{
$this->model = $model;
}
/**
* {@inheritDoc}
*/
public function save(array $values): bool
{
foreach ($values as $key => $value) {
$this->model::query()
->updateOrCreate(
array_merge(
$this->filter,
[
$this->keyAttribute => $key,
]
),
array_merge(
$this->filter,
[
$this->keyAttribute => $key,
$this->valueAttribute => $value,
]
)
);
}
return true;
}
/**
* {@inheritDoc}
*/
public function get(): array
{
return $this->model::query()
->where($this->filter)
->pluck($this->valueAttribute, $this->keyAttribute)
->toArray();
}
/**
* {@inheritDoc}
*/
public function clear(): bool
{
$this->model::query()
->where($this->filter)
->get()
->each(function (Model $model) {
$model->delete();
});
return true;
}
/**
* {@inheritDoc}
*/
public function clearValue($key): bool
{
$this->model::query()
->where($this->filter)
->where([$this->keyAttribute => $key])
->get()
->each(function (Model $model) {
$model->delete();
});
return true;
}
// Self Configure :
/**
* @param string $keyAttribute name of the model attribute, which should store config item key.
* @return static self reference.
*/
public function setKeyAttribute(string $keyAttribute): self
{
$this->keyAttribute = $keyAttribute;
return $this;
}
/**
* @param string $valueAttribute name of the model attribute, which should store config item value.
* @return static self reference.
*/
public function setValueAttribute(string $valueAttribute): self
{
$this->valueAttribute = $valueAttribute;
return $this;
}
/**
* @see \Illuminate\Database\Eloquent\Builder::where()
*
* @param array $filter filter condition for records query restriction.
* @return static self reference.
*/
public function setFilter($filter): self
{
$this->filter = $filter;
return $this;
}
}