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
|
|
|
* @var bool |
89
|
|
|
*/ |
90
|
|
|
protected $useStaticItemCaching = true; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $args |
94
|
|
|
* ArrayObject constructor. |
95
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
96
|
|
|
* @throws ReflectionException |
97
|
|
|
*/ |
98
|
|
|
public function __construct(...$args) |
99
|
|
|
{ |
100
|
|
|
parent::__construct(...$args); |
101
|
|
|
$array =& $this->getArray(); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Detect unwanted keys and throw an exception. |
105
|
|
|
* No more kidding now, it's 21th century. |
106
|
|
|
*/ |
107
|
|
|
if (\array_diff_key($array, \get_object_vars($this))) { |
108
|
|
|
throw new PhpfastcacheInvalidConfigurationException( |
109
|
|
|
sprintf( |
110
|
|
|
'Invalid option(s) for the config %s: %s', |
111
|
|
|
static::class, |
112
|
|
|
\implode(', ', \array_keys(\array_diff_key($array, \get_object_vars($this)))) |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach (\get_object_vars($this) as $property => $value) { |
118
|
|
|
if (\array_key_exists($property, $array)) { |
119
|
|
|
$this->$property = &$array[$property]; |
120
|
|
|
} else { |
121
|
|
|
$array[$property] = &$this->$property; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach (\get_class_methods($this) as $method) { |
126
|
|
|
if (\strpos($method, 'set') === 0) { |
127
|
|
|
$value = null; |
|
|
|
|
128
|
|
|
try { |
129
|
|
|
/** |
130
|
|
|
* We use property instead of getter |
131
|
|
|
* because of is/get conditions and |
132
|
|
|
* to allow us to retrieve the value |
133
|
|
|
* in catch statement bloc |
134
|
|
|
*/ |
135
|
|
|
$value = $this->{\lcfirst(\substr($method, 3))}; |
136
|
|
|
$this->{$method}($value); |
137
|
|
|
} catch (TypeError $e) { |
138
|
|
|
$typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
139
|
|
|
$reflectionMethod = new ReflectionMethod($this, $method); |
140
|
|
|
$parameter = $reflectionMethod->getParameters()[0] ?? null; |
141
|
|
|
$typeHintExpected = ($parameter instanceof ReflectionParameter ? ($parameter->getType()->getName() === 'object' ? $parameter->getClass() : $parameter->getType( |
142
|
|
|
)->getName()) : 'Unknown type'); |
143
|
|
|
|
144
|
|
|
throw new PhpfastcacheInvalidConfigurationException( |
145
|
|
|
\sprintf( |
146
|
|
|
'Invalid type hint found for "%s", expected "%s" got "%s"', |
147
|
|
|
\lcfirst(\substr($method, 3)), |
148
|
|
|
$typeHintExpected, |
149
|
|
|
$typeHintGot |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $optionName |
159
|
|
|
* @return mixed|null |
160
|
|
|
*/ |
161
|
|
|
public function isValidOption(string $optionName) |
162
|
|
|
{ |
163
|
|
|
return property_exists($this, $optionName); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function isItemDetailedDate(): bool |
170
|
|
|
{ |
171
|
|
|
return $this->itemDetailedDate; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param bool $itemDetailedDate |
176
|
|
|
* @return ConfigurationOption |
177
|
|
|
*/ |
178
|
|
|
public function setItemDetailedDate(bool $itemDetailedDate): self |
179
|
|
|
{ |
180
|
|
|
$this->itemDetailedDate = $itemDetailedDate; |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function isAutoTmpFallback(): bool |
188
|
|
|
{ |
189
|
|
|
return $this->autoTmpFallback; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param bool $autoTmpFallback |
194
|
|
|
* @return ConfigurationOption |
195
|
|
|
*/ |
196
|
|
|
public function setAutoTmpFallback(bool $autoTmpFallback): self |
197
|
|
|
{ |
198
|
|
|
$this->autoTmpFallback = $autoTmpFallback; |
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return int |
204
|
|
|
*/ |
205
|
|
|
public function getDefaultTtl(): int |
206
|
|
|
{ |
207
|
|
|
return $this->defaultTtl; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param int $defaultTtl |
212
|
|
|
* @return ConfigurationOption |
213
|
|
|
*/ |
214
|
|
|
public function setDefaultTtl(int $defaultTtl): self |
215
|
|
|
{ |
216
|
|
|
$this->defaultTtl = $defaultTtl; |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return Callable|string |
222
|
|
|
*/ |
223
|
|
|
public function getDefaultKeyHashFunction() |
224
|
|
|
{ |
225
|
|
|
return $this->defaultKeyHashFunction; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param Callable|string $defaultKeyHashFunction |
230
|
|
|
* @return ConfigurationOption |
231
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
232
|
|
|
*/ |
233
|
|
|
public function setDefaultKeyHashFunction($defaultKeyHashFunction): self |
234
|
|
|
{ |
235
|
|
|
if (!\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) { |
236
|
|
|
throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string'); |
237
|
|
|
} |
238
|
|
|
$this->defaultKeyHashFunction = $defaultKeyHashFunction; |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return Callable|string |
244
|
|
|
*/ |
245
|
|
|
public function getDefaultFileNameHashFunction() |
246
|
|
|
{ |
247
|
|
|
return $this->defaultFileNameHashFunction; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param Callable|string $defaultFileNameHashFunction |
252
|
|
|
* @return ConfigurationOption |
253
|
|
|
* @throws PhpfastcacheInvalidConfigurationException |
254
|
|
|
*/ |
255
|
|
|
public function setDefaultFileNameHashFunction($defaultFileNameHashFunction): self |
256
|
|
|
{ |
257
|
|
|
if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) { |
258
|
|
|
throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
259
|
|
|
} |
260
|
|
|
$this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return int |
266
|
|
|
*/ |
267
|
|
|
public function getDefaultChmod(): int |
268
|
|
|
{ |
269
|
|
|
return $this->defaultChmod; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param int $defaultChmod |
274
|
|
|
* @return ConfigurationOption |
275
|
|
|
*/ |
276
|
|
|
public function setDefaultChmod(int $defaultChmod): self |
277
|
|
|
{ |
278
|
|
|
$this->defaultChmod = $defaultChmod; |
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
public function getPath(): string |
286
|
|
|
{ |
287
|
|
|
return $this->path; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $path |
292
|
|
|
* @return ConfigurationOption |
293
|
|
|
*/ |
294
|
|
|
public function setPath(string $path): self |
295
|
|
|
{ |
296
|
|
|
$this->path = $path; |
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @return int |
302
|
|
|
*/ |
303
|
|
|
public function getLimitedMemoryByObject(): int |
304
|
|
|
{ |
305
|
|
|
return $this->limitedMemoryByObject; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param int $limitedMemoryByObject |
310
|
|
|
* @return ConfigurationOption |
311
|
|
|
*/ |
312
|
|
|
public function setLimitedMemoryByObject(int $limitedMemoryByObject): self |
313
|
|
|
{ |
314
|
|
|
$this->limitedMemoryByObject = $limitedMemoryByObject; |
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
public function isCompressData(): bool |
322
|
|
|
{ |
323
|
|
|
return $this->compressData; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param bool $compressData |
328
|
|
|
* @return ConfigurationOption |
329
|
|
|
*/ |
330
|
|
|
public function setCompressData(bool $compressData): self |
331
|
|
|
{ |
332
|
|
|
$this->compressData = $compressData; |
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @return bool |
338
|
|
|
*/ |
339
|
|
|
public function isPreventCacheSlams(): bool |
340
|
|
|
{ |
341
|
|
|
return $this->preventCacheSlams; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param bool $preventCacheSlams |
346
|
|
|
* @return ConfigurationOption |
347
|
|
|
*/ |
348
|
|
|
public function setPreventCacheSlams(bool $preventCacheSlams): self |
349
|
|
|
{ |
350
|
|
|
$this->preventCacheSlams = $preventCacheSlams; |
351
|
|
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return int |
356
|
|
|
*/ |
357
|
|
|
public function getCacheSlamsTimeout(): int |
358
|
|
|
{ |
359
|
|
|
return $this->cacheSlamsTimeout; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param int $cacheSlamsTimeout |
364
|
|
|
* @return ConfigurationOption |
365
|
|
|
*/ |
366
|
|
|
public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self |
367
|
|
|
{ |
368
|
|
|
$this->cacheSlamsTimeout = $cacheSlamsTimeout; |
369
|
|
|
return $this; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return bool |
374
|
|
|
*/ |
375
|
|
|
public function isUseStaticItemCaching(): bool |
376
|
|
|
{ |
377
|
|
|
return $this->useStaticItemCaching; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param bool $useStaticItemCaching |
382
|
|
|
* @return ConfigurationOption |
383
|
|
|
*/ |
384
|
|
|
public function setUseStaticItemCaching(bool $useStaticItemCaching): self |
385
|
|
|
{ |
386
|
|
|
$this->useStaticItemCaching = $useStaticItemCaching; |
387
|
|
|
return $this; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|