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 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() |
|
275 | |||
276 | /** |
||
277 | * Get the changes. |
||
278 | * |
||
279 | * @param \Illuminate\Database\Eloquent\Collection $saved |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | 28 | private function getChanges($saved) |
|
295 | |||
296 | /** |
||
297 | * Grab the settings domain name from the key. |
||
298 | * |
||
299 | * @param string $key |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | 32 | private function grabDomain(&$key) |
|
313 | |||
314 | /* ------------------------------------------------------------------------------------------------ |
||
315 | | Other Functions |
||
316 | | ------------------------------------------------------------------------------------------------ |
||
317 | */ |
||
318 | /** |
||
319 | * Check if the data is loaded |
||
320 | */ |
||
321 | 36 | private function checkLoaded() |
|
331 | |||
332 | /** |
||
333 | * Load the data. |
||
334 | */ |
||
335 | 36 | private function loadData() |
|
346 | |||
347 | /** |
||
348 | * Get cached settings. |
||
349 | * |
||
350 | * @return \Illuminate\Database\Eloquent\Collection |
||
351 | */ |
||
352 | 36 | private function getCachedSettings() |
|
362 | |||
363 | /** |
||
364 | * Flush the cached settings. |
||
365 | */ |
||
366 | 28 | private function flushCachedSettings() |
|
372 | } |
||
373 |