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
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Config; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
19
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; |
20
|
|
|
use Phpfastcache\Util\ArrayObject; |
21
|
|
|
|
22
|
|
|
class ConfigurationOption extends ArrayObject implements ConfigurationOptionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $itemDetailedDate = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $autoTmpFallback = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
* @deprecated Do not use this option anymore |
37
|
|
|
*/ |
38
|
|
|
protected $ignoreSymfonyNotice = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $defaultTtl = 900; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string|Callable |
47
|
|
|
*/ |
48
|
|
|
protected $defaultKeyHashFunction = 'md5'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string|Callable |
52
|
|
|
*/ |
53
|
|
|
protected $defaultFileNameHashFunction = 'md5'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
protected $defaultChmod = 0777; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $path = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $fallback = ''; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \Phpfastcache\Config\ConfigurationOption |
72
|
|
|
*/ |
73
|
|
|
protected $fallbackConfig; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected $limitedMemoryByObject = 4096; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
protected $compressData = false; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var bool |
87
|
|
|
*/ |
88
|
|
|
protected $preventCacheSlams = false; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var int |
92
|
|
|
*/ |
93
|
|
|
protected $cacheSlamsTimeout = 15; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $cacheFileExtension = 'txt'; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $args |
102
|
|
|
* ArrayObject constructor. |
103
|
|
|
*/ |
104
|
|
|
public function __construct(...$args) |
105
|
|
|
{ |
106
|
|
|
parent::__construct(...$args); |
107
|
|
|
$array =& $this->getArray(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Detect unwanted keys and throw an exception. |
111
|
|
|
* No more kidding now, it's 21th century. |
112
|
|
|
*/ |
113
|
|
|
if (array_diff_key($array, get_object_vars($this))) { |
114
|
|
|
throw new PhpfastcacheInvalidConfigurationException(\sprintf( |
115
|
|
|
'Invalid option(s) for the config %s: %s', |
116
|
|
|
static::class, |
117
|
|
|
implode(', ', array_keys(array_diff_key($array, get_object_vars($this)))) |
118
|
|
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach (get_object_vars($this) as $property => $value) { |
122
|
|
|
|
123
|
|
|
if (array_key_exists($property, $array)) { |
124
|
|
|
$this->$property = &$array[$property]; |
125
|
|
|
} else { |
126
|
|
|
$array[$property] = &$this->$property; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
foreach (get_class_methods($this) as $method) { |
131
|
|
|
if (strpos($method, 'set') === 0) { |
132
|
|
|
$value = null; |
|
|
|
|
133
|
|
|
try { |
134
|
|
|
/** |
135
|
|
|
* We use property instead of getter |
136
|
|
|
* because of is/get conditions and |
137
|
|
|
* to allow us to retrieve the value |
138
|
|
|
* in catch statement bloc |
139
|
|
|
*/ |
140
|
|
|
$value = $this->{lcfirst(substr($method, 3))}; |
141
|
|
|
$this->{$method}($value); |
142
|
|
|
} catch (\TypeError $e) { |
143
|
|
|
$typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
144
|
|
|
$reflectionMethod = new \ReflectionMethod($this, $method); |
145
|
|
|
$parameter = $reflectionMethod->getParameters()[0] ?? null; |
146
|
|
|
$typeHintExpected = ($parameter instanceof \ReflectionParameter ? ($parameter->getType() === 'object' ? $parameter->getClass() : $parameter->getType()) : 'Unknown type'); |
147
|
|
|
|
148
|
|
|
throw new PhpfastcacheInvalidConfigurationException(\sprintf( |
149
|
|
|
'Invalid type hint found for "%s", expected "%s" got "%s"', |
150
|
|
|
lcfirst(substr($method, 3)), |
151
|
|
|
$typeHintExpected, |
152
|
|
|
$typeHintGot |
153
|
|
|
)); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $optionName |
161
|
|
|
* @return mixed|null |
162
|
|
|
* @deprecated Use ->getOptionName() instead |
163
|
|
|
*/ |
164
|
|
|
public function getOption(string $optionName) |
165
|
|
|
{ |
166
|
|
|
\trigger_error(\sprintf('Method "%s" is deprecated, use "getOptionName()" instead', __METHOD__), E_USER_DEPRECATED); |
167
|
|
|
return $this->$optionName ?? null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $optionName |
172
|
|
|
* @return mixed|null |
173
|
|
|
*/ |
174
|
|
|
public function isValidOption(string $optionName) |
175
|
|
|
{ |
176
|
|
|
return property_exists($this, $optionName); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function isItemDetailedDate(): bool |
183
|
|
|
{ |
184
|
|
|
return $this->itemDetailedDate; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param bool $itemDetailedDate |
189
|
|
|
* @return ConfigurationOption |
190
|
|
|
*/ |
191
|
|
|
public function setItemDetailedDate(bool $itemDetailedDate): self |
192
|
|
|
{ |
193
|
|
|
$this->itemDetailedDate = $itemDetailedDate; |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function isAutoTmpFallback(): bool |
201
|
|
|
{ |
202
|
|
|
return $this->autoTmpFallback; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param bool $autoTmpFallback |
207
|
|
|
* @return ConfigurationOption |
208
|
|
|
*/ |
209
|
|
|
public function setAutoTmpFallback(bool $autoTmpFallback): self |
210
|
|
|
{ |
211
|
|
|
$this->autoTmpFallback = $autoTmpFallback; |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return bool |
217
|
|
|
* @deprecated As of V7 |
218
|
|
|
*/ |
219
|
|
|
public function isIgnoreSymfonyNotice(): bool |
220
|
|
|
{ |
221
|
|
|
return $this->ignoreSymfonyNotice; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param bool $ignoreSymfonyNotice |
226
|
|
|
* @return ConfigurationOption |
227
|
|
|
* @deprecated As of V7 |
228
|
|
|
*/ |
229
|
|
|
public function setIgnoreSymfonyNotice(bool $ignoreSymfonyNotice): self |
230
|
|
|
{ |
231
|
|
|
if ($ignoreSymfonyNotice) { |
232
|
|
|
\trigger_error('Configuration option "ignoreSymfonyNotice" is deprecated as of the V7', E_USER_DEPRECATED); |
233
|
|
|
} |
234
|
|
|
$this->ignoreSymfonyNotice = $ignoreSymfonyNotice; |
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return int |
240
|
|
|
*/ |
241
|
|
|
public function getDefaultTtl(): int |
242
|
|
|
{ |
243
|
|
|
return $this->defaultTtl; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param int $defaultTtl |
248
|
|
|
* @return ConfigurationOption |
249
|
|
|
*/ |
250
|
|
|
public function setDefaultTtl(int $defaultTtl): self |
251
|
|
|
{ |
252
|
|
|
$this->defaultTtl = $defaultTtl; |
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return Callable|string |
258
|
|
|
*/ |
259
|
|
|
public function getDefaultKeyHashFunction() |
260
|
|
|
{ |
261
|
|
|
return $this->defaultKeyHashFunction; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param Callable|string $defaultKeyHashFunction |
266
|
|
|
* @return ConfigurationOption |
267
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
268
|
|
|
*/ |
269
|
|
|
public function setDefaultKeyHashFunction($defaultKeyHashFunction) |
270
|
|
|
{ |
271
|
|
|
if (!\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) { |
272
|
|
|
throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string'); |
273
|
|
|
} |
274
|
|
|
$this->defaultKeyHashFunction = $defaultKeyHashFunction; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return Callable|string |
280
|
|
|
*/ |
281
|
|
|
public function getDefaultFileNameHashFunction() |
282
|
|
|
{ |
283
|
|
|
return $this->defaultFileNameHashFunction; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param Callable|string $defaultKeyHashFunction |
288
|
|
|
* @return ConfigurationOption |
289
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
290
|
|
|
*/ |
291
|
|
|
public function setDefaultFileNameHashFunction($defaultFileNameHashFunction) |
292
|
|
|
{ |
293
|
|
|
if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) { |
294
|
|
|
throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
295
|
|
|
} |
296
|
|
|
$this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @return int |
302
|
|
|
*/ |
303
|
|
|
public function getDefaultChmod(): int |
304
|
|
|
{ |
305
|
|
|
return $this->defaultChmod; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param int $defaultChmod |
310
|
|
|
* @return ConfigurationOption |
311
|
|
|
*/ |
312
|
|
|
public function setDefaultChmod(int $defaultChmod): self |
313
|
|
|
{ |
314
|
|
|
$this->defaultChmod = $defaultChmod; |
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function getPath(): string |
322
|
|
|
{ |
323
|
|
|
return $this->path; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param string $path |
328
|
|
|
* @return ConfigurationOption |
329
|
|
|
*/ |
330
|
|
|
public function setPath(string $path): self |
331
|
|
|
{ |
332
|
|
|
$this->path = $path; |
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @return bool|string |
338
|
|
|
*/ |
339
|
|
|
public function getFallback() |
340
|
|
|
{ |
341
|
|
|
return $this->fallback; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param string $fallback |
346
|
|
|
* @return ConfigurationOption |
347
|
|
|
*/ |
348
|
|
|
public function setFallback(string $fallback): self |
349
|
|
|
{ |
350
|
|
|
$this->fallback = $fallback; |
351
|
|
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return \Phpfastcache\Config\ConfigurationOption|null |
356
|
|
|
*/ |
357
|
|
|
public function getFallbackConfig() |
358
|
|
|
{ |
359
|
|
|
return $this->fallbackConfig; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param \Phpfastcache\Config\ConfigurationOption|null $fallbackConfig |
364
|
|
|
* @return ConfigurationOption |
365
|
|
|
*/ |
366
|
|
|
public function setFallbackConfig($fallbackConfig): self |
367
|
|
|
{ |
368
|
|
|
if ($fallbackConfig !== null && !($fallbackConfig instanceof self)) { |
|
|
|
|
369
|
|
|
throw new PhpfastcacheInvalidArgumentException(\sprintf( |
370
|
|
|
'Invalid argument "%s" for %s', |
371
|
|
|
\gettype($fallbackConfig) === 'object' ? \get_class($fallbackConfig) : \gettype($fallbackConfig), |
372
|
|
|
__METHOD__ |
373
|
|
|
)); |
374
|
|
|
} |
375
|
|
|
$this->fallbackConfig = $fallbackConfig; |
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @return int |
381
|
|
|
*/ |
382
|
|
|
public function getLimitedMemoryByObject(): int |
383
|
|
|
{ |
384
|
|
|
return $this->limitedMemoryByObject; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param int $limitedMemoryByObject |
389
|
|
|
* @return ConfigurationOption |
390
|
|
|
*/ |
391
|
|
|
public function setLimitedMemoryByObject(int $limitedMemoryByObject): self |
392
|
|
|
{ |
393
|
|
|
$this->limitedMemoryByObject = $limitedMemoryByObject; |
394
|
|
|
return $this; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @return bool |
399
|
|
|
*/ |
400
|
|
|
public function isCompressData(): bool |
401
|
|
|
{ |
402
|
|
|
return $this->compressData; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @param bool $compressData |
407
|
|
|
* @return ConfigurationOption |
408
|
|
|
*/ |
409
|
|
|
public function setCompressData(bool $compressData): self |
410
|
|
|
{ |
411
|
|
|
$this->compressData = $compressData; |
412
|
|
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @return bool |
417
|
|
|
*/ |
418
|
|
|
public function isPreventCacheSlams(): bool |
419
|
|
|
{ |
420
|
|
|
return $this->preventCacheSlams; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param bool $preventCacheSlams |
425
|
|
|
* @return ConfigurationOption |
426
|
|
|
*/ |
427
|
|
|
public function setPreventCacheSlams(bool $preventCacheSlams): self |
428
|
|
|
{ |
429
|
|
|
$this->preventCacheSlams = $preventCacheSlams; |
430
|
|
|
return $this; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @return int |
435
|
|
|
*/ |
436
|
|
|
public function getCacheSlamsTimeout(): int |
437
|
|
|
{ |
438
|
|
|
return $this->cacheSlamsTimeout; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @param int $cacheSlamsTimeout |
443
|
|
|
* @return ConfigurationOption |
444
|
|
|
*/ |
445
|
|
|
public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self |
446
|
|
|
{ |
447
|
|
|
$this->cacheSlamsTimeout = $cacheSlamsTimeout; |
448
|
|
|
return $this; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @return string |
453
|
|
|
*/ |
454
|
|
|
public function getCacheFileExtension(): string |
455
|
|
|
{ |
456
|
|
|
return $this->cacheFileExtension; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @param string $cacheFileExtension |
461
|
|
|
* @return ConfigurationOption |
462
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
463
|
|
|
*/ |
464
|
|
|
public function setCacheFileExtension(string $cacheFileExtension): self |
465
|
|
|
{ |
466
|
|
|
/** |
467
|
|
|
* Feel free to propose your own one |
468
|
|
|
* by opening a pull request :) |
469
|
|
|
*/ |
470
|
|
|
static $safeFileExtensions = [ |
471
|
|
|
'txt', |
472
|
|
|
'cache', |
473
|
|
|
'db', |
474
|
|
|
'pfc', |
475
|
|
|
]; |
476
|
|
|
|
477
|
|
|
if (\strpos($cacheFileExtension, '.') !== false) { |
478
|
|
|
throw new PhpfastcacheInvalidConfigurationException('cacheFileExtension cannot contain a dot "."'); |
479
|
|
|
} |
480
|
|
|
if (!\in_array($cacheFileExtension, $safeFileExtensions, true)) { |
481
|
|
|
throw new PhpfastcacheInvalidConfigurationException( |
482
|
|
|
"Extension \"{$cacheFileExtension}\" is not safe, currently allowed extension names: " . \implode(', ', $safeFileExtensions) |
483
|
|
|
); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$this->cacheFileExtension = $cacheFileExtension; |
487
|
|
|
return $this; |
488
|
|
|
} |
489
|
|
|
} |