Skip to content

Commit 9adf424

Browse files
authored
Merge pull request #457 from Geolim4/final
Fixed #456 + Fixed ambiguous `$badPracticeOmeter` counter
2 parents 23c54c1 + 8a77cd0 commit 9adf424

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/phpFastCache/CacheManager.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class CacheManager
171171
* @param array $config
172172
* @return ExtendedCacheItemPoolInterface
173173
* @throws phpFastCacheDriverCheckException
174+
* @throws phpFastCacheInvalidConfigurationException
174175
*/
175176
public static function getInstance($driver = 'auto', array $config = [])
176177
{
@@ -197,21 +198,31 @@ public static function getInstance($driver = 'auto', array $config = [])
197198
self::$instances[ $instance ] = new $class($config);
198199
self::$instances[ $instance ]->setEventManager(EventManager::getInstance());
199200
}catch(phpFastCacheDriverCheckException $e){
200-
$fallback = self::standardizeDriverName($config['fallback']);
201-
if($fallback && $fallback !== $driver){
202-
$class = self::getNamespacePath() . $fallback . '\Driver';
203-
self::$instances[ $instance ] = new $class($config);
204-
self::$instances[ $instance ]->setEventManager(EventManager::getInstance());
205-
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING);
201+
if($config['fallback']){
202+
try{
203+
$fallback = self::standardizeDriverName($config['fallback']);
204+
if($fallback !== $driver){
205+
$class = self::getNamespacePath() . $fallback . '\Driver';
206+
self::$instances[ $instance ] = new $class($config);
207+
self::$instances[ $instance ]->setEventManager(EventManager::getInstance());
208+
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING);
209+
}else{
210+
throw new phpFastCacheInvalidConfigurationException('The fallback driver cannot be the same than the default driver', 0, $e);
211+
}
212+
}catch (phpFastCacheInvalidArgumentException $e){
213+
throw new phpFastCacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e);
214+
}
206215
}else{
207216
throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e);
208217
}
209218
}
210-
} else if(++$badPracticeOmeter[$driver] >= 5){
219+
} else if($badPracticeOmeter[$driver] >= 5){
211220
trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances.
212221
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F');
213222
}
214223

224+
$badPracticeOmeter[$driver]++;
225+
215226
return self::$instances[ $instance ];
216227
}
217228

0 commit comments

Comments
 (0)