From 836db3c8eea5af8867c5ddb89154674c168178c7 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Fri, 12 May 2017 18:19:12 +0200 Subject: [PATCH 1/2] Fixed #456 // Bug of Exception workflow in cacheManager introduced in v6 --- src/phpFastCache/CacheManager.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/phpFastCache/CacheManager.php b/src/phpFastCache/CacheManager.php index 87271a622..f69f9d397 100644 --- a/src/phpFastCache/CacheManager.php +++ b/src/phpFastCache/CacheManager.php @@ -171,6 +171,7 @@ class CacheManager * @param array $config * @return ExtendedCacheItemPoolInterface * @throws phpFastCacheDriverCheckException + * @throws phpFastCacheInvalidConfigurationException */ public static function getInstance($driver = 'auto', array $config = []) { @@ -197,12 +198,20 @@ public static function getInstance($driver = 'auto', array $config = []) self::$instances[ $instance ] = new $class($config); self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); }catch(phpFastCacheDriverCheckException $e){ - $fallback = self::standardizeDriverName($config['fallback']); - if($fallback && $fallback !== $driver){ - $class = self::getNamespacePath() . $fallback . '\Driver'; - self::$instances[ $instance ] = new $class($config); - self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); - trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); + if($config['fallback']){ + try{ + $fallback = self::standardizeDriverName($config['fallback']); + if($fallback !== $driver){ + $class = self::getNamespacePath() . $fallback . '\Driver'; + self::$instances[ $instance ] = new $class($config); + self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); + trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); + }else{ + throw new phpFastCacheInvalidConfigurationException('The fallback driver cannot be the same than the default driver', 0, $e); + } + }catch (phpFastCacheInvalidArgumentException $e){ + throw new phpFastCacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e); + } }else{ throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e); } From 8a77cd06dfe228efb191870d339292ff860b5340 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Fri, 12 May 2017 18:22:02 +0200 Subject: [PATCH 2/2] Fixed ambiguous `$badPracticeOmeter` counter --- src/phpFastCache/CacheManager.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/phpFastCache/CacheManager.php b/src/phpFastCache/CacheManager.php index f69f9d397..46cefad26 100644 --- a/src/phpFastCache/CacheManager.php +++ b/src/phpFastCache/CacheManager.php @@ -216,11 +216,13 @@ public static function getInstance($driver = 'auto', array $config = []) throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e); } } - } else if(++$badPracticeOmeter[$driver] >= 5){ + } else if($badPracticeOmeter[$driver] >= 5){ trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); } + $badPracticeOmeter[$driver]++; + return self::$instances[ $instance ]; }