-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
StorageDb.php
197 lines (171 loc) · 4.68 KB
/
StorageDb.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
<?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\Connection;
/**
* StorageDb uses database table for the config values storage.
*
* Database migration example:
*
* ```php
* Schema::create('configs', function (Blueprint $table) {
* $table->string('key')->primary();
* $table->string('value')->nullable();
* });
* ```
*
* Instantiation example:
*
* ```php
* use Illuminatech\Config\StorageDb;
* use Illuminate\Support\Facades\App;
*
* $storage = (new StorageDb(App::make('db.connection')))
* ->setTable('configs')
* ->setKeyColumn('key')
* ->setValueColumn('value')
* ->setFilter(['category_id' => 'global']);
* ```
*
* > Note: this storage requires "illuminate/database" package installed.
*
* @see \Illuminatech\Config\StorageEloquent
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class StorageDb implements StorageContract
{
/**
* @var string name of the table, which should store values.
*/
public $table = 'configs';
/**
* @var string name of the column, which should store config item key.
*/
public $keyColumn = 'key';
/**
* @var string name of the column, which should store config item value.
*/
public $valueColumn = 'value';
/**
* @var array filter condition for records query restriction.
* @see \Illuminate\Database\Query\Builder::where()
*/
public $filter = [];
/**
* @var \Illuminate\Database\Connection the DB connection object.
*/
protected $connection;
/**
* Constructor.
*
* @param \Illuminate\Database\Connection $connection DB connection to be used.
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* {@inheritDoc}
*/
public function save(array $values): bool
{
$existingValues = $this->get();
foreach ($values as $key => $value) {
if (array_key_exists($key, $existingValues)) {
if ($value === $existingValues[$key]) {
continue;
}
$this->connection->table($this->table)
->where($this->filter)
->where([$this->keyColumn => $key])
->update([$this->valueColumn => $value]);
} else {
$this->connection->table($this->table)
->insert(array_merge(
$this->filter,
[
$this->keyColumn => $key,
$this->valueColumn => $value,
]
));
}
}
return true;
}
/**
* {@inheritDoc}
*/
public function get(): array
{
return $this->connection->table($this->table)
->where($this->filter)
->pluck($this->valueColumn, $this->keyColumn)
->toArray();
}
/**
* {@inheritDoc}
*/
public function clear(): bool
{
$this->connection->table($this->table)
->where($this->filter)
->delete();
return true;
}
/**
* {@inheritDoc}
*/
public function clearValue($key): bool
{
$this->connection->table($this->table)
->where($this->filter)
->where([$this->keyColumn => $key])
->delete();
return true;
}
// Self Configure :
/**
* @param string $table name of the table, which should store values.
* @return static self reference.
*/
public function setTable(string $table): self
{
$this->table = $table;
return $this;
}
/**
* @param string $keyColumn name of the column, which should store config item key.
* @return static self reference.
*/
public function setKeyColumn(string $keyColumn): self
{
$this->keyColumn = $keyColumn;
return $this;
}
/**
* @param string $valueColumn name of the column, which should store config item value.
* @return static self reference.
*/
public function setValueColumn(string $valueColumn): self
{
$this->valueColumn = $valueColumn;
return $this;
}
/**
* @see \Illuminate\Database\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;
}
}