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