1 | <?php namespace Arcanesoft\Settings; |
||
15 | class SettingsManager implements Contracts\Settings |
||
16 | { |
||
17 | /* ------------------------------------------------------------------------------------------------ |
||
18 | | Properties |
||
19 | | ------------------------------------------------------------------------------------------------ |
||
20 | */ |
||
21 | /** |
||
22 | * The settings data. |
||
23 | * |
||
24 | * @var \Arcanedev\Support\Collection |
||
25 | */ |
||
26 | protected $data; |
||
27 | |||
28 | /** |
||
29 | * Whether the store has changed since it was last loaded. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $unsaved = false; |
||
34 | |||
35 | /** |
||
36 | * Whether the settings data are loaded. |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $loaded = false; |
||
41 | |||
42 | /** @var \Arcanesoft\Settings\Stores\EloquentStore */ |
||
43 | private $store; |
||
44 | |||
45 | /* ------------------------------------------------------------------------------------------------ |
||
46 | | Constructor |
||
47 | | ------------------------------------------------------------------------------------------------ |
||
48 | */ |
||
49 | /** |
||
50 | * SettingsManager constructor. |
||
51 | * |
||
52 | * @param \Arcanesoft\Settings\Models\Setting $model |
||
53 | * @param \Illuminate\Contracts\Cache\Repository $cache |
||
54 | */ |
||
55 | 30 | public function __construct(Setting $model, Cache $cache) |
|
56 | { |
||
57 | 30 | $this->store = new Stores\EloquentStore($model, $cache); |
|
58 | 30 | $this->data = new Collection; |
|
59 | 30 | } |
|
60 | |||
61 | /* ------------------------------------------------------------------------------------------------ |
||
62 | | Getters & Setters |
||
63 | | ------------------------------------------------------------------------------------------------ |
||
64 | */ |
||
65 | /** |
||
66 | * Get the settings default domain. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | 27 | protected function getDefaultDomain() |
|
71 | { |
||
72 | 27 | return $this->config('default-domain', 'default'); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the config value by key. |
||
77 | * |
||
78 | * @param string $key |
||
79 | * @param mixed $default |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | 27 | private function config($key, $default = null) |
|
84 | { |
||
85 | 27 | return config("arcanesoft.settings.$key", $default); |
|
86 | } |
||
87 | |||
88 | /* ------------------------------------------------------------------------------------------------ |
||
89 | | Main Functions |
||
90 | | ------------------------------------------------------------------------------------------------ |
||
91 | */ |
||
92 | /** |
||
93 | * Get a setting by key. |
||
94 | * |
||
95 | * @param string $key |
||
96 | * @param mixed|null $default |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 18 | public function get($key, $default = null) |
|
101 | { |
||
102 | 18 | $this->checkLoaded(); |
|
103 | |||
104 | 18 | $domain = $this->grabDomain($key); |
|
105 | |||
106 | 18 | return Arr::get($this->data->get($domain, []), $key, $default); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Set a setting. |
||
111 | * |
||
112 | * @param string $key |
||
113 | * @param mixed $value |
||
114 | */ |
||
115 | 24 | public function set($key, $value) |
|
116 | { |
||
117 | 24 | $this->checkLoaded(); |
|
118 | |||
119 | 24 | $domain = $this->grabDomain($key); |
|
120 | 24 | $data = []; |
|
121 | |||
122 | 24 | if ($this->data->has($domain)) { |
|
123 | 12 | $data = $this->data->get($domain); |
|
124 | 8 | } |
|
125 | |||
126 | 24 | Arr::set($data, $key, $value); |
|
127 | |||
128 | 24 | $this->data->put($domain, $data); |
|
129 | 24 | } |
|
130 | |||
131 | /** |
||
132 | * Check if a setting exists by the key. |
||
133 | * |
||
134 | * @param string $key |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | 18 | public function has($key) |
|
150 | |||
151 | /** |
||
152 | * Get all the setting by a specific domain. |
||
153 | * |
||
154 | * @param string|null $domain |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | 15 | public function all($domain = null) |
|
168 | |||
169 | /** |
||
170 | * Delete a setting. |
||
171 | * |
||
172 | * @param string $key |
||
173 | */ |
||
174 | 6 | public function delete($key) |
|
195 | |||
196 | /** |
||
197 | * Reset/Delete all the settings. |
||
198 | * |
||
199 | * @param string|null $domain |
||
200 | */ |
||
201 | 3 | public function reset($domain = null) |
|
202 | { |
||
203 | 3 | $this->checkLoaded(); |
|
204 | |||
205 | 3 | if (is_null($domain)) { |
|
206 | 3 | $domain = $this->getDefaultDomain(); |
|
207 | 2 | } |
|
208 | |||
209 | 3 | $this->data->forget($domain); |
|
210 | 3 | } |
|
211 | |||
212 | /** |
||
213 | * Save the settings. |
||
214 | */ |
||
215 | 21 | public function save() |
|
216 | { |
||
217 | 21 | $changes = $this->getChanges( |
|
218 | 21 | $saved = $this->store->all() |
|
219 | 14 | ); |
|
220 | |||
221 | 21 | $this->store->save($saved, $changes); |
|
222 | 21 | } |
|
223 | |||
224 | /** |
||
225 | * Get the changes. |
||
226 | * |
||
227 | * @param \Illuminate\Database\Eloquent\Collection $saved |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 21 | private function getChanges($saved) |
|
243 | |||
244 | /** |
||
245 | * Grab the settings domain name from the key. |
||
246 | * |
||
247 | * @param string $key |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 24 | private function grabDomain(&$key) |
|
261 | |||
262 | /* ------------------------------------------------------------------------------------------------ |
||
263 | | Other Functions |
||
264 | | ------------------------------------------------------------------------------------------------ |
||
265 | */ |
||
266 | /** |
||
267 | * Check if the data is loaded |
||
268 | */ |
||
269 | 27 | private function checkLoaded() |
|
270 | { |
||
271 | 27 | if ( ! $this->loaded) { |
|
272 | 27 | $this->data->reset(); |
|
273 | 27 | $this->loadData(); |
|
274 | 27 | $this->loaded = true; |
|
275 | 18 | } |
|
276 | 27 | } |
|
277 | |||
278 | /** |
||
279 | * Load the data. |
||
280 | */ |
||
281 | 27 | private function loadData() |
|
290 | } |
||
291 |