1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phpfastcache\Config; |
18
|
|
|
|
19
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; |
20
|
|
|
use Phpfastcache\Util\ArrayObject; |
21
|
|
|
use ReflectionException; |
22
|
|
|
use ReflectionMethod; |
23
|
|
|
use ReflectionParameter; |
24
|
|
|
use TypeError; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ConfigurationOption |
28
|
|
|
* @package Phpfastcache\Config |
29
|
|
|
*/ |
30
|
|
|
class ConfigurationOption extends ArrayObject implements ConfigurationOptionInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $itemDetailedDate = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $autoTmpFallback = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $defaultTtl = 900; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|Callable |
49
|
|
|
*/ |
50
|
|
|
protected $defaultKeyHashFunction = 'md5'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string|Callable |
54
|
|
|
*/ |
55
|
|
|
protected $defaultFileNameHashFunction = 'md5'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
protected $defaultChmod = 0777; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $path = ''; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
protected $limitedMemoryByObject = 4096; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
protected $compressData = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
protected $preventCacheSlams = false; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var int |
84
|
|
|
*/ |
85
|
|
|
protected $cacheSlamsTimeout = 15; |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $args |
90
|
|
|
* ArrayObject constructor. |
91
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
92
|
|
|
* @throws ReflectionException |
93
|
|
|
*/ |
94
|
|
|
public function __construct(...$args) |
95
|
|
|
{ |
96
|
|
|
parent::__construct(...$args); |
97
|
|
|
$array =& $this->getArray(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Detect unwanted keys and throw an exception. |
101
|
|
|
* No more kidding now, it's 21th century. |
102
|
|
|
*/ |
103
|
|
|
if (\array_diff_key($array, \get_object_vars($this))) { |
104
|
|
|
throw new PhpfastcacheInvalidConfigurationException( |
105
|
|
|
sprintf( |
106
|
|
|
'Invalid option(s) for the config %s: %s', |
107
|
|
|
static::class, |
108
|
|
|
\implode(', ', \array_keys(\array_diff_key($array, \get_object_vars($this)))) |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach (\get_object_vars($this) as $property => $value) { |
114
|
|
|
if (\array_key_exists($property, $array)) { |
115
|
|
|
$this->$property = &$array[$property]; |
116
|
|
|
} else { |
117
|
|
|
$array[$property] = &$this->$property; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach (\get_class_methods($this) as $method) { |
122
|
|
|
if (\strpos($method, 'set') === 0) { |
123
|
|
|
$value = null; |
|
|
|
|
124
|
|
|
try { |
125
|
|
|
/** |
126
|
|
|
* We use property instead of getter |
127
|
|
|
* because of is/get conditions and |
128
|
|
|
* to allow us to retrieve the value |
129
|
|
|
* in catch statement bloc |
130
|
|
|
*/ |
131
|
|
|
$value = $this->{\lcfirst(\substr($method, 3))}; |
132
|
|
|
$this->{$method}($value); |
133
|
|
|
} catch (TypeError $e) { |
134
|
|
|
$typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
135
|
|
|
$reflectionMethod = new ReflectionMethod($this, $method); |
136
|
|
|
$parameter = $reflectionMethod->getParameters()[0] ?? null; |
137
|
|
|
$typeHintExpected = ($parameter instanceof ReflectionParameter ? ($parameter->getType() === 'object' ? $parameter->getClass() : $parameter->getType( |
138
|
|
|
)) : 'Unknown type'); |
139
|
|
|
|
140
|
|
|
throw new PhpfastcacheInvalidConfigurationException( |
141
|
|
|
\sprintf( |
142
|
|
|
'Invalid type hint found for "%s", expected "%s" got "%s"', |
143
|
|
|
\lcfirst(\substr($method, 3)), |
144
|
|
|
$typeHintExpected, |
145
|
|
|
$typeHintGot |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $optionName |
155
|
|
|
* @return mixed|null |
156
|
|
|
*/ |
157
|
|
|
public function isValidOption(string $optionName) |
158
|
|
|
{ |
159
|
|
|
return property_exists($this, $optionName); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function isItemDetailedDate(): bool |
166
|
|
|
{ |
167
|
|
|
return $this->itemDetailedDate; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param bool $itemDetailedDate |
172
|
|
|
* @return ConfigurationOption |
173
|
|
|
*/ |
174
|
|
|
public function setItemDetailedDate(bool $itemDetailedDate): self |
175
|
|
|
{ |
176
|
|
|
$this->itemDetailedDate = $itemDetailedDate; |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function isAutoTmpFallback(): bool |
184
|
|
|
{ |
185
|
|
|
return $this->autoTmpFallback; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param bool $autoTmpFallback |
190
|
|
|
* @return ConfigurationOption |
191
|
|
|
*/ |
192
|
|
|
public function setAutoTmpFallback(bool $autoTmpFallback): self |
193
|
|
|
{ |
194
|
|
|
$this->autoTmpFallback = $autoTmpFallback; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
|
|
public function getDefaultTtl(): int |
202
|
|
|
{ |
203
|
|
|
return $this->defaultTtl; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param int $defaultTtl |
208
|
|
|
* @return ConfigurationOption |
209
|
|
|
*/ |
210
|
|
|
public function setDefaultTtl(int $defaultTtl): self |
211
|
|
|
{ |
212
|
|
|
$this->defaultTtl = $defaultTtl; |
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return Callable|string |
218
|
|
|
*/ |
219
|
|
|
public function getDefaultKeyHashFunction() |
220
|
|
|
{ |
221
|
|
|
return $this->defaultKeyHashFunction; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param Callable|string $defaultKeyHashFunction |
226
|
|
|
* @return ConfigurationOption |
227
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
228
|
|
|
*/ |
229
|
|
|
public function setDefaultKeyHashFunction($defaultKeyHashFunction): self |
230
|
|
|
{ |
231
|
|
|
if (!\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) { |
232
|
|
|
throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string'); |
233
|
|
|
} |
234
|
|
|
$this->defaultKeyHashFunction = $defaultKeyHashFunction; |
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return Callable|string |
240
|
|
|
*/ |
241
|
|
|
public function getDefaultFileNameHashFunction() |
242
|
|
|
{ |
243
|
|
|
return $this->defaultFileNameHashFunction; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param Callable|string $defaultFileNameHashFunction |
248
|
|
|
* @return ConfigurationOption |
249
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
250
|
|
|
*/ |
251
|
|
|
public function setDefaultFileNameHashFunction($defaultFileNameHashFunction): self |
252
|
|
|
{ |
253
|
|
|
if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) { |
254
|
|
|
throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
255
|
|
|
} |
256
|
|
|
$this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return int |
262
|
|
|
*/ |
263
|
|
|
public function getDefaultChmod(): int |
264
|
|
|
{ |
265
|
|
|
return $this->defaultChmod; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param int $defaultChmod |
270
|
|
|
* @return ConfigurationOption |
271
|
|
|
*/ |
272
|
|
|
public function setDefaultChmod(int $defaultChmod): self |
273
|
|
|
{ |
274
|
|
|
$this->defaultChmod = $defaultChmod; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
public function getPath(): string |
282
|
|
|
{ |
283
|
|
|
return $this->path; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $path |
288
|
|
|
* @return ConfigurationOption |
289
|
|
|
*/ |
290
|
|
|
public function setPath(string $path): self |
291
|
|
|
{ |
292
|
|
|
$this->path = $path; |
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return int |
298
|
|
|
*/ |
299
|
|
|
public function getLimitedMemoryByObject(): int |
300
|
|
|
{ |
301
|
|
|
return $this->limitedMemoryByObject; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param int $limitedMemoryByObject |
306
|
|
|
* @return ConfigurationOption |
307
|
|
|
*/ |
308
|
|
|
public function setLimitedMemoryByObject(int $limitedMemoryByObject): self |
309
|
|
|
{ |
310
|
|
|
$this->limitedMemoryByObject = $limitedMemoryByObject; |
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return bool |
316
|
|
|
*/ |
317
|
|
|
public function isCompressData(): bool |
318
|
|
|
{ |
319
|
|
|
return $this->compressData; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param bool $compressData |
324
|
|
|
* @return ConfigurationOption |
325
|
|
|
*/ |
326
|
|
|
public function setCompressData(bool $compressData): self |
327
|
|
|
{ |
328
|
|
|
$this->compressData = $compressData; |
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return bool |
334
|
|
|
*/ |
335
|
|
|
public function isPreventCacheSlams(): bool |
336
|
|
|
{ |
337
|
|
|
return $this->preventCacheSlams; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param bool $preventCacheSlams |
342
|
|
|
* @return ConfigurationOption |
343
|
|
|
*/ |
344
|
|
|
public function setPreventCacheSlams(bool $preventCacheSlams): self |
345
|
|
|
{ |
346
|
|
|
$this->preventCacheSlams = $preventCacheSlams; |
347
|
|
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return int |
352
|
|
|
*/ |
353
|
|
|
public function getCacheSlamsTimeout(): int |
354
|
|
|
{ |
355
|
|
|
return $this->cacheSlamsTimeout; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param int $cacheSlamsTimeout |
360
|
|
|
* @return ConfigurationOption |
361
|
|
|
*/ |
362
|
|
|
public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self |
363
|
|
|
{ |
364
|
|
|
$this->cacheSlamsTimeout = $cacheSlamsTimeout; |
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|