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 | 29 | public function init() |
|
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 | 11 | public function get($key, $section = null, $default = null) |
|
105 | { |
||
106 | 3 | if (is_null($section)) { |
|
107 | 1 | $pieces = explode('.', $key, 2); |
|
108 | 1 | if (count($pieces) > 1) { |
|
109 | 1 | $section = $pieces[0]; |
|
110 | 8 | $key = $pieces[1]; |
|
111 | 1 | } else { |
|
112 | 8 | $section = ''; |
|
113 | 8 | } |
|
114 | 9 | } |
|
115 | |||
116 | 3 | $data = $this->getRawConfig(); |
|
117 | |||
118 | 11 | if (isset($data[$section][$key][0])) { |
|
119 | |||
120 | 10 | $value = $data[$section][$key][0]; |
|
121 | 3 | $type = $data[$section][$key][1]; |
|
122 | |||
123 | //convert value to needed type |
||
124 | 3 | if (in_array($type, ['object', 'boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) { |
|
125 | 3 | if ($this->autoDecodeJson && $type === 'object') { |
|
126 | $value = Json::decode($value); |
||
127 | } else { |
||
128 | 3 | settype($value, $type); |
|
129 | } |
||
130 | 3 | } |
|
131 | 3 | } else { |
|
132 | $value = $default; |
||
133 | 5 | } |
|
134 | 5 | 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 | 1 | $setting = $this->get($key, $section); |
|
151 | } |
||
152 | 1 | 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 | 8 | public function set($key, $value, $section = null, $type = null) |
|
163 | { |
||
164 | 8 | if (is_null($section)) { |
|
165 | 7 | $pieces = explode('.', $key); |
|
166 | 7 | $section = $pieces[0]; |
|
167 | 7 | $key = $pieces[1]; |
|
168 | 7 | } |
|
169 | |||
170 | 8 | if ($this->model->setSetting($section, $key, $value, $type)) { |
|
171 | 8 | return true; |
|
172 | } |
||
173 | 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 |
||
185 | */ |
||
186 | public function getOrSet($key, $value, $section = null, $type = null) |
||
194 | |||
195 | /** |
||
196 | * Deletes a setting |
||
197 | * |
||
198 | * @param $key |
||
199 | * @param null|string $section |
||
200 | * @return bool |
||
201 | */ |
||
202 | 1 | public function delete($key, $section = null) |
|
211 | |||
212 | /** |
||
213 | * Deletes all setting. Be careful! |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 1 | public function deleteAll() |
|
221 | |||
222 | /** |
||
223 | * Activates a setting |
||
224 | * |
||
225 | * @param $key |
||
226 | * @param null|string $section |
||
227 | * @return bool |
||
228 | */ |
||
229 | 5 | public function activate($key, $section = null) |
|
238 | |||
239 | /** |
||
240 | * Deactivates a setting |
||
241 | * |
||
242 | * @param $key |
||
243 | * @param null|string $section |
||
244 | * @return bool |
||
245 | */ |
||
246 | 1 | public function deactivate($key, $section = null) |
|
255 | |||
256 | /** |
||
257 | * Clears the settings cache on demand. |
||
258 | * If you haven't configured cache this does nothing. |
||
259 | * |
||
260 | * @return boolean True if the cache key was deleted and false otherwise |
||
261 | */ |
||
262 | 29 | public function clearCache() |
|
273 | |||
274 | /** |
||
275 | * Returns the raw configuration array |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | 3 | public function getRawConfig() |
|
295 | } |
||
296 |