|
1
|
|
|
<?php namespace Arcanesoft\Settings\Stores; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Settings\Models\Setting; |
|
4
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class EloquentStore |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanesoft\Settings\Stores |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class EloquentStore |
|
14
|
|
|
{ |
|
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
*/ |
|
19
|
|
|
/** |
|
20
|
|
|
* The Setting Eloquent Model. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Arcanesoft\Settings\Models\Setting |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $model; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The cache repository. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Illuminate\Contracts\Cache\Repository |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $cache; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \Illuminate\Database\Eloquent\Collection |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $saved; |
|
37
|
|
|
|
|
38
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
39
|
|
|
| Constructor |
|
40
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
41
|
|
|
*/ |
|
42
|
|
|
/** |
|
43
|
|
|
* SettingsManager constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Arcanesoft\Settings\Models\Setting $model |
|
46
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
|
47
|
|
|
*/ |
|
48
|
40 |
|
public function __construct(Setting $model, Cache $cache) |
|
49
|
|
|
{ |
|
50
|
40 |
|
$this->model = $model; |
|
51
|
40 |
|
$this->cache = $cache; |
|
52
|
40 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
55
|
|
|
| Getters & Setters |
|
56
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
57
|
|
|
*/ |
|
58
|
|
|
/** |
|
59
|
|
|
* Get the cache key. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
40 |
|
protected function getCacheKey() |
|
64
|
|
|
{ |
|
65
|
40 |
|
return $this->config('cache.key', 'cached_settings'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Check if cache is enabled. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
40 |
|
protected function isCached() |
|
74
|
|
|
{ |
|
75
|
40 |
|
return $this->config('cache.enabled', false); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the config value by key. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $key |
|
82
|
|
|
* @param mixed $default |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
40 |
|
private function config($key, $default = null) |
|
87
|
|
|
{ |
|
88
|
40 |
|
return config("arcanesoft.settings.$key", $default); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set the saved entries. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $saved |
|
95
|
|
|
* |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
28 |
|
private function setSaved(EloquentCollection $saved) |
|
99
|
|
|
{ |
|
100
|
28 |
|
$this->saved = $saved; |
|
101
|
|
|
|
|
102
|
28 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
106
|
|
|
| Main Functions |
|
107
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
108
|
|
|
*/ |
|
109
|
|
|
/** |
|
110
|
|
|
* Get all the setting entries. |
|
111
|
|
|
* |
|
112
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
113
|
|
|
*/ |
|
114
|
40 |
|
public function all() |
|
115
|
|
|
{ |
|
116
|
40 |
|
return ! $this->isCached() |
|
117
|
30 |
|
? $this->model->all() |
|
118
|
40 |
|
: $this->cache->rememberForever($this->getCacheKey(), function() { |
|
119
|
40 |
|
return $this->model->all(); |
|
120
|
40 |
|
}); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Save the changes. |
|
125
|
|
|
* |
|
126
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $saved |
|
127
|
|
|
* @param array $changes |
|
128
|
|
|
*/ |
|
129
|
28 |
|
public function save($saved, array $changes) |
|
130
|
|
|
{ |
|
131
|
28 |
|
$this->setSaved($saved); |
|
132
|
28 |
|
$this->saveInserted($changes['inserted']); |
|
133
|
28 |
|
$this->saveUpdated($changes['updated']); |
|
134
|
28 |
|
$this->saveDeleted($changes['deleted']); |
|
135
|
|
|
|
|
136
|
28 |
|
if ($this->isCached()) { |
|
137
|
28 |
|
$this->cache->forget($this->getCacheKey()); |
|
138
|
21 |
|
} |
|
139
|
28 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
142
|
|
|
| Other Functions |
|
143
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
144
|
|
|
*/ |
|
145
|
|
|
/** |
|
146
|
|
|
* Save the inserted entries. |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $inserted |
|
149
|
|
|
*/ |
|
150
|
28 |
|
private function saveInserted(array $inserted) |
|
151
|
|
|
{ |
|
152
|
28 |
|
foreach ($inserted as $domain => $values) { |
|
153
|
24 |
|
foreach ($values as $key => $value) { |
|
154
|
24 |
|
$this->model->createOne($domain, $key, $value); |
|
155
|
18 |
|
} |
|
156
|
21 |
|
} |
|
157
|
28 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Save the updated entries. |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $updated |
|
163
|
|
|
*/ |
|
164
|
28 |
|
private function saveUpdated(array $updated) |
|
165
|
|
|
{ |
|
166
|
28 |
|
foreach ($updated as $domain => $values) { |
|
167
|
4 |
|
foreach ($values as $key => $value) { |
|
168
|
4 |
|
$model = $this->getSavedOne($domain, $key); |
|
169
|
4 |
|
$model->updateValue($value); |
|
170
|
4 |
|
$model->save(); |
|
171
|
3 |
|
} |
|
172
|
21 |
|
} |
|
173
|
28 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Save the deleted entries. |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $deleted |
|
179
|
|
|
*/ |
|
180
|
28 |
|
private function saveDeleted(array $deleted) |
|
181
|
|
|
{ |
|
182
|
28 |
|
foreach ($deleted as $domain => $values) { |
|
183
|
12 |
|
foreach ($values as $key) { |
|
184
|
12 |
|
$this->getSavedOne($domain, $key)->delete(); |
|
185
|
9 |
|
} |
|
186
|
21 |
|
} |
|
187
|
28 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get the first saved entry. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $domain |
|
193
|
|
|
* @param string $key |
|
194
|
|
|
* |
|
195
|
|
|
* @return \Arcanesoft\Settings\Models\Setting |
|
196
|
|
|
*/ |
|
197
|
16 |
|
private function getSavedOne($domain, $key) |
|
198
|
|
|
{ |
|
199
|
16 |
|
return $this->saved->groupBy('domain')->get($domain)->where('key', $key)->first(); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|