forked from rafalkot/yii2-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.php
executable file
·264 lines (229 loc) · 6.95 KB
/
Settings.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace rafalkot\yii2settings;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\db\Connection;
use yii\db\Query;
use yii\helpers\Json;
/**
* Settings component class.
*
* Provides an easy access to app configuration stored in database.
*
* @package rafalkot\yii2settings
*/
class Settings extends Component
{
/**
* @var string DB component ID
*/
public $db = 'db';
/**
* @var array Categories to be preloaded on init
*/
public $preLoad = [];
/**
* @var array Loaded settings
*/
protected $items = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!empty($this->preLoad)) {
$this->load($this->preLoad);
}
}
/**
* Returns settings.
*
* @param string $category Category name
* @param string|array $key Single or multiple keys in array
* @param mixed $default Default value when setting does not exist
* @return mixed Setting value
* @throws Exception
*/
public function get($category, $key = null, $default = null)
{
$this->load($category);
if ($key === null) {
return isset($this->items[$category]) && !empty($this->items[$category]) ? $this->items[$category] : $default;
}
if (is_string($key)) {
return isset($this->items[$category][$key]) ? $this->items[$category][$key] : $default;
}
if (is_array($key)) {
$result = [];
foreach ($key as $val) {
$result[$val] = isset($this->items[$category][$val]) ? $this->items[$category][$val] : (is_array($default) && isset($default[$val]) ? $default[$val] : null);
}
return $result;
}
return $default;
}
/**
* Saves settings
*
* @param string $category Category name
* @param mixed $key Setting key or array of settings ie.: ['key' => value'', 'key2' => 'value2']
* @param mixed $value Setting value
* @throws Exception
*/
public function set($category, $key, $value = null)
{
if (is_string($key)) {
$encodedVal = Json::encode($value);
$this->get($category, $key, null);
if (array_key_exists($key, $this->items[$category])) {
$this->dbUpdate($category, $key, $encodedVal);
} else {
$this->dbInsert($category, $key, $encodedVal);
}
$this->items[$category][$key] = $value;
}
if (is_array($key)) {
foreach ($key as $idx => $val) {
$this->set($category, $idx, $val);
}
}
}
/**
* Removes settings
*
* @param string $category Category name
* @param array|string|null $key Setting key, keys array or null to delete all settings from category
* @throws Exception
*/
public function remove($category, $key = null)
{
if ($key === null) {
$this->dbDelete($category);
if (isset($this->items[$category])) {
unset($this->items[$category]);
}
}
if (is_string($key)) {
$this->dbDelete($category, $key);
if (isset($this->items[$category][$key])) {
unset($this->items[$category][$key]);
}
}
if (is_array($key)) {
foreach ($key as $idx => $val) {
$this->remove($category, $key);
}
}
}
/**
* Loads settings from category or multiple categories
* @param array|string $categories Category name
*/
public function load($categories)
{
if (is_string($categories)) {
$categories = [$categories];
}
foreach ($categories as $idx => $category) {
if (isset($this->items[$category])) {
unset($categories[$idx]);
} else {
$this->items[$category] = [];
}
}
if (empty($categories)) {
return;
}
$result = (new Query())->select(['category', 'key', 'value'])->from('setting')->where([
'category' => $categories
])->all();
foreach ($result as $row) {
try {
$this->items[$row['category']][$row['key']] = Json::decode($row['value']);
} catch (InvalidParamException $ex) {
$this->items[$row['category']][$row['key']] = $row['value'];
}
}
}
/**
* @return Connection Database connection
*/
protected function getDb()
{
return \Yii::$app->{$this->db};
}
/**
* Stores settings in database
*
* @param string $category Category name
* @param string $key Setting key
* @param string $value Setting value
* @throws \yii\db\Exception
*/
protected function dbInsert($category, $key, $value)
{
$created_by = \Yii::$app->user->getId();
$this->getDb()->createCommand('
INSERT INTO
{{setting}} (`category`, `key`, `value`, `created_at`, `created_by`)
VALUES
(:category, :key, :value, NOW(), :created_by)
', compact('category', 'key', 'value', 'created_by'))->execute();
}
/**
* Updates setting in database
*
* @param string $category Category name
* @param string $key Setting key
* @param string $value Setting value
* @throws \yii\db\Exception
*/
protected function dbUpdate($category, $key, $value)
{
$updated_by = \Yii::$app->user->getId();
$this->getDb()->createCommand('
UPDATE
{{setting}}
SET
`value` = :value,
`updated_at` = NOW(),
`updated_by` = :updated_by
WHERE
category = :category
AND `key` = :key
', compact('category', 'key', 'value', 'updated_by'))->execute();
}
/**
* Deletes setting from database
*
* @param string|array $category Category name(s)
* @param string|array|null $key Setting key(s) or null to delete all values from category
* @throws \yii\db\Exception
*/
protected function dbDelete($category, $key = null)
{
if ($key === null) {
$this->getDb()->createCommand('
DELETE FROM
{{setting}}
WHERE
`category` = :category
', compact('category'))->execute();
}
if (is_string($key)) {
$this->getDb()->createCommand('
DELETE FROM
{{setting}}
WHERE
`category` = :category
AND `key` = :key
', compact('category', 'key'))->execute();
}
if (is_array($key)) {
foreach ($key as $val) {
$this->dbDelete($category, $val);
}
}
}
}