1 | <?php |
||
2 | |||
3 | namespace yii2mod\settings\components; |
||
4 | |||
5 | use Yii; |
||
6 | use yii\base\Component; |
||
7 | use yii\caching\Cache; |
||
8 | use yii\di\Instance; |
||
9 | use yii\helpers\ArrayHelper; |
||
10 | use yii2mod\settings\models\enumerables\SettingType; |
||
11 | |||
12 | /** |
||
13 | * Class Settings |
||
14 | * |
||
15 | * @package yii2mod\settings\components |
||
16 | */ |
||
17 | class Settings extends Component |
||
18 | { |
||
19 | /** |
||
20 | * @var string setting model class name |
||
21 | */ |
||
22 | public $modelClass = 'yii2mod\settings\models\SettingModel'; |
||
23 | |||
24 | /** |
||
25 | * @var Cache|array|string the cache used to improve RBAC performance. This can be one of the followings: |
||
26 | * |
||
27 | * - an application component ID (e.g. `cache`) |
||
28 | * - a configuration array |
||
29 | * - a [[yii\caching\Cache]] object |
||
30 | * |
||
31 | * When this is not set, it means caching is not enabled |
||
32 | */ |
||
33 | public $cache = 'cache'; |
||
34 | |||
35 | /** |
||
36 | * @var string the key used to store settings data in cache |
||
37 | */ |
||
38 | public $cacheKey = 'yii2mod-setting'; |
||
39 | |||
40 | /** |
||
41 | * @var \yii2mod\settings\models\SettingModel setting model |
||
42 | */ |
||
43 | protected $model; |
||
44 | |||
45 | /** |
||
46 | * @var array list of settings |
||
47 | */ |
||
48 | protected $items; |
||
49 | |||
50 | /** |
||
51 | * @var mixed setting value |
||
52 | */ |
||
53 | protected $setting; |
||
54 | |||
55 | /** |
||
56 | * Initialize the component |
||
57 | */ |
||
58 | public function init() |
||
59 | { |
||
60 | parent::init(); |
||
61 | |||
62 | if ($this->cache !== null) { |
||
63 | $this->cache = Instance::ensure($this->cache, Cache::class); |
||
64 | } |
||
65 | |||
66 | $this->model = Yii::createObject($this->modelClass); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get's all values in the specific section. |
||
71 | * |
||
72 | * @param string $section |
||
73 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getAllBySection($section, $default = null) |
||
78 | { |
||
79 | $items = $this->getSettingsConfig(); |
||
80 | |||
81 | if (isset($items[$section])) { |
||
82 | $this->setting = ArrayHelper::getColumn($items[$section], 'value'); |
||
83 | } else { |
||
84 | $this->setting = $default; |
||
85 | } |
||
86 | |||
87 | return $this->setting; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get's the value for the given section and key. |
||
92 | * |
||
93 | * @param string $section |
||
94 | * @param string $key |
||
95 | * @param null $default |
||
0 ignored issues
–
show
|
|||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function get($section, $key, $default = null) |
||
100 | { |
||
101 | $items = $this->getSettingsConfig(); |
||
102 | |||
103 | if (isset($items[$section][$key])) { |
||
104 | $this->setting = ArrayHelper::getValue($items[$section][$key], 'value'); |
||
105 | $type = ArrayHelper::getValue($items[$section][$key], 'type'); |
||
106 | $this->convertSettingType($type); |
||
107 | } else { |
||
108 | $this->setting = $default; |
||
109 | } |
||
110 | |||
111 | return $this->setting; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Add a new setting or update an existing one. |
||
116 | * |
||
117 | * @param null $section |
||
0 ignored issues
–
show
|
|||
118 | * @param string $key |
||
119 | * @param string $value |
||
120 | * @param null $type |
||
0 ignored issues
–
show
|
|||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function set($section, $key, $value, $type = null): bool |
||
125 | { |
||
126 | if ($this->model->setSetting($section, $key, $value, $type)) { |
||
127 | if ($this->invalidateCache()) { |
||
128 | return true; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | return false; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Checking existence of setting |
||
137 | * |
||
138 | * @param string $section |
||
139 | * @param string $key |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function has($section, $key): bool |
||
144 | { |
||
145 | $setting = $this->get($section, $key); |
||
146 | |||
147 | return !empty($setting); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Remove setting by section and key |
||
152 | * |
||
153 | * @param string $section |
||
154 | * @param string $key |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function remove($section, $key): bool |
||
159 | { |
||
160 | if ($this->model->removeSetting($section, $key)) { |
||
161 | if ($this->invalidateCache()) { |
||
162 | return true; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | return false; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Remove all settings |
||
171 | * |
||
172 | * @return int |
||
173 | */ |
||
174 | public function removeAll(): int |
||
175 | { |
||
176 | return $this->model->removeAllSettings(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Activates a setting |
||
181 | * |
||
182 | * @param string $key |
||
183 | * @param string $section |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function activate($section, $key): bool |
||
188 | { |
||
189 | return $this->model->activateSetting($section, $key); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Deactivates a setting |
||
194 | * |
||
195 | * @param string $key |
||
196 | * @param string $section |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function deactivate($section, $key): bool |
||
201 | { |
||
202 | return $this->model->deactivateSetting($section, $key); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Returns the settings config |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | protected function getSettingsConfig(): array |
||
211 | { |
||
212 | if (!$this->cache instanceof Cache) { |
||
213 | $this->items = $this->model->getSettings(); |
||
214 | } else { |
||
215 | $cacheItems = $this->cache->get($this->cacheKey); |
||
216 | if (!empty($cacheItems)) { |
||
217 | $this->items = $cacheItems; |
||
218 | } else { |
||
219 | $this->items = $this->model->getSettings(); |
||
220 | $this->cache->set($this->cacheKey, $this->items); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | return $this->items; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Invalidate the cache |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function invalidateCache(): bool |
||
233 | { |
||
234 | if ($this->cache !== null) { |
||
235 | $this->cache->delete($this->cacheKey); |
||
236 | $this->items = null; |
||
237 | } |
||
238 | |||
239 | return true; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Set type for setting |
||
244 | * |
||
245 | * @param $type |
||
246 | */ |
||
247 | protected function convertSettingType($type) |
||
248 | { |
||
249 | if ($type === SettingType::BOOLEAN_TYPE) { |
||
250 | $this->setting = filter_var($this->setting, FILTER_VALIDATE_BOOLEAN); |
||
251 | } else { |
||
252 | settype($this->setting, $type); |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 |