Skip to content

Commit d60d47d

Browse files
committed
Reworked CacheManager::getInstance, fixed code quality
1 parent 3cd9685 commit d60d47d

File tree

6 files changed

+52
-18
lines changed

6 files changed

+52
-18
lines changed

lib/Phpfastcache/CacheManager.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException;
2828
use Phpfastcache\Util\ClassNamespaceResolverTrait;
2929

30+
/**
31+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
32+
*/
3033
class CacheManager
3134
{
3235
public const CORE_DRIVER_NAMESPACE = 'Phpfastcache\Drivers\\';
@@ -96,16 +99,18 @@ public static function getInstances(): array
9699
*/
97100
public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null): ExtendedCacheItemPoolInterface
98101
{
102+
if (\class_exists($driver) && \str_starts_with($driver, 'Phpfastcache')) {
103+
$driverClass = $driver;
104+
} else {
105+
$driver = self::normalizeDriverName($driver);
106+
$driverClass = self::validateDriverClass(self::getDriverClass($driver));
107+
}
99108
$config = self::validateConfig($config);
100-
$driver = self::standardizeDriverName($driver);
101-
$instanceId = $instanceId ?: md5($driver . \serialize(\array_filter($config->toArray(), static fn ($val) => !\is_callable($val))));
109+
$instanceId = $instanceId ?: self::getInstanceHash($driverClass, $config);
102110

103111
if (!isset(self::$instances[$instanceId])) {
104-
$driverClass = self::validateDriverClass(self::getDriverClass($driver));
105-
106-
if (\class_exists($driverClass)) {
107-
$configClass = $driverClass::getConfigClass();
108-
if ($configClass !== $config::class) {
112+
if (\is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)) {
113+
if (($configClass = $driverClass::getConfigClass()) !== $config::class) {
109114
$config = new $configClass($config->toArray());
110115
}
111116
self::$instances[$instanceId] = new $driverClass(
@@ -114,13 +119,32 @@ public static function getInstance(string $driver, ?ConfigurationOptionInterface
114119
EventManager::getInstance()
115120
);
116121
} else {
117-
throw new PhpfastcacheDriverNotFoundException(sprintf('The driver "%s" does not exists', $driver));
122+
throw new PhpfastcacheDriverNotFoundException(sprintf(
123+
'The driver "%s" does not exists or does not implements %s',
124+
$driver,
125+
ExtendedCacheItemPoolInterface::class
126+
));
118127
}
119128
}
120129

121130
return self::$instances[$instanceId];
122131
}
123132

133+
/**
134+
* @param string $driverClass
135+
* @param ConfigurationOptionInterface $config
136+
* @return string
137+
*/
138+
protected static function getInstanceHash(string $driverClass, ConfigurationOptionInterface $config): string
139+
{
140+
return \md5($driverClass . \serialize(
141+
\array_filter(
142+
$config->toArray(),
143+
static fn ($val) => $config->isValueSerializable($val)
144+
)
145+
));
146+
}
147+
124148
/**
125149
* @param ConfigurationOptionInterface|null $config
126150
* @return ConfigurationOptionInterface
@@ -146,7 +170,7 @@ public static function getDefaultConfig(): ConfigurationOptionInterface
146170
* @param string $driverName
147171
* @return string
148172
*/
149-
public static function standardizeDriverName(string $driverName): string
173+
public static function normalizeDriverName(string $driverName): string
150174
{
151175
return \ucfirst(\strtolower(\trim($driverName)));
152176
}
@@ -261,7 +285,7 @@ public static function setDefaultConfig(ConfigurationOptionInterface $config): v
261285
*/
262286
public static function addCustomDriver(string $driverName, string $className): void
263287
{
264-
$driverName = self::standardizeDriverName($driverName);
288+
$driverName = self::normalizeDriverName($driverName);
265289

266290
if (empty($driverName)) {
267291
throw new PhpfastcacheInvalidArgumentException("Can't add a custom driver because its name is empty");
@@ -335,7 +359,7 @@ public static function getDriverList(bool $fqcnAsKey = false): array
335359
*/
336360
public static function removeCustomDriver(string $driverName): void
337361
{
338-
$driverName = self::standardizeDriverName($driverName);
362+
$driverName = self::normalizeDriverName($driverName);
339363

340364
if (empty($driverName)) {
341365
throw new PhpfastcacheInvalidArgumentException("Can't remove a custom driver because its name is empty");
@@ -358,7 +382,7 @@ public static function removeCustomDriver(string $driverName): void
358382
*/
359383
public static function addCoreDriverOverride(string $driverName, string $className): void
360384
{
361-
$driverName = self::standardizeDriverName($driverName);
385+
$driverName = self::normalizeDriverName($driverName);
362386

363387
if (empty($driverName)) {
364388
throw new PhpfastcacheInvalidArgumentException("Can't add a core driver override because its name is empty");
@@ -400,7 +424,7 @@ public static function addCoreDriverOverride(string $driverName, string $classNa
400424
*/
401425
public static function removeCoreDriverOverride(string $driverName): void
402426
{
403-
$driverName = self::standardizeDriverName($driverName);
427+
$driverName = self::normalizeDriverName($driverName);
404428

405429
if (empty($driverName)) {
406430
throw new PhpfastcacheInvalidArgumentException("Can't remove a core driver override because its name is empty");

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public function toArray(): array
8484
return \get_object_vars($this);
8585
}
8686

87+
/**
88+
* @throws \ReflectionException
89+
*/
90+
public function isValueSerializable(mixed $val): bool
91+
{
92+
return !\is_callable($val) && !(is_object($val) && (new \ReflectionClass($val))->isAnonymous());
93+
}
94+
8795
/**
8896
* @param string $optionName
8997
* @return bool

lib/Phpfastcache/Config/ConfigurationOptionInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public function __construct(array $parameters = []);
2929
*/
3030
public function toArray(): array;
3131

32+
/**
33+
* @param mixed $val
34+
* @return bool
35+
*/
36+
public function isValueSerializable(mixed $val): bool;
37+
3238
/**
3339
* @param string $optionName
3440
* @return bool

lib/Phpfastcache/Drivers/Solr/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Config extends ConfigurationOption
4949

5050
protected array $mappingSchema = self::DEFAULT_MAPPING_SCHEMA;
5151

52-
private EventDispatcherInterface $eventDispatcher;
52+
protected EventDispatcherInterface $eventDispatcher;
5353

5454
public function __construct(array $parameters = [])
5555
{

lib/Phpfastcache/Drivers/Solr/Driver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
109109
$update = $this->instance->createUpdate();
110110

111111
$doc = $update->createDocument();
112-
/** @SuppressWarnings(PHPMD.UndefinedVariable) */
113112
$doc->{$this->getSolrField(self::SOLR_DEFAULT_ID_FIELD)} = $item->getEncodedKey();
114113
$doc->{$this->getSolrField(self::SOLR_DISCRIMINATOR_FIELD)} = self::SOLR_DISCRIMINATOR_VALUE;
115114
$doc->{$this->getSolrField(self::DRIVER_KEY_WRAPPER_INDEX)} = $item->getKey();

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,3 @@ parameters:
1111
-
1212
message: '#Cannot call method getPayload\(\) on int#' # Issue in predis/predis => https://github.com/predis/predis/pull/710
1313
path: lib/Phpfastcache/Drivers/Predis/Driver.php
14-
-
15-
message: '#Access to an undefined property Solarium\\Core\\Query\\DocumentInterface#' # Issue in predis/predis => https://github.com/predis/predis/pull/710
16-
path: lib/Phpfastcache/Drivers/Solr/Driver.php

0 commit comments

Comments
 (0)