woocommerce-pos
/
vendor_prefixed
/
phpfastcache
/
phpfastcache
/
lib
/
Phpfastcache
/
CacheManager.php
CacheManager.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.15, at vendor_prefixed/phpfastcache/phpfastcache/lib/Phpfastcache/CacheManager.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * This file is part of phpFastCache. |
| 6 | * |
| 7 | * @license MIT License (MIT) |
| 8 | * |
| 9 | * For full copyright and license information, please see the docs/CREDITS.txt file. |
| 10 | * |
| 11 | * @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com |
| 12 | * @author Georges.L (Geolim4) <contact@geolim4.com> |
| 13 | * |
| 14 | */ |
| 15 | declare (strict_types=1); |
| 16 | namespace WCPOS\Vendor\Phpfastcache; |
| 17 | |
| 18 | use WCPOS\Vendor\Phpfastcache\Cluster\AggregatablePoolInterface; |
| 19 | use WCPOS\Vendor\Phpfastcache\Config\ConfigurationOption; |
| 20 | use WCPOS\Vendor\Phpfastcache\Config\ConfigurationOptionInterface; |
| 21 | use WCPOS\Vendor\Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 22 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
| 23 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheDriverException; |
| 24 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException; |
| 25 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheInstanceNotFoundException; |
| 26 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
| 27 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; |
| 28 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheLogicException; |
| 29 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException; |
| 30 | use WCPOS\Vendor\Phpfastcache\Util\ClassNamespaceResolverTrait; |
| 31 | /** |
| 32 | * Class CacheManager |
| 33 | * @package phpFastCache |
| 34 | * |
| 35 | * @method static ExtendedCacheItemPoolInterface Apcu() Apcu($config = []) Return a driver "Apcu" instance |
| 36 | * @method static ExtendedCacheItemPoolInterface Cassandra() Cassandra($config = []) Return a driver "Cassandra" instance |
| 37 | * @method static ExtendedCacheItemPoolInterface Cookie() Cookie($config = []) Return a driver "Cookie" instance |
| 38 | * @method static ExtendedCacheItemPoolInterface Couchbase() Couchbase($config = []) Return a driver "Couchbase" instance |
| 39 | * @method static ExtendedCacheItemPoolInterface Couchdb() Couchdb($config = []) Return a driver "Couchdb" instance |
| 40 | * @method static ExtendedCacheItemPoolInterface Devnull() Devnull($config = []) Return a driver "Devnull" instance |
| 41 | * @method static ExtendedCacheItemPoolInterface Files() Files($config = []) Return a driver "files" instance |
| 42 | * @method static ExtendedCacheItemPoolInterface Leveldb() Leveldb($config = []) Return a driver "Leveldb" instance |
| 43 | * @method static ExtendedCacheItemPoolInterface Memcache() Memcache($config = []) Return a driver "Memcache" instance |
| 44 | * @method static ExtendedCacheItemPoolInterface Memcached() Memcached($config = []) Return a driver "Memcached" instance |
| 45 | * @method static ExtendedCacheItemPoolInterface Memstatic() Memstatic($config = []) Return a driver "Memstatic" instance |
| 46 | * @method static ExtendedCacheItemPoolInterface Mongodb() Mongodb($config = []) Return a driver "Mongodb" instance |
| 47 | * @method static ExtendedCacheItemPoolInterface Predis() Predis($config = []) Return a driver "Predis" instance |
| 48 | * @method static ExtendedCacheItemPoolInterface Redis() Redis($config = []) Return a driver "Pedis" instance |
| 49 | * @method static ExtendedCacheItemPoolInterface Sqlite() Sqlite($config = []) Return a driver "Sqlite" instance |
| 50 | * @method static ExtendedCacheItemPoolInterface Ssdb() Ssdb($config = []) Return a driver "Ssdb" instance |
| 51 | * @method static ExtendedCacheItemPoolInterface Wincache() Wincache($config = []) Return a driver "Wincache" instance |
| 52 | * @method static ExtendedCacheItemPoolInterface Zenddisk() Zenddisk($config = []) Return a driver "Zend disk cache" instance |
| 53 | * @method static ExtendedCacheItemPoolInterface Zendshm() Zendshm($config = []) Return a driver "Zend memory cache" instance |
| 54 | * |
| 55 | */ |
| 56 | class CacheManager |
| 57 | { |
| 58 | public const CORE_DRIVER_NAMESPACE = 'Phpfastcache\\Drivers\\'; |
| 59 | use ClassNamespaceResolverTrait; |
| 60 | /** |
| 61 | * @var ConfigurationOption |
| 62 | */ |
| 63 | protected static $config; |
| 64 | /** |
| 65 | * @var string |
| 66 | */ |
| 67 | protected static $namespacePath; |
| 68 | /** |
| 69 | * @var ExtendedCacheItemPoolInterface[] |
| 70 | */ |
| 71 | protected static $instances = []; |
| 72 | /** |
| 73 | * @var array |
| 74 | */ |
| 75 | protected static $driverOverrides = []; |
| 76 | /** |
| 77 | * @var array |
| 78 | */ |
| 79 | protected static $driverCustoms = []; |
| 80 | /** |
| 81 | * @var array |
| 82 | */ |
| 83 | protected static $badPracticeOmeter = []; |
| 84 | /** |
| 85 | * CacheManager constructor. |
| 86 | */ |
| 87 | protected final function __construct() |
| 88 | { |
| 89 | // The cache manager is not meant to be instantiated |
| 90 | } |
| 91 | /** |
| 92 | * @param string $instanceId |
| 93 | * @return ExtendedCacheItemPoolInterface |
| 94 | * @throws PhpfastcacheInstanceNotFoundException |
| 95 | */ |
| 96 | public static function getInstanceById(string $instanceId) : ExtendedCacheItemPoolInterface |
| 97 | { |
| 98 | if (isset(self::$instances[$instanceId])) { |
| 99 | return self::$instances[$instanceId]; |
| 100 | } |
| 101 | throw new PhpfastcacheInstanceNotFoundException(\sprintf('Instance ID %s not found', $instanceId)); |
| 102 | } |
| 103 | /** |
| 104 | * Return the list of instances |
| 105 | * |
| 106 | * @return ExtendedCacheItemPoolInterface[] |
| 107 | */ |
| 108 | public static function getInstances() : array |
| 109 | { |
| 110 | return self::$instances; |
| 111 | } |
| 112 | /** |
| 113 | * This method is intended for internal |
| 114 | * use only and should not be used for |
| 115 | * any external development use the |
| 116 | * getInstances() method instead |
| 117 | * |
| 118 | * @return ExtendedCacheItemPoolInterface[] |
| 119 | * @internal |
| 120 | * @todo Use a proper way to passe them as a reference ? |
| 121 | */ |
| 122 | public static function &getInternalInstances() : array |
| 123 | { |
| 124 | return self::$instances; |
| 125 | } |
| 126 | /** |
| 127 | * @param string $name |
| 128 | * @param array $arguments |
| 129 | * @return ExtendedCacheItemPoolInterface |
| 130 | * @throws PhpfastcacheDriverCheckException |
| 131 | * @throws PhpfastcacheDriverException |
| 132 | * @throws PhpfastcacheDriverNotFoundException |
| 133 | * @throws PhpfastcacheInvalidArgumentException |
| 134 | * @throws PhpfastcacheInvalidConfigurationException |
| 135 | * @throws PhpfastcacheLogicException |
| 136 | * @throws \ReflectionException |
| 137 | */ |
| 138 | public static function __callStatic(string $name, array $arguments) : ExtendedCacheItemPoolInterface |
| 139 | { |
| 140 | $options = \array_key_exists(0, $arguments) && \is_array($arguments) ? $arguments[0] : []; |
| 141 | return self::getInstance($name, $options); |
| 142 | } |
| 143 | /** |
| 144 | * @param string $driver |
| 145 | * @param ConfigurationOptionInterface $config |
| 146 | * @param string|null $instanceId |
| 147 | * @return ExtendedCacheItemPoolInterface|AggregatablePoolInterface |
| 148 | * @throws PhpfastcacheDriverCheckException |
| 149 | * @throws PhpfastcacheDriverException |
| 150 | * @throws PhpfastcacheDriverNotFoundException |
| 151 | * @throws PhpfastcacheInvalidArgumentException |
| 152 | * @throws PhpfastcacheInvalidConfigurationException |
| 153 | * @throws PhpfastcacheLogicException |
| 154 | * @throws \ReflectionException |
| 155 | */ |
| 156 | public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null) : ExtendedCacheItemPoolInterface |
| 157 | { |
| 158 | $config = self::validateConfig($config); |
| 159 | $driver = self::standardizeDriverName($driver); |
| 160 | $instanceId = $instanceId ?: \md5($driver . \serialize(\array_filter($config->toArray(), static function ($val) { |
| 161 | return !\is_callable($val); |
| 162 | }))); |
| 163 | if (!isset(self::$instances[$instanceId])) { |
| 164 | self::$badPracticeOmeter[$driver] = 1; |
| 165 | $driverClass = self::validateDriverClass(self::getDriverClass($driver)); |
| 166 | if (\class_exists($driverClass)) { |
| 167 | $configClass = $driverClass::getConfigClass(); |
| 168 | self::$instances[$instanceId] = new $driverClass(new $configClass($config->toArray()), $instanceId); |
| 169 | self::$instances[$instanceId]->setEventManager(EventManager::getInstance()); |
| 170 | } else { |
| 171 | throw new PhpfastcacheDriverNotFoundException(\sprintf('The driver "%s" does not exists', $driver)); |
| 172 | } |
| 173 | } else { |
| 174 | if (self::$badPracticeOmeter[$driver] >= 2) { |
| 175 | \trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
| 176 | See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); |
| 177 | } |
| 178 | } |
| 179 | self::$badPracticeOmeter[$driver]++; |
| 180 | return self::$instances[$instanceId]; |
| 181 | } |
| 182 | /** |
| 183 | * @param ConfigurationOptionInterface|null $config |
| 184 | * @return ConfigurationOption |
| 185 | * @throws PhpfastcacheInvalidArgumentException |
| 186 | * @throws PhpfastcacheInvalidConfigurationException |
| 187 | * @throws \ReflectionException |
| 188 | */ |
| 189 | protected static function validateConfig(?ConfigurationOptionInterface $config) : ConfigurationOption |
| 190 | { |
| 191 | if ($config === null) { |
| 192 | $config = self::getDefaultConfig(); |
| 193 | } else { |
| 194 | if (!$config instanceof ConfigurationOption) { |
| 195 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config))); |
| 196 | } |
| 197 | } |
| 198 | return $config; |
| 199 | } |
| 200 | /** |
| 201 | * @return ConfigurationOptionInterface |
| 202 | * @throws PhpfastcacheInvalidConfigurationException |
| 203 | * @throws \ReflectionException |
| 204 | */ |
| 205 | public static function getDefaultConfig() : ConfigurationOptionInterface |
| 206 | { |
| 207 | return self::$config ?: (self::$config = new ConfigurationOption()); |
| 208 | } |
| 209 | /** |
| 210 | * @param string $driverName |
| 211 | * @return string |
| 212 | */ |
| 213 | public static function standardizeDriverName(string $driverName) : string |
| 214 | { |
| 215 | return \ucfirst(\strtolower(\trim($driverName))); |
| 216 | } |
| 217 | /** |
| 218 | * @param string $driverClass |
| 219 | * @return string|ExtendedCacheItemPoolInterface |
| 220 | * @throws PhpfastcacheDriverException |
| 221 | */ |
| 222 | protected static function validateDriverClass(string $driverClass) : string |
| 223 | { |
| 224 | if (!\is_a($driverClass, ExtendedCacheItemPoolInterface::class, \true)) { |
| 225 | throw new PhpfastcacheDriverException(\sprintf('Class "%s" does not implement "%s"', $driverClass, ExtendedCacheItemPoolInterface::class)); |
| 226 | } |
| 227 | return $driverClass; |
| 228 | } |
| 229 | /** |
| 230 | * @param string $driverName |
| 231 | * @return string |
| 232 | */ |
| 233 | public static function getDriverClass(string $driverName) : string |
| 234 | { |
| 235 | if (!empty(self::$driverCustoms[$driverName])) { |
| 236 | $driverClass = self::$driverCustoms[$driverName]; |
| 237 | } else { |
| 238 | if (!empty(self::$driverOverrides[$driverName])) { |
| 239 | $driverClass = self::$driverOverrides[$driverName]; |
| 240 | } else { |
| 241 | $driverClass = self::getNamespacePath() . $driverName . '\\Driver'; |
| 242 | } |
| 243 | } |
| 244 | return $driverClass; |
| 245 | } |
| 246 | /** |
| 247 | * @return string |
| 248 | */ |
| 249 | public static function getNamespacePath() : string |
| 250 | { |
| 251 | return self::$namespacePath ?: self::getDefaultNamespacePath(); |
| 252 | } |
| 253 | /** |
| 254 | * @return string |
| 255 | */ |
| 256 | public static function getDefaultNamespacePath() : string |
| 257 | { |
| 258 | return self::CORE_DRIVER_NAMESPACE; |
| 259 | } |
| 260 | /** |
| 261 | * @return bool |
| 262 | */ |
| 263 | public static function clearInstances() : bool |
| 264 | { |
| 265 | self::$instances = []; |
| 266 | \gc_collect_cycles(); |
| 267 | return !\count(self::$instances); |
| 268 | } |
| 269 | /** |
| 270 | * @param ExtendedCacheItemPoolInterface $cachePoolInstance |
| 271 | * @return bool |
| 272 | * @since 7.0.4 |
| 273 | */ |
| 274 | public static function clearInstance(ExtendedCacheItemPoolInterface $cachePoolInstance) : bool |
| 275 | { |
| 276 | $found = \false; |
| 277 | self::$instances = \array_filter(\array_map(static function (ExtendedCacheItemPoolInterface $cachePool) use($cachePoolInstance, &$found) { |
| 278 | if (\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)) { |
| 279 | $found = \true; |
| 280 | return null; |
| 281 | } |
| 282 | return $cachePool; |
| 283 | }, self::$instances)); |
| 284 | return $found; |
| 285 | } |
| 286 | /** |
| 287 | * @param ConfigurationOption $config |
| 288 | */ |
| 289 | public static function setDefaultConfig(ConfigurationOption $config) : void |
| 290 | { |
| 291 | self::$config = $config; |
| 292 | } |
| 293 | /** |
| 294 | * @param string $driverName |
| 295 | * @param string $className |
| 296 | * @return void |
| 297 | * @throws PhpfastcacheLogicException |
| 298 | * @throws PhpfastcacheUnsupportedOperationException |
| 299 | * @throws PhpfastcacheInvalidArgumentException |
| 300 | */ |
| 301 | public static function addCustomDriver(string $driverName, string $className) : void |
| 302 | { |
| 303 | $driverName = self::standardizeDriverName($driverName); |
| 304 | if (empty($driverName)) { |
| 305 | throw new PhpfastcacheInvalidArgumentException("Can't add a custom driver because its name is empty"); |
| 306 | } |
| 307 | if (!\class_exists($className)) { |
| 308 | throw new PhpfastcacheInvalidArgumentException(\sprintf("Can't add '%s' because the class '%s' does not exists", $driverName, $className)); |
| 309 | } |
| 310 | if (!empty(self::$driverCustoms[$driverName])) { |
| 311 | throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already added", $driverName)); |
| 312 | } |
| 313 | if (\in_array($driverName, self::getDriverList(), \true)) { |
| 314 | throw new PhpfastcacheLogicException(\sprintf("Driver '%s' is already a part of the PhpFastCache core", $driverName)); |
| 315 | } |
| 316 | self::$driverCustoms[$driverName] = $className; |
| 317 | } |
| 318 | /** |
| 319 | * Return the list of available drivers Capitalized |
| 320 | * with optional FQCN as key |
| 321 | * |
| 322 | * @param bool $FQCNAsKey Describe keys with Full Qualified Class Name |
| 323 | * @return string[] |
| 324 | * @throws PhpfastcacheUnsupportedOperationException |
| 325 | */ |
| 326 | public static function getDriverList(bool $FQCNAsKey = \false) : array |
| 327 | { |
| 328 | static $driverList; |
| 329 | if (self::getDefaultNamespacePath() === self::getNamespacePath()) { |
| 330 | if ($driverList === null) { |
| 331 | $prefix = self::CORE_DRIVER_NAMESPACE; |
| 332 | $classMap = self::createClassMap(__DIR__ . '/Drivers'); |
| 333 | $driverList = []; |
| 334 | foreach ($classMap as $class => $file) { |
| 335 | $driverList[] = \str_replace($prefix, '', \substr($class, 0, \strrpos($class, '\\'))); |
| 336 | } |
| 337 | $driverList = \array_values(\array_unique($driverList)); |
| 338 | } |
| 339 | $driverList = \array_merge($driverList, \array_keys(self::$driverCustoms)); |
| 340 | if ($FQCNAsKey) { |
| 341 | $realDriverList = []; |
| 342 | foreach ($driverList as $driverName) { |
| 343 | $realDriverList[self::getDriverClass($driverName)] = $driverName; |
| 344 | } |
| 345 | $driverList = $realDriverList; |
| 346 | } |
| 347 | \asort($driverList); |
| 348 | return $driverList; |
| 349 | } |
| 350 | throw new PhpfastcacheUnsupportedOperationException('Cannot get the driver list if the default namespace path has changed.'); |
| 351 | } |
| 352 | /** |
| 353 | * @param string $driverName |
| 354 | * @return void |
| 355 | * @throws PhpfastcacheLogicException |
| 356 | * @throws PhpfastcacheInvalidArgumentException |
| 357 | */ |
| 358 | public static function removeCustomDriver(string $driverName) : void |
| 359 | { |
| 360 | $driverName = self::standardizeDriverName($driverName); |
| 361 | if (empty($driverName)) { |
| 362 | throw new PhpfastcacheInvalidArgumentException("Can't remove a custom driver because its name is empty"); |
| 363 | } |
| 364 | if (!isset(self::$driverCustoms[$driverName])) { |
| 365 | throw new PhpfastcacheLogicException(\sprintf("Driver '%s' does not exists", $driverName)); |
| 366 | } |
| 367 | unset(self::$driverCustoms[$driverName]); |
| 368 | } |
| 369 | /** |
| 370 | * @param string $driverName |
| 371 | * @param string $className |
| 372 | * @return void |
| 373 | * @throws PhpfastcacheLogicException |
| 374 | * @throws PhpfastcacheUnsupportedOperationException |
| 375 | * @throws PhpfastcacheInvalidArgumentException |
| 376 | */ |
| 377 | public static function addCoreDriverOverride(string $driverName, string $className) : void |
| 378 | { |
| 379 | $driverName = self::standardizeDriverName($driverName); |
| 380 | if (empty($driverName)) { |
| 381 | throw new PhpfastcacheInvalidArgumentException("Can't add a core driver override because its name is empty"); |
| 382 | } |
| 383 | if (!\class_exists($className)) { |
| 384 | throw new PhpfastcacheInvalidArgumentException(\sprintf("Can't override '%s' because the class '%s' does not exists", $driverName, $className)); |
| 385 | } |
| 386 | if (!empty(self::$driverOverrides[$driverName])) { |
| 387 | throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already overridden", $driverName)); |
| 388 | } |
| 389 | if (!\in_array($driverName, self::getDriverList(), \true)) { |
| 390 | throw new PhpfastcacheLogicException(\sprintf("Driver '%s' can't be overridden since its not a part of the PhpFastCache core", $driverName)); |
| 391 | } |
| 392 | if (!\is_subclass_of($className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver', \true)) { |
| 393 | throw new PhpfastcacheLogicException(\sprintf("Can't override '%s' because the class '%s' MUST extend '%s'", $driverName, $className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver')); |
| 394 | } |
| 395 | self::$driverOverrides[$driverName] = $className; |
| 396 | } |
| 397 | /** |
| 398 | * @param string $driverName |
| 399 | * @return void |
| 400 | * @throws PhpfastcacheLogicException |
| 401 | * @throws PhpfastcacheInvalidArgumentException |
| 402 | */ |
| 403 | public static function removeCoreDriverOverride(string $driverName) : void |
| 404 | { |
| 405 | $driverName = self::standardizeDriverName($driverName); |
| 406 | if (empty($driverName)) { |
| 407 | throw new PhpfastcacheInvalidArgumentException("Can't remove a core driver override because its name is empty"); |
| 408 | } |
| 409 | if (!isset(self::$driverOverrides[$driverName])) { |
| 410 | throw new PhpfastcacheLogicException(\sprintf("Driver '%s' were not overridden", $driverName)); |
| 411 | } |
| 412 | unset(self::$driverOverrides[$driverName]); |
| 413 | } |
| 414 | } |
| 415 |