| Total Complexity | 65 |
| Total Lines | 487 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CacheManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CacheManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class CacheManager |
||
| 54 | { |
||
| 55 | const AUTOMATIC_DRIVER_CLASS = 'Auto'; |
||
| 56 | const CORE_DRIVER_NAMESPACE = 'Phpfastcache\Drivers\\'; |
||
| 57 | |||
| 58 | use ClassNamespaceResolverTrait; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ConfigurationOption |
||
| 62 | */ |
||
| 63 | protected static $config; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | public static $ReadHits = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | public static $WriteHits = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected static $namespacePath; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var ExtendedCacheItemPoolInterface[] |
||
| 82 | */ |
||
| 83 | protected static $instances = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected static $driverOverrides = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected static $driverCustoms = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected static $badPracticeOmeter = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $driver |
||
| 102 | * @param array|ConfigurationOption $config |
||
| 103 | * @param string $instanceId |
||
| 104 | * |
||
| 105 | * @return ExtendedCacheItemPoolInterface |
||
| 106 | * |
||
| 107 | * @throws PhpfastcacheDriverCheckException |
||
| 108 | * @throws PhpfastcacheInvalidConfigurationException |
||
| 109 | * @throws PhpfastcacheDriverNotFoundException |
||
| 110 | * @throws PhpfastcacheInvalidArgumentException |
||
| 111 | * @throws PhpfastcacheDriverException |
||
| 112 | */ |
||
| 113 | public static function getInstance(string $driver = 'auto', $config = null, string $instanceId = null): ExtendedCacheItemPoolInterface |
||
| 114 | { |
||
| 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 if(!($config instanceof ConfigurationOption)){ |
||
|
|
|||
| 124 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', gettype($config))); |
||
| 125 | } |
||
| 126 | |||
| 127 | $driver = self::standardizeDriverName($driver); |
||
| 128 | |||
| 129 | if (!$driver || $driver === self::AUTOMATIC_DRIVER_CLASS) { |
||
| 130 | $driver = self::getAutoClass($config); |
||
| 131 | } |
||
| 132 | |||
| 133 | $instance = $instanceId ?: md5($driver . \serialize($config->toArray())); |
||
| 134 | |||
| 135 | if (!isset(self::$instances[ $instance ])) { |
||
| 136 | self::$badPracticeOmeter[ $driver ] = 1; |
||
| 137 | $driverClass = self::getDriverClass($driver); |
||
| 138 | |||
| 139 | if(!is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)){ |
||
| 140 | throw new PhpfastcacheDriverException(sprintf( |
||
| 141 | 'Class "%s" does not implement "%s"', |
||
| 142 | $driverClass, |
||
| 143 | ExtendedCacheItemPoolInterface::class |
||
| 144 | )); |
||
| 145 | } |
||
| 146 | try { |
||
| 147 | if (\class_exists($driverClass)) { |
||
| 148 | $configClass = $driverClass::getConfigClass(); |
||
| 149 | self::$instances[ $instance ] = new $driverClass(new $configClass($config->toArray()), $instance); |
||
| 150 | self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); |
||
| 151 | } else { |
||
| 152 | throw new PhpfastcacheDriverNotFoundException(\sprintf('The driver "%s" does not exists', $driver)); |
||
| 153 | } |
||
| 154 | } catch (PhpfastcacheDriverCheckException $e) { |
||
| 155 | if ($config->getFallback()) { |
||
| 156 | try { |
||
| 157 | $fallback = $config->getFallback(); |
||
| 158 | $config->setFallback(''); |
||
| 159 | trigger_error(\sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); |
||
| 160 | return self::getInstance($fallback, $config->getFallbackConfig()); |
||
| 161 | } catch (PhpfastcacheInvalidArgumentException $e) { |
||
| 162 | throw new PhpfastcacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e); |
||
| 163 | } |
||
| 164 | } else { |
||
| 165 | throw new PhpfastcacheDriverCheckException($e->getMessage(), $e->getCode(), $e); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } else if (self::$badPracticeOmeter[ $driver ] >= 2) { |
||
| 169 | trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
||
| 170 | See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); |
||
| 171 | } |
||
| 172 | |||
| 173 | self::$badPracticeOmeter[ $driver ]++; |
||
| 174 | |||
| 175 | return self::$instances[ $instance ]; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $instanceId |
||
| 180 | * |
||
| 181 | * @return ExtendedCacheItemPoolInterface |
||
| 182 | * |
||
| 183 | * @throws PhpfastcacheInvalidArgumentException |
||
| 184 | * @throws PhpfastcacheInstanceNotFoundException |
||
| 185 | */ |
||
| 186 | public static function getInstanceById(string $instanceId): ExtendedCacheItemPoolInterface |
||
| 187 | { |
||
| 188 | if ($instanceId !== null && !\is_string($instanceId)) { |
||
| 189 | throw new PhpfastcacheInvalidArgumentException('The Instance ID must be a string'); |
||
| 190 | } |
||
| 191 | |||
| 192 | if (isset(self::$instances[ $instanceId ])) { |
||
| 193 | return self::$instances[ $instanceId ]; |
||
| 194 | } |
||
| 195 | |||
| 196 | throw new PhpfastcacheInstanceNotFoundException(\sprintf('Instance ID %s not found', $instanceId)); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * This method is intended for internal |
||
| 201 | * use only and should not be used for |
||
| 202 | * any external development use the |
||
| 203 | * getInstances() method instead |
||
| 204 | * |
||
| 205 | * @internal |
||
| 206 | * @return ExtendedCacheItemPoolInterface[] |
||
| 207 | */ |
||
| 208 | public static function getInstances(): array |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * This method is intended for internal |
||
| 215 | * use only and should not be used for |
||
| 216 | * any external development use the |
||
| 217 | * getInstances() method instead |
||
| 218 | * |
||
| 219 | * @todo Use a proper way to passe them as a reference ? |
||
| 220 | * @internal |
||
| 221 | * @return ExtendedCacheItemPoolInterface[] |
||
| 222 | */ |
||
| 223 | public static function &getInternalInstances(): array |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param ConfigurationOption $config |
||
| 230 | * @return string |
||
| 231 | * @throws PhpfastcacheDriverCheckException |
||
| 232 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
| 233 | */ |
||
| 234 | public static function getAutoClass(ConfigurationOption $config): string |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $name |
||
| 264 | * @param array $arguments |
||
| 265 | * @return \Psr\Cache\ExtendedCacheItemPoolInterface |
||
| 266 | */ |
||
| 267 | public static function __callStatic(string $name, array $arguments): ExtendedCacheItemPoolInterface |
||
| 268 | { |
||
| 269 | $options = (\array_key_exists(0, $arguments) && \is_array($arguments) ? $arguments[ 0 ] : []); |
||
| 270 | |||
| 271 | return self::getInstance($name, $options); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public static function clearInstances(): bool |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public static function getNamespacePath(): string |
||
| 289 | { |
||
| 290 | return self::$namespacePath ?: self::getDefaultNamespacePath(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public static function getDefaultNamespacePath(): string |
||
| 297 | { |
||
| 298 | return __NAMESPACE__ . '\Drivers\\'; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $path |
||
| 303 | * @deprecated This method has been deprecated as of V7, please use driver override feature instead |
||
| 304 | */ |
||
| 305 | public static function setNamespacePath($path) |
||
| 306 | { |
||
| 307 | trigger_error('This method has been deprecated as of V7, please use cache manager "override" or "custom driver" features instead', E_USER_DEPRECATED); |
||
| 308 | self::$namespacePath = \trim($path, "\\") . '\\'; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param ConfigurationOption $config |
||
| 313 | */ |
||
| 314 | public static function setDefaultConfig(ConfigurationOption $config) |
||
| 315 | { |
||
| 316 | self::$config = $config; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return ConfigurationOption |
||
| 321 | */ |
||
| 322 | public static function getDefaultConfig(): ConfigurationOption |
||
| 323 | { |
||
| 324 | return self::$config ?: self::$config = new ConfigurationOption(); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return array |
||
| 329 | * @deprecated As of V7 will be removed soon or later, use CacheManager::getDriverList() instead |
||
| 330 | */ |
||
| 331 | public static function getStaticSystemDrivers(): array |
||
| 332 | { |
||
| 333 | trigger_error(\sprintf('Method "%s" is deprecated as of the V7 and will be removed soon or later, use CacheManager::getDriverList() instead.', __METHOD__), E_USER_DEPRECATED); |
||
| 334 | return [ |
||
| 335 | 'Apc', |
||
| 336 | 'Apcu', |
||
| 337 | 'Cassandra', |
||
| 338 | 'Couchbase', |
||
| 339 | 'Couchdb', |
||
| 340 | 'Devnull', |
||
| 341 | 'Files', |
||
| 342 | 'Leveldb', |
||
| 343 | 'Memcache', |
||
| 344 | 'Memcached', |
||
| 345 | 'Memstatic', |
||
| 346 | 'Mongodb', |
||
| 347 | 'Predis', |
||
| 348 | 'Redis', |
||
| 349 | 'Riak', |
||
| 350 | 'Ssdb', |
||
| 351 | 'Sqlite', |
||
| 352 | 'Wincache', |
||
| 353 | 'Xcache', |
||
| 354 | 'Zenddisk', |
||
| 355 | 'Zendshm', |
||
| 356 | ]; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return array |
||
| 361 | * @deprecated As of V7 will be removed soon or later, use CacheManager::getDriverList() instead |
||
| 362 | */ |
||
| 363 | public static function getStaticAllDrivers(): array |
||
| 364 | { |
||
| 365 | trigger_error(\sprintf('Method "%s" is deprecated as of the V7 and will be removed soon or later, use CacheManager::getDriverList() instead.', __METHOD__), E_USER_DEPRECATED); |
||
| 366 | return \array_merge(self::getStaticSystemDrivers(), [ |
||
|
1 ignored issue
–
show
|
|||
| 367 | 'Devtrue', |
||
| 368 | 'Devfalse', |
||
| 369 | 'Cookie', |
||
| 370 | ]); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return string[] |
||
| 375 | * @throws PhpfastcacheUnsupportedOperationException |
||
| 376 | */ |
||
| 377 | public static function getDriverList(): array |
||
| 378 | { |
||
| 379 | static $driverList; |
||
| 380 | |||
| 381 | if(self::getDefaultNamespacePath() === self::getNamespacePath()){ |
||
| 382 | if($driverList === null){ |
||
| 383 | $prefix = self::CORE_DRIVER_NAMESPACE; |
||
| 384 | $classMap = self::createClassMap(__DIR__ . '/Drivers'); |
||
| 385 | $driverList = []; |
||
| 386 | |||
| 387 | foreach ($classMap as $class => $file) { |
||
| 388 | $driverList[] = str_replace($prefix, '', substr($class, 0, strrpos($class, '\\') )); |
||
| 389 | } |
||
| 390 | |||
| 391 | $driverList = array_values(array_unique($driverList)); |
||
| 392 | } |
||
| 393 | |||
| 394 | $driverList = array_merge($driverList, array_keys(self::$driverCustoms)); |
||
| 395 | |||
| 396 | sort($driverList); |
||
| 397 | |||
| 398 | return $driverList; |
||
| 399 | } |
||
| 400 | |||
| 401 | throw new PhpfastcacheUnsupportedOperationException('Cannot get the driver list if the default namespace path has changed.'); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $driverName |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public static function standardizeDriverName(string $driverName): string |
||
| 409 | { |
||
| 410 | return \ucfirst(\strtolower(\trim($driverName))); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $driverName |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public static function getDriverClass(string $driverName): string |
||
| 418 | { |
||
| 419 | if(!empty(self::$driverCustoms[$driverName])){ |
||
| 420 | $driverClass = self::$driverCustoms[$driverName]; |
||
| 421 | }else if(!empty(self::$driverOverrides[$driverName])){ |
||
| 422 | $driverClass = self::$driverOverrides[$driverName]; |
||
| 423 | } else{ |
||
| 424 | $driverClass = self::getNamespacePath() . $driverName . '\Driver'; |
||
| 425 | } |
||
| 426 | |||
| 427 | return $driverClass; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $driverName |
||
| 432 | * @param string $className |
||
| 433 | * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
||
| 434 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
| 435 | * @throws \Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | public static function addCustomDriver(string $driverName, string $className){ |
||
| 439 | $driverName = self::standardizeDriverName($driverName); |
||
| 440 | |||
| 441 | if(empty($driverName)){ |
||
| 442 | throw new PhpfastcacheInvalidArgumentException("Can't add a custom driver because its name is empty"); |
||
| 443 | } |
||
| 444 | |||
| 445 | if(!\class_exists($className)){ |
||
| 446 | throw new PhpfastcacheInvalidArgumentException( |
||
| 447 | sprintf("Can't add '%s' because the class '%s' does not exists", $driverName, $className) |
||
| 448 | ); |
||
| 449 | } |
||
| 450 | |||
| 451 | if(!empty(self::$driverCustoms[$driverName])){ |
||
| 452 | throw new PhpfastcacheLogicException(sprintf("Driver '%s' has been already added", $driverName)); |
||
| 453 | } |
||
| 454 | |||
| 455 | if(\in_array($driverName, self::getDriverList(), true)){ |
||
| 456 | throw new PhpfastcacheLogicException(sprintf("Driver '%s' is already a part of the PhpFastCache core", $driverName)); |
||
| 457 | } |
||
| 458 | |||
| 459 | self::$driverCustoms[$driverName] = $className; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $driverName |
||
| 464 | * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
||
| 465 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
| 466 | * @return void |
||
| 467 | */ |
||
| 468 | public static function removeCustomDriver(string $driverName) |
||
| 469 | { |
||
| 470 | $driverName = self::standardizeDriverName($driverName); |
||
| 471 | |||
| 472 | if(empty($driverName)){ |
||
| 473 | throw new PhpfastcacheInvalidArgumentException("Can't remove a custom driver because its name is empty"); |
||
| 474 | } |
||
| 475 | |||
| 476 | if(!isset(self::$driverCustoms[$driverName])){ |
||
| 477 | throw new PhpfastcacheLogicException(sprintf("Driver '%s' does not exists", $driverName)); |
||
| 478 | } |
||
| 479 | |||
| 480 | unset(self::$driverCustoms[$driverName]); |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $driverName |
||
| 485 | * @param string $className |
||
| 486 | * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
||
| 487 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
| 488 | * @throws \Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | public static function addCoreDriverOverride(string $driverName, string $className){ |
||
| 492 | $driverName = self::standardizeDriverName($driverName); |
||
| 493 | |||
| 494 | if(empty($driverName)){ |
||
| 495 | throw new PhpfastcacheInvalidArgumentException("Can't add a core driver override because its name is empty"); |
||
| 496 | } |
||
| 497 | |||
| 498 | if(!\class_exists($className)){ |
||
| 499 | throw new PhpfastcacheInvalidArgumentException( |
||
| 500 | sprintf("Can't override '%s' because the class '%s' does not exists", $driverName, $className) |
||
| 501 | ); |
||
| 502 | } |
||
| 503 | |||
| 504 | if(!empty(self::$driverOverrides[$driverName])){ |
||
| 505 | throw new PhpfastcacheLogicException(sprintf("Driver '%s' has been already overridden", $driverName)); |
||
| 506 | } |
||
| 507 | |||
| 508 | if(!\in_array($driverName, self::getDriverList(), true)){ |
||
| 509 | throw new PhpfastcacheLogicException(sprintf("Driver '%s' can't be overridden since its not a part of the PhpFastCache core", $driverName)); |
||
| 510 | } |
||
| 511 | |||
| 512 | if(!\is_subclass_of($className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver', true)){ |
||
| 513 | throw new PhpfastcacheLogicException( |
||
| 514 | sprintf("Can't override '%s' because the class '%s' MUST extend '%s'", $driverName, $className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver') |
||
| 515 | ); |
||
| 516 | } |
||
| 517 | |||
| 518 | self::$driverOverrides[$driverName] = $className; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param string $driverName |
||
| 523 | * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException |
||
| 524 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | public static function removeCoreDriverOverride(string $driverName) |
||
| 540 | } |
||
| 541 | } |
||
| 542 |