1 | <?php |
||
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 | 22 | public function init() |
|
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 | 4 | public function get($key, $section = null, $default = null) |
|
98 | { |
||
99 | 3 | if (is_null($section)) { |
|
100 | 1 | $pieces = explode('.', $key, 2); |
|
101 | 1 | if (count($pieces) > 1) { |
|
102 | 1 | $section = $pieces[0]; |
|
103 | 1 | $key = $pieces[1]; |
|
104 | 1 | } else { |
|
105 | $section = ''; |
||
106 | } |
||
107 | 1 | } |
|
108 | |||
109 | 3 | $data = $this->getRawConfig(); |
|
110 | |||
111 | 3 | if (isset($data[$section][$key][0])) { |
|
112 | 3 | if (in_array($data[$section][$key][1], ['boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) { |
|
113 | 4 | settype($data[$section][$key][0], $data[$section][$key][1]); |
|
114 | 4 | } |
|
115 | 4 | } else { |
|
116 | $data[$section][$key][0] = $default; |
||
117 | } |
||
118 | 3 | 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 | 1 | if ($searchDisabled) { |
|
132 | 1 | $setting = $this->model->findSetting($key, $section); |
|
133 | } else { |
||
134 | 1 | $setting = $this->get($key, $section); |
|
135 | } |
||
136 | 1 | return 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 | 1 | return false; |
|
160 | 1 | } |
|
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) |
|
178 | |||
179 | /** |
||
180 | * Deletes all setting. Be careful! |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | public function deleteAll() |
|
188 | |||
189 | /** |
||
190 | * Activates a setting |
||
191 | * |
||
192 | * @param $key |
||
193 | * @param null|string $section |
||
194 | * @return bool |
||
195 | */ |
||
196 | 5 | public function activate($key, $section = null) |
|
205 | |||
206 | /** |
||
207 | * Deactivates a setting |
||
208 | * |
||
209 | * @param $key |
||
210 | * @param null|string $section |
||
211 | * @return bool |
||
212 | */ |
||
213 | 1 | public function deactivate($key, $section = null) |
|
214 | { |
||
215 | 1 | if (is_null($section)) { |
|
216 | $pieces = explode('.', $key); |
||
217 | $section = $pieces[0]; |
||
218 | $key = $pieces[1]; |
||
219 | } |
||
220 | 1 | 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 | 22 | public function clearCache() |
|
240 | |||
241 | /** |
||
242 | * Returns the raw configuration array |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | 3 | public function getRawConfig() |
|
262 | } |
||
263 |