|
1
|
|
|
<?php namespace Cornford\Setter; |
|
2
|
|
|
|
|
3
|
|
|
use DateTime; |
|
4
|
|
|
use Illuminate\Database\DatabaseManager as Query; |
|
5
|
|
|
use Illuminate\Config\Repository; |
|
6
|
|
|
use Illuminate\Cache\Repository as Cache; |
|
7
|
|
|
|
|
8
|
|
|
abstract class SettingBase { |
|
9
|
|
|
|
|
10
|
|
|
const LOCATION_DATABASE = 'database'; |
|
11
|
|
|
const LOCATION_CACHE = 'cache'; |
|
12
|
|
|
|
|
13
|
|
|
const CACHE_TAG = 'setter::'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Database |
|
17
|
|
|
* |
|
18
|
|
|
* @var \Illuminate\Database\DatabaseManager |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $database; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Config |
|
24
|
|
|
* |
|
25
|
|
|
* @var \Illuminate\Config\Repository |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $config; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Cache |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Illuminate\Cache\Repository |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $cache; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Cache |
|
38
|
|
|
* |
|
39
|
|
|
* @var integer|datetime|boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $expiry = true; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Construct Setter |
|
45
|
|
|
* |
|
46
|
|
|
* @param Query $database |
|
47
|
|
|
* @param Repository $config |
|
48
|
|
|
* @param Cache $cache |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(Query $database, Repository $config, Cache $cache) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->database = $database; |
|
53
|
|
|
$this->config = $config; |
|
54
|
|
|
$this->cache = $cache; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Return a key with an attached cache tag |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $key |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function attachTag($key) |
|
65
|
|
|
{ |
|
66
|
|
|
return self::CACHE_TAG . $key; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check a setting exists in cache |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $key |
|
73
|
|
|
* |
|
74
|
|
|
* @return boolean |
|
75
|
|
|
*/ |
|
76
|
|
|
public function cacheHas($key) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->cache->has($this->attachTag($key)) ? true : false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Forget a cached setting by key |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* |
|
87
|
|
|
* @return boolean |
|
88
|
|
|
*/ |
|
89
|
|
|
public function cacheForget($key) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->cache |
|
92
|
|
|
->forget($this->attachTag($key)); |
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Clear all cached settings |
|
99
|
|
|
* |
|
100
|
|
|
* @return boolean |
|
101
|
|
|
*/ |
|
102
|
|
|
public function cacheClear() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->cache |
|
105
|
|
|
->flush(); |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Check a setting exists in config |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* |
|
115
|
|
|
* @return boolean |
|
116
|
|
|
*/ |
|
117
|
|
|
public function configHas($key) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->config->has($key) ? true : false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Arrange results into an associative array |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $results |
|
126
|
|
|
* @param string $key |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function arrangeResults($results, $key = null) |
|
131
|
|
|
{ |
|
132
|
|
|
$return = array(); |
|
133
|
|
|
|
|
134
|
|
|
foreach ($results as $path => $value) { |
|
135
|
|
|
$parts = strpos($path, '.') > 0 ? explode('.', trim(preg_replace('/^' . $key . '/', '', $path), '.')) : array($path); |
|
136
|
|
|
$target =& $return; |
|
137
|
|
|
|
|
138
|
|
|
foreach ($parts as $part) { |
|
139
|
|
|
$target =& $target[$part]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$target = $this->decodeJson($value); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $return; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Return result values |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $results |
|
152
|
|
|
* @param string $key |
|
153
|
|
|
* |
|
154
|
|
|
* @return string|array |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function returnResults($results = array(), $key) |
|
157
|
|
|
{ |
|
158
|
|
|
$items = $this->arrangeResults($results, $key); |
|
159
|
|
|
$return = $this->combineResults($items, $key); |
|
160
|
|
|
|
|
161
|
|
|
if ((!is_array($this->returnConfig($key)) || count($this->returnConfig($key)) == 0) && |
|
162
|
|
|
(array_key_exists($key, $return) || array_key_exists('', $return)) |
|
163
|
|
|
&& count($return) == 1 |
|
164
|
|
|
) { |
|
165
|
|
|
$return = reset($return); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$this->cache->forget($this->attachTag($key)); |
|
169
|
|
|
$this->cache->add($this->attachTag($key), $return, $this->expiry); |
|
170
|
|
|
|
|
171
|
|
|
return $this->decodeJson($return); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Combine result values from the database and configuration |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $results |
|
178
|
|
|
* @param string $key |
|
179
|
|
|
* |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function combineResults(array $results = array(), $key) |
|
183
|
|
|
{ |
|
184
|
|
|
$config = $this->returnConfig($key); |
|
185
|
|
|
|
|
186
|
|
|
if (is_array($config)) { |
|
187
|
|
|
return array_replace_recursive($config, ((array_key_exists($key, $results) || array_key_exists('', $results)) ? reset($results) : $results)); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $results; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Re-cache item and its parents |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $value |
|
197
|
|
|
* @param string $key |
|
198
|
|
|
* |
|
199
|
|
|
* @return void |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function recacheItem($value, $key) |
|
202
|
|
|
{ |
|
203
|
|
|
for ($i = 0; $i <= substr_count($key, '.') - 1; $i++) { |
|
204
|
|
|
$j = $i; |
|
205
|
|
|
$position = 0; |
|
206
|
|
|
|
|
207
|
|
|
while ($j >= 0) { |
|
208
|
|
|
$position =+ strpos($key, '.', $position) + 1; |
|
209
|
|
|
$j--; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$this->cache |
|
213
|
|
|
->forget($this->attachTag(rtrim(substr_replace($key, '', $position), '.'))); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$this->cache |
|
217
|
|
|
->forget($this->attachTag($key)); |
|
218
|
|
|
$this->cache |
|
219
|
|
|
->add($this->attachTag($key), $value, $this->expiry); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Return cache values |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $key |
|
226
|
|
|
* |
|
227
|
|
|
* @return string|array |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function returnCache($key) |
|
230
|
|
|
{ |
|
231
|
|
|
$value = $this->cache->get($this->attachTag($key)); |
|
232
|
|
|
|
|
233
|
|
|
return $this->decodeJson($value); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Return config values |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $key |
|
240
|
|
|
* |
|
241
|
|
|
* @return string|array |
|
242
|
|
|
*/ |
|
243
|
|
|
protected function returnConfig($key) |
|
244
|
|
|
{ |
|
245
|
|
|
$value = $this->config->get($key); |
|
246
|
|
|
|
|
247
|
|
|
return $this->decodeJson($value); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Is the string Json encoded. |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $string |
|
254
|
|
|
* @return boolean |
|
255
|
|
|
*/ |
|
256
|
|
|
protected function isJson($string) |
|
257
|
|
|
{ |
|
258
|
|
|
if (!is_string($string)) { |
|
259
|
|
|
return false; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
json_decode($string); |
|
263
|
|
|
|
|
264
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Decode a Json item. |
|
269
|
|
|
* |
|
270
|
|
|
* @param mixed $value |
|
271
|
|
|
* |
|
272
|
|
|
* @return mixed |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function decodeJson($value) |
|
275
|
|
|
{ |
|
276
|
|
|
if ($this->isJson($value)) { |
|
277
|
|
|
return ($value === '""' || $value === '' ? '' : json_decode($value)); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
return $value; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|