1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
215 | |||
216 | /** |
||
217 | * Deletes all setting. Be careful! |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 1 | public function deleteAll() |
|
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) |
|
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) |
|
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() |
|
276 | |||
277 | /** |
||
278 | * Returns the raw configuration array |
||
279 | * |
||
280 | * @return array |
||
281 | */ |
||
282 | 6 | public function getRawConfig() |
|
298 | } |
||
299 |