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
|
|
|
$this->setUncached(false); |
59
|
|
|
|
60
|
|
|
if ($results) { |
61
|
|
|
return $this->returnResults($results, $key); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($default) { |
|
|
|
|
65
|
|
|
return $default; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->configHas($key)) { |
69
|
|
|
return $this->returnConfig($key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Forget a setting by key |
77
|
|
|
* |
78
|
|
|
* @param string $key |
79
|
|
|
* |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
|
|
public function forget($key) |
83
|
|
|
{ |
84
|
|
|
$result = $this->database |
|
|
|
|
85
|
|
|
->table('settings') |
86
|
|
|
->where('key', '=', $key) |
87
|
|
|
->delete(); |
88
|
|
|
|
89
|
|
|
if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) { |
90
|
|
|
$this->cache |
91
|
|
|
->forget($this->attachCacheTag($key)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $result ? true : false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check a setting exists by key |
99
|
|
|
* |
100
|
|
|
* @param string $key |
101
|
|
|
* |
102
|
|
|
* @return boolean |
103
|
|
|
*/ |
104
|
|
|
public function has($key) |
105
|
|
|
{ |
106
|
|
|
if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) { |
107
|
|
|
$result = true; |
108
|
|
|
} else { |
109
|
|
|
$result = $this->database |
|
|
|
|
110
|
|
|
->table('settings') |
111
|
|
|
->select('settings.value') |
112
|
|
|
->where('settings.key', '=', $key) |
113
|
|
|
->count(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return ($result ? true : false); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get all stored settings |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function all() |
125
|
|
|
{ |
126
|
|
|
$results = $this->database |
|
|
|
|
127
|
|
|
->table('settings') |
128
|
|
|
->lists('value', 'key'); |
129
|
|
|
|
130
|
|
|
return $this->arrangeResults($results); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Clear all stored settings |
135
|
|
|
* |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
public function clear() |
139
|
|
|
{ |
140
|
|
|
$result = $this->database |
|
|
|
|
141
|
|
|
->table('settings') |
142
|
|
|
->truncate(); |
143
|
|
|
|
144
|
|
|
if ($this->cacheEnabled()) { |
145
|
|
|
$this->cacheClear(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $result ? true : false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the expiry |
153
|
|
|
* |
154
|
|
|
* @param boolean|integer|Datetime $expiry |
155
|
|
|
* |
156
|
|
|
* @throws SettingArgumentException |
157
|
|
|
* |
158
|
|
|
* @return self |
159
|
|
|
*/ |
160
|
|
|
public function expires($expiry) |
161
|
|
|
{ |
162
|
|
|
$this->cacheExpires($expiry); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Enable caching. |
169
|
|
|
* |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
public function enableCache() |
173
|
|
|
{ |
174
|
|
|
$this->setCacheEnabled(true); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Disable caching. |
181
|
|
|
* |
182
|
|
|
* @return self |
183
|
|
|
*/ |
184
|
|
|
public function disableCache() |
185
|
|
|
{ |
186
|
|
|
$this->setCacheEnabled(false); |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Sets the uncached flag to request an item from the DB and re-cache the item. |
193
|
|
|
* |
194
|
|
|
* @return self |
195
|
|
|
*/ |
196
|
|
|
public function uncached() |
197
|
|
|
{ |
198
|
|
|
$this->setUncached(true); |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set the cache expiry |
205
|
|
|
* |
206
|
|
|
* @param boolean|integer|Datetime $expiry |
207
|
|
|
* |
208
|
|
|
* @throws SettingArgumentException |
209
|
|
|
* |
210
|
|
|
* @return self |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function cacheExpires($expiry) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
if (!is_bool($expiry) && !is_integer($expiry) && !$expiry instanceof DateTime) { |
215
|
|
|
throw new SettingArgumentException('Expiry is required in boolean, integer or DateTime format.'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->cacheExpiry = $expiry; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.