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