2727use Phpfastcache \Exceptions \PhpfastcacheUnsupportedOperationException ;
2828use Phpfastcache \Util \ClassNamespaceResolverTrait ;
2929
30+ /**
31+ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
32+ */
3033class 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 " );
0 commit comments