-
-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathCacheManager.php
445 lines (382 loc) · 14.9 KB
/
CacheManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <[email protected]>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
declare(strict_types=1);
namespace Phpfastcache;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Config\ConfigurationOptionInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotFoundException;
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotInstalledException;
use Phpfastcache\Exceptions\PhpfastcacheInstanceNotFoundException;
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException;
use Phpfastcache\Helper\UninstanciableObjectTrait;
use Phpfastcache\Util\ClassNamespaceResolverTrait;
/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class CacheManager
{
use ClassNamespaceResolverTrait;
use UninstanciableObjectTrait;
public const CORE_DRIVER_NAMESPACE = 'Phpfastcache\Drivers\\';
protected static ConfigurationOptionInterface $config;
protected static string $namespacePath;
/**
* @var ExtendedCacheItemPoolInterface[]
*/
protected static array $instances = [];
/**
* @var string[]
*/
protected static array $driverOverrides = [];
/**
* @var string[]
*/
protected static array $driverCustoms = [];
/**
* @var string[]
*/
protected static array $driverExtensions = [];
/**
* @param string $instanceId
* @return ExtendedCacheItemPoolInterface
* @throws PhpfastcacheInstanceNotFoundException
*/
public static function getInstanceById(string $instanceId): ExtendedCacheItemPoolInterface
{
if (isset(self::$instances[$instanceId])) {
return self::$instances[$instanceId];
}
throw new PhpfastcacheInstanceNotFoundException(sprintf('Instance ID %s not found', $instanceId));
}
/**
* Return the list of instances
*
* @return ExtendedCacheItemPoolInterface[]
*/
public static function getInstances(): array
{
return self::$instances;
}
/**
* @param string $driver
* @param ConfigurationOptionInterface|null $config
* @param string|null $instanceId
* @return ExtendedCacheItemPoolInterface
* @throws PhpfastcacheDriverCheckException
* @throws PhpfastcacheDriverException
* @throws PhpfastcacheDriverNotFoundException
* @throws PhpfastcacheExtensionNotInstalledException
* @throws PhpfastcacheLogicException
*/
public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null): ExtendedCacheItemPoolInterface
{
if (\class_exists($driver) && \str_starts_with($driver, 'Phpfastcache')) {
$driverClass = $driver;
} else {
$driver = self::normalizeDriverName($driver);
$driverClass = self::getDriverClass($driver);
}
$config = self::validateConfig($config);
$instanceId = $instanceId ?: self::getInstanceHash($driverClass, $config);
if (!isset(self::$instances[$instanceId])) {
if (\is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)) {
if (($configClass = $driverClass::getConfigClass()) !== $config::class) {
$config = new $configClass($config->toArray());
}
self::$instances[$instanceId] = new $driverClass(
$config,
$instanceId,
EventManager::getInstance()
);
} else {
try {
self::$driverExtensions[$driver] = ExtensionManager::getExtension($driver);
return CacheManager::getInstance($driver, $config, $instanceId);
} catch (PhpfastcacheExtensionNotFoundException) {
if (in_array($driver, ExtensionManager::KNOWN_EXTENSION_NAMES, true)) {
throw new PhpfastcacheExtensionNotInstalledException(sprintf(
'You requested a driver which is now an extension. Run the following command to solve this issue: %s',
sprintf('composer install phpfastcache/%s-extension', strtolower($driver))
));
}
throw new PhpfastcacheDriverNotFoundException(sprintf(
'The driver "%s" does not exist or does not implement %s.',
$driver,
ExtendedCacheItemPoolInterface::class,
));
}
}
}
return self::$instances[$instanceId];
}
/**
* @param string $driverClass
* @param ConfigurationOptionInterface $config
* @return string
*/
protected static function getInstanceHash(string $driverClass, ConfigurationOptionInterface $config): string
{
return \md5($driverClass . \serialize(
\array_filter(
$config->toArray(),
static fn ($val) => $config->isValueSerializable($val)
)
));
}
/**
* @param ConfigurationOptionInterface|null $config
* @return ConfigurationOptionInterface
* @throws PhpfastcacheLogicException
*/
protected static function validateConfig(?ConfigurationOptionInterface $config): ConfigurationOptionInterface
{
if ($config instanceof ConfigurationOptionInterface && $config->isLocked()) {
throw new PhpfastcacheLogicException('You provided an already locked configuration, cannot continue.');
}
return $config ?? self::getDefaultConfig();
}
/**
* @return ConfigurationOptionInterface
*/
public static function getDefaultConfig(): ConfigurationOptionInterface
{
return self::$config ?? self::$config = new ConfigurationOption();
}
/**
* @param string $driverName
* @return string
*/
public static function normalizeDriverName(string $driverName): string
{
return \ucfirst(\strtolower(\trim($driverName)));
}
/**
* @param string $driverName
* @return string
*/
public static function getDriverClass(string $driverName): string
{
if (!empty(self::$driverExtensions[$driverName])) {
$driverClass = self::$driverExtensions[$driverName];
} elseif (!empty(self::$driverCustoms[$driverName])) {
$driverClass = self::$driverCustoms[$driverName];
} elseif (!empty(self::$driverOverrides[$driverName])) {
$driverClass = self::$driverOverrides[$driverName];
} else {
$driverClass = self::getNamespacePath() . $driverName . '\Driver';
}
return $driverClass;
}
/**
* @return string
*/
public static function getNamespacePath(): string
{
return self::$namespacePath ?? self::getDefaultNamespacePath();
}
/**
* @return string
*/
public static function getDefaultNamespacePath(): string
{
return self::CORE_DRIVER_NAMESPACE;
}
/**
* @return bool
*/
public static function clearInstances(): bool
{
self::$instances = [];
\gc_collect_cycles();
return true;
}
/**
* @param ExtendedCacheItemPoolInterface $cachePoolInstance
* @return bool
* @since 7.0.4
*/
public static function clearInstance(ExtendedCacheItemPoolInterface $cachePoolInstance): bool
{
$found = false;
self::$instances = \array_filter(
\array_map(
static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found) {
if (\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)) {
$found = true;
return null;
}
return $cachePool;
},
self::$instances
)
);
return $found;
}
/**
* @param ConfigurationOptionInterface $config
* @throws PhpfastcacheInvalidArgumentException
*/
public static function setDefaultConfig(ConfigurationOptionInterface $config): void
{
if (\is_subclass_of($config, ConfigurationOption::class)) {
throw new PhpfastcacheInvalidArgumentException('Default configuration cannot be a child class of ConfigurationOption::class');
}
self::$config = $config;
}
/**
* @param string $driverName
* @param string $className
* @return void
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheUnsupportedOperationException
* @throws PhpfastcacheInvalidArgumentException
*/
public static function addCustomDriver(string $driverName, string $className): void
{
$driverName = self::normalizeDriverName($driverName);
if (empty($driverName)) {
throw new PhpfastcacheInvalidArgumentException("Can't add a custom driver because its name is empty");
}
if (!\class_exists($className)) {
throw new PhpfastcacheInvalidArgumentException(
\sprintf("Can't add '%s' because the class '%s' does not exist", $driverName, $className)
);
}
if (!empty(self::$driverCustoms[$driverName])) {
throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already added", $driverName));
}
if (\in_array($driverName, self::getDriverList(), true)) {
throw new PhpfastcacheLogicException(\sprintf("Driver '%s' is already a part of the Phpfastcache core", $driverName));
}
self::$driverCustoms[$driverName] = $className;
}
/**
* @param string $driverName
* @return bool
*/
public static function customDriverExists(string $driverName): bool
{
return isset(self::$driverCustoms[$driverName]);
}
/**
* Return the list of available drivers Capitalized
* with optional FQCN as key
*
* @param bool $fqcnAsKey Describe keys with Full Qualified Class Name
* @return string[]
* @throws PhpfastcacheUnsupportedOperationException
*/
public static function getDriverList(bool $fqcnAsKey = false): array
{
static $driverList;
if (self::getDefaultNamespacePath() === self::getNamespacePath()) {
if ($driverList === null) {
$prefix = self::CORE_DRIVER_NAMESPACE;
$classMap = self::createClassMap(__DIR__ . '/Drivers');
$driverList = [];
foreach (\array_keys($classMap) as $class) {
$driverList[] = \str_replace($prefix, '', \substr($class, 0, \strrpos($class, '\\')));
}
$driverList = \array_values(\array_unique($driverList));
}
$driverList = \array_merge($driverList, \array_keys(self::$driverCustoms));
if ($fqcnAsKey) {
$realDriverList = [];
foreach ($driverList as $driverName) {
$realDriverList[self::getDriverClass($driverName)] = $driverName;
}
$driverList = $realDriverList;
}
\asort($driverList);
return $driverList;
}
throw new PhpfastcacheUnsupportedOperationException('Cannot get the driver list if the default namespace path has changed.');
}
/**
* @param string $driverName
* @return void
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheInvalidArgumentException
*/
public static function removeCustomDriver(string $driverName): void
{
$driverName = self::normalizeDriverName($driverName);
if (empty($driverName)) {
throw new PhpfastcacheInvalidArgumentException("Can't remove a custom driver because its name is empty");
}
if (!isset(self::$driverCustoms[$driverName])) {
throw new PhpfastcacheLogicException(\sprintf("Driver '%s' does not exist", $driverName));
}
unset(self::$driverCustoms[$driverName]);
}
/**
* @param string $driverName
* @param string $className
* @return void
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheUnsupportedOperationException
* @throws PhpfastcacheInvalidArgumentException
*/
public static function addCoreDriverOverride(string $driverName, string $className): void
{
$driverName = self::normalizeDriverName($driverName);
if (empty($driverName)) {
throw new PhpfastcacheInvalidArgumentException("Can't add a core driver override because its name is empty");
}
if (!\class_exists($className)) {
throw new PhpfastcacheInvalidArgumentException(
\sprintf("Can't override '%s' because the class '%s' does not exist", $driverName, $className)
);
}
if (!empty(self::$driverOverrides[$driverName])) {
throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already overridden", $driverName));
}
if (!\in_array($driverName, self::getDriverList(), true)) {
throw new PhpfastcacheLogicException(\sprintf("Driver '%s' can't be overridden since its not a part of the Phpfastcache core", $driverName));
}
if (!\is_subclass_of($className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver', true)) {
throw new PhpfastcacheLogicException(
\sprintf(
"Can't override '%s' because the class '%s' MUST extend '%s'",
$driverName,
$className,
self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver'
)
);
}
self::$driverOverrides[$driverName] = $className;
}
/**
* @param string $driverName
* @return void
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheInvalidArgumentException
*/
public static function removeCoreDriverOverride(string $driverName): void
{
$driverName = self::normalizeDriverName($driverName);
if (empty($driverName)) {
throw new PhpfastcacheInvalidArgumentException("Can't remove a core driver override because its name is empty");
}
if (!isset(self::$driverOverrides[$driverName])) {
throw new PhpfastcacheLogicException(\sprintf("Driver '%s' were not overridden", $driverName));
}
unset(self::$driverOverrides[$driverName]);
}
}