1 <?php namespace CupOfTea\EasyCfg;
2
3 use DB;
4 use Closure;
5
6 use CupOfTea\Package\Package;
7 use CupOfTea\EasyCfg\Exceptions\InvalidKeyException;
8 use CupOfTea\EasyCfg\Contracts\Provider as ProviderContract;
9
10 use Illuminate\Foundation\Application;
11 use Illuminate\Database\Eloquent\Model;
12
13 class EasyCfg implements ProviderContract
14 {
15
16 use Package;
17
18 19 20 21 22
23 const PACKAGE = 'CupOfTea/EasyCfg';
24 25 26 27 28
29 const VERSION = '1.1.1';
30
31 32 33 34 35
36 protected $all = [];
37 38 39 40 41
42 protected $cfg = [];
43 44 45 46 47
48 protected $all_id = [];
49 50 51 52 53
54 protected $cfg_id = [];
55
56 57 58 59 60 61
62 protected function getConfigurable($configurable)
63 {
64 if (is_a(Application::class, $configurable)) {
65 return null;
66 }
67
68 if (is_object($configurable)) {
69 return get_class($configurable);
70 }
71
72 return $configurable;
73 }
74
75 76 77 78 79 80 81
82 protected function getConfigurableId($configurable, $configurable_id)
83 {
84 if($configurable_id !== null){
85 return $configurable_id;
86 }
87
88 if (is_a($configurable, Model::class)) {
89 return $configurable->primaryKey;
90 }
91
92 if (is_object($configurable) && isset($configurable->id)) {
93 return $configurable->id;
94 }
95
96 return $configurable_id;
97 }
98
99 100 101 102 103 104
105 protected function mapAll($all)
106 {
107 $map = [];
108 foreach ($all as $result) {
109 $map[$this->key($result)] = $this->value($result);
110 }
111
112 return $map;
113 }
114
115 116 117 118 119 120 121
122 protected function getValues($k = null, $id = null)
123 {
124 if ($k === null) {
125 return isset($this->all[Application::class]) ? $this->all[Application::class] : null;
126 } elseif ($id === null) {
127 return isset($this->all[$k]) ? $this->all[$k] : null;
128 } else {
129 return isset($this->all_id[$k]) ? isset($this->all_id[$k][$id]) ? $this->all_id[$k][$id] : null : null;
130 }
131 }
132
133 134 135 136 137 138 139
140 protected function unsetValues($k = null, $id = null)
141 {
142 if ($k === null) {
143 if (isset($this->all[Application::class])) {
144 unset($this->all[Application::class]);
145 }
146 } elseif ($id === null) {
147 if (isset($this->all[$k])){
148 unset($this->all[$k]);
149 }
150 } else {
151 if (isset($this->cfg_id[$k]) && isset($this->cfg_id[$k][$id])) {
152 unset($this->cfg_id[$k][$id]);
153 }
154 }
155 }
156
157 158 159 160 161 162 163 164
165 protected function getValue($key, $configurable = null, $configurable_id = null)
166 {
167 $k = $configurable ? $configurable . ':' . $key : $key;
168
169 if ($configurable_id !== null) {
170 return isset($this->cfg_id[$k]) ? isset($this->cfg_id[$k][$configurable_id]) ? $this->cfg_id[$k][$configurable_id] : null : null;
171 } else {
172 return isset($this->cfg[$k]) ? $this->cfg[$k] : null;
173 }
174 }
175
176 177 178 179 180 181 182 183 184
185 protected function setValue($key, $result, $configurable = null, $configurable_id = null)
186 {
187 $k = $configurable ? $configurable . ':' . $key : $key;
188 $value = $this->value($result);
189
190 if ($configurable_id !== null) {
191 return $this->cfg_id[$k][$configurable_id] = $value;
192 } else {
193 return $this->cfg[$k] = $value;
194 }
195 }
196
197 198 199 200 201 202 203 204
205 protected function unsetValue($key, $configurable = null, $configurable_id = null)
206 {
207 $k = $configurable ? $configurable . ':' . $key : $key;
208
209 if ($configurable_id !== null) {
210 if (isset($this->cfg_id[$k]) && isset($this->cfg_id[$k][$configurable_id])) {
211 unset($this->cfg_id[$k][$configurable_id]);
212 }
213 } else {
214 if (isset($this->cfg[$k])){
215 unset($this->cfg[$k]);
216 }
217 }
218 }
219
220 221 222 223 224 225
226 protected function key($result)
227 {
228 if ($result === null || !isset($result->key)) {
229 return null;
230 }
231
232 return $result->key;
233 }
234
235 236 237 238 239 240
241 protected function value($result)
242 {
243 if ($result === null || !isset($result->value)) {
244 return null;
245 } else {
246 $value = $result->value;
247 }
248
249 $json = json_decode($value);
250
251 return $json ? $json : (string)$value;
252 }
253
254 255 256
257 public function all($configurable = null, $configurable_id = null)
258 {
259 $configurable_id = $this->getConfigurableId($configurable, $configurable_id);
260 $configurable = $this->getConfigurable($configurable);
261
262 if ($values = $this->getValues($configurable, $configurable_id) !== null) {
263 return $values;
264 }
265
266 if ($configurable === null) {
267 $result = DB::table(config('easycfg.table'))
268 ->whereNull('configurable')
269 ->get();
270
271 return $this->all[Application::class] = $this->mapAll($result);
272 } elseif ($configurable_id === null) {
273 $result = DB::table(config('easycfg.table'))
274 ->where('configurable', $configurable)
275 ->whereNull('configurable_id')
276 ->get();
277
278 return $this->all[$configurable] = $this->mapAll($result);
279 } else {
280 $result = DB::table(config('easycfg.table'))
281 ->where('configurable', $configurable)
282 ->whereNull('configurable_id')
283 ->orWhere(function($query) use ($configurable, $configurable_id) {
284 $query->where('configurable', $configurable)
285 ->where('configurable_id', $configurable_id);
286 })
287 ->get();
288
289 return $this->all_id[$configurable][$configurable_id] = $this->mapAll($result);
290 }
291 }
292
293 294 295
296 public function get($key, $configurable = null, $configurable_id = null)
297 {
298 $configurable_id = $this->getConfigurableId($configurable, $configurable_id);
299 $configurable = $this->getConfigurable($configurable);
300
301 if ($value = $this->getValue($key, $configurable, $configurable_id) !== null) {
302 return $value;
303 }
304
305 if ($configurable === null) {
306 $result = DB::table(config('easycfg.table'))
307 ->select('value')
308 ->where('key', $key)
309 ->whereNull('configurable')
310 ->first();
311
312 return $this->setValue($key, $result);
313 } elseif ($configurable_id === null) {
314 $result = DB::table(config('easycfg.table'))
315 ->select('value')
316 ->where('key', $key)
317 ->where('configurable', $configurable)
318 ->whereNull('configurable_id')
319 ->first();
320
321 return $this->setValue($key, $result, $configurable);
322 } else {
323 $result = DB::table(config('easycfg.table'))
324 ->select('value')
325 ->where('key', $key)
326 ->where('configurable', $configurable)
327 ->where('configurable_id', $configurable_id)
328 ->first();
329
330 return $this->setValue($key, $result, $configurable, $configurable_id);
331 }
332 }
333
334 335 336
337 public function set($key, $value, $configurable = null, $configurable_id = null)
338 {
339 $configurable_id = $this->getConfigurableId($configurable, $configurable_id);
340 $configurable = $this->getConfigurable($configurable);
341
342 if (str_contains($key, ':')) {
343 throw new InvalidKeyException('The character \':\' is not allowed in the Configuration key.');
344 } elseif (strlen($key) > 128) {
345 throw new InvalidKeyException('The Configuration key is too long (max length is 128 characters).');
346 }
347
348 if ($value instanceof Closure) {
349 $value = $value();
350 }
351
352 $this->setValue($key, $value, $configurable, $configurable_id);
353
354 if (is_array($value) || is_object($value)) {
355 $value = json_encode($value);
356 }
357
358 $value = (string)$value;
359
360 if ($configurable === null) {
361 if ($this->get($key)) {
362 return DB::table(config('easycfg.table'))
363 ->where('key', $key)
364 ->whereNull('configurable')
365 ->update(['value' => $value]);
366 } else {
367 return DB::table(config('easycfg.table'))
368 ->insert(['key' => $key, 'value' => $value]);
369 }
370 } elseif ($configurable_id === null) {
371 if ($this->get($key, $configurable)) {
372 return DB::table(config('easycfg.table'))
373 ->where('configurable', $configurable)
374 ->update(['value' => $value]);
375 } else {
376 return DB::table(config('easycfg.table'))
377 ->insert(['key' => $key, 'value' => $value, 'configurable' => $configurable]);
378 }
379 } else {
380 if ($this->get($key, $configurable, $configurable_id)) {
381 return DB::table(config('easycfg.table'))
382 ->where('configurable', $configurable)
383 ->where('configurable_id', $configurable_id)
384 ->update(['value' => $value]);
385 } else {
386 return DB::table(config('easycfg.table'))
387 ->insert(['key' => $key, 'value' => $value, 'configurable' => $configurable, 'configurable_id' => $configurable_id]);
388 }
389 }
390 }
391
392 393 394
395 public function delete($key, $configurable = null, $configurable_id = null)
396 {
397 $configurable_id = $this->getConfigurableId($configurable, $configurable_id);
398 $configurable = $this->getConfigurable($configurable);
399
400 $this->unsetValue($key, $configurable, $configurable_id);
401
402 if ($configurable === null) {
403 return DB::table(config('easycfg.table'))
404 ->where('key', $key)
405 ->whereNull('configurable')
406 ->delete();
407 } elseif ($configurable_id === null) {
408 return DB::table(config('easycfg.table'))
409 ->where('key', $key)
410 ->where('configurable', $configurable)
411 ->whereNull('configurable_id')
412 ->delete();
413 } else {
414 return DB::table(config('easycfg.table'))
415 ->where('key', $key)
416 ->where('configurable', $configurable)
417 ->where('configurable_id', $configurable_id)
418 ->delete();
419 }
420 }
421
422 423 424
425 public function deleteAll($configurable = null, $configurable_id = null)
426 {
427 $configurable_id = $this->getConfigurableId($configurable, $configurable_id);
428 $configurable = $this->getConfigurable($configurable);
429
430 $this->unsetValues($configurable, $configurable_id);
431
432 if ($configurable === null) {
433 return DB::table(config('easycfg.table'))
434 ->whereNull('configurable')
435 ->delete();
436 } elseif ($configurable_id === null) {
437 return DB::table(config('easycfg.table'))
438 ->where('configurable', $configurable)
439 ->whereNull('configurable_id')
440 ->delete();
441 } else {
442 return DB::table(config('easycfg.table'))
443 ->where('configurable', $configurable)
444 ->where('configurable_id', $configurable_id)
445 ->delete();
446 }
447 }
448
449 }
450