1 | <?php namespace Arcanesoft\Settings; |
||
14 | class SettingsManager implements Contracts\Settings |
||
15 | { |
||
16 | /* ------------------------------------------------------------------------------------------------ |
||
17 | | Properties |
||
18 | | ------------------------------------------------------------------------------------------------ |
||
19 | */ |
||
20 | /** |
||
21 | * The settings data. |
||
22 | * |
||
23 | * @var \Arcanedev\Support\Collection |
||
24 | */ |
||
25 | protected $data; |
||
26 | |||
27 | /** |
||
28 | * Whether the store has changed since it was last loaded. |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $unsaved = false; |
||
33 | |||
34 | /** |
||
35 | * Whether the settings data are loaded. |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $loaded = false; |
||
40 | |||
41 | /** |
||
42 | * The Setting model. |
||
43 | * |
||
44 | * @var \Arcanesoft\Settings\Models\Setting |
||
45 | */ |
||
46 | private $model; |
||
47 | |||
48 | /** |
||
49 | * The cache repository |
||
50 | * |
||
51 | * @var \Illuminate\Contracts\Cache\Repository |
||
52 | */ |
||
53 | private $cache; |
||
54 | |||
55 | /* ------------------------------------------------------------------------------------------------ |
||
56 | | Constructor |
||
57 | | ------------------------------------------------------------------------------------------------ |
||
58 | */ |
||
59 | /** |
||
60 | * SettingsManager constructor. |
||
61 | * |
||
62 | * @param \Arcanesoft\Settings\Models\Setting $model |
||
63 | * @param \Illuminate\Contracts\Cache\Repository $cache |
||
64 | */ |
||
65 | 40 | public function __construct(Setting $model, Cache $cache) |
|
71 | |||
72 | /* ------------------------------------------------------------------------------------------------ |
||
73 | | Getters & Setters |
||
74 | | ------------------------------------------------------------------------------------------------ |
||
75 | */ |
||
76 | /** |
||
77 | * Get the settings default domain. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 36 | protected function getDefaultDomain() |
|
85 | |||
86 | /** |
||
87 | * Get the cache key. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 40 | protected function getCacheKey() |
|
95 | |||
96 | /** |
||
97 | * Check if cache is enabled. |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | 40 | protected function isCached() |
|
105 | |||
106 | /** |
||
107 | * Get the config value by key. |
||
108 | * |
||
109 | * @param string $key |
||
110 | * @param mixed $default |
||
111 | * |
||
112 | * @return mixed |
||
113 | */ |
||
114 | 40 | private function config($key, $default = null) |
|
118 | |||
119 | /* ------------------------------------------------------------------------------------------------ |
||
120 | | Main Functions |
||
121 | | ------------------------------------------------------------------------------------------------ |
||
122 | */ |
||
123 | /** |
||
124 | * Get a setting by key. |
||
125 | * |
||
126 | * @param string $key |
||
127 | * @param mixed|null $default |
||
128 | * |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 24 | public function get($key, $default = null) |
|
139 | |||
140 | /** |
||
141 | * Set a setting. |
||
142 | * |
||
143 | * @param string $key |
||
144 | * @param mixed $value |
||
145 | */ |
||
146 | 32 | public function set($key, $value) |
|
161 | |||
162 | /** |
||
163 | * Check if a setting exists by the key. |
||
164 | * |
||
165 | * @param string $key |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | 24 | public function has($key) |
|
181 | |||
182 | /** |
||
183 | * Get all the setting by a specific domain. |
||
184 | * |
||
185 | * @param string|null $domain |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 20 | public function all($domain = null) |
|
199 | |||
200 | /** |
||
201 | * Delete a setting. |
||
202 | * |
||
203 | * @param string $key |
||
204 | */ |
||
205 | 8 | public function delete($key) |
|
226 | |||
227 | /** |
||
228 | * Reset/Delete all the settings. |
||
229 | * |
||
230 | * @param string|null $domain |
||
231 | */ |
||
232 | 4 | public function reset($domain = null) |
|
242 | |||
243 | /** |
||
244 | * Save the settings. |
||
245 | */ |
||
246 | 28 | public function save() |
|
260 | |||
261 | /** |
||
262 | * Get the changes. |
||
263 | * |
||
264 | * @param \Illuminate\Database\Eloquent\Collection $saved |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | 28 | private function getChanges($saved) |
|
280 | |||
281 | /** |
||
282 | * Save the inserted entries. |
||
283 | * |
||
284 | * @param array $inserted |
||
285 | */ |
||
286 | 28 | private function saveInserted(array $inserted) |
|
294 | |||
295 | /** |
||
296 | * Save the updated entries. |
||
297 | * |
||
298 | * @param \Illuminate\Database\Eloquent\Collection $saved |
||
299 | * @param array $updated |
||
300 | */ |
||
301 | 28 | private function saveUpdated($saved, array $updated) |
|
312 | |||
313 | /** |
||
314 | * Save the deleted entries. |
||
315 | * |
||
316 | * @param \Illuminate\Database\Eloquent\Collection $saved |
||
317 | * @param array $deleted |
||
318 | */ |
||
319 | 28 | private function saveDeleted($saved, array $deleted) |
|
329 | |||
330 | /** |
||
331 | * Grab the settings domain name from the key. |
||
332 | * |
||
333 | * @param string $key |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | 32 | private function grabDomain(&$key) |
|
347 | |||
348 | /* ------------------------------------------------------------------------------------------------ |
||
349 | | Other Functions |
||
350 | | ------------------------------------------------------------------------------------------------ |
||
351 | */ |
||
352 | /** |
||
353 | * Check if the data is loaded |
||
354 | */ |
||
355 | 36 | private function checkLoaded() |
|
365 | |||
366 | /** |
||
367 | * Load the data. |
||
368 | */ |
||
369 | 36 | private function loadData() |
|
380 | |||
381 | /** |
||
382 | * Get cached settings. |
||
383 | * |
||
384 | * @return \Illuminate\Database\Eloquent\Collection |
||
385 | */ |
||
386 | 36 | private function getCachedSettings() |
|
394 | } |
||
395 |