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