|
1
|
|
|
<?php namespace Arcanesoft\Settings\Stores; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Settings\Models\Setting; |
|
4
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class EloquentStore |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanesoft\Settings\Stores |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class EloquentStore |
|
13
|
|
|
{ |
|
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
*/ |
|
18
|
|
|
/** |
|
19
|
|
|
* The Setting Eloquent Model. |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Arcanesoft\Settings\Models\Setting |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $model; |
|
24
|
|
|
|
|
25
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
26
|
|
|
| Constructor |
|
27
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
28
|
|
|
*/ |
|
29
|
|
|
/** |
|
30
|
|
|
* SettingsManager constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Arcanesoft\Settings\Models\Setting $model |
|
33
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
|
34
|
|
|
*/ |
|
35
|
40 |
|
public function __construct(Setting $model, Cache $cache) |
|
36
|
|
|
{ |
|
37
|
40 |
|
$this->model = $model; |
|
38
|
40 |
|
$this->cache = $cache; |
|
|
|
|
|
|
39
|
40 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
42
|
|
|
| Getters & Setters |
|
43
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
44
|
|
|
*/ |
|
45
|
|
|
/** |
|
46
|
|
|
* Get the cache key. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
40 |
|
protected function getCacheKey() |
|
51
|
|
|
{ |
|
52
|
40 |
|
return $this->config('cache.key', 'cached_settings'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check if cache is enabled. |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
40 |
|
protected function isCached() |
|
61
|
|
|
{ |
|
62
|
40 |
|
return $this->config('cache.enabled', false); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the config value by key. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param mixed $default |
|
70
|
|
|
* |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
40 |
|
private function config($key, $default = null) |
|
74
|
|
|
{ |
|
75
|
40 |
|
return config("arcanesoft.settings.$key", $default); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
79
|
|
|
| Main Functions |
|
80
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
81
|
|
|
*/ |
|
82
|
40 |
|
public function all() |
|
83
|
|
|
{ |
|
84
|
40 |
|
return ! $this->isCached() |
|
85
|
30 |
|
? $this->model->all() |
|
86
|
40 |
|
: $this->cache->rememberForever($this->getCacheKey(), function() { |
|
87
|
40 |
|
return $this->model->all(); |
|
88
|
40 |
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
28 |
|
public function save($saved, $changes) |
|
92
|
|
|
{ |
|
93
|
28 |
|
$this->saveInserted($changes['inserted']); |
|
94
|
28 |
|
$this->saveUpdated($saved, $changes['updated']); |
|
95
|
28 |
|
$this->saveDeleted($saved, $changes['deleted']); |
|
96
|
|
|
|
|
97
|
28 |
|
if ($this->isCached()) { |
|
98
|
28 |
|
$this->cache->forget($this->getCacheKey()); |
|
99
|
21 |
|
} |
|
100
|
28 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
103
|
|
|
| Other Functions |
|
104
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
105
|
|
|
*/ |
|
106
|
|
|
/** |
|
107
|
|
|
* Save the inserted entries. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $inserted |
|
110
|
|
|
*/ |
|
111
|
28 |
|
private function saveInserted(array $inserted) |
|
112
|
3 |
|
{ |
|
113
|
28 |
|
foreach ($inserted as $domain => $values) { |
|
114
|
24 |
|
foreach ($values as $key => $value) { |
|
115
|
24 |
|
$this->model->createOne($domain, $key, $value); |
|
116
|
18 |
|
} |
|
117
|
21 |
|
} |
|
118
|
28 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Save the updated entries. |
|
122
|
|
|
* |
|
123
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $saved |
|
124
|
|
|
* @param array $updated |
|
125
|
|
|
*/ |
|
126
|
28 |
|
private function saveUpdated($saved, array $updated) |
|
127
|
|
|
{ |
|
128
|
28 |
|
foreach ($updated as $domain => $values) { |
|
129
|
4 |
|
foreach ($values as $key => $value) { |
|
130
|
|
|
/** @var \Arcanesoft\Settings\Models\Setting $model */ |
|
131
|
4 |
|
$model = $saved->groupBy('domain')->get($domain)->where('key', $key)->first(); |
|
132
|
4 |
|
$model->updateValue($value); |
|
133
|
4 |
|
$model->save(); |
|
134
|
3 |
|
} |
|
135
|
21 |
|
} |
|
136
|
28 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Save the deleted entries. |
|
140
|
|
|
* |
|
141
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $saved |
|
142
|
|
|
* @param array $deleted |
|
143
|
|
|
*/ |
|
144
|
28 |
|
private function saveDeleted($saved, array $deleted) |
|
145
|
|
|
{ |
|
146
|
28 |
|
foreach ($deleted as $domain => $values) { |
|
147
|
12 |
|
foreach ($values as $key) { |
|
148
|
|
|
/** @var \Arcanesoft\Settings\Models\Setting $model */ |
|
149
|
12 |
|
$model = $saved->groupBy('domain')->get($domain)->where('key', $key)->first(); |
|
150
|
12 |
|
$model->delete(); |
|
151
|
9 |
|
} |
|
152
|
21 |
|
} |
|
153
|
28 |
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: