1818use Phpfastcache \Config \ConfigurationOption ;
1919use Phpfastcache \Core \Pool \ExtendedCacheItemPoolInterface ;
2020use Phpfastcache \Exceptions \{
21- PhpfastcacheDriverCheckException , PhpfastcacheDriverException , PhpfastcacheDriverNotFoundException , PhpfastcacheInstanceNotFoundException , PhpfastcacheInvalidArgumentException , PhpfastcacheInvalidConfigurationException , PhpfastcacheLogicException , PhpfastcacheUnsupportedOperationException
21+ PhpfastcacheDriverCheckException , PhpfastcacheDriverException , PhpfastcacheDriverNotFoundException , PhpfastcacheExceptionInterface , PhpfastcacheInstanceNotFoundException , PhpfastcacheInvalidArgumentException , PhpfastcacheInvalidConfigurationException , PhpfastcacheLogicException , PhpfastcacheRootException , PhpfastcacheUnsupportedOperationException
2222};
2323use Phpfastcache \Util \ClassNamespaceResolverTrait ;
2424
@@ -112,61 +112,29 @@ class CacheManager
112112 */
113113 public static function getInstance (string $ driver = self ::AUTOMATIC_DRIVER_CLASS , $ config = null , string $ instanceId = null ): ExtendedCacheItemPoolInterface
114114 {
115- if (\is_array ($ config )) {
116- $ config = new ConfigurationOption ($ config );
117- \trigger_error (
118- 'The CacheManager will drops the support of primitive configuration arrays, use a "\Phpfastcache\Config\ConfigurationOption" object instead ' ,
119- E_USER_DEPRECATED
120- );
121- } elseif ($ config === null ) {
122- $ config = self ::getDefaultConfig ();
123- } else {
124- if (!($ config instanceof ConfigurationOption)) {
125- throw new PhpfastcacheInvalidArgumentException (\sprintf ('Unsupported config type: %s ' , \gettype ($ config )));
126- }
127- }
128-
115+ $ config = self ::validateConfig ($ config );
129116 $ driver = self ::standardizeDriverName ($ driver );
130117
131118 if (!$ driver || $ driver === self ::AUTOMATIC_DRIVER_CLASS ) {
132119 $ driver = self ::getAutoClass ($ config );
133120 }
134121
135- $ instance = $ instanceId ?: \md5 ($ driver . \serialize ($ config ->toArray ()));
122+ $ instanceId = $ instanceId ?: \md5 ($ driver . \serialize ($ config ->toArray ()));
136123
137- if (!isset (self ::$ instances [$ instance ])) {
124+ if (!isset (self ::$ instances [$ instanceId ])) {
138125 self ::$ badPracticeOmeter [$ driver ] = 1 ;
139- $ driverClass = self ::getDriverClass ($ driver );
140-
141- if (!is_a ($ driverClass , ExtendedCacheItemPoolInterface::class, true )) {
142- throw new PhpfastcacheDriverException (\sprintf (
143- 'Class "%s" does not implement "%s" ' ,
144- $ driverClass ,
145- ExtendedCacheItemPoolInterface::class
146- ));
147- }
126+ $ driverClass = self ::validateDriverClass (self ::getDriverClass ($ driver ));
127+
148128 try {
149129 if (\class_exists ($ driverClass )) {
150130 $ configClass = $ driverClass ::getConfigClass ();
151- self ::$ instances [$ instance ] = new $ driverClass (new $ configClass ($ config ->toArray ()), $ instance );
152- self ::$ instances [$ instance ]->setEventManager (EventManager::getInstance ());
131+ self ::$ instances [$ instanceId ] = new $ driverClass (new $ configClass ($ config ->toArray ()), $ instanceId );
132+ self ::$ instances [$ instanceId ]->setEventManager (EventManager::getInstance ());
153133 } else {
154134 throw new PhpfastcacheDriverNotFoundException (\sprintf ('The driver "%s" does not exists ' , $ driver ));
155135 }
156136 } catch (PhpfastcacheDriverCheckException $ e ) {
157- if ($ config ->getFallback ()) {
158- try {
159- $ fallback = $ config ->getFallback ();
160- $ config ->setFallback ('' );
161- \trigger_error (\sprintf ('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead. ' , $ driver ,
162- $ fallback ), E_USER_WARNING );
163- return self ::getInstance ($ fallback , $ config ->getFallbackConfig ());
164- } catch (PhpfastcacheInvalidArgumentException $ e ) {
165- throw new PhpfastcacheInvalidConfigurationException ('Invalid fallback driver configuration ' , 0 , $ e );
166- }
167- } else {
168- throw new PhpfastcacheDriverCheckException ($ e ->getMessage (), $ e ->getCode (), $ e );
169- }
137+ return self ::getFallbackInstance ($ driver , $ config , $ e );
170138 }
171139 } else {
172140 if (self ::$ badPracticeOmeter [$ driver ] >= 2 ) {
@@ -177,7 +145,34 @@ public static function getInstance(string $driver = self::AUTOMATIC_DRIVER_CLASS
177145
178146 self ::$ badPracticeOmeter [$ driver ]++;
179147
180- return self ::$ instances [$ instance ];
148+ return self ::$ instances [$ instanceId ];
149+ }
150+
151+ /**
152+ * @param string $driver
153+ * @param ConfigurationOption $config
154+ * @param PhpfastcacheDriverCheckException $e
155+ * @return ExtendedCacheItemPoolInterface
156+ * @throws PhpfastcacheDriverCheckException
157+ * @throws PhpfastcacheDriverException
158+ * @throws PhpfastcacheDriverNotFoundException
159+ * @throws PhpfastcacheInvalidConfigurationException
160+ */
161+ protected static function getFallbackInstance (string $ driver , ConfigurationOption $ config , PhpfastcacheDriverCheckException $ e )
162+ {
163+ if ($ config ->getFallback ()) {
164+ try {
165+ $ fallback = $ config ->getFallback ();
166+ $ config ->setFallback ('' );
167+ \trigger_error (\sprintf ('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead. ' , $ driver ,
168+ $ fallback ), E_USER_WARNING );
169+ return self ::getInstance ($ fallback , $ config ->getFallbackConfig ());
170+ } catch (PhpfastcacheInvalidArgumentException $ e ) {
171+ throw new PhpfastcacheInvalidConfigurationException ('Invalid fallback driver configuration ' , 0 , $ e );
172+ }
173+ } else {
174+ throw new PhpfastcacheDriverCheckException ($ e ->getMessage (), $ e ->getCode (), $ e );
175+ }
181176 }
182177
183178 /**
@@ -190,10 +185,6 @@ public static function getInstance(string $driver = self::AUTOMATIC_DRIVER_CLASS
190185 */
191186 public static function getInstanceById (string $ instanceId ): ExtendedCacheItemPoolInterface
192187 {
193- if ($ instanceId !== null && !\is_string ($ instanceId )) {
194- throw new PhpfastcacheInvalidArgumentException ('The Instance ID must be a string ' );
195- }
196-
197188 if (isset (self ::$ instances [$ instanceId ])) {
198189 return self ::$ instances [$ instanceId ];
199190 }
@@ -242,8 +233,9 @@ public static function getAutoClass(ConfigurationOption $config): string
242233
243234 if ($ autoDriver === null ) {
244235 foreach (self ::getDriverList () as $ driver ) {
245- /** @var ExtendedCacheItemPoolInterface $driver */
246- if ((self ::CORE_DRIVER_NAMESPACE . $ driver . '\Driver ' )::isUsableInAutoContext ()) {
236+ /** @var ExtendedCacheItemPoolInterface $driverClass */
237+ $ driverClass = self ::CORE_DRIVER_NAMESPACE . $ driver . '\Driver ' ;
238+ if ($ driverClass ::isUsableInAutoContext ()) {
247239 try {
248240 self ::getInstance ($ driver , $ config );
249241 $ autoDriver = $ driver ;
@@ -267,7 +259,7 @@ public static function getAutoClass(ConfigurationOption $config): string
267259 /**
268260 * @param string $name
269261 * @param array $arguments
270- * @return \Psr\Cache\ ExtendedCacheItemPoolInterface
262+ * @return ExtendedCacheItemPoolInterface
271263 */
272264 public static function __callStatic (string $ name , array $ arguments ): ExtendedCacheItemPoolInterface
273265 {
@@ -559,4 +551,46 @@ public static function removeCoreDriverOverride(string $driverName)
559551
560552 unset(self ::$ driverOverrides [$ driverName ]);
561553 }
554+
555+ /**
556+ * @param array|ConfigurationOption
557+ * @return ConfigurationOption
558+ * @throws PhpfastcacheInvalidArgumentException
559+ * @throws PhpfastcacheInvalidConfigurationException
560+ */
561+ protected static function validateConfig ($ config ): ConfigurationOption
562+ {
563+ if (\is_array ($ config )) {
564+ $ config = new ConfigurationOption ($ config );
565+ \trigger_error (
566+ 'The CacheManager will drops the support of primitive configuration arrays, use a "\Phpfastcache\Config\ConfigurationOption" object instead ' ,
567+ E_USER_DEPRECATED
568+ );
569+ } elseif ($ config === null ) {
570+ $ config = self ::getDefaultConfig ();
571+ } else {
572+ if (!($ config instanceof ConfigurationOption)) {
573+ throw new PhpfastcacheInvalidArgumentException (\sprintf ('Unsupported config type: %s ' , \gettype ($ config )));
574+ }
575+ }
576+
577+ return $ config ;
578+ }
579+
580+ /**
581+ * @param string $driverClass
582+ * @return string
583+ * @throws PhpfastcacheDriverException
584+ */
585+ protected static function validateDriverClass (string $ driverClass ): string
586+ {
587+ if (!is_a ($ driverClass , ExtendedCacheItemPoolInterface::class, true )) {
588+ throw new PhpfastcacheDriverException (\sprintf (
589+ 'Class "%s" does not implement "%s" ' ,
590+ $ driverClass ,
591+ ExtendedCacheItemPoolInterface::class
592+ ));
593+ }
594+ return $ driverClass ;
595+ }
562596}
0 commit comments