1
|
|
|
<?php namespace Cornford\Setter; |
2
|
|
|
|
3
|
|
|
use Cornford\Setter\Contracts\CacheableInterface; |
4
|
|
|
use Cornford\Setter\Contracts\SettableInterface; |
5
|
|
|
use Cornford\Setter\Exceptions\SettingArgumentException; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class Setting extends SettingBase implements SettableInterface, CacheableInterface { |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Set a setting by key and value |
12
|
|
|
* |
13
|
|
|
* @param string $key |
14
|
|
|
* @param string $value |
15
|
|
|
* |
16
|
|
|
* @return boolean |
17
|
|
|
*/ |
18
|
|
|
public function set($key, $value) |
19
|
|
|
{ |
20
|
|
|
$query = $this->database |
|
|
|
|
21
|
|
|
->table('settings'); |
22
|
|
|
$value = json_encode($value); |
23
|
|
|
|
24
|
|
|
if ($this->has($key)) { |
25
|
|
|
$result = $query->where('key', $key) |
26
|
|
|
->update(array('value' => $value)); |
27
|
|
|
} else { |
28
|
|
|
$result = $query->insert(array('key' => $key, 'value' => $value)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ($this->cacheEnabled()) { |
32
|
|
|
$this->recacheItem($value, $key); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $result ? true : false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get a setting by key, optionally set a default or fallback to config lookup |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* @param string $default |
43
|
|
|
* |
44
|
|
|
* @return string|array|boolean |
45
|
|
|
*/ |
46
|
|
|
public function get($key, $default = null) |
47
|
|
|
{ |
48
|
|
|
if (!$this->getUncached() && $this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) { |
49
|
|
|
return $this->returnCache($key); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$results = $this->database |
|
|
|
|
53
|
|
|
->table('settings') |
54
|
|
|
->where('settings.key', '=', $key) |
55
|
|
|
->whereRaw('settings.key LIKE "' . $key . '.%"', array(), 'or') |
56
|
|
|
->lists('value', 'key'); |
57
|
|
|
|
58
|
|
|
if ($results) { |
59
|
|
|
return $this->returnResults($results, $key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($default) { |
|
|
|
|
63
|
|
|
return $default; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->configHas($key)) { |
67
|
|
|
return $this->returnConfig($key); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Forget a setting by key |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function forget($key) |
81
|
|
|
{ |
82
|
|
|
$result = $this->database |
|
|
|
|
83
|
|
|
->table('settings') |
84
|
|
|
->where('key', '=', $key) |
85
|
|
|
->delete(); |
86
|
|
|
|
87
|
|
|
if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) { |
88
|
|
|
$this->cache |
89
|
|
|
->forget($this->attachCacheTag($key)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $result ? true : false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check a setting exists by key |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* |
100
|
|
|
* @return boolean |
101
|
|
|
*/ |
102
|
|
|
public function has($key) |
103
|
|
|
{ |
104
|
|
|
if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) { |
105
|
|
|
$result = true; |
106
|
|
|
} else { |
107
|
|
|
$result = $this->database |
|
|
|
|
108
|
|
|
->table('settings') |
109
|
|
|
->select('settings.value') |
110
|
|
|
->where('settings.key', '=', $key) |
111
|
|
|
->count(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return ($result ? true : false); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get all stored settings |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function all() |
123
|
|
|
{ |
124
|
|
|
$results = $this->database |
|
|
|
|
125
|
|
|
->table('settings') |
126
|
|
|
->lists('value', 'key'); |
127
|
|
|
|
128
|
|
|
return $this->arrangeResults($results); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Clear all stored settings |
133
|
|
|
* |
134
|
|
|
* @return boolean |
135
|
|
|
*/ |
136
|
|
|
public function clear() |
137
|
|
|
{ |
138
|
|
|
$result = $this->database |
|
|
|
|
139
|
|
|
->table('settings') |
140
|
|
|
->truncate(); |
141
|
|
|
|
142
|
|
|
if ($this->cacheEnabled()) { |
143
|
|
|
$this->cacheClear(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $result ? true : false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set the expiry |
151
|
|
|
* |
152
|
|
|
* @param boolean|integer|Datetime $expiry |
153
|
|
|
* |
154
|
|
|
* @throws SettingArgumentException |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
|
|
public function expires($expiry) |
159
|
|
|
{ |
160
|
|
|
$this->cacheExpires($expiry); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Enable caching. |
167
|
|
|
* |
168
|
|
|
* @return self |
169
|
|
|
*/ |
170
|
|
|
public function enableCache() |
171
|
|
|
{ |
172
|
|
|
$this->setCacheEnabled(true); |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Disable caching. |
179
|
|
|
* |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
|
|
public function disableCache() |
183
|
|
|
{ |
184
|
|
|
$this->setCacheEnabled(false); |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Sets the uncached flag to request an item from the DB and re-cache the item. |
191
|
|
|
* |
192
|
|
|
* @return self |
193
|
|
|
*/ |
194
|
|
|
public function uncached() |
195
|
|
|
{ |
196
|
|
|
$this->setUncached(true); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the cache expiry |
201
|
|
|
* |
202
|
|
|
* @param boolean|integer|Datetime $expiry |
203
|
|
|
* |
204
|
|
|
* @throws SettingArgumentException |
205
|
|
|
* |
206
|
|
|
* @return self |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
public function cacheExpires($expiry) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
if (!is_bool($expiry) && !is_integer($expiry) && !$expiry instanceof DateTime) { |
211
|
|
|
throw new SettingArgumentException('Expiry is required in boolean, integer or DateTime format.'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->cacheExpiry = $expiry; |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|