1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link http://phe.me |
5
|
|
|
* @copyright Copyright (c) 2014 Pheme |
6
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace pheme\settings\components; |
10
|
|
|
|
11
|
|
|
use yii\base\Component; |
12
|
|
|
use yii\caching\Cache; |
13
|
|
|
use Yii; |
14
|
|
|
use yii\helpers\Json; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Aris Karageorgos <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Settings extends Component |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string settings model. Make sure your settings model calls clearCache in the afterSave callback |
23
|
|
|
*/ |
24
|
|
|
public $modelClass = 'pheme\settings\models\BaseSetting'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Model to for storing and retrieving settings |
28
|
|
|
* @var \pheme\settings\models\SettingInterface |
29
|
|
|
*/ |
30
|
|
|
protected $model; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Cache|string the cache object or the application component ID of the cache object. |
34
|
|
|
* Settings will be cached through this cache object, if it is available. |
35
|
|
|
* |
36
|
|
|
* After the Settings object is created, if you want to change this property, |
37
|
|
|
* you should only assign it with a cache object. |
38
|
|
|
* Set this property to null if you do not want to cache the settings. |
39
|
|
|
*/ |
40
|
|
|
public $cache = 'cache'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Cache|string the front cache object or the application component ID of the front cache object. |
44
|
|
|
* Front cache will be cleared through this cache object, if it is available. |
45
|
|
|
* |
46
|
|
|
* After the Settings object is created, if you want to change this property, |
47
|
|
|
* you should only assign it with a cache object. |
48
|
|
|
* Set this property to null if you do not want to clear the front cache. |
49
|
|
|
*/ |
50
|
|
|
public $frontCache; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* To be used by the cache component. |
54
|
|
|
* |
55
|
|
|
* @var string cache key |
56
|
|
|
*/ |
57
|
|
|
public $cacheKey = 'pheme/settings'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var bool Whether to convert objects stored as JSON into an PHP array |
61
|
|
|
* @since 0.6 |
62
|
|
|
*/ |
63
|
|
|
public $autoDecodeJson = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Holds a cached copy of the data for the current request |
67
|
|
|
* |
68
|
|
|
* @var mixed |
69
|
|
|
*/ |
70
|
|
|
private $_data = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initialize the component |
74
|
|
|
* |
75
|
|
|
* @throws \yii\base\InvalidConfigException |
76
|
|
|
*/ |
77
|
34 |
|
public function init() |
78
|
|
|
{ |
79
|
34 |
|
parent::init(); |
80
|
|
|
|
81
|
34 |
|
$this->model = new $this->modelClass; |
82
|
|
|
|
83
|
34 |
|
if (is_string($this->cache)) { |
84
|
34 |
|
$this->cache = Yii::$app->get($this->cache, false); |
85
|
|
|
} |
86
|
34 |
|
if (is_string($this->frontCache)) { |
87
|
34 |
|
$this->frontCache = Yii::$app->get($this->frontCache, false); |
88
|
|
|
} |
89
|
34 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get's the value for the given key and section. |
93
|
|
|
* You can use dot notation to separate the section from the key: |
94
|
|
|
* $value = $settings->get('section.key'); |
95
|
|
|
* and |
96
|
|
|
* $value = $settings->get('key', 'section'); |
97
|
|
|
* are equivalent |
98
|
|
|
* |
99
|
|
|
* @param $key |
100
|
|
|
* @param string|null $section |
101
|
|
|
* @param string|null $default |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
6 |
|
public function get($key, $section = null, $default = null) |
105
|
|
|
{ |
106
|
6 |
|
if (is_null($section)) { |
107
|
4 |
|
$pieces = explode('.', $key, 2); |
108
|
4 |
|
if (count($pieces) > 1) { |
109
|
3 |
|
$section = $pieces[0]; |
110
|
3 |
|
$key = $pieces[1]; |
111
|
|
|
} else { |
112
|
1 |
|
$section = ''; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
$data = $this->getRawConfig(); |
117
|
|
|
|
118
|
6 |
|
if (isset($data[$section][$key][0])) { |
119
|
|
|
|
120
|
5 |
|
$value = $data[$section][$key][0]; |
121
|
5 |
|
$type = $data[$section][$key][1]; |
122
|
|
|
|
123
|
|
|
//convert value to needed type |
124
|
5 |
|
if (in_array($type, ['object', 'boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) { |
125
|
5 |
|
if ($this->autoDecodeJson && $type === 'object') { |
126
|
1 |
|
$value = Json::decode($value); |
127
|
|
|
} else { |
128
|
5 |
|
settype($value, $type); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
2 |
|
$value = $default; |
133
|
|
|
} |
134
|
6 |
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Checks to see if a setting exists. |
139
|
|
|
* If $searchDisabled is set to true, calling this function will result in an additional query. |
140
|
|
|
* @param $key |
141
|
|
|
* @param string|null $section |
142
|
|
|
* @param boolean $searchDisabled |
143
|
|
|
* @return boolean |
144
|
|
|
*/ |
145
|
2 |
|
public function has($key, $section = null, $searchDisabled = false) |
146
|
|
|
{ |
147
|
2 |
|
if ($searchDisabled) { |
148
|
1 |
|
$setting = $this->model->findSetting($key, $section); |
149
|
|
|
} else { |
150
|
2 |
|
$setting = $this->get($key, $section); |
151
|
|
|
} |
152
|
2 |
|
return is_null($setting) ? false : true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $key |
157
|
|
|
* @param $value |
158
|
|
|
* @param null $section |
159
|
|
|
* @param null $type |
160
|
|
|
* @return boolean |
161
|
|
|
*/ |
162
|
13 |
|
public function set($key, $value, $section = null, $type = null) |
163
|
|
|
{ |
164
|
13 |
|
if (is_null($section)) { |
165
|
12 |
|
$pieces = explode('.', $key); |
166
|
12 |
|
$section = $pieces[0]; |
167
|
12 |
|
$key = $pieces[1]; |
168
|
|
|
} |
169
|
|
|
|
170
|
13 |
|
if ($this->model->setSetting($section, $key, $value, $type)) { |
171
|
12 |
|
return true; |
172
|
|
|
} |
173
|
2 |
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns the specified key or sets the key with the supplied (default) value |
178
|
|
|
* |
179
|
|
|
* @param $key |
180
|
|
|
* @param $value |
181
|
|
|
* @param null $section |
182
|
|
|
* @param null $type |
183
|
|
|
* |
184
|
|
|
* @return bool|mixed|null |
185
|
|
|
*/ |
186
|
1 |
|
public function getOrSet($key, $value, $section = null, $type = null) |
187
|
|
|
{ |
188
|
1 |
|
if ($this->has($key, $section, true)) { |
189
|
1 |
|
return $this->get($key, $section); |
190
|
|
|
} else { |
191
|
1 |
|
if ($this->set($key, $value, $section, $type)) { |
192
|
1 |
|
return $value; |
193
|
|
|
} else { |
194
|
1 |
|
return null; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Deletes a setting |
201
|
|
|
* |
202
|
|
|
* @param $key |
203
|
|
|
* @param null|string $section |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
1 |
|
public function delete($key, $section = null) |
207
|
|
|
{ |
208
|
1 |
|
if (is_null($section)) { |
209
|
1 |
|
$pieces = explode('.', $key); |
210
|
1 |
|
$section = $pieces[0]; |
211
|
1 |
|
$key = $pieces[1]; |
212
|
|
|
} |
213
|
1 |
|
return $this->model->deleteSetting($section, $key); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Deletes all setting. Be careful! |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
1 |
|
public function deleteAll() |
222
|
|
|
{ |
223
|
1 |
|
return $this->model->deleteAllSettings(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Activates a setting |
228
|
|
|
* |
229
|
|
|
* @param $key |
230
|
|
|
* @param null|string $section |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
5 |
|
public function activate($key, $section = null) |
234
|
|
|
{ |
235
|
5 |
|
if (is_null($section)) { |
236
|
2 |
|
$pieces = explode('.', $key); |
237
|
2 |
|
$section = $pieces[0]; |
238
|
2 |
|
$key = $pieces[1]; |
239
|
|
|
} |
240
|
5 |
|
return $this->model->activateSetting($section, $key); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Deactivates a setting |
245
|
|
|
* |
246
|
|
|
* @param $key |
247
|
|
|
* @param null|string $section |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
2 |
|
public function deactivate($key, $section = null) |
251
|
|
|
{ |
252
|
2 |
|
if (is_null($section)) { |
253
|
1 |
|
$pieces = explode('.', $key); |
254
|
1 |
|
$section = $pieces[0]; |
255
|
1 |
|
$key = $pieces[1]; |
256
|
|
|
} |
257
|
2 |
|
return $this->model->deactivateSetting($section, $key); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Clears the settings cache on demand. |
262
|
|
|
* If you haven't configured cache this does nothing. |
263
|
|
|
* |
264
|
|
|
* @return boolean True if the cache key was deleted and false otherwise |
265
|
|
|
*/ |
266
|
34 |
|
public function clearCache() |
267
|
|
|
{ |
268
|
34 |
|
$this->_data = null; |
269
|
34 |
|
if ($this->frontCache instanceof Cache) { |
270
|
|
|
$this->frontCache->delete($this->cacheKey); |
271
|
|
|
} |
272
|
34 |
|
if ($this->cache instanceof Cache) { |
273
|
34 |
|
return $this->cache->delete($this->cacheKey); |
274
|
|
|
} |
275
|
1 |
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Returns the raw configuration array |
279
|
|
|
* |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
6 |
|
public function getRawConfig() |
283
|
|
|
{ |
284
|
6 |
|
if ($this->_data === null) { |
285
|
6 |
|
if ($this->cache instanceof Cache) { |
286
|
6 |
|
$data = $this->cache->get($this->cacheKey); |
287
|
6 |
|
if ($data === false) { |
288
|
6 |
|
$data = $this->model->getSettings(); |
289
|
6 |
|
$this->cache->set($this->cacheKey, $data); |
290
|
|
|
} |
291
|
|
|
} else { |
292
|
1 |
|
$data = $this->model->getSettings(); |
293
|
|
|
} |
294
|
6 |
|
$this->_data = $data; |
295
|
|
|
} |
296
|
6 |
|
return $this->_data; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|