@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | $config = self::validateConfig($config); |
| 177 | 177 | $driver = self::standardizeDriverName($driver); |
| 178 | 178 | |
| 179 | - $instanceId = $instanceId ?: md5($driver . \serialize(\array_filter($config->toArray(), static function ($val){ |
|
| 179 | + $instanceId = $instanceId ?: md5($driver . \serialize(\array_filter($config->toArray(), static function($val) { |
|
| 180 | 180 | return !\is_callable($val); |
| 181 | 181 | }))); |
| 182 | 182 | |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | $found = false; |
| 320 | 320 | self::$instances = \array_filter( |
| 321 | 321 | \array_map( |
| 322 | - static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found) { |
|
| 322 | + static function(ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found) { |
|
| 323 | 323 | if (\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)) { |
| 324 | 324 | $found = true; |
| 325 | 325 | return null; |
@@ -21,13 +21,13 @@ discard block |
||
| 21 | 21 | use Phpfastcache\Config\ConfigurationOptionInterface; |
| 22 | 22 | use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 23 | 23 | use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, |
| 24 | - PhpfastcacheDriverException, |
|
| 25 | - PhpfastcacheDriverNotFoundException, |
|
| 26 | - PhpfastcacheInstanceNotFoundException, |
|
| 27 | - PhpfastcacheInvalidArgumentException, |
|
| 28 | - PhpfastcacheInvalidConfigurationException, |
|
| 29 | - PhpfastcacheLogicException, |
|
| 30 | - PhpfastcacheUnsupportedOperationException |
|
| 24 | + PhpfastcacheDriverException, |
|
| 25 | + PhpfastcacheDriverNotFoundException, |
|
| 26 | + PhpfastcacheInstanceNotFoundException, |
|
| 27 | + PhpfastcacheInvalidArgumentException, |
|
| 28 | + PhpfastcacheInvalidConfigurationException, |
|
| 29 | + PhpfastcacheLogicException, |
|
| 30 | + PhpfastcacheUnsupportedOperationException |
|
| 31 | 31 | }; |
| 32 | 32 | use Phpfastcache\Util\ClassNamespaceResolverTrait; |
| 33 | 33 | |
@@ -59,447 +59,447 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | class CacheManager |
| 61 | 61 | { |
| 62 | - public const CORE_DRIVER_NAMESPACE = 'Phpfastcache\Drivers\\'; |
|
| 63 | - |
|
| 64 | - use ClassNamespaceResolverTrait; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var ConfigurationOption |
|
| 68 | - */ |
|
| 69 | - protected static $config; |
|
| 70 | - /** |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - protected static $namespacePath; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @var ExtendedCacheItemPoolInterface[] |
|
| 77 | - */ |
|
| 78 | - protected static $instances = []; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var array |
|
| 82 | - */ |
|
| 83 | - protected static $driverOverrides = []; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - protected static $driverCustoms = []; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var array |
|
| 92 | - */ |
|
| 93 | - protected static $badPracticeOmeter = []; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * CacheManager constructor. |
|
| 97 | - */ |
|
| 98 | - final protected function __construct() |
|
| 99 | - { |
|
| 100 | - // The cache manager is not meant to be instantiated |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $instanceId |
|
| 105 | - * @return ExtendedCacheItemPoolInterface |
|
| 106 | - * @throws PhpfastcacheInstanceNotFoundException |
|
| 107 | - */ |
|
| 108 | - public static function getInstanceById(string $instanceId): ExtendedCacheItemPoolInterface |
|
| 109 | - { |
|
| 110 | - if (isset(self::$instances[$instanceId])) { |
|
| 111 | - return self::$instances[$instanceId]; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - throw new PhpfastcacheInstanceNotFoundException(sprintf('Instance ID %s not found', $instanceId)); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Return the list of instances |
|
| 119 | - * |
|
| 120 | - * @return ExtendedCacheItemPoolInterface[] |
|
| 121 | - */ |
|
| 122 | - public static function getInstances(): array |
|
| 123 | - { |
|
| 124 | - return self::$instances; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * This method is intended for internal |
|
| 129 | - * use only and should not be used for |
|
| 130 | - * any external development use the |
|
| 131 | - * getInstances() method instead |
|
| 132 | - * |
|
| 133 | - * @return ExtendedCacheItemPoolInterface[] |
|
| 134 | - * @internal |
|
| 135 | - * @todo Use a proper way to passe them as a reference ? |
|
| 136 | - */ |
|
| 137 | - public static function &getInternalInstances(): array |
|
| 138 | - { |
|
| 139 | - return self::$instances; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param string $name |
|
| 144 | - * @param array $arguments |
|
| 145 | - * @return ExtendedCacheItemPoolInterface |
|
| 146 | - * @throws PhpfastcacheDriverCheckException |
|
| 147 | - * @throws PhpfastcacheDriverException |
|
| 148 | - * @throws PhpfastcacheDriverNotFoundException |
|
| 149 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 150 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 151 | - * @throws PhpfastcacheLogicException |
|
| 152 | - * @throws \ReflectionException |
|
| 153 | - */ |
|
| 154 | - public static function __callStatic(string $name, array $arguments): ExtendedCacheItemPoolInterface |
|
| 155 | - { |
|
| 156 | - $options = (\array_key_exists(0, $arguments) && \is_array($arguments) ? $arguments[0] : []); |
|
| 157 | - |
|
| 158 | - return self::getInstance($name, $options); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param string $driver |
|
| 163 | - * @param ConfigurationOptionInterface $config |
|
| 164 | - * @param string|null $instanceId |
|
| 165 | - * @return ExtendedCacheItemPoolInterface|AggregatablePoolInterface |
|
| 166 | - * @throws PhpfastcacheDriverCheckException |
|
| 167 | - * @throws PhpfastcacheDriverException |
|
| 168 | - * @throws PhpfastcacheDriverNotFoundException |
|
| 169 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 170 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 171 | - * @throws PhpfastcacheLogicException |
|
| 172 | - * @throws \ReflectionException |
|
| 173 | - */ |
|
| 174 | - public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null): ExtendedCacheItemPoolInterface |
|
| 175 | - { |
|
| 176 | - $config = self::validateConfig($config); |
|
| 177 | - $driver = self::standardizeDriverName($driver); |
|
| 178 | - |
|
| 179 | - $instanceId = $instanceId ?: md5($driver . \serialize(\array_filter($config->toArray(), static function ($val){ |
|
| 180 | - return !\is_callable($val); |
|
| 181 | - }))); |
|
| 182 | - |
|
| 183 | - if (!isset(self::$instances[$instanceId])) { |
|
| 184 | - self::$badPracticeOmeter[$driver] = 1; |
|
| 185 | - $driverClass = self::validateDriverClass(self::getDriverClass($driver)); |
|
| 186 | - |
|
| 187 | - if (\class_exists($driverClass)) { |
|
| 188 | - $configClass = $driverClass::getConfigClass(); |
|
| 189 | - self::$instances[$instanceId] = new $driverClass(new $configClass($config->toArray()), $instanceId); |
|
| 190 | - self::$instances[$instanceId]->setEventManager(EventManager::getInstance()); |
|
| 191 | - } else { |
|
| 192 | - throw new PhpfastcacheDriverNotFoundException(sprintf('The driver "%s" does not exists', $driver)); |
|
| 193 | - } |
|
| 194 | - } else { |
|
| 195 | - if (self::$badPracticeOmeter[$driver] >= 2) { |
|
| 196 | - \trigger_error( |
|
| 197 | - '[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
|
| 62 | + public const CORE_DRIVER_NAMESPACE = 'Phpfastcache\Drivers\\'; |
|
| 63 | + |
|
| 64 | + use ClassNamespaceResolverTrait; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var ConfigurationOption |
|
| 68 | + */ |
|
| 69 | + protected static $config; |
|
| 70 | + /** |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + protected static $namespacePath; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @var ExtendedCacheItemPoolInterface[] |
|
| 77 | + */ |
|
| 78 | + protected static $instances = []; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var array |
|
| 82 | + */ |
|
| 83 | + protected static $driverOverrides = []; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + protected static $driverCustoms = []; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var array |
|
| 92 | + */ |
|
| 93 | + protected static $badPracticeOmeter = []; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * CacheManager constructor. |
|
| 97 | + */ |
|
| 98 | + final protected function __construct() |
|
| 99 | + { |
|
| 100 | + // The cache manager is not meant to be instantiated |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $instanceId |
|
| 105 | + * @return ExtendedCacheItemPoolInterface |
|
| 106 | + * @throws PhpfastcacheInstanceNotFoundException |
|
| 107 | + */ |
|
| 108 | + public static function getInstanceById(string $instanceId): ExtendedCacheItemPoolInterface |
|
| 109 | + { |
|
| 110 | + if (isset(self::$instances[$instanceId])) { |
|
| 111 | + return self::$instances[$instanceId]; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + throw new PhpfastcacheInstanceNotFoundException(sprintf('Instance ID %s not found', $instanceId)); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Return the list of instances |
|
| 119 | + * |
|
| 120 | + * @return ExtendedCacheItemPoolInterface[] |
|
| 121 | + */ |
|
| 122 | + public static function getInstances(): array |
|
| 123 | + { |
|
| 124 | + return self::$instances; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * This method is intended for internal |
|
| 129 | + * use only and should not be used for |
|
| 130 | + * any external development use the |
|
| 131 | + * getInstances() method instead |
|
| 132 | + * |
|
| 133 | + * @return ExtendedCacheItemPoolInterface[] |
|
| 134 | + * @internal |
|
| 135 | + * @todo Use a proper way to passe them as a reference ? |
|
| 136 | + */ |
|
| 137 | + public static function &getInternalInstances(): array |
|
| 138 | + { |
|
| 139 | + return self::$instances; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param string $name |
|
| 144 | + * @param array $arguments |
|
| 145 | + * @return ExtendedCacheItemPoolInterface |
|
| 146 | + * @throws PhpfastcacheDriverCheckException |
|
| 147 | + * @throws PhpfastcacheDriverException |
|
| 148 | + * @throws PhpfastcacheDriverNotFoundException |
|
| 149 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 150 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 151 | + * @throws PhpfastcacheLogicException |
|
| 152 | + * @throws \ReflectionException |
|
| 153 | + */ |
|
| 154 | + public static function __callStatic(string $name, array $arguments): ExtendedCacheItemPoolInterface |
|
| 155 | + { |
|
| 156 | + $options = (\array_key_exists(0, $arguments) && \is_array($arguments) ? $arguments[0] : []); |
|
| 157 | + |
|
| 158 | + return self::getInstance($name, $options); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param string $driver |
|
| 163 | + * @param ConfigurationOptionInterface $config |
|
| 164 | + * @param string|null $instanceId |
|
| 165 | + * @return ExtendedCacheItemPoolInterface|AggregatablePoolInterface |
|
| 166 | + * @throws PhpfastcacheDriverCheckException |
|
| 167 | + * @throws PhpfastcacheDriverException |
|
| 168 | + * @throws PhpfastcacheDriverNotFoundException |
|
| 169 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 170 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 171 | + * @throws PhpfastcacheLogicException |
|
| 172 | + * @throws \ReflectionException |
|
| 173 | + */ |
|
| 174 | + public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null): ExtendedCacheItemPoolInterface |
|
| 175 | + { |
|
| 176 | + $config = self::validateConfig($config); |
|
| 177 | + $driver = self::standardizeDriverName($driver); |
|
| 178 | + |
|
| 179 | + $instanceId = $instanceId ?: md5($driver . \serialize(\array_filter($config->toArray(), static function ($val){ |
|
| 180 | + return !\is_callable($val); |
|
| 181 | + }))); |
|
| 182 | + |
|
| 183 | + if (!isset(self::$instances[$instanceId])) { |
|
| 184 | + self::$badPracticeOmeter[$driver] = 1; |
|
| 185 | + $driverClass = self::validateDriverClass(self::getDriverClass($driver)); |
|
| 186 | + |
|
| 187 | + if (\class_exists($driverClass)) { |
|
| 188 | + $configClass = $driverClass::getConfigClass(); |
|
| 189 | + self::$instances[$instanceId] = new $driverClass(new $configClass($config->toArray()), $instanceId); |
|
| 190 | + self::$instances[$instanceId]->setEventManager(EventManager::getInstance()); |
|
| 191 | + } else { |
|
| 192 | + throw new PhpfastcacheDriverNotFoundException(sprintf('The driver "%s" does not exists', $driver)); |
|
| 193 | + } |
|
| 194 | + } else { |
|
| 195 | + if (self::$badPracticeOmeter[$driver] >= 2) { |
|
| 196 | + \trigger_error( |
|
| 197 | + '[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
|
| 198 | 198 | See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F' |
| 199 | - ); |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - self::$badPracticeOmeter[$driver]++; |
|
| 204 | - |
|
| 205 | - return self::$instances[$instanceId]; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @param ConfigurationOptionInterface|null $config |
|
| 210 | - * @return ConfigurationOption |
|
| 211 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 212 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 213 | - * @throws \ReflectionException |
|
| 214 | - */ |
|
| 215 | - protected static function validateConfig(?ConfigurationOptionInterface $config): ConfigurationOption |
|
| 216 | - { |
|
| 217 | - if ($config === null) { |
|
| 218 | - $config = self::getDefaultConfig(); |
|
| 219 | - } else { |
|
| 220 | - if (!($config instanceof ConfigurationOption)) { |
|
| 221 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config))); |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - return $config; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * @return ConfigurationOptionInterface |
|
| 230 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 231 | - * @throws \ReflectionException |
|
| 232 | - */ |
|
| 233 | - public static function getDefaultConfig(): ConfigurationOptionInterface |
|
| 234 | - { |
|
| 235 | - return self::$config ?: self::$config = new ConfigurationOption(); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @param string $driverName |
|
| 240 | - * @return string |
|
| 241 | - */ |
|
| 242 | - public static function standardizeDriverName(string $driverName): string |
|
| 243 | - { |
|
| 244 | - return \ucfirst(\strtolower(\trim($driverName))); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @param string $driverClass |
|
| 249 | - * @return string|ExtendedCacheItemPoolInterface |
|
| 250 | - * @throws PhpfastcacheDriverException |
|
| 251 | - */ |
|
| 252 | - protected static function validateDriverClass(string $driverClass): string |
|
| 253 | - { |
|
| 254 | - if (!\is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)) { |
|
| 255 | - throw new PhpfastcacheDriverException( |
|
| 256 | - \sprintf( |
|
| 257 | - 'Class "%s" does not implement "%s"', |
|
| 258 | - $driverClass, |
|
| 259 | - ExtendedCacheItemPoolInterface::class |
|
| 260 | - ) |
|
| 261 | - ); |
|
| 262 | - } |
|
| 263 | - return $driverClass; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @param string $driverName |
|
| 268 | - * @return string |
|
| 269 | - */ |
|
| 270 | - public static function getDriverClass(string $driverName): string |
|
| 271 | - { |
|
| 272 | - if (!empty(self::$driverCustoms[$driverName])) { |
|
| 273 | - $driverClass = self::$driverCustoms[$driverName]; |
|
| 274 | - } else { |
|
| 275 | - if (!empty(self::$driverOverrides[$driverName])) { |
|
| 276 | - $driverClass = self::$driverOverrides[$driverName]; |
|
| 277 | - } else { |
|
| 278 | - $driverClass = self::getNamespacePath() . $driverName . '\Driver'; |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - return $driverClass; |
|
| 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 self::CORE_DRIVER_NAMESPACE; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * @return bool |
|
| 303 | - */ |
|
| 304 | - public static function clearInstances(): bool |
|
| 305 | - { |
|
| 306 | - self::$instances = []; |
|
| 307 | - |
|
| 308 | - \gc_collect_cycles(); |
|
| 309 | - return !\count(self::$instances); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @param ExtendedCacheItemPoolInterface $cachePoolInstance |
|
| 314 | - * @return bool |
|
| 315 | - * @since 7.0.4 |
|
| 316 | - */ |
|
| 317 | - public static function clearInstance(ExtendedCacheItemPoolInterface $cachePoolInstance): bool |
|
| 318 | - { |
|
| 319 | - $found = false; |
|
| 320 | - self::$instances = \array_filter( |
|
| 321 | - \array_map( |
|
| 322 | - static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found) { |
|
| 323 | - if (\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)) { |
|
| 324 | - $found = true; |
|
| 325 | - return null; |
|
| 326 | - } |
|
| 327 | - return $cachePool; |
|
| 328 | - }, |
|
| 329 | - self::$instances |
|
| 330 | - ) |
|
| 331 | - ); |
|
| 332 | - |
|
| 333 | - return $found; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * @param ConfigurationOption $config |
|
| 338 | - */ |
|
| 339 | - public static function setDefaultConfig(ConfigurationOption $config): void |
|
| 340 | - { |
|
| 341 | - self::$config = $config; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * @param string $driverName |
|
| 346 | - * @param string $className |
|
| 347 | - * @return void |
|
| 348 | - * @throws PhpfastcacheLogicException |
|
| 349 | - * @throws PhpfastcacheUnsupportedOperationException |
|
| 350 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 351 | - */ |
|
| 352 | - public static function addCustomDriver(string $driverName, string $className): void |
|
| 353 | - { |
|
| 354 | - $driverName = self::standardizeDriverName($driverName); |
|
| 355 | - |
|
| 356 | - if (empty($driverName)) { |
|
| 357 | - throw new PhpfastcacheInvalidArgumentException("Can't add a custom driver because its name is empty"); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - if (!\class_exists($className)) { |
|
| 361 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 362 | - \sprintf("Can't add '%s' because the class '%s' does not exists", $driverName, $className) |
|
| 363 | - ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - if (!empty(self::$driverCustoms[$driverName])) { |
|
| 367 | - throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already added", $driverName)); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - if (\in_array($driverName, self::getDriverList(), true)) { |
|
| 371 | - throw new PhpfastcacheLogicException(\sprintf("Driver '%s' is already a part of the PhpFastCache core", $driverName)); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - self::$driverCustoms[$driverName] = $className; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Return the list of available drivers Capitalized |
|
| 379 | - * with optional FQCN as key |
|
| 380 | - * |
|
| 381 | - * @param bool $FQCNAsKey Describe keys with Full Qualified Class Name |
|
| 382 | - * @return string[] |
|
| 383 | - * @throws PhpfastcacheUnsupportedOperationException |
|
| 384 | - */ |
|
| 385 | - public static function getDriverList(bool $FQCNAsKey = false): array |
|
| 386 | - { |
|
| 387 | - static $driverList; |
|
| 388 | - |
|
| 389 | - if (self::getDefaultNamespacePath() === self::getNamespacePath()) { |
|
| 390 | - if ($driverList === null) { |
|
| 391 | - $prefix = self::CORE_DRIVER_NAMESPACE; |
|
| 392 | - $classMap = self::createClassMap(__DIR__ . '/Drivers'); |
|
| 393 | - $driverList = []; |
|
| 394 | - |
|
| 395 | - foreach ($classMap as $class => $file) { |
|
| 396 | - $driverList[] = \str_replace($prefix, '', \substr($class, 0, \strrpos($class, '\\'))); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - $driverList = \array_values(\array_unique($driverList)); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - $driverList = \array_merge($driverList, \array_keys(self::$driverCustoms)); |
|
| 403 | - |
|
| 404 | - if ($FQCNAsKey) { |
|
| 405 | - $realDriverList = []; |
|
| 406 | - foreach ($driverList as $driverName) { |
|
| 407 | - $realDriverList[self::getDriverClass($driverName)] = $driverName; |
|
| 408 | - } |
|
| 409 | - $driverList = $realDriverList; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - \asort($driverList); |
|
| 413 | - |
|
| 414 | - return $driverList; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - throw new PhpfastcacheUnsupportedOperationException('Cannot get the driver list if the default namespace path has changed.'); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * @param string $driverName |
|
| 422 | - * @return void |
|
| 423 | - * @throws PhpfastcacheLogicException |
|
| 424 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 425 | - */ |
|
| 426 | - public static function removeCustomDriver(string $driverName): void |
|
| 427 | - { |
|
| 428 | - $driverName = self::standardizeDriverName($driverName); |
|
| 429 | - |
|
| 430 | - if (empty($driverName)) { |
|
| 431 | - throw new PhpfastcacheInvalidArgumentException("Can't remove a custom driver because its name is empty"); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - if (!isset(self::$driverCustoms[$driverName])) { |
|
| 435 | - throw new PhpfastcacheLogicException(\sprintf("Driver '%s' does not exists", $driverName)); |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - unset(self::$driverCustoms[$driverName]); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * @param string $driverName |
|
| 443 | - * @param string $className |
|
| 444 | - * @return void |
|
| 445 | - * @throws PhpfastcacheLogicException |
|
| 446 | - * @throws PhpfastcacheUnsupportedOperationException |
|
| 447 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 448 | - */ |
|
| 449 | - public static function addCoreDriverOverride(string $driverName, string $className): void |
|
| 450 | - { |
|
| 451 | - $driverName = self::standardizeDriverName($driverName); |
|
| 452 | - |
|
| 453 | - if (empty($driverName)) { |
|
| 454 | - throw new PhpfastcacheInvalidArgumentException("Can't add a core driver override because its name is empty"); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - if (!\class_exists($className)) { |
|
| 458 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 459 | - \sprintf("Can't override '%s' because the class '%s' does not exists", $driverName, $className) |
|
| 460 | - ); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - if (!empty(self::$driverOverrides[$driverName])) { |
|
| 464 | - throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already overridden", $driverName)); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - if (!\in_array($driverName, self::getDriverList(), true)) { |
|
| 468 | - throw new PhpfastcacheLogicException(\sprintf("Driver '%s' can't be overridden since its not a part of the PhpFastCache core", $driverName)); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - if (!\is_subclass_of($className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver', true)) { |
|
| 472 | - throw new PhpfastcacheLogicException( |
|
| 473 | - \sprintf( |
|
| 474 | - "Can't override '%s' because the class '%s' MUST extend '%s'", |
|
| 475 | - $driverName, |
|
| 476 | - $className, |
|
| 477 | - self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver' |
|
| 478 | - ) |
|
| 479 | - ); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - self::$driverOverrides[$driverName] = $className; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * @param string $driverName |
|
| 487 | - * @return void |
|
| 488 | - * @throws PhpfastcacheLogicException |
|
| 489 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 490 | - */ |
|
| 491 | - public static function removeCoreDriverOverride(string $driverName): void |
|
| 492 | - { |
|
| 493 | - $driverName = self::standardizeDriverName($driverName); |
|
| 494 | - |
|
| 495 | - if (empty($driverName)) { |
|
| 496 | - throw new PhpfastcacheInvalidArgumentException("Can't remove a core driver override because its name is empty"); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - if (!isset(self::$driverOverrides[$driverName])) { |
|
| 500 | - throw new PhpfastcacheLogicException(\sprintf("Driver '%s' were not overridden", $driverName)); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - unset(self::$driverOverrides[$driverName]); |
|
| 504 | - } |
|
| 199 | + ); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + self::$badPracticeOmeter[$driver]++; |
|
| 204 | + |
|
| 205 | + return self::$instances[$instanceId]; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @param ConfigurationOptionInterface|null $config |
|
| 210 | + * @return ConfigurationOption |
|
| 211 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 212 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 213 | + * @throws \ReflectionException |
|
| 214 | + */ |
|
| 215 | + protected static function validateConfig(?ConfigurationOptionInterface $config): ConfigurationOption |
|
| 216 | + { |
|
| 217 | + if ($config === null) { |
|
| 218 | + $config = self::getDefaultConfig(); |
|
| 219 | + } else { |
|
| 220 | + if (!($config instanceof ConfigurationOption)) { |
|
| 221 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config))); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + return $config; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * @return ConfigurationOptionInterface |
|
| 230 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 231 | + * @throws \ReflectionException |
|
| 232 | + */ |
|
| 233 | + public static function getDefaultConfig(): ConfigurationOptionInterface |
|
| 234 | + { |
|
| 235 | + return self::$config ?: self::$config = new ConfigurationOption(); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @param string $driverName |
|
| 240 | + * @return string |
|
| 241 | + */ |
|
| 242 | + public static function standardizeDriverName(string $driverName): string |
|
| 243 | + { |
|
| 244 | + return \ucfirst(\strtolower(\trim($driverName))); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @param string $driverClass |
|
| 249 | + * @return string|ExtendedCacheItemPoolInterface |
|
| 250 | + * @throws PhpfastcacheDriverException |
|
| 251 | + */ |
|
| 252 | + protected static function validateDriverClass(string $driverClass): string |
|
| 253 | + { |
|
| 254 | + if (!\is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)) { |
|
| 255 | + throw new PhpfastcacheDriverException( |
|
| 256 | + \sprintf( |
|
| 257 | + 'Class "%s" does not implement "%s"', |
|
| 258 | + $driverClass, |
|
| 259 | + ExtendedCacheItemPoolInterface::class |
|
| 260 | + ) |
|
| 261 | + ); |
|
| 262 | + } |
|
| 263 | + return $driverClass; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @param string $driverName |
|
| 268 | + * @return string |
|
| 269 | + */ |
|
| 270 | + public static function getDriverClass(string $driverName): string |
|
| 271 | + { |
|
| 272 | + if (!empty(self::$driverCustoms[$driverName])) { |
|
| 273 | + $driverClass = self::$driverCustoms[$driverName]; |
|
| 274 | + } else { |
|
| 275 | + if (!empty(self::$driverOverrides[$driverName])) { |
|
| 276 | + $driverClass = self::$driverOverrides[$driverName]; |
|
| 277 | + } else { |
|
| 278 | + $driverClass = self::getNamespacePath() . $driverName . '\Driver'; |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + return $driverClass; |
|
| 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 self::CORE_DRIVER_NAMESPACE; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * @return bool |
|
| 303 | + */ |
|
| 304 | + public static function clearInstances(): bool |
|
| 305 | + { |
|
| 306 | + self::$instances = []; |
|
| 307 | + |
|
| 308 | + \gc_collect_cycles(); |
|
| 309 | + return !\count(self::$instances); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param ExtendedCacheItemPoolInterface $cachePoolInstance |
|
| 314 | + * @return bool |
|
| 315 | + * @since 7.0.4 |
|
| 316 | + */ |
|
| 317 | + public static function clearInstance(ExtendedCacheItemPoolInterface $cachePoolInstance): bool |
|
| 318 | + { |
|
| 319 | + $found = false; |
|
| 320 | + self::$instances = \array_filter( |
|
| 321 | + \array_map( |
|
| 322 | + static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found) { |
|
| 323 | + if (\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)) { |
|
| 324 | + $found = true; |
|
| 325 | + return null; |
|
| 326 | + } |
|
| 327 | + return $cachePool; |
|
| 328 | + }, |
|
| 329 | + self::$instances |
|
| 330 | + ) |
|
| 331 | + ); |
|
| 332 | + |
|
| 333 | + return $found; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * @param ConfigurationOption $config |
|
| 338 | + */ |
|
| 339 | + public static function setDefaultConfig(ConfigurationOption $config): void |
|
| 340 | + { |
|
| 341 | + self::$config = $config; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * @param string $driverName |
|
| 346 | + * @param string $className |
|
| 347 | + * @return void |
|
| 348 | + * @throws PhpfastcacheLogicException |
|
| 349 | + * @throws PhpfastcacheUnsupportedOperationException |
|
| 350 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 351 | + */ |
|
| 352 | + public static function addCustomDriver(string $driverName, string $className): void |
|
| 353 | + { |
|
| 354 | + $driverName = self::standardizeDriverName($driverName); |
|
| 355 | + |
|
| 356 | + if (empty($driverName)) { |
|
| 357 | + throw new PhpfastcacheInvalidArgumentException("Can't add a custom driver because its name is empty"); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + if (!\class_exists($className)) { |
|
| 361 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 362 | + \sprintf("Can't add '%s' because the class '%s' does not exists", $driverName, $className) |
|
| 363 | + ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + if (!empty(self::$driverCustoms[$driverName])) { |
|
| 367 | + throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already added", $driverName)); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + if (\in_array($driverName, self::getDriverList(), true)) { |
|
| 371 | + throw new PhpfastcacheLogicException(\sprintf("Driver '%s' is already a part of the PhpFastCache core", $driverName)); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + self::$driverCustoms[$driverName] = $className; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Return the list of available drivers Capitalized |
|
| 379 | + * with optional FQCN as key |
|
| 380 | + * |
|
| 381 | + * @param bool $FQCNAsKey Describe keys with Full Qualified Class Name |
|
| 382 | + * @return string[] |
|
| 383 | + * @throws PhpfastcacheUnsupportedOperationException |
|
| 384 | + */ |
|
| 385 | + public static function getDriverList(bool $FQCNAsKey = false): array |
|
| 386 | + { |
|
| 387 | + static $driverList; |
|
| 388 | + |
|
| 389 | + if (self::getDefaultNamespacePath() === self::getNamespacePath()) { |
|
| 390 | + if ($driverList === null) { |
|
| 391 | + $prefix = self::CORE_DRIVER_NAMESPACE; |
|
| 392 | + $classMap = self::createClassMap(__DIR__ . '/Drivers'); |
|
| 393 | + $driverList = []; |
|
| 394 | + |
|
| 395 | + foreach ($classMap as $class => $file) { |
|
| 396 | + $driverList[] = \str_replace($prefix, '', \substr($class, 0, \strrpos($class, '\\'))); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + $driverList = \array_values(\array_unique($driverList)); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + $driverList = \array_merge($driverList, \array_keys(self::$driverCustoms)); |
|
| 403 | + |
|
| 404 | + if ($FQCNAsKey) { |
|
| 405 | + $realDriverList = []; |
|
| 406 | + foreach ($driverList as $driverName) { |
|
| 407 | + $realDriverList[self::getDriverClass($driverName)] = $driverName; |
|
| 408 | + } |
|
| 409 | + $driverList = $realDriverList; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + \asort($driverList); |
|
| 413 | + |
|
| 414 | + return $driverList; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + throw new PhpfastcacheUnsupportedOperationException('Cannot get the driver list if the default namespace path has changed.'); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * @param string $driverName |
|
| 422 | + * @return void |
|
| 423 | + * @throws PhpfastcacheLogicException |
|
| 424 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 425 | + */ |
|
| 426 | + public static function removeCustomDriver(string $driverName): void |
|
| 427 | + { |
|
| 428 | + $driverName = self::standardizeDriverName($driverName); |
|
| 429 | + |
|
| 430 | + if (empty($driverName)) { |
|
| 431 | + throw new PhpfastcacheInvalidArgumentException("Can't remove a custom driver because its name is empty"); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + if (!isset(self::$driverCustoms[$driverName])) { |
|
| 435 | + throw new PhpfastcacheLogicException(\sprintf("Driver '%s' does not exists", $driverName)); |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + unset(self::$driverCustoms[$driverName]); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * @param string $driverName |
|
| 443 | + * @param string $className |
|
| 444 | + * @return void |
|
| 445 | + * @throws PhpfastcacheLogicException |
|
| 446 | + * @throws PhpfastcacheUnsupportedOperationException |
|
| 447 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 448 | + */ |
|
| 449 | + public static function addCoreDriverOverride(string $driverName, string $className): void |
|
| 450 | + { |
|
| 451 | + $driverName = self::standardizeDriverName($driverName); |
|
| 452 | + |
|
| 453 | + if (empty($driverName)) { |
|
| 454 | + throw new PhpfastcacheInvalidArgumentException("Can't add a core driver override because its name is empty"); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + if (!\class_exists($className)) { |
|
| 458 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 459 | + \sprintf("Can't override '%s' because the class '%s' does not exists", $driverName, $className) |
|
| 460 | + ); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + if (!empty(self::$driverOverrides[$driverName])) { |
|
| 464 | + throw new PhpfastcacheLogicException(\sprintf("Driver '%s' has been already overridden", $driverName)); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + if (!\in_array($driverName, self::getDriverList(), true)) { |
|
| 468 | + throw new PhpfastcacheLogicException(\sprintf("Driver '%s' can't be overridden since its not a part of the PhpFastCache core", $driverName)); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + if (!\is_subclass_of($className, self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver', true)) { |
|
| 472 | + throw new PhpfastcacheLogicException( |
|
| 473 | + \sprintf( |
|
| 474 | + "Can't override '%s' because the class '%s' MUST extend '%s'", |
|
| 475 | + $driverName, |
|
| 476 | + $className, |
|
| 477 | + self::CORE_DRIVER_NAMESPACE . $driverName . '\\Driver' |
|
| 478 | + ) |
|
| 479 | + ); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + self::$driverOverrides[$driverName] = $className; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * @param string $driverName |
|
| 487 | + * @return void |
|
| 488 | + * @throws PhpfastcacheLogicException |
|
| 489 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 490 | + */ |
|
| 491 | + public static function removeCoreDriverOverride(string $driverName): void |
|
| 492 | + { |
|
| 493 | + $driverName = self::standardizeDriverName($driverName); |
|
| 494 | + |
|
| 495 | + if (empty($driverName)) { |
|
| 496 | + throw new PhpfastcacheInvalidArgumentException("Can't remove a core driver override because its name is empty"); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + if (!isset(self::$driverOverrides[$driverName])) { |
|
| 500 | + throw new PhpfastcacheLogicException(\sprintf("Driver '%s' were not overridden", $driverName)); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + unset(self::$driverOverrides[$driverName]); |
|
| 504 | + } |
|
| 505 | 505 | } |
@@ -95,8 +95,7 @@ discard block |
||
| 95 | 95 | /** |
| 96 | 96 | * CacheManager constructor. |
| 97 | 97 | */ |
| 98 | - final protected function __construct() |
|
| 99 | - { |
|
| 98 | + final protected function __construct() { |
|
| 100 | 99 | // The cache manager is not meant to be instantiated |
| 101 | 100 | } |
| 102 | 101 | |
@@ -188,10 +187,12 @@ discard block |
||
| 188 | 187 | $configClass = $driverClass::getConfigClass(); |
| 189 | 188 | self::$instances[$instanceId] = new $driverClass(new $configClass($config->toArray()), $instanceId); |
| 190 | 189 | self::$instances[$instanceId]->setEventManager(EventManager::getInstance()); |
| 191 | - } else { |
|
| 190 | + } |
|
| 191 | + else { |
|
| 192 | 192 | throw new PhpfastcacheDriverNotFoundException(sprintf('The driver "%s" does not exists', $driver)); |
| 193 | 193 | } |
| 194 | - } else { |
|
| 194 | + } |
|
| 195 | + else { |
|
| 195 | 196 | if (self::$badPracticeOmeter[$driver] >= 2) { |
| 196 | 197 | \trigger_error( |
| 197 | 198 | '[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
@@ -216,7 +217,8 @@ discard block |
||
| 216 | 217 | { |
| 217 | 218 | if ($config === null) { |
| 218 | 219 | $config = self::getDefaultConfig(); |
| 219 | - } else { |
|
| 220 | + } |
|
| 221 | + else { |
|
| 220 | 222 | if (!($config instanceof ConfigurationOption)) { |
| 221 | 223 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config))); |
| 222 | 224 | } |
@@ -271,10 +273,12 @@ discard block |
||
| 271 | 273 | { |
| 272 | 274 | if (!empty(self::$driverCustoms[$driverName])) { |
| 273 | 275 | $driverClass = self::$driverCustoms[$driverName]; |
| 274 | - } else { |
|
| 276 | + } |
|
| 277 | + else { |
|
| 275 | 278 | if (!empty(self::$driverOverrides[$driverName])) { |
| 276 | 279 | $driverClass = self::$driverOverrides[$driverName]; |
| 277 | - } else { |
|
| 280 | + } |
|
| 281 | + else { |
|
| 278 | 282 | $driverClass = self::getNamespacePath() . $driverName . '\Driver'; |
| 279 | 283 | } |
| 280 | 284 | } |
@@ -152,7 +152,7 @@ |
||
| 152 | 152 | } |
| 153 | 153 | try { |
| 154 | 154 | return \array_map( |
| 155 | - static function (ExtendedCacheItemInterface $item) use ($default) { |
|
| 155 | + static function(ExtendedCacheItemInterface $item) use ($default) { |
|
| 156 | 156 | return $item->isHit() ? $item->get() : $default; |
| 157 | 157 | }, |
| 158 | 158 | $this->internalCacheInstance->getItems($keys) |
@@ -23,10 +23,10 @@ discard block |
||
| 23 | 23 | use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
| 24 | 24 | use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 25 | 25 | use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, |
| 26 | - PhpfastcacheInvalidArgumentException, |
|
| 27 | - PhpfastcacheLogicException, |
|
| 28 | - PhpfastcacheRootException, |
|
| 29 | - PhpfastcacheSimpleCacheException}; |
|
| 26 | + PhpfastcacheInvalidArgumentException, |
|
| 27 | + PhpfastcacheLogicException, |
|
| 28 | + PhpfastcacheRootException, |
|
| 29 | + PhpfastcacheSimpleCacheException}; |
|
| 30 | 30 | use Psr\SimpleCache\CacheInterface; |
| 31 | 31 | use Traversable; |
| 32 | 32 | |
@@ -36,206 +36,206 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | class Psr16Adapter implements CacheInterface |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * @var ExtendedCacheItemPoolInterface |
|
| 41 | - */ |
|
| 42 | - protected $internalCacheInstance; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Psr16Adapter constructor. |
|
| 46 | - * @param $driver |
|
| 47 | - * @param null|ConfigurationOptionInterface $config |
|
| 48 | - * @throws PhpfastcacheDriverCheckException |
|
| 49 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 50 | - * @throws PhpfastcacheLogicException |
|
| 51 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
| 52 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException |
|
| 53 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException |
|
| 54 | - * @throws \ReflectionException |
|
| 55 | - */ |
|
| 56 | - public function __construct($driver, ConfigurationOptionInterface $config = null) |
|
| 57 | - { |
|
| 58 | - if ($driver instanceof ExtendedCacheItemPoolInterface) { |
|
| 59 | - if ($config !== null) { |
|
| 60 | - throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter."); |
|
| 61 | - } |
|
| 62 | - $this->internalCacheInstance = $driver; |
|
| 63 | - } else { |
|
| 64 | - $this->internalCacheInstance = CacheManager::getInstance($driver, $config); |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param string $key |
|
| 70 | - * @param mixed|null $default |
|
| 71 | - * @return mixed|null |
|
| 72 | - * @throws PhpfastcacheSimpleCacheException |
|
| 73 | - * @throws \Psr\Cache\InvalidArgumentException |
|
| 74 | - */ |
|
| 75 | - public function get($key, $default = null) |
|
| 76 | - { |
|
| 77 | - try { |
|
| 78 | - $cacheItem = $this->internalCacheInstance->getItem($key); |
|
| 79 | - if (!$cacheItem->isExpired() && $cacheItem->get() !== null) { |
|
| 80 | - return $cacheItem->get(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $default; |
|
| 84 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 85 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param string $key |
|
| 91 | - * @param mixed $value |
|
| 92 | - * @param null|int|DateInterval $ttl |
|
| 93 | - * @return bool |
|
| 94 | - * @throws PhpfastcacheSimpleCacheException |
|
| 95 | - * @throws \Psr\Cache\InvalidArgumentException |
|
| 96 | - */ |
|
| 97 | - public function set($key, $value, $ttl = null): bool |
|
| 98 | - { |
|
| 99 | - try { |
|
| 100 | - $cacheItem = $this->internalCacheInstance |
|
| 101 | - ->getItem($key) |
|
| 102 | - ->set($value); |
|
| 103 | - if (\is_int($ttl) && $ttl <= 0) { |
|
| 104 | - $cacheItem->expiresAt((new DateTime('@0'))); |
|
| 105 | - } elseif ($ttl !== null) { |
|
| 106 | - $cacheItem->expiresAfter($ttl); |
|
| 107 | - } |
|
| 108 | - return $this->internalCacheInstance->save($cacheItem); |
|
| 109 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 110 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param string $key |
|
| 116 | - * @return bool |
|
| 117 | - * @throws PhpfastcacheSimpleCacheException |
|
| 118 | - */ |
|
| 119 | - public function delete($key): bool |
|
| 120 | - { |
|
| 121 | - try { |
|
| 122 | - return $this->internalCacheInstance->deleteItem($key); |
|
| 123 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 124 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return bool |
|
| 130 | - * @throws PhpfastcacheSimpleCacheException |
|
| 131 | - */ |
|
| 132 | - public function clear(): bool |
|
| 133 | - { |
|
| 134 | - try { |
|
| 135 | - return $this->internalCacheInstance->clear(); |
|
| 136 | - } catch (PhpfastcacheRootException $e) { |
|
| 137 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param iterable $keys |
|
| 143 | - * @param null $default |
|
| 144 | - * @return ExtendedCacheItemInterface[]|iterable |
|
| 145 | - * @throws PhpfastcacheSimpleCacheException |
|
| 146 | - * @throws \Psr\Cache\InvalidArgumentException |
|
| 147 | - */ |
|
| 148 | - public function getMultiple($keys, $default = null) |
|
| 149 | - { |
|
| 150 | - if ($keys instanceof Traversable) { |
|
| 151 | - $keys = \iterator_to_array($keys); |
|
| 152 | - } |
|
| 153 | - try { |
|
| 154 | - return \array_map( |
|
| 155 | - static function (ExtendedCacheItemInterface $item) use ($default) { |
|
| 156 | - return $item->isHit() ? $item->get() : $default; |
|
| 157 | - }, |
|
| 158 | - $this->internalCacheInstance->getItems($keys) |
|
| 159 | - ); |
|
| 160 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 161 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param string[] $values |
|
| 167 | - * @param null|int|DateInterval $ttl |
|
| 168 | - * @return bool |
|
| 169 | - * @throws PhpfastcacheSimpleCacheException |
|
| 170 | - * @throws \Psr\Cache\InvalidArgumentException |
|
| 171 | - */ |
|
| 172 | - public function setMultiple($values, $ttl = null): bool |
|
| 173 | - { |
|
| 174 | - try { |
|
| 175 | - foreach ($values as $key => $value) { |
|
| 176 | - $cacheItem = $this->internalCacheInstance->getItem($key)->set($value); |
|
| 177 | - |
|
| 178 | - if (\is_int($ttl) && $ttl <= 0) { |
|
| 179 | - $cacheItem->expiresAt((new DateTime('@0'))); |
|
| 180 | - } elseif ($ttl !== null) { |
|
| 181 | - $cacheItem->expiresAfter($ttl); |
|
| 182 | - } |
|
| 183 | - $this->internalCacheInstance->saveDeferred($cacheItem); |
|
| 184 | - unset($cacheItem); |
|
| 185 | - } |
|
| 186 | - return $this->internalCacheInstance->commit(); |
|
| 187 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 188 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @param iterable|array $keys |
|
| 194 | - * @return bool |
|
| 195 | - * @throws PhpfastcacheSimpleCacheException |
|
| 196 | - * @throws \Psr\Cache\InvalidArgumentException |
|
| 197 | - */ |
|
| 198 | - public function deleteMultiple($keys): bool |
|
| 199 | - { |
|
| 200 | - try { |
|
| 201 | - if ($keys instanceof Traversable) { |
|
| 202 | - return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys)); |
|
| 203 | - } elseif (\is_array($keys)) { |
|
| 204 | - return $this->internalCacheInstance->deleteItems($keys); |
|
| 205 | - } else { |
|
| 206 | - throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.'); |
|
| 207 | - } |
|
| 208 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 209 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @param string $key |
|
| 215 | - * @return bool |
|
| 216 | - * @throws PhpfastcacheSimpleCacheException |
|
| 217 | - */ |
|
| 218 | - public function has($key): bool |
|
| 219 | - { |
|
| 220 | - try { |
|
| 221 | - $cacheItem = $this->internalCacheInstance->getItem($key); |
|
| 222 | - return $cacheItem->isHit() && !$cacheItem->isExpired(); |
|
| 223 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 224 | - throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Extra methods that are not part of |
|
| 230 | - * psr16 specifications |
|
| 231 | - */ |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @return ExtendedCacheItemPoolInterface |
|
| 235 | - * @internal |
|
| 236 | - */ |
|
| 237 | - public function getInternalCacheInstance(): ExtendedCacheItemPoolInterface |
|
| 238 | - { |
|
| 239 | - return $this->internalCacheInstance; |
|
| 240 | - } |
|
| 39 | + /** |
|
| 40 | + * @var ExtendedCacheItemPoolInterface |
|
| 41 | + */ |
|
| 42 | + protected $internalCacheInstance; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Psr16Adapter constructor. |
|
| 46 | + * @param $driver |
|
| 47 | + * @param null|ConfigurationOptionInterface $config |
|
| 48 | + * @throws PhpfastcacheDriverCheckException |
|
| 49 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 50 | + * @throws PhpfastcacheLogicException |
|
| 51 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
| 52 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException |
|
| 53 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException |
|
| 54 | + * @throws \ReflectionException |
|
| 55 | + */ |
|
| 56 | + public function __construct($driver, ConfigurationOptionInterface $config = null) |
|
| 57 | + { |
|
| 58 | + if ($driver instanceof ExtendedCacheItemPoolInterface) { |
|
| 59 | + if ($config !== null) { |
|
| 60 | + throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter."); |
|
| 61 | + } |
|
| 62 | + $this->internalCacheInstance = $driver; |
|
| 63 | + } else { |
|
| 64 | + $this->internalCacheInstance = CacheManager::getInstance($driver, $config); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param string $key |
|
| 70 | + * @param mixed|null $default |
|
| 71 | + * @return mixed|null |
|
| 72 | + * @throws PhpfastcacheSimpleCacheException |
|
| 73 | + * @throws \Psr\Cache\InvalidArgumentException |
|
| 74 | + */ |
|
| 75 | + public function get($key, $default = null) |
|
| 76 | + { |
|
| 77 | + try { |
|
| 78 | + $cacheItem = $this->internalCacheInstance->getItem($key); |
|
| 79 | + if (!$cacheItem->isExpired() && $cacheItem->get() !== null) { |
|
| 80 | + return $cacheItem->get(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $default; |
|
| 84 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 85 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param string $key |
|
| 91 | + * @param mixed $value |
|
| 92 | + * @param null|int|DateInterval $ttl |
|
| 93 | + * @return bool |
|
| 94 | + * @throws PhpfastcacheSimpleCacheException |
|
| 95 | + * @throws \Psr\Cache\InvalidArgumentException |
|
| 96 | + */ |
|
| 97 | + public function set($key, $value, $ttl = null): bool |
|
| 98 | + { |
|
| 99 | + try { |
|
| 100 | + $cacheItem = $this->internalCacheInstance |
|
| 101 | + ->getItem($key) |
|
| 102 | + ->set($value); |
|
| 103 | + if (\is_int($ttl) && $ttl <= 0) { |
|
| 104 | + $cacheItem->expiresAt((new DateTime('@0'))); |
|
| 105 | + } elseif ($ttl !== null) { |
|
| 106 | + $cacheItem->expiresAfter($ttl); |
|
| 107 | + } |
|
| 108 | + return $this->internalCacheInstance->save($cacheItem); |
|
| 109 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 110 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param string $key |
|
| 116 | + * @return bool |
|
| 117 | + * @throws PhpfastcacheSimpleCacheException |
|
| 118 | + */ |
|
| 119 | + public function delete($key): bool |
|
| 120 | + { |
|
| 121 | + try { |
|
| 122 | + return $this->internalCacheInstance->deleteItem($key); |
|
| 123 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 124 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return bool |
|
| 130 | + * @throws PhpfastcacheSimpleCacheException |
|
| 131 | + */ |
|
| 132 | + public function clear(): bool |
|
| 133 | + { |
|
| 134 | + try { |
|
| 135 | + return $this->internalCacheInstance->clear(); |
|
| 136 | + } catch (PhpfastcacheRootException $e) { |
|
| 137 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param iterable $keys |
|
| 143 | + * @param null $default |
|
| 144 | + * @return ExtendedCacheItemInterface[]|iterable |
|
| 145 | + * @throws PhpfastcacheSimpleCacheException |
|
| 146 | + * @throws \Psr\Cache\InvalidArgumentException |
|
| 147 | + */ |
|
| 148 | + public function getMultiple($keys, $default = null) |
|
| 149 | + { |
|
| 150 | + if ($keys instanceof Traversable) { |
|
| 151 | + $keys = \iterator_to_array($keys); |
|
| 152 | + } |
|
| 153 | + try { |
|
| 154 | + return \array_map( |
|
| 155 | + static function (ExtendedCacheItemInterface $item) use ($default) { |
|
| 156 | + return $item->isHit() ? $item->get() : $default; |
|
| 157 | + }, |
|
| 158 | + $this->internalCacheInstance->getItems($keys) |
|
| 159 | + ); |
|
| 160 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 161 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param string[] $values |
|
| 167 | + * @param null|int|DateInterval $ttl |
|
| 168 | + * @return bool |
|
| 169 | + * @throws PhpfastcacheSimpleCacheException |
|
| 170 | + * @throws \Psr\Cache\InvalidArgumentException |
|
| 171 | + */ |
|
| 172 | + public function setMultiple($values, $ttl = null): bool |
|
| 173 | + { |
|
| 174 | + try { |
|
| 175 | + foreach ($values as $key => $value) { |
|
| 176 | + $cacheItem = $this->internalCacheInstance->getItem($key)->set($value); |
|
| 177 | + |
|
| 178 | + if (\is_int($ttl) && $ttl <= 0) { |
|
| 179 | + $cacheItem->expiresAt((new DateTime('@0'))); |
|
| 180 | + } elseif ($ttl !== null) { |
|
| 181 | + $cacheItem->expiresAfter($ttl); |
|
| 182 | + } |
|
| 183 | + $this->internalCacheInstance->saveDeferred($cacheItem); |
|
| 184 | + unset($cacheItem); |
|
| 185 | + } |
|
| 186 | + return $this->internalCacheInstance->commit(); |
|
| 187 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 188 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @param iterable|array $keys |
|
| 194 | + * @return bool |
|
| 195 | + * @throws PhpfastcacheSimpleCacheException |
|
| 196 | + * @throws \Psr\Cache\InvalidArgumentException |
|
| 197 | + */ |
|
| 198 | + public function deleteMultiple($keys): bool |
|
| 199 | + { |
|
| 200 | + try { |
|
| 201 | + if ($keys instanceof Traversable) { |
|
| 202 | + return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys)); |
|
| 203 | + } elseif (\is_array($keys)) { |
|
| 204 | + return $this->internalCacheInstance->deleteItems($keys); |
|
| 205 | + } else { |
|
| 206 | + throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.'); |
|
| 207 | + } |
|
| 208 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 209 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @param string $key |
|
| 215 | + * @return bool |
|
| 216 | + * @throws PhpfastcacheSimpleCacheException |
|
| 217 | + */ |
|
| 218 | + public function has($key): bool |
|
| 219 | + { |
|
| 220 | + try { |
|
| 221 | + $cacheItem = $this->internalCacheInstance->getItem($key); |
|
| 222 | + return $cacheItem->isHit() && !$cacheItem->isExpired(); |
|
| 223 | + } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 224 | + throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Extra methods that are not part of |
|
| 230 | + * psr16 specifications |
|
| 231 | + */ |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @return ExtendedCacheItemPoolInterface |
|
| 235 | + * @internal |
|
| 236 | + */ |
|
| 237 | + public function getInternalCacheInstance(): ExtendedCacheItemPoolInterface |
|
| 238 | + { |
|
| 239 | + return $this->internalCacheInstance; |
|
| 240 | + } |
|
| 241 | 241 | } |
@@ -53,14 +53,14 @@ discard block |
||
| 53 | 53 | * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException |
| 54 | 54 | * @throws \ReflectionException |
| 55 | 55 | */ |
| 56 | - public function __construct($driver, ConfigurationOptionInterface $config = null) |
|
| 57 | - { |
|
| 56 | + public function __construct($driver, ConfigurationOptionInterface $config = null) { |
|
| 58 | 57 | if ($driver instanceof ExtendedCacheItemPoolInterface) { |
| 59 | 58 | if ($config !== null) { |
| 60 | 59 | throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter."); |
| 61 | 60 | } |
| 62 | 61 | $this->internalCacheInstance = $driver; |
| 63 | - } else { |
|
| 62 | + } |
|
| 63 | + else { |
|
| 64 | 64 | $this->internalCacheInstance = CacheManager::getInstance($driver, $config); |
| 65 | 65 | } |
| 66 | 66 | } |
@@ -72,8 +72,7 @@ discard block |
||
| 72 | 72 | * @throws PhpfastcacheSimpleCacheException |
| 73 | 73 | * @throws \Psr\Cache\InvalidArgumentException |
| 74 | 74 | */ |
| 75 | - public function get($key, $default = null) |
|
| 76 | - { |
|
| 75 | + public function get($key, $default = null) { |
|
| 77 | 76 | try { |
| 78 | 77 | $cacheItem = $this->internalCacheInstance->getItem($key); |
| 79 | 78 | if (!$cacheItem->isExpired() && $cacheItem->get() !== null) { |
@@ -81,7 +80,8 @@ discard block |
||
| 81 | 80 | } |
| 82 | 81 | |
| 83 | 82 | return $default; |
| 84 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 83 | + } |
|
| 84 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 85 | 85 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -102,11 +102,13 @@ discard block |
||
| 102 | 102 | ->set($value); |
| 103 | 103 | if (\is_int($ttl) && $ttl <= 0) { |
| 104 | 104 | $cacheItem->expiresAt((new DateTime('@0'))); |
| 105 | - } elseif ($ttl !== null) { |
|
| 105 | + } |
|
| 106 | + elseif ($ttl !== null) { |
|
| 106 | 107 | $cacheItem->expiresAfter($ttl); |
| 107 | 108 | } |
| 108 | 109 | return $this->internalCacheInstance->save($cacheItem); |
| 109 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 110 | + } |
|
| 111 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 110 | 112 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 111 | 113 | } |
| 112 | 114 | } |
@@ -120,7 +122,8 @@ discard block |
||
| 120 | 122 | { |
| 121 | 123 | try { |
| 122 | 124 | return $this->internalCacheInstance->deleteItem($key); |
| 123 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 125 | + } |
|
| 126 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 124 | 127 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 125 | 128 | } |
| 126 | 129 | } |
@@ -133,7 +136,8 @@ discard block |
||
| 133 | 136 | { |
| 134 | 137 | try { |
| 135 | 138 | return $this->internalCacheInstance->clear(); |
| 136 | - } catch (PhpfastcacheRootException $e) { |
|
| 139 | + } |
|
| 140 | + catch (PhpfastcacheRootException $e) { |
|
| 137 | 141 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 138 | 142 | } |
| 139 | 143 | } |
@@ -145,8 +149,7 @@ discard block |
||
| 145 | 149 | * @throws PhpfastcacheSimpleCacheException |
| 146 | 150 | * @throws \Psr\Cache\InvalidArgumentException |
| 147 | 151 | */ |
| 148 | - public function getMultiple($keys, $default = null) |
|
| 149 | - { |
|
| 152 | + public function getMultiple($keys, $default = null) { |
|
| 150 | 153 | if ($keys instanceof Traversable) { |
| 151 | 154 | $keys = \iterator_to_array($keys); |
| 152 | 155 | } |
@@ -157,7 +160,8 @@ discard block |
||
| 157 | 160 | }, |
| 158 | 161 | $this->internalCacheInstance->getItems($keys) |
| 159 | 162 | ); |
| 160 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 163 | + } |
|
| 164 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 161 | 165 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 162 | 166 | } |
| 163 | 167 | } |
@@ -177,14 +181,16 @@ discard block |
||
| 177 | 181 | |
| 178 | 182 | if (\is_int($ttl) && $ttl <= 0) { |
| 179 | 183 | $cacheItem->expiresAt((new DateTime('@0'))); |
| 180 | - } elseif ($ttl !== null) { |
|
| 184 | + } |
|
| 185 | + elseif ($ttl !== null) { |
|
| 181 | 186 | $cacheItem->expiresAfter($ttl); |
| 182 | 187 | } |
| 183 | 188 | $this->internalCacheInstance->saveDeferred($cacheItem); |
| 184 | 189 | unset($cacheItem); |
| 185 | 190 | } |
| 186 | 191 | return $this->internalCacheInstance->commit(); |
| 187 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 192 | + } |
|
| 193 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 188 | 194 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 189 | 195 | } |
| 190 | 196 | } |
@@ -200,12 +206,15 @@ discard block |
||
| 200 | 206 | try { |
| 201 | 207 | if ($keys instanceof Traversable) { |
| 202 | 208 | return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys)); |
| 203 | - } elseif (\is_array($keys)) { |
|
| 209 | + } |
|
| 210 | + elseif (\is_array($keys)) { |
|
| 204 | 211 | return $this->internalCacheInstance->deleteItems($keys); |
| 205 | - } else { |
|
| 212 | + } |
|
| 213 | + else { |
|
| 206 | 214 | throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.'); |
| 207 | 215 | } |
| 208 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 216 | + } |
|
| 217 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 209 | 218 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 210 | 219 | } |
| 211 | 220 | } |
@@ -220,7 +229,8 @@ discard block |
||
| 220 | 229 | try { |
| 221 | 230 | $cacheItem = $this->internalCacheInstance->getItem($key); |
| 222 | 231 | return $cacheItem->isHit() && !$cacheItem->isExpired(); |
| 223 | - } catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 232 | + } |
|
| 233 | + catch (PhpfastcacheInvalidArgumentException $e) { |
|
| 224 | 234 | throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
| 225 | 235 | } |
| 226 | 236 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * Register Autoload |
| 31 | 31 | */ |
| 32 | 32 | spl_autoload_register( |
| 33 | - static function ($entity): void { |
|
| 33 | + static function($entity): void { |
|
| 34 | 34 | $module = explode('\\', $entity, 2); |
| 35 | 35 | if (!\in_array($module[0], ['Phpfastcache', 'Psr'])) { |
| 36 | 36 | /** |
@@ -64,9 +64,9 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | $entityPath = str_replace('\\', '/', $entity); |
| 66 | 66 | |
| 67 | - if(\strpos($entity, PFC_TESTS_NS) === 0){ |
|
| 67 | + if (\strpos($entity, PFC_TESTS_NS) === 0) { |
|
| 68 | 68 | $path = PFC_TESTS_DIR . \str_replace(str_replace('\\', '/', PFC_TESTS_NS), '', $entityPath) . '.' . PFC_PHP_EXT; |
| 69 | - }else{ |
|
| 69 | + } else { |
|
| 70 | 70 | $path = PFC_LIB_DIR . $entityPath . '.' . PFC_PHP_EXT; |
| 71 | 71 | } |
| 72 | 72 | |
@@ -30,56 +30,56 @@ |
||
| 30 | 30 | * Register Autoload |
| 31 | 31 | */ |
| 32 | 32 | spl_autoload_register( |
| 33 | - static function ($entity): void { |
|
| 34 | - $module = explode('\\', $entity, 2); |
|
| 35 | - if (!\in_array($module[0], ['Phpfastcache', 'Psr'])) { |
|
| 36 | - /** |
|
| 37 | - * Not a part of phpFastCache file |
|
| 38 | - * then we return here. |
|
| 39 | - */ |
|
| 40 | - return; |
|
| 41 | - } |
|
| 33 | + static function ($entity): void { |
|
| 34 | + $module = explode('\\', $entity, 2); |
|
| 35 | + if (!\in_array($module[0], ['Phpfastcache', 'Psr'])) { |
|
| 36 | + /** |
|
| 37 | + * Not a part of phpFastCache file |
|
| 38 | + * then we return here. |
|
| 39 | + */ |
|
| 40 | + return; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - if (\strpos($entity, 'Psr\Cache') === 0) { |
|
| 44 | - $path = PFC_BIN_DIR . 'dependencies/Psr/Cache/src/' . substr(strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
| 43 | + if (\strpos($entity, 'Psr\Cache') === 0) { |
|
| 44 | + $path = PFC_BIN_DIR . 'dependencies/Psr/Cache/src/' . substr(strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
| 45 | 45 | |
| 46 | - if (\is_readable($path)) { |
|
| 47 | - require_once $path; |
|
| 48 | - } else { |
|
| 49 | - \trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR); |
|
| 50 | - } |
|
| 51 | - return; |
|
| 52 | - } |
|
| 46 | + if (\is_readable($path)) { |
|
| 47 | + require_once $path; |
|
| 48 | + } else { |
|
| 49 | + \trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR); |
|
| 50 | + } |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - if (\strpos($entity, 'Psr\SimpleCache') === 0) { |
|
| 55 | - $path = PFC_BIN_DIR . 'dependencies/Psr/SimpleCache/src/' . \substr(\strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
| 54 | + if (\strpos($entity, 'Psr\SimpleCache') === 0) { |
|
| 55 | + $path = PFC_BIN_DIR . 'dependencies/Psr/SimpleCache/src/' . \substr(\strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT; |
|
| 56 | 56 | |
| 57 | - if (\is_readable($path)) { |
|
| 58 | - require_once $path; |
|
| 59 | - } else { |
|
| 60 | - \trigger_error('Cannot locate the Psr/SimpleCache files', E_USER_ERROR); |
|
| 61 | - } |
|
| 62 | - return; |
|
| 63 | - } |
|
| 57 | + if (\is_readable($path)) { |
|
| 58 | + require_once $path; |
|
| 59 | + } else { |
|
| 60 | + \trigger_error('Cannot locate the Psr/SimpleCache files', E_USER_ERROR); |
|
| 61 | + } |
|
| 62 | + return; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - $entityPath = str_replace('\\', '/', $entity); |
|
| 65 | + $entityPath = str_replace('\\', '/', $entity); |
|
| 66 | 66 | |
| 67 | - if(\strpos($entity, PFC_TESTS_NS) === 0){ |
|
| 68 | - $path = PFC_TESTS_DIR . \str_replace(str_replace('\\', '/', PFC_TESTS_NS), '', $entityPath) . '.' . PFC_PHP_EXT; |
|
| 69 | - }else{ |
|
| 70 | - $path = PFC_LIB_DIR . $entityPath . '.' . PFC_PHP_EXT; |
|
| 71 | - } |
|
| 67 | + if(\strpos($entity, PFC_TESTS_NS) === 0){ |
|
| 68 | + $path = PFC_TESTS_DIR . \str_replace(str_replace('\\', '/', PFC_TESTS_NS), '', $entityPath) . '.' . PFC_PHP_EXT; |
|
| 69 | + }else{ |
|
| 70 | + $path = PFC_LIB_DIR . $entityPath . '.' . PFC_PHP_EXT; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $path = \realpath($path); |
|
| 74 | - if (\is_readable($path)) { |
|
| 75 | - require_once $path; |
|
| 76 | - } |
|
| 77 | - } |
|
| 73 | + $path = \realpath($path); |
|
| 74 | + if (\is_readable($path)) { |
|
| 75 | + require_once $path; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | if ((!\defined('PFC_IGNORE_COMPOSER_WARNING') || !PFC_IGNORE_COMPOSER_WARNING) && \class_exists(ClassLoader::class)) { |
| 81 | - trigger_error( |
|
| 82 | - 'Your project already makes use of Composer. You SHOULD use the composer dependency "phpfastcache/phpfastcache" instead of hard-autoloading.', |
|
| 83 | - E_USER_WARNING |
|
| 84 | - ); |
|
| 81 | + trigger_error( |
|
| 82 | + 'Your project already makes use of Composer. You SHOULD use the composer dependency "phpfastcache/phpfastcache" instead of hard-autoloading.', |
|
| 83 | + E_USER_WARNING |
|
| 84 | + ); |
|
| 85 | 85 | } |
@@ -45,7 +45,8 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | if (\is_readable($path)) { |
| 47 | 47 | require_once $path; |
| 48 | - } else { |
|
| 48 | + } |
|
| 49 | + else { |
|
| 49 | 50 | \trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR); |
| 50 | 51 | } |
| 51 | 52 | return; |
@@ -56,7 +57,8 @@ discard block |
||
| 56 | 57 | |
| 57 | 58 | if (\is_readable($path)) { |
| 58 | 59 | require_once $path; |
| 59 | - } else { |
|
| 60 | + } |
|
| 61 | + else { |
|
| 60 | 62 | \trigger_error('Cannot locate the Psr/SimpleCache files', E_USER_ERROR); |
| 61 | 63 | } |
| 62 | 64 | return; |
@@ -66,7 +68,8 @@ discard block |
||
| 66 | 68 | |
| 67 | 69 | if(\strpos($entity, PFC_TESTS_NS) === 0){ |
| 68 | 70 | $path = PFC_TESTS_DIR . \str_replace(str_replace('\\', '/', PFC_TESTS_NS), '', $entityPath) . '.' . PFC_PHP_EXT; |
| 69 | - }else{ |
|
| 71 | + } |
|
| 72 | + else{ |
|
| 70 | 73 | $path = PFC_LIB_DIR . $entityPath . '.' . PFC_PHP_EXT; |
| 71 | 74 | } |
| 72 | 75 | |
@@ -91,8 +91,7 @@ discard block |
||
| 91 | 91 | return unlink($source); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - $files = new RecursiveIteratorIterator |
|
| 95 | - ( |
|
| 94 | + $files = new RecursiveIteratorIterator( |
|
| 96 | 95 | new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
| 97 | 96 | RecursiveIteratorIterator::CHILD_FIRST |
| 98 | 97 | ); |
@@ -102,7 +101,7 @@ discard block |
||
| 102 | 101 | * @var SplFileInfo $fileinfo |
| 103 | 102 | */ |
| 104 | 103 | $realpath = $fileinfo->getRealPath(); |
| 105 | - if($realpath){ |
|
| 104 | + if ($realpath) { |
|
| 106 | 105 | if ($fileinfo->isDir()) { |
| 107 | 106 | if (self::rrmdir($fileinfo->getRealPath()) === false) { |
| 108 | 107 | return false; |
@@ -111,7 +110,7 @@ discard block |
||
| 111 | 110 | return false; |
| 112 | 111 | } |
| 113 | 112 | } |
| 114 | - else{ |
|
| 113 | + else { |
|
| 115 | 114 | return false; |
| 116 | 115 | } |
| 117 | 116 | } |
@@ -148,7 +147,7 @@ discard block |
||
| 148 | 147 | /** |
| 149 | 148 | * Allows to dereference char |
| 150 | 149 | */ |
| 151 | - $__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 150 | + $__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__); // remove file protocols such as "phar://" etc. |
|
| 152 | 151 | $prefix = $__FILE__[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
| 153 | 152 | return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
| 154 | 153 | } |
@@ -26,130 +26,130 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class Directory |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * Get the directory size |
|
| 31 | - * @param string $directory |
|
| 32 | - * @param bool $includeDirAllocSize |
|
| 33 | - * @return integer |
|
| 34 | - */ |
|
| 35 | - public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
| 36 | - { |
|
| 37 | - $size = 0; |
|
| 38 | - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
| 39 | - /** |
|
| 40 | - * @var SplFileInfo $file |
|
| 41 | - */ |
|
| 42 | - if ($file->isFile()) { |
|
| 43 | - $size += filesize($file->getRealPath()); |
|
| 44 | - } else { |
|
| 45 | - if ($includeDirAllocSize) { |
|
| 46 | - $size += $file->getSize(); |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - } |
|
| 29 | + /** |
|
| 30 | + * Get the directory size |
|
| 31 | + * @param string $directory |
|
| 32 | + * @param bool $includeDirAllocSize |
|
| 33 | + * @return integer |
|
| 34 | + */ |
|
| 35 | + public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
| 36 | + { |
|
| 37 | + $size = 0; |
|
| 38 | + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
| 39 | + /** |
|
| 40 | + * @var SplFileInfo $file |
|
| 41 | + */ |
|
| 42 | + if ($file->isFile()) { |
|
| 43 | + $size += filesize($file->getRealPath()); |
|
| 44 | + } else { |
|
| 45 | + if ($includeDirAllocSize) { |
|
| 46 | + $size += $file->getSize(); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - return $size; |
|
| 52 | - } |
|
| 51 | + return $size; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @param string $path |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public static function getFileCount(string $path): int |
|
| 59 | - { |
|
| 60 | - $count = 0; |
|
| 61 | - $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
| 62 | - foreach ($objects as $object) { |
|
| 63 | - /** |
|
| 64 | - * @var SplFileInfo $object |
|
| 65 | - */ |
|
| 66 | - if ($object->isFile()) { |
|
| 67 | - $count++; |
|
| 68 | - } |
|
| 69 | - } |
|
| 54 | + /** |
|
| 55 | + * @param string $path |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public static function getFileCount(string $path): int |
|
| 59 | + { |
|
| 60 | + $count = 0; |
|
| 61 | + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
| 62 | + foreach ($objects as $object) { |
|
| 63 | + /** |
|
| 64 | + * @var SplFileInfo $object |
|
| 65 | + */ |
|
| 66 | + if ($object->isFile()) { |
|
| 67 | + $count++; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return $count; |
|
| 72 | - } |
|
| 71 | + return $count; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line. |
|
| 76 | - * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
| 77 | - * |
|
| 78 | - * @param string $source absolute path to directory or file to delete. |
|
| 79 | - * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
| 80 | - * |
|
| 81 | - * @return bool true on success; false on failure |
|
| 82 | - */ |
|
| 83 | - public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
| 84 | - { |
|
| 85 | - if (empty($source) || file_exists($source) === false) { |
|
| 86 | - return false; |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line. |
|
| 76 | + * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
| 77 | + * |
|
| 78 | + * @param string $source absolute path to directory or file to delete. |
|
| 79 | + * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
| 80 | + * |
|
| 81 | + * @return bool true on success; false on failure |
|
| 82 | + */ |
|
| 83 | + public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
| 84 | + { |
|
| 85 | + if (empty($source) || file_exists($source) === false) { |
|
| 86 | + return false; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - if (is_file($source) || is_link($source)) { |
|
| 90 | - clearstatcache(true, $source); |
|
| 91 | - return unlink($source); |
|
| 92 | - } |
|
| 89 | + if (is_file($source) || is_link($source)) { |
|
| 90 | + clearstatcache(true, $source); |
|
| 91 | + return unlink($source); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - $files = new RecursiveIteratorIterator |
|
| 95 | - ( |
|
| 96 | - new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
|
| 97 | - RecursiveIteratorIterator::CHILD_FIRST |
|
| 98 | - ); |
|
| 94 | + $files = new RecursiveIteratorIterator |
|
| 95 | + ( |
|
| 96 | + new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
|
| 97 | + RecursiveIteratorIterator::CHILD_FIRST |
|
| 98 | + ); |
|
| 99 | 99 | |
| 100 | - foreach ($files as $fileinfo) { |
|
| 101 | - /** |
|
| 102 | - * @var SplFileInfo $fileinfo |
|
| 103 | - */ |
|
| 104 | - $realpath = $fileinfo->getRealPath(); |
|
| 105 | - if($realpath){ |
|
| 106 | - if ($fileinfo->isDir()) { |
|
| 107 | - if (self::rrmdir($fileinfo->getRealPath()) === false) { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - } elseif (unlink($realpath) === false) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - else{ |
|
| 115 | - return false; |
|
| 116 | - } |
|
| 117 | - } |
|
| 100 | + foreach ($files as $fileinfo) { |
|
| 101 | + /** |
|
| 102 | + * @var SplFileInfo $fileinfo |
|
| 103 | + */ |
|
| 104 | + $realpath = $fileinfo->getRealPath(); |
|
| 105 | + if($realpath){ |
|
| 106 | + if ($fileinfo->isDir()) { |
|
| 107 | + if (self::rrmdir($fileinfo->getRealPath()) === false) { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + } elseif (unlink($realpath) === false) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + else{ |
|
| 115 | + return false; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - if ($removeOnlyChildren === false) { |
|
| 120 | - return rmdir($source); |
|
| 121 | - } |
|
| 119 | + if ($removeOnlyChildren === false) { |
|
| 120 | + return rmdir($source); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - return true; |
|
| 124 | - } |
|
| 123 | + return true; |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Alias of realpath() but work |
|
| 128 | - * on non-existing files |
|
| 129 | - * |
|
| 130 | - * @param string $path |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - public static function getAbsolutePath(string $path): string |
|
| 134 | - { |
|
| 135 | - $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
| 136 | - $absolutes = []; |
|
| 137 | - foreach ($parts as $part) { |
|
| 138 | - if ('.' === $part) { |
|
| 139 | - continue; |
|
| 140 | - } |
|
| 141 | - if ('..' === $part) { |
|
| 142 | - array_pop($absolutes); |
|
| 143 | - } else { |
|
| 144 | - $absolutes[] = $part; |
|
| 145 | - } |
|
| 146 | - } |
|
| 126 | + /** |
|
| 127 | + * Alias of realpath() but work |
|
| 128 | + * on non-existing files |
|
| 129 | + * |
|
| 130 | + * @param string $path |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + public static function getAbsolutePath(string $path): string |
|
| 134 | + { |
|
| 135 | + $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
| 136 | + $absolutes = []; |
|
| 137 | + foreach ($parts as $part) { |
|
| 138 | + if ('.' === $part) { |
|
| 139 | + continue; |
|
| 140 | + } |
|
| 141 | + if ('..' === $part) { |
|
| 142 | + array_pop($absolutes); |
|
| 143 | + } else { |
|
| 144 | + $absolutes[] = $part; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Allows to dereference char |
|
| 150 | - */ |
|
| 151 | - $__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 152 | - $prefix = $__FILE__[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
| 153 | - return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 154 | - } |
|
| 148 | + /** |
|
| 149 | + * Allows to dereference char |
|
| 150 | + */ |
|
| 151 | + $__FILE__ = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 152 | + $prefix = $__FILE__[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
| 153 | + return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -41,7 +41,8 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | if ($file->isFile()) { |
| 43 | 43 | $size += filesize($file->getRealPath()); |
| 44 | - } else { |
|
| 44 | + } |
|
| 45 | + else { |
|
| 45 | 46 | if ($includeDirAllocSize) { |
| 46 | 47 | $size += $file->getSize(); |
| 47 | 48 | } |
@@ -107,7 +108,8 @@ discard block |
||
| 107 | 108 | if (self::rrmdir($fileinfo->getRealPath()) === false) { |
| 108 | 109 | return false; |
| 109 | 110 | } |
| 110 | - } elseif (unlink($realpath) === false) { |
|
| 111 | + } |
|
| 112 | + elseif (unlink($realpath) === false) { |
|
| 111 | 113 | return false; |
| 112 | 114 | } |
| 113 | 115 | } |
@@ -140,7 +142,8 @@ discard block |
||
| 140 | 142 | } |
| 141 | 143 | if ('..' === $part) { |
| 142 | 144 | array_pop($absolutes); |
| 143 | - } else { |
|
| 145 | + } |
|
| 146 | + else { |
|
| 144 | 147 | $absolutes[] = $part; |
| 145 | 148 | } |
| 146 | 149 | } |
@@ -99,14 +99,14 @@ |
||
| 99 | 99 | case T_NAMESPACE: |
| 100 | 100 | $namespace = ''; |
| 101 | 101 | // If there is a namespace, extract it (PHP 8 test) |
| 102 | - if(\defined('T_NAME_QUALIFIED')){ |
|
| 102 | + if (\defined('T_NAME_QUALIFIED')) { |
|
| 103 | 103 | while (isset($tokens[++$i][1])) { |
| 104 | 104 | if ($tokens[$i][0] === T_NAME_QUALIFIED) { |
| 105 | 105 | $namespace = $tokens[$i][1]; |
| 106 | 106 | break; |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | - }else{ |
|
| 109 | + } else { |
|
| 110 | 110 | while (isset($tokens[++$i][1])) { |
| 111 | 111 | if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) { |
| 112 | 112 | $namespace .= $tokens[$i][1]; |
@@ -28,148 +28,148 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | trait ClassNamespaceResolverTrait |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $namespace; |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $namespace; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Iterate over all files in the given directory searching for classes. |
|
| 38 | - * |
|
| 39 | - * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 40 | - * deprecated the whole component as of SF4. Our thanks to them. |
|
| 41 | - * |
|
| 42 | - * @param Iterator|string|array $dir The directory to search in or an iterator |
|
| 43 | - * |
|
| 44 | - * @return array A class map array |
|
| 45 | - */ |
|
| 46 | - protected static function createClassMap($dir): array |
|
| 47 | - { |
|
| 48 | - if (\is_string($dir)) { |
|
| 49 | - $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
|
| 50 | - } |
|
| 51 | - $map = []; |
|
| 36 | + /** |
|
| 37 | + * Iterate over all files in the given directory searching for classes. |
|
| 38 | + * |
|
| 39 | + * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 40 | + * deprecated the whole component as of SF4. Our thanks to them. |
|
| 41 | + * |
|
| 42 | + * @param Iterator|string|array $dir The directory to search in or an iterator |
|
| 43 | + * |
|
| 44 | + * @return array A class map array |
|
| 45 | + */ |
|
| 46 | + protected static function createClassMap($dir): array |
|
| 47 | + { |
|
| 48 | + if (\is_string($dir)) { |
|
| 49 | + $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
|
| 50 | + } |
|
| 51 | + $map = []; |
|
| 52 | 52 | |
| 53 | - if (\is_iterable($dir)) { |
|
| 54 | - foreach ($dir as $file) { |
|
| 55 | - if (!$file->isFile()) { |
|
| 56 | - continue; |
|
| 57 | - } |
|
| 58 | - $path = $file->getRealPath() ?: $file->getPathname(); |
|
| 59 | - if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { |
|
| 60 | - continue; |
|
| 61 | - } |
|
| 62 | - $classes = self::findClasses($path); |
|
| 63 | - if (PHP_VERSION_ID >= 70000) { |
|
| 64 | - // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
|
| 65 | - gc_mem_caches(); |
|
| 66 | - } |
|
| 67 | - foreach ($classes as $class) { |
|
| 68 | - $map[$class] = $path; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - } |
|
| 53 | + if (\is_iterable($dir)) { |
|
| 54 | + foreach ($dir as $file) { |
|
| 55 | + if (!$file->isFile()) { |
|
| 56 | + continue; |
|
| 57 | + } |
|
| 58 | + $path = $file->getRealPath() ?: $file->getPathname(); |
|
| 59 | + if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { |
|
| 60 | + continue; |
|
| 61 | + } |
|
| 62 | + $classes = self::findClasses($path); |
|
| 63 | + if (PHP_VERSION_ID >= 70000) { |
|
| 64 | + // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
|
| 65 | + gc_mem_caches(); |
|
| 66 | + } |
|
| 67 | + foreach ($classes as $class) { |
|
| 68 | + $map[$class] = $path; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return $map; |
|
| 74 | - } |
|
| 73 | + return $map; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Extract the classes in the given file. |
|
| 78 | - * |
|
| 79 | - * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 80 | - * deprecated the whole component as of SF4. Our thanks to them. |
|
| 81 | - * |
|
| 82 | - * @param string $path The file to check |
|
| 83 | - * |
|
| 84 | - * @return array The found classes |
|
| 85 | - */ |
|
| 86 | - protected static function findClasses(string $path): array |
|
| 87 | - { |
|
| 88 | - $contents = file_get_contents($path); |
|
| 89 | - $tokens = token_get_all($contents); |
|
| 90 | - $classes = []; |
|
| 91 | - $namespace = ''; |
|
| 92 | - for ($i = 0; isset($tokens[$i]); ++$i) { |
|
| 93 | - $token = $tokens[$i]; |
|
| 94 | - if (!isset($token[1])) { |
|
| 95 | - continue; |
|
| 96 | - } |
|
| 97 | - $class = ''; |
|
| 98 | - switch ($token[0]) { |
|
| 99 | - case T_NAMESPACE: |
|
| 100 | - $namespace = ''; |
|
| 101 | - // If there is a namespace, extract it (PHP 8 test) |
|
| 102 | - if(\defined('T_NAME_QUALIFIED')){ |
|
| 103 | - while (isset($tokens[++$i][1])) { |
|
| 104 | - if ($tokens[$i][0] === T_NAME_QUALIFIED) { |
|
| 105 | - $namespace = $tokens[$i][1]; |
|
| 106 | - break; |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - }else{ |
|
| 110 | - while (isset($tokens[++$i][1])) { |
|
| 111 | - if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
| 112 | - $namespace .= $tokens[$i][1]; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - $namespace .= '\\'; |
|
| 117 | - break; |
|
| 118 | - case T_CLASS: |
|
| 119 | - case T_INTERFACE: |
|
| 120 | - case T_TRAIT: |
|
| 121 | - // Skip usage of ::class constant |
|
| 122 | - $isClassConstant = false; |
|
| 123 | - for ($j = $i - 1; $j > 0; --$j) { |
|
| 124 | - if (!isset($tokens[$j][1])) { |
|
| 125 | - break; |
|
| 126 | - } |
|
| 127 | - if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
| 128 | - $isClassConstant = true; |
|
| 129 | - break; |
|
| 130 | - } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
| 131 | - break; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - if ($isClassConstant) { |
|
| 135 | - break; |
|
| 136 | - } |
|
| 137 | - // Find the classname |
|
| 138 | - while (isset($tokens[++$i][1])) { |
|
| 139 | - $t = $tokens[$i]; |
|
| 140 | - if (T_STRING === $t[0]) { |
|
| 141 | - $class .= $t[1]; |
|
| 142 | - } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 143 | - break; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - $classes[] = ltrim($namespace . $class, '\\'); |
|
| 147 | - break; |
|
| 148 | - default: |
|
| 149 | - break; |
|
| 150 | - } |
|
| 151 | - } |
|
| 76 | + /** |
|
| 77 | + * Extract the classes in the given file. |
|
| 78 | + * |
|
| 79 | + * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 80 | + * deprecated the whole component as of SF4. Our thanks to them. |
|
| 81 | + * |
|
| 82 | + * @param string $path The file to check |
|
| 83 | + * |
|
| 84 | + * @return array The found classes |
|
| 85 | + */ |
|
| 86 | + protected static function findClasses(string $path): array |
|
| 87 | + { |
|
| 88 | + $contents = file_get_contents($path); |
|
| 89 | + $tokens = token_get_all($contents); |
|
| 90 | + $classes = []; |
|
| 91 | + $namespace = ''; |
|
| 92 | + for ($i = 0; isset($tokens[$i]); ++$i) { |
|
| 93 | + $token = $tokens[$i]; |
|
| 94 | + if (!isset($token[1])) { |
|
| 95 | + continue; |
|
| 96 | + } |
|
| 97 | + $class = ''; |
|
| 98 | + switch ($token[0]) { |
|
| 99 | + case T_NAMESPACE: |
|
| 100 | + $namespace = ''; |
|
| 101 | + // If there is a namespace, extract it (PHP 8 test) |
|
| 102 | + if(\defined('T_NAME_QUALIFIED')){ |
|
| 103 | + while (isset($tokens[++$i][1])) { |
|
| 104 | + if ($tokens[$i][0] === T_NAME_QUALIFIED) { |
|
| 105 | + $namespace = $tokens[$i][1]; |
|
| 106 | + break; |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + }else{ |
|
| 110 | + while (isset($tokens[++$i][1])) { |
|
| 111 | + if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
| 112 | + $namespace .= $tokens[$i][1]; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + $namespace .= '\\'; |
|
| 117 | + break; |
|
| 118 | + case T_CLASS: |
|
| 119 | + case T_INTERFACE: |
|
| 120 | + case T_TRAIT: |
|
| 121 | + // Skip usage of ::class constant |
|
| 122 | + $isClassConstant = false; |
|
| 123 | + for ($j = $i - 1; $j > 0; --$j) { |
|
| 124 | + if (!isset($tokens[$j][1])) { |
|
| 125 | + break; |
|
| 126 | + } |
|
| 127 | + if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
| 128 | + $isClassConstant = true; |
|
| 129 | + break; |
|
| 130 | + } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
| 131 | + break; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + if ($isClassConstant) { |
|
| 135 | + break; |
|
| 136 | + } |
|
| 137 | + // Find the classname |
|
| 138 | + while (isset($tokens[++$i][1])) { |
|
| 139 | + $t = $tokens[$i]; |
|
| 140 | + if (T_STRING === $t[0]) { |
|
| 141 | + $class .= $t[1]; |
|
| 142 | + } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 143 | + break; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + $classes[] = ltrim($namespace . $class, '\\'); |
|
| 147 | + break; |
|
| 148 | + default: |
|
| 149 | + break; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - return $classes; |
|
| 154 | - } |
|
| 153 | + return $classes; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function getClassNamespace(): string |
|
| 160 | - { |
|
| 161 | - if (!$this->namespace) { |
|
| 162 | - $this->namespace = substr(static::class, 0, strrpos(static::class, '\\')); |
|
| 163 | - } |
|
| 156 | + /** |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function getClassNamespace(): string |
|
| 160 | + { |
|
| 161 | + if (!$this->namespace) { |
|
| 162 | + $this->namespace = substr(static::class, 0, strrpos(static::class, '\\')); |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - return $this->namespace; |
|
| 166 | - } |
|
| 165 | + return $this->namespace; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @return string |
|
| 170 | - */ |
|
| 171 | - public function getClassName(): string |
|
| 172 | - { |
|
| 173 | - return static::class; |
|
| 174 | - } |
|
| 168 | + /** |
|
| 169 | + * @return string |
|
| 170 | + */ |
|
| 171 | + public function getClassName(): string |
|
| 172 | + { |
|
| 173 | + return static::class; |
|
| 174 | + } |
|
| 175 | 175 | } |
@@ -106,7 +106,8 @@ discard block |
||
| 106 | 106 | break; |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | - }else{ |
|
| 109 | + } |
|
| 110 | + else{ |
|
| 110 | 111 | while (isset($tokens[++$i][1])) { |
| 111 | 112 | if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], true)) { |
| 112 | 113 | $namespace .= $tokens[$i][1]; |
@@ -127,7 +128,8 @@ discard block |
||
| 127 | 128 | if (T_DOUBLE_COLON === $tokens[$j][0]) { |
| 128 | 129 | $isClassConstant = true; |
| 129 | 130 | break; |
| 130 | - } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
| 131 | + } |
|
| 132 | + elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
| 131 | 133 | break; |
| 132 | 134 | } |
| 133 | 135 | } |
@@ -139,7 +141,8 @@ discard block |
||
| 139 | 141 | $t = $tokens[$i]; |
| 140 | 142 | if (T_STRING === $t[0]) { |
| 141 | 143 | $class .= $t[1]; |
| 142 | - } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 144 | + } |
|
| 145 | + elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 143 | 146 | break; |
| 144 | 147 | } |
| 145 | 148 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | if (\is_string($key)) { |
| 67 | 67 | $this->key = $key; |
| 68 | 68 | $this->driver = $driver; |
| 69 | - if($driver->getConfig()->isUseStaticItemCaching()){ |
|
| 69 | + if ($driver->getConfig()->isUseStaticItemCaching()) { |
|
| 70 | 70 | $this->driver->setItem($this); |
| 71 | 71 | } |
| 72 | 72 | $this->expirationDate = new DateTime(); |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | $this->data[] = $data; |
| 283 | 283 | } else { |
| 284 | 284 | if (\is_string($data)) { |
| 285 | - $this->data .= (string)$data; |
|
| 285 | + $this->data .= (string) $data; |
|
| 286 | 286 | } else { |
| 287 | 287 | throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
| 288 | 288 | } |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | \array_unshift($this->data, $data); |
| 304 | 304 | } else { |
| 305 | 305 | if (\is_string($data)) { |
| 306 | - $this->data = (string)$data . $this->data; |
|
| 306 | + $this->data = (string) $data . $this->data; |
|
| 307 | 307 | } else { |
| 308 | 308 | throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
| 309 | 309 | } |
@@ -36,333 +36,333 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | trait ItemExtendedTrait |
| 38 | 38 | { |
| 39 | - use ClassNamespaceResolverTrait; |
|
| 40 | - use TaggableCacheItemTrait; |
|
| 39 | + use ClassNamespaceResolverTrait; |
|
| 40 | + use TaggableCacheItemTrait; |
|
| 41 | 41 | |
| 42 | - /******************** |
|
| 42 | + /******************** |
|
| 43 | 43 | * |
| 44 | 44 | * PSR-6 Extended Methods |
| 45 | 45 | * |
| 46 | 46 | *******************/ |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @var ExtendedCacheItemPoolInterface |
|
| 50 | - */ |
|
| 51 | - protected $driver; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $encodedKey; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Item constructor. |
|
| 60 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 61 | - * @param $key |
|
| 62 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 63 | - */ |
|
| 64 | - public function __construct(ExtendedCacheItemPoolInterface $driver, $key) |
|
| 65 | - { |
|
| 66 | - if (\is_string($key)) { |
|
| 67 | - $this->key = $key; |
|
| 68 | - $this->driver = $driver; |
|
| 69 | - if($driver->getConfig()->isUseStaticItemCaching()){ |
|
| 70 | - $this->driver->setItem($this); |
|
| 71 | - } |
|
| 72 | - $this->expirationDate = new DateTime(); |
|
| 73 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 74 | - $this->creationDate = new DateTime(); |
|
| 75 | - $this->modificationDate = new DateTime(); |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - throw new PhpfastcacheInvalidArgumentTypeException('string', $key); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public function getEncodedKey(): string |
|
| 86 | - { |
|
| 87 | - if (!$this->encodedKey) { |
|
| 88 | - $keyHashFunction = $this->driver->getConfig()->getDefaultKeyHashFunction(); |
|
| 89 | - |
|
| 90 | - if ($keyHashFunction) { |
|
| 91 | - $this->encodedKey = $keyHashFunction($this->getKey()); |
|
| 92 | - } else { |
|
| 93 | - $this->encodedKey = $this->getKey(); |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return $this->encodedKey; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @return DateTimeInterface |
|
| 102 | - */ |
|
| 103 | - public function getExpirationDate(): DateTimeInterface |
|
| 104 | - { |
|
| 105 | - return $this->expirationDate; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Alias of expireAt() with forced $expiration param |
|
| 110 | - * |
|
| 111 | - * @param DateTimeInterface $expiration |
|
| 112 | - * The point in time after which the item MUST be considered expired. |
|
| 113 | - * If null is passed explicitly, a default value MAY be used. If none is set, |
|
| 114 | - * the value should be stored permanently or for as long as the |
|
| 115 | - * implementation allows. |
|
| 116 | - * |
|
| 117 | - * @return ExtendedCacheItemInterface |
|
| 118 | - * The called object. |
|
| 119 | - */ |
|
| 120 | - public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
| 121 | - { |
|
| 122 | - return $this->expiresAt($expiration); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @return DateTimeInterface |
|
| 127 | - * @throws PhpfastcacheLogicException |
|
| 128 | - */ |
|
| 129 | - public function getCreationDate(): DateTimeInterface |
|
| 130 | - { |
|
| 131 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 132 | - return $this->creationDate; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param DateTimeInterface $date |
|
| 140 | - * @return ExtendedCacheItemInterface |
|
| 141 | - * @throws PhpfastcacheLogicException |
|
| 142 | - */ |
|
| 143 | - public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 144 | - { |
|
| 145 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 146 | - $this->creationDate = $date; |
|
| 147 | - return $this; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @return DateTimeInterface |
|
| 155 | - * @throws PhpfastcacheLogicException |
|
| 156 | - */ |
|
| 157 | - public function getModificationDate(): DateTimeInterface |
|
| 158 | - { |
|
| 159 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 160 | - return $this->modificationDate; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param DateTimeInterface $date |
|
| 168 | - * @return ExtendedCacheItemInterface |
|
| 169 | - * @throws PhpfastcacheLogicException |
|
| 170 | - */ |
|
| 171 | - public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 172 | - { |
|
| 173 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 174 | - $this->modificationDate = $date; |
|
| 175 | - return $this; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @return int |
|
| 183 | - */ |
|
| 184 | - public function getTtl(): int |
|
| 185 | - { |
|
| 186 | - return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @return bool |
|
| 191 | - */ |
|
| 192 | - public function isExpired(): bool |
|
| 193 | - { |
|
| 194 | - return $this->expirationDate->getTimestamp() < (new DateTime())->getTimestamp(); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @return bool |
|
| 199 | - */ |
|
| 200 | - public function isNull(): bool |
|
| 201 | - { |
|
| 202 | - return $this->data === null; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @return bool |
|
| 207 | - */ |
|
| 208 | - public function isEmpty(): bool |
|
| 209 | - { |
|
| 210 | - return empty($this->data); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Return the data length: |
|
| 215 | - * Either the string length if it's a string (binary mode) |
|
| 216 | - * # or the number of element (count) if it's an array |
|
| 217 | - * # or the number returned by count() if it's an object implementing \Countable interface |
|
| 218 | - * # -1 for anything else |
|
| 219 | - * @return int |
|
| 220 | - */ |
|
| 221 | - public function getLength(): int |
|
| 222 | - { |
|
| 223 | - switch (\gettype($this->data)) { |
|
| 224 | - case 'array': |
|
| 225 | - case 'object': |
|
| 226 | - if (\is_array($this->data) || $this->data instanceof Countable) { |
|
| 227 | - return \count($this->data); |
|
| 228 | - } |
|
| 229 | - break; |
|
| 230 | - |
|
| 231 | - case 'string': |
|
| 232 | - return \strlen($this->data); |
|
| 233 | - break; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return -1; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @param int $step |
|
| 242 | - * @return ExtendedCacheItemInterface |
|
| 243 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 244 | - */ |
|
| 245 | - public function increment($step = 1): ExtendedCacheItemInterface |
|
| 246 | - { |
|
| 247 | - if (\is_int($step)) { |
|
| 248 | - $this->fetched = true; |
|
| 249 | - $this->data += $step; |
|
| 250 | - } else { |
|
| 251 | - throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $this; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @param int $step |
|
| 259 | - * @return ExtendedCacheItemInterface |
|
| 260 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 261 | - */ |
|
| 262 | - public function decrement($step = 1): ExtendedCacheItemInterface |
|
| 263 | - { |
|
| 264 | - if (\is_int($step)) { |
|
| 265 | - $this->fetched = true; |
|
| 266 | - $this->data -= $step; |
|
| 267 | - } else { |
|
| 268 | - throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return $this; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @param array|string $data |
|
| 276 | - * @return ExtendedCacheItemInterface |
|
| 277 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 278 | - */ |
|
| 279 | - public function append($data): ExtendedCacheItemInterface |
|
| 280 | - { |
|
| 281 | - if (\is_array($this->data)) { |
|
| 282 | - $this->data[] = $data; |
|
| 283 | - } else { |
|
| 284 | - if (\is_string($data)) { |
|
| 285 | - $this->data .= (string)$data; |
|
| 286 | - } else { |
|
| 287 | - throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - return $this; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * @param array|string $data |
|
| 297 | - * @return ExtendedCacheItemInterface |
|
| 298 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 299 | - */ |
|
| 300 | - public function prepend($data): ExtendedCacheItemInterface |
|
| 301 | - { |
|
| 302 | - if (\is_array($this->data)) { |
|
| 303 | - \array_unshift($this->data, $data); |
|
| 304 | - } else { |
|
| 305 | - if (\is_string($data)) { |
|
| 306 | - $this->data = (string)$data . $this->data; |
|
| 307 | - } else { |
|
| 308 | - throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - return $this; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Return the data as a well-formatted string. |
|
| 317 | - * Any scalar value will be casted to an array |
|
| 318 | - * @param int $option \json_encode() options |
|
| 319 | - * @param int $depth \json_encode() depth |
|
| 320 | - * @return string |
|
| 321 | - */ |
|
| 322 | - public function getDataAsJsonString(int $option = 0, int $depth = 512): string |
|
| 323 | - { |
|
| 324 | - $data = $this->get(); |
|
| 325 | - |
|
| 326 | - if (\is_object($data) || \is_array($data)) { |
|
| 327 | - $data = \json_encode($data, $option, $depth); |
|
| 328 | - } else { |
|
| 329 | - $data = \json_encode([$data], $option, $depth); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return \json_encode($data, $option, $depth); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Implements \JsonSerializable interface |
|
| 337 | - * @return mixed |
|
| 338 | - */ |
|
| 339 | - #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
| 340 | - public function jsonSerialize() |
|
| 341 | - { |
|
| 342 | - return $this->get(); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * @param ExtendedCacheItemPoolInterface $driverPool |
|
| 347 | - * @return bool |
|
| 348 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 349 | - */ |
|
| 350 | - public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
| 351 | - { |
|
| 352 | - return $driverPool->getClassNamespace() === $this->getClassNamespace(); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * @return array |
|
| 357 | - * @todo Is it still useful ?? |
|
| 358 | - * |
|
| 359 | - * Prevent recursions for Debug (php 5.6+) |
|
| 360 | - */ |
|
| 361 | - final public function __debugInfo() |
|
| 362 | - { |
|
| 363 | - $info = \get_object_vars($this); |
|
| 364 | - $info['driver'] = 'object(' . \get_class($info['driver']) . ')'; |
|
| 365 | - |
|
| 366 | - return $info; |
|
| 367 | - } |
|
| 48 | + /** |
|
| 49 | + * @var ExtendedCacheItemPoolInterface |
|
| 50 | + */ |
|
| 51 | + protected $driver; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $encodedKey; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Item constructor. |
|
| 60 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 61 | + * @param $key |
|
| 62 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 63 | + */ |
|
| 64 | + public function __construct(ExtendedCacheItemPoolInterface $driver, $key) |
|
| 65 | + { |
|
| 66 | + if (\is_string($key)) { |
|
| 67 | + $this->key = $key; |
|
| 68 | + $this->driver = $driver; |
|
| 69 | + if($driver->getConfig()->isUseStaticItemCaching()){ |
|
| 70 | + $this->driver->setItem($this); |
|
| 71 | + } |
|
| 72 | + $this->expirationDate = new DateTime(); |
|
| 73 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 74 | + $this->creationDate = new DateTime(); |
|
| 75 | + $this->modificationDate = new DateTime(); |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + throw new PhpfastcacheInvalidArgumentTypeException('string', $key); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public function getEncodedKey(): string |
|
| 86 | + { |
|
| 87 | + if (!$this->encodedKey) { |
|
| 88 | + $keyHashFunction = $this->driver->getConfig()->getDefaultKeyHashFunction(); |
|
| 89 | + |
|
| 90 | + if ($keyHashFunction) { |
|
| 91 | + $this->encodedKey = $keyHashFunction($this->getKey()); |
|
| 92 | + } else { |
|
| 93 | + $this->encodedKey = $this->getKey(); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return $this->encodedKey; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @return DateTimeInterface |
|
| 102 | + */ |
|
| 103 | + public function getExpirationDate(): DateTimeInterface |
|
| 104 | + { |
|
| 105 | + return $this->expirationDate; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Alias of expireAt() with forced $expiration param |
|
| 110 | + * |
|
| 111 | + * @param DateTimeInterface $expiration |
|
| 112 | + * The point in time after which the item MUST be considered expired. |
|
| 113 | + * If null is passed explicitly, a default value MAY be used. If none is set, |
|
| 114 | + * the value should be stored permanently or for as long as the |
|
| 115 | + * implementation allows. |
|
| 116 | + * |
|
| 117 | + * @return ExtendedCacheItemInterface |
|
| 118 | + * The called object. |
|
| 119 | + */ |
|
| 120 | + public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
| 121 | + { |
|
| 122 | + return $this->expiresAt($expiration); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @return DateTimeInterface |
|
| 127 | + * @throws PhpfastcacheLogicException |
|
| 128 | + */ |
|
| 129 | + public function getCreationDate(): DateTimeInterface |
|
| 130 | + { |
|
| 131 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 132 | + return $this->creationDate; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param DateTimeInterface $date |
|
| 140 | + * @return ExtendedCacheItemInterface |
|
| 141 | + * @throws PhpfastcacheLogicException |
|
| 142 | + */ |
|
| 143 | + public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 144 | + { |
|
| 145 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 146 | + $this->creationDate = $date; |
|
| 147 | + return $this; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @return DateTimeInterface |
|
| 155 | + * @throws PhpfastcacheLogicException |
|
| 156 | + */ |
|
| 157 | + public function getModificationDate(): DateTimeInterface |
|
| 158 | + { |
|
| 159 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 160 | + return $this->modificationDate; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param DateTimeInterface $date |
|
| 168 | + * @return ExtendedCacheItemInterface |
|
| 169 | + * @throws PhpfastcacheLogicException |
|
| 170 | + */ |
|
| 171 | + public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 172 | + { |
|
| 173 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 174 | + $this->modificationDate = $date; |
|
| 175 | + return $this; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @return int |
|
| 183 | + */ |
|
| 184 | + public function getTtl(): int |
|
| 185 | + { |
|
| 186 | + return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @return bool |
|
| 191 | + */ |
|
| 192 | + public function isExpired(): bool |
|
| 193 | + { |
|
| 194 | + return $this->expirationDate->getTimestamp() < (new DateTime())->getTimestamp(); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @return bool |
|
| 199 | + */ |
|
| 200 | + public function isNull(): bool |
|
| 201 | + { |
|
| 202 | + return $this->data === null; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @return bool |
|
| 207 | + */ |
|
| 208 | + public function isEmpty(): bool |
|
| 209 | + { |
|
| 210 | + return empty($this->data); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Return the data length: |
|
| 215 | + * Either the string length if it's a string (binary mode) |
|
| 216 | + * # or the number of element (count) if it's an array |
|
| 217 | + * # or the number returned by count() if it's an object implementing \Countable interface |
|
| 218 | + * # -1 for anything else |
|
| 219 | + * @return int |
|
| 220 | + */ |
|
| 221 | + public function getLength(): int |
|
| 222 | + { |
|
| 223 | + switch (\gettype($this->data)) { |
|
| 224 | + case 'array': |
|
| 225 | + case 'object': |
|
| 226 | + if (\is_array($this->data) || $this->data instanceof Countable) { |
|
| 227 | + return \count($this->data); |
|
| 228 | + } |
|
| 229 | + break; |
|
| 230 | + |
|
| 231 | + case 'string': |
|
| 232 | + return \strlen($this->data); |
|
| 233 | + break; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return -1; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @param int $step |
|
| 242 | + * @return ExtendedCacheItemInterface |
|
| 243 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 244 | + */ |
|
| 245 | + public function increment($step = 1): ExtendedCacheItemInterface |
|
| 246 | + { |
|
| 247 | + if (\is_int($step)) { |
|
| 248 | + $this->fetched = true; |
|
| 249 | + $this->data += $step; |
|
| 250 | + } else { |
|
| 251 | + throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $this; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @param int $step |
|
| 259 | + * @return ExtendedCacheItemInterface |
|
| 260 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 261 | + */ |
|
| 262 | + public function decrement($step = 1): ExtendedCacheItemInterface |
|
| 263 | + { |
|
| 264 | + if (\is_int($step)) { |
|
| 265 | + $this->fetched = true; |
|
| 266 | + $this->data -= $step; |
|
| 267 | + } else { |
|
| 268 | + throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return $this; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @param array|string $data |
|
| 276 | + * @return ExtendedCacheItemInterface |
|
| 277 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 278 | + */ |
|
| 279 | + public function append($data): ExtendedCacheItemInterface |
|
| 280 | + { |
|
| 281 | + if (\is_array($this->data)) { |
|
| 282 | + $this->data[] = $data; |
|
| 283 | + } else { |
|
| 284 | + if (\is_string($data)) { |
|
| 285 | + $this->data .= (string)$data; |
|
| 286 | + } else { |
|
| 287 | + throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + return $this; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * @param array|string $data |
|
| 297 | + * @return ExtendedCacheItemInterface |
|
| 298 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 299 | + */ |
|
| 300 | + public function prepend($data): ExtendedCacheItemInterface |
|
| 301 | + { |
|
| 302 | + if (\is_array($this->data)) { |
|
| 303 | + \array_unshift($this->data, $data); |
|
| 304 | + } else { |
|
| 305 | + if (\is_string($data)) { |
|
| 306 | + $this->data = (string)$data . $this->data; |
|
| 307 | + } else { |
|
| 308 | + throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + return $this; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Return the data as a well-formatted string. |
|
| 317 | + * Any scalar value will be casted to an array |
|
| 318 | + * @param int $option \json_encode() options |
|
| 319 | + * @param int $depth \json_encode() depth |
|
| 320 | + * @return string |
|
| 321 | + */ |
|
| 322 | + public function getDataAsJsonString(int $option = 0, int $depth = 512): string |
|
| 323 | + { |
|
| 324 | + $data = $this->get(); |
|
| 325 | + |
|
| 326 | + if (\is_object($data) || \is_array($data)) { |
|
| 327 | + $data = \json_encode($data, $option, $depth); |
|
| 328 | + } else { |
|
| 329 | + $data = \json_encode([$data], $option, $depth); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return \json_encode($data, $option, $depth); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Implements \JsonSerializable interface |
|
| 337 | + * @return mixed |
|
| 338 | + */ |
|
| 339 | + #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
|
| 340 | + public function jsonSerialize() |
|
| 341 | + { |
|
| 342 | + return $this->get(); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * @param ExtendedCacheItemPoolInterface $driverPool |
|
| 347 | + * @return bool |
|
| 348 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 349 | + */ |
|
| 350 | + public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
| 351 | + { |
|
| 352 | + return $driverPool->getClassNamespace() === $this->getClassNamespace(); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * @return array |
|
| 357 | + * @todo Is it still useful ?? |
|
| 358 | + * |
|
| 359 | + * Prevent recursions for Debug (php 5.6+) |
|
| 360 | + */ |
|
| 361 | + final public function __debugInfo() |
|
| 362 | + { |
|
| 363 | + $info = \get_object_vars($this); |
|
| 364 | + $info['driver'] = 'object(' . \get_class($info['driver']) . ')'; |
|
| 365 | + |
|
| 366 | + return $info; |
|
| 367 | + } |
|
| 368 | 368 | } |
@@ -61,8 +61,7 @@ discard block |
||
| 61 | 61 | * @param $key |
| 62 | 62 | * @throws PhpfastcacheInvalidArgumentException |
| 63 | 63 | */ |
| 64 | - public function __construct(ExtendedCacheItemPoolInterface $driver, $key) |
|
| 65 | - { |
|
| 64 | + public function __construct(ExtendedCacheItemPoolInterface $driver, $key) { |
|
| 66 | 65 | if (\is_string($key)) { |
| 67 | 66 | $this->key = $key; |
| 68 | 67 | $this->driver = $driver; |
@@ -74,7 +73,8 @@ discard block |
||
| 74 | 73 | $this->creationDate = new DateTime(); |
| 75 | 74 | $this->modificationDate = new DateTime(); |
| 76 | 75 | } |
| 77 | - } else { |
|
| 76 | + } |
|
| 77 | + else { |
|
| 78 | 78 | throw new PhpfastcacheInvalidArgumentTypeException('string', $key); |
| 79 | 79 | } |
| 80 | 80 | } |
@@ -89,7 +89,8 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | if ($keyHashFunction) { |
| 91 | 91 | $this->encodedKey = $keyHashFunction($this->getKey()); |
| 92 | - } else { |
|
| 92 | + } |
|
| 93 | + else { |
|
| 93 | 94 | $this->encodedKey = $this->getKey(); |
| 94 | 95 | } |
| 95 | 96 | } |
@@ -247,7 +248,8 @@ discard block |
||
| 247 | 248 | if (\is_int($step)) { |
| 248 | 249 | $this->fetched = true; |
| 249 | 250 | $this->data += $step; |
| 250 | - } else { |
|
| 251 | + } |
|
| 252 | + else { |
|
| 251 | 253 | throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
| 252 | 254 | } |
| 253 | 255 | |
@@ -264,7 +266,8 @@ discard block |
||
| 264 | 266 | if (\is_int($step)) { |
| 265 | 267 | $this->fetched = true; |
| 266 | 268 | $this->data -= $step; |
| 267 | - } else { |
|
| 269 | + } |
|
| 270 | + else { |
|
| 268 | 271 | throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
| 269 | 272 | } |
| 270 | 273 | |
@@ -280,10 +283,12 @@ discard block |
||
| 280 | 283 | { |
| 281 | 284 | if (\is_array($this->data)) { |
| 282 | 285 | $this->data[] = $data; |
| 283 | - } else { |
|
| 286 | + } |
|
| 287 | + else { |
|
| 284 | 288 | if (\is_string($data)) { |
| 285 | 289 | $this->data .= (string)$data; |
| 286 | - } else { |
|
| 290 | + } |
|
| 291 | + else { |
|
| 287 | 292 | throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
| 288 | 293 | } |
| 289 | 294 | } |
@@ -301,10 +306,12 @@ discard block |
||
| 301 | 306 | { |
| 302 | 307 | if (\is_array($this->data)) { |
| 303 | 308 | \array_unshift($this->data, $data); |
| 304 | - } else { |
|
| 309 | + } |
|
| 310 | + else { |
|
| 305 | 311 | if (\is_string($data)) { |
| 306 | 312 | $this->data = (string)$data . $this->data; |
| 307 | - } else { |
|
| 313 | + } |
|
| 314 | + else { |
|
| 308 | 315 | throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
| 309 | 316 | } |
| 310 | 317 | } |
@@ -325,7 +332,8 @@ discard block |
||
| 325 | 332 | |
| 326 | 333 | if (\is_object($data) || \is_array($data)) { |
| 327 | 334 | $data = \json_encode($data, $option, $depth); |
| 328 | - } else { |
|
| 335 | + } |
|
| 336 | + else { |
|
| 329 | 337 | $data = \json_encode([$data], $option, $depth); |
| 330 | 338 | } |
| 331 | 339 | |
@@ -337,8 +345,7 @@ discard block |
||
| 337 | 345 | * @return mixed |
| 338 | 346 | */ |
| 339 | 347 | #[\ReturnTypeWillChange] // PHP 8.1 compatibility |
| 340 | - public function jsonSerialize() |
|
| 341 | - { |
|
| 348 | + public function jsonSerialize() { |
|
| 342 | 349 | return $this->get(); |
| 343 | 350 | } |
| 344 | 351 | |
@@ -358,8 +365,7 @@ discard block |
||
| 358 | 365 | * |
| 359 | 366 | * Prevent recursions for Debug (php 5.6+) |
| 360 | 367 | */ |
| 361 | - final public function __debugInfo() |
|
| 362 | - { |
|
| 368 | + final public function __debugInfo() { |
|
| 363 | 369 | $info = \get_object_vars($this); |
| 364 | 370 | $info['driver'] = 'object(' . \get_class($info['driver']) . ')'; |
| 365 | 371 | |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string |
| 42 | 42 | { |
| 43 | - $callback = static function (CacheItemInterface $item) { |
|
| 43 | + $callback = static function(CacheItemInterface $item) { |
|
| 44 | 44 | return $item->get(); |
| 45 | 45 | }; |
| 46 | 46 | return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | ); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
| 94 | + if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
| 95 | 95 | throw new PhpfastcacheLogicException( |
| 96 | 96 | 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
| 97 | 97 | ); |
@@ -27,129 +27,129 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | trait ExtendedCacheItemPoolTrait |
| 29 | 29 | { |
| 30 | - use CacheItemPoolTrait; |
|
| 31 | - use AbstractDriverPoolTrait; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var DriverIO |
|
| 35 | - */ |
|
| 36 | - protected $IO; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @inheritdoc |
|
| 40 | - */ |
|
| 41 | - public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string |
|
| 42 | - { |
|
| 43 | - $callback = static function (CacheItemInterface $item) { |
|
| 44 | - return $item->get(); |
|
| 45 | - }; |
|
| 46 | - return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @inheritdoc |
|
| 51 | - */ |
|
| 52 | - public function detachAllItems() |
|
| 53 | - { |
|
| 54 | - foreach ($this->itemInstances as $item) { |
|
| 55 | - $this->detachItem($item); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param CacheItemInterface $item |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public function detachItem(CacheItemInterface $item) |
|
| 64 | - { |
|
| 65 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
| 66 | - $this->deregisterItem($item->getKey()); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param string $item |
|
| 72 | - * @internal This method de-register an item from $this->itemInstances |
|
| 73 | - */ |
|
| 74 | - protected function deregisterItem(string $item) |
|
| 75 | - { |
|
| 76 | - unset($this->itemInstances[$item]); |
|
| 77 | - |
|
| 78 | - if (\gc_enabled()) { |
|
| 79 | - \gc_collect_cycles(); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @inheritdoc |
|
| 85 | - */ |
|
| 86 | - public function attachItem(CacheItemInterface $item) |
|
| 87 | - { |
|
| 88 | - if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 89 | - throw new PhpfastcacheLogicException( |
|
| 90 | - 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.' |
|
| 91 | - ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
| 95 | - throw new PhpfastcacheLogicException( |
|
| 96 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
| 97 | - ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $this->itemInstances[$item->getKey()] = $item; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Returns true if the item exists, is attached and the Spl Hash matches |
|
| 105 | - * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
| 106 | - * Returns null if the item does not exists |
|
| 107 | - * |
|
| 108 | - * @param CacheItemInterface $item |
|
| 109 | - * @return bool|null |
|
| 110 | - */ |
|
| 111 | - public function isAttached(CacheItemInterface $item) |
|
| 112 | - { |
|
| 113 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
| 114 | - return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
| 115 | - } |
|
| 116 | - return null; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @inheritdoc |
|
| 121 | - */ |
|
| 122 | - public function saveMultiple(...$items): bool |
|
| 123 | - { |
|
| 124 | - if (isset($items[0]) && \is_array($items[0])) { |
|
| 125 | - foreach ($items[0] as $item) { |
|
| 126 | - $this->save($item); |
|
| 127 | - } |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if (\is_array($items)) { |
|
| 132 | - foreach ($items as $item) { |
|
| 133 | - $this->save($item); |
|
| 134 | - } |
|
| 135 | - return true; |
|
| 136 | - } |
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return DriverIO |
|
| 142 | - */ |
|
| 143 | - public function getIO(): DriverIO |
|
| 144 | - { |
|
| 145 | - return $this->IO; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 151 | - public function getHelp(): string |
|
| 152 | - { |
|
| 153 | - return ''; |
|
| 154 | - } |
|
| 30 | + use CacheItemPoolTrait; |
|
| 31 | + use AbstractDriverPoolTrait; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var DriverIO |
|
| 35 | + */ |
|
| 36 | + protected $IO; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @inheritdoc |
|
| 40 | + */ |
|
| 41 | + public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string |
|
| 42 | + { |
|
| 43 | + $callback = static function (CacheItemInterface $item) { |
|
| 44 | + return $item->get(); |
|
| 45 | + }; |
|
| 46 | + return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @inheritdoc |
|
| 51 | + */ |
|
| 52 | + public function detachAllItems() |
|
| 53 | + { |
|
| 54 | + foreach ($this->itemInstances as $item) { |
|
| 55 | + $this->detachItem($item); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param CacheItemInterface $item |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public function detachItem(CacheItemInterface $item) |
|
| 64 | + { |
|
| 65 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
| 66 | + $this->deregisterItem($item->getKey()); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param string $item |
|
| 72 | + * @internal This method de-register an item from $this->itemInstances |
|
| 73 | + */ |
|
| 74 | + protected function deregisterItem(string $item) |
|
| 75 | + { |
|
| 76 | + unset($this->itemInstances[$item]); |
|
| 77 | + |
|
| 78 | + if (\gc_enabled()) { |
|
| 79 | + \gc_collect_cycles(); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @inheritdoc |
|
| 85 | + */ |
|
| 86 | + public function attachItem(CacheItemInterface $item) |
|
| 87 | + { |
|
| 88 | + if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 89 | + throw new PhpfastcacheLogicException( |
|
| 90 | + 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.' |
|
| 91 | + ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
| 95 | + throw new PhpfastcacheLogicException( |
|
| 96 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $this->itemInstances[$item->getKey()] = $item; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Returns true if the item exists, is attached and the Spl Hash matches |
|
| 105 | + * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
| 106 | + * Returns null if the item does not exists |
|
| 107 | + * |
|
| 108 | + * @param CacheItemInterface $item |
|
| 109 | + * @return bool|null |
|
| 110 | + */ |
|
| 111 | + public function isAttached(CacheItemInterface $item) |
|
| 112 | + { |
|
| 113 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
| 114 | + return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
| 115 | + } |
|
| 116 | + return null; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @inheritdoc |
|
| 121 | + */ |
|
| 122 | + public function saveMultiple(...$items): bool |
|
| 123 | + { |
|
| 124 | + if (isset($items[0]) && \is_array($items[0])) { |
|
| 125 | + foreach ($items[0] as $item) { |
|
| 126 | + $this->save($item); |
|
| 127 | + } |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if (\is_array($items)) { |
|
| 132 | + foreach ($items as $item) { |
|
| 133 | + $this->save($item); |
|
| 134 | + } |
|
| 135 | + return true; |
|
| 136 | + } |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return DriverIO |
|
| 142 | + */ |
|
| 143 | + public function getIO(): DriverIO |
|
| 144 | + { |
|
| 145 | + return $this->IO; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | + public function getHelp(): string |
|
| 152 | + { |
|
| 153 | + return ''; |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -49,8 +49,7 @@ discard block |
||
| 49 | 49 | /** |
| 50 | 50 | * @inheritdoc |
| 51 | 51 | */ |
| 52 | - public function detachAllItems() |
|
| 53 | - { |
|
| 52 | + public function detachAllItems() { |
|
| 54 | 53 | foreach ($this->itemInstances as $item) { |
| 55 | 54 | $this->detachItem($item); |
| 56 | 55 | } |
@@ -60,8 +59,7 @@ discard block |
||
| 60 | 59 | * @param CacheItemInterface $item |
| 61 | 60 | * @return void |
| 62 | 61 | */ |
| 63 | - public function detachItem(CacheItemInterface $item) |
|
| 64 | - { |
|
| 62 | + public function detachItem(CacheItemInterface $item) { |
|
| 65 | 63 | if (isset($this->itemInstances[$item->getKey()])) { |
| 66 | 64 | $this->deregisterItem($item->getKey()); |
| 67 | 65 | } |
@@ -71,8 +69,7 @@ discard block |
||
| 71 | 69 | * @param string $item |
| 72 | 70 | * @internal This method de-register an item from $this->itemInstances |
| 73 | 71 | */ |
| 74 | - protected function deregisterItem(string $item) |
|
| 75 | - { |
|
| 72 | + protected function deregisterItem(string $item) { |
|
| 76 | 73 | unset($this->itemInstances[$item]); |
| 77 | 74 | |
| 78 | 75 | if (\gc_enabled()) { |
@@ -83,8 +80,7 @@ discard block |
||
| 83 | 80 | /** |
| 84 | 81 | * @inheritdoc |
| 85 | 82 | */ |
| 86 | - public function attachItem(CacheItemInterface $item) |
|
| 87 | - { |
|
| 83 | + public function attachItem(CacheItemInterface $item) { |
|
| 88 | 84 | if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
| 89 | 85 | throw new PhpfastcacheLogicException( |
| 90 | 86 | 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.' |
@@ -108,8 +104,7 @@ discard block |
||
| 108 | 104 | * @param CacheItemInterface $item |
| 109 | 105 | * @return bool|null |
| 110 | 106 | */ |
| 111 | - public function isAttached(CacheItemInterface $item) |
|
| 112 | - { |
|
| 107 | + public function isAttached(CacheItemInterface $item) { |
|
| 113 | 108 | if (isset($this->itemInstances[$item->getKey()])) { |
| 114 | 109 | return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
| 115 | 110 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): string |
| 36 | 36 | { |
| 37 | - $callback = static function (CacheItemInterface $item) { |
|
| 37 | + $callback = static function(CacheItemInterface $item) { |
|
| 38 | 38 | return $item->get(); |
| 39 | 39 | }; |
| 40 | 40 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | if (\is_string($tagName)) { |
| 88 | 88 | $driverResponse = $this->getItem($this->getTagKey($tagName)); |
| 89 | 89 | if ($driverResponse->isHit()) { |
| 90 | - $tagsItems = (array)$driverResponse->get(); |
|
| 90 | + $tagsItems = (array) $driverResponse->get(); |
|
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * getItems() may provides expired item(s) |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | return \array_filter( |
| 103 | 103 | $this->getItems(\array_unique(\array_keys($tagsItems))), |
| 104 | - static function (ExtendedCacheItemInterface $item) { |
|
| 104 | + static function(ExtendedCacheItemInterface $item) { |
|
| 105 | 105 | return $item->isHit(); |
| 106 | 106 | } |
| 107 | 107 | ); |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - return (bool)$return; |
|
| 138 | + return (bool) $return; |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | /** |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - return (bool)$return; |
|
| 155 | + return (bool) $return; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - return (bool)$return; |
|
| 190 | + return (bool) $return; |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | /** |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | $this->saveDeferred($item); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - return (bool)$this->commit(); |
|
| 204 | + return (bool) $this->commit(); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - return (bool)$return; |
|
| 223 | + return (bool) $return; |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | $this->saveDeferred($item); |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - return (bool)$this->commit(); |
|
| 237 | + return (bool) $this->commit(); |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - return (bool)$return; |
|
| 256 | + return (bool) $return; |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | $this->saveDeferred($item); |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - return (bool)$this->commit(); |
|
| 270 | + return (bool) $this->commit(); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | - return (bool)$return; |
|
| 289 | + return (bool) $return; |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | /** |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | $this->saveDeferred($item); |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - return (bool)$this->commit(); |
|
| 303 | + return (bool) $this->commit(); |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
@@ -361,7 +361,7 @@ discard block |
||
| 361 | 361 | * that has slow performances |
| 362 | 362 | */ |
| 363 | 363 | |
| 364 | - $tagsItem->set(\array_merge((array)$data, [$item->getKey() => $expTimestamp])) |
|
| 364 | + $tagsItem->set(\array_merge((array) $data, [$item->getKey() => $expTimestamp])) |
|
| 365 | 365 | ->expiresAt($item->getExpirationDate()); |
| 366 | 366 | |
| 367 | 367 | $this->driverWrite($tagsItem); |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
| 376 | 376 | |
| 377 | 377 | foreach ($tagsItems as $tagsItem) { |
| 378 | - $data = (array)$tagsItem->get(); |
|
| 378 | + $data = (array) $tagsItem->get(); |
|
| 379 | 379 | |
| 380 | 380 | unset($data[$item->getKey()]); |
| 381 | 381 | $tagsItem->set($data); |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | protected function getTagKeys(array $keys): array |
| 407 | 407 | { |
| 408 | 408 | return \array_map( |
| 409 | - function (string $key) { |
|
| 409 | + function(string $key) { |
|
| 410 | 410 | return $this->getTagKey($key); |
| 411 | 411 | }, |
| 412 | 412 | $keys |
@@ -29,387 +29,387 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | trait TaggableCacheItemPoolTrait |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * @inheritdoc |
|
| 34 | - */ |
|
| 35 | - public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): string |
|
| 36 | - { |
|
| 37 | - $callback = static function (CacheItemInterface $item) { |
|
| 38 | - return $item->get(); |
|
| 39 | - }; |
|
| 40 | - |
|
| 41 | - return \json_encode(\array_map($callback, \array_values($this->getItemsByTags($tagNames, $strategy))), $option, $depth); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @inheritdoc |
|
| 46 | - */ |
|
| 47 | - public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 48 | - { |
|
| 49 | - $items = []; |
|
| 50 | - foreach (\array_unique($tagNames) as $tagName) { |
|
| 51 | - if (\is_string($tagName)) { |
|
| 52 | - $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
| 53 | - } else { |
|
| 54 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $items = \array_merge([], ...$items); |
|
| 59 | - |
|
| 60 | - switch ($strategy) { |
|
| 61 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
| 62 | - foreach ($items as $key => $item) { |
|
| 63 | - if (\array_diff($tagNames, $item->getTags())) { |
|
| 64 | - unset($items[$key]); |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - break; |
|
| 68 | - |
|
| 69 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
| 70 | - foreach ($items as $key => $item) { |
|
| 71 | - if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
| 72 | - unset($items[$key]); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - break; |
|
| 76 | - } |
|
| 77 | - return $items; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param string $tagName |
|
| 82 | - * @return array |
|
| 83 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 84 | - */ |
|
| 85 | - protected function fetchItemsByTagFromBackend(string $tagName): array |
|
| 86 | - { |
|
| 87 | - if (\is_string($tagName)) { |
|
| 88 | - $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
| 89 | - if ($driverResponse->isHit()) { |
|
| 90 | - $tagsItems = (array)$driverResponse->get(); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * getItems() may provides expired item(s) |
|
| 94 | - * themselves provided by a cache of item |
|
| 95 | - * keys based stored the tag item. |
|
| 96 | - * Therefore we pass a filter callback |
|
| 97 | - * to remove the expired Item(s) provided by |
|
| 98 | - * the item keys passed through getItems() |
|
| 99 | - * |
|
| 100 | - * #headache |
|
| 101 | - */ |
|
| 102 | - return \array_filter( |
|
| 103 | - $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
| 104 | - static function (ExtendedCacheItemInterface $item) { |
|
| 105 | - return $item->isHit(); |
|
| 106 | - } |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - return []; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @param string $key |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - protected function getTagKey(string $key): string |
|
| 120 | - { |
|
| 121 | - return self::DRIVER_TAGS_KEY_PREFIX . $key; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @inheritdoc |
|
| 126 | - */ |
|
| 127 | - public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 128 | - { |
|
| 129 | - $return = null; |
|
| 130 | - |
|
| 131 | - foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
| 132 | - $result = $this->deleteItem($item->getKey()); |
|
| 133 | - if ($return !== false) { |
|
| 134 | - $return = $result; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - return (bool)$return; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @inheritdoc |
|
| 143 | - */ |
|
| 144 | - public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 145 | - { |
|
| 146 | - if (\is_string($tagName)) { |
|
| 147 | - $return = null; |
|
| 148 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 149 | - $result = $this->deleteItem($item->getKey()); |
|
| 150 | - if ($return !== false) { |
|
| 151 | - $return = $result; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return (bool)$return; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @inheritdoc |
|
| 163 | - */ |
|
| 164 | - public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 165 | - { |
|
| 166 | - $items = $this->fetchItemsByTagFromBackend($tagName); |
|
| 167 | - if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
| 168 | - foreach ($items as $key => $item) { |
|
| 169 | - if (\array_diff($item->getTags(), $tagName)) { |
|
| 170 | - unset($items[$key]); |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - return $items; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @inheritdoc |
|
| 179 | - */ |
|
| 180 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 181 | - { |
|
| 182 | - $return = null; |
|
| 183 | - foreach ($tagNames as $tagName) { |
|
| 184 | - $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
| 185 | - if ($return !== false) { |
|
| 186 | - $return = $result; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return (bool)$return; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @inheritdoc |
|
| 195 | - */ |
|
| 196 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 197 | - { |
|
| 198 | - if (\is_string($tagName) && \is_int($step)) { |
|
| 199 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 200 | - $item->increment($step); |
|
| 201 | - $this->saveDeferred($item); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return (bool)$this->commit(); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * @inheritdoc |
|
| 212 | - */ |
|
| 213 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 214 | - { |
|
| 215 | - $return = null; |
|
| 216 | - foreach ($tagNames as $tagName) { |
|
| 217 | - $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
| 218 | - if ($return !== false) { |
|
| 219 | - $return = $result; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return (bool)$return; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @inheritdoc |
|
| 228 | - */ |
|
| 229 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 230 | - { |
|
| 231 | - if (\is_string($tagName) && \is_int($step)) { |
|
| 232 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 233 | - $item->decrement($step); |
|
| 234 | - $this->saveDeferred($item); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return (bool)$this->commit(); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @inheritdoc |
|
| 245 | - */ |
|
| 246 | - public function appendItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 247 | - { |
|
| 248 | - $return = null; |
|
| 249 | - foreach ($tagNames as $tagName) { |
|
| 250 | - $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
| 251 | - if ($return !== false) { |
|
| 252 | - $return = $result; |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - return (bool)$return; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * @inheritdoc |
|
| 261 | - */ |
|
| 262 | - public function appendItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 263 | - { |
|
| 264 | - if (\is_string($tagName)) { |
|
| 265 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 266 | - $item->append($data); |
|
| 267 | - $this->saveDeferred($item); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - return (bool)$this->commit(); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * @inheritdoc |
|
| 278 | - */ |
|
| 279 | - public function prependItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 280 | - { |
|
| 281 | - $return = null; |
|
| 282 | - foreach ($tagNames as $tagName) { |
|
| 283 | - $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
| 284 | - if ($return !== false) { |
|
| 285 | - $return = $result; |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return (bool)$return; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * @inheritdoc |
|
| 294 | - */ |
|
| 295 | - public function prependItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 296 | - { |
|
| 297 | - if (\is_string($tagName)) { |
|
| 298 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 299 | - $item->prepend($data); |
|
| 300 | - $this->saveDeferred($item); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - return (bool)$this->commit(); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * @param array $wrapper |
|
| 311 | - * @return mixed |
|
| 312 | - */ |
|
| 313 | - protected function driverUnwrapTags(array $wrapper) |
|
| 314 | - { |
|
| 315 | - return $wrapper[self::DRIVER_TAGS_WRAPPER_INDEX]; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param ExtendedCacheItemInterface $item |
|
| 320 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 321 | - * @throws PhpfastcacheLogicException |
|
| 322 | - */ |
|
| 323 | - protected function cleanItemTags(ExtendedCacheItemInterface $item) |
|
| 324 | - { |
|
| 325 | - $this->driverWriteTags($item->removeTags($item->getTags())); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * @param ExtendedCacheItemInterface $item |
|
| 330 | - * @return bool |
|
| 331 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 332 | - * @throws PhpfastcacheLogicException |
|
| 333 | - */ |
|
| 334 | - protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
| 335 | - { |
|
| 336 | - /** |
|
| 337 | - * Do not attempt to write tags |
|
| 338 | - * on tags item, it can leads |
|
| 339 | - * to an infinite recursive calls |
|
| 340 | - */ |
|
| 341 | - if (\strpos($item->getKey(), self::DRIVER_TAGS_KEY_PREFIX) === 0) { |
|
| 342 | - throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if (!$item->getTags() && !$item->getRemovedTags()) { |
|
| 346 | - return true; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * @var $tagsItems ExtendedCacheItemInterface[] |
|
| 351 | - */ |
|
| 352 | - $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
| 353 | - |
|
| 354 | - foreach ($tagsItems as $tagsItem) { |
|
| 355 | - $data = $tagsItem->get(); |
|
| 356 | - $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Using the key will |
|
| 360 | - * avoid to use array_unique |
|
| 361 | - * that has slow performances |
|
| 362 | - */ |
|
| 363 | - |
|
| 364 | - $tagsItem->set(\array_merge((array)$data, [$item->getKey() => $expTimestamp])) |
|
| 365 | - ->expiresAt($item->getExpirationDate()); |
|
| 366 | - |
|
| 367 | - $this->driverWrite($tagsItem); |
|
| 368 | - $tagsItem->setHit(true); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Also update removed tags to |
|
| 373 | - * keep the index up to date |
|
| 374 | - */ |
|
| 375 | - $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
| 376 | - |
|
| 377 | - foreach ($tagsItems as $tagsItem) { |
|
| 378 | - $data = (array)$tagsItem->get(); |
|
| 379 | - |
|
| 380 | - unset($data[$item->getKey()]); |
|
| 381 | - $tagsItem->set($data); |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Recalculate the expiration date |
|
| 385 | - * |
|
| 386 | - * If the $tagsItem does not have |
|
| 387 | - * any cache item references left |
|
| 388 | - * then remove it from tagsItems index |
|
| 389 | - */ |
|
| 390 | - if (\count($data)) { |
|
| 391 | - $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 392 | - $this->driverWrite($tagsItem); |
|
| 393 | - $tagsItem->setHit(true); |
|
| 394 | - } else { |
|
| 395 | - $this->deleteItem($tagsItem->getKey()); |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - return true; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * @param array $keys |
|
| 404 | - * @return array |
|
| 405 | - */ |
|
| 406 | - protected function getTagKeys(array $keys): array |
|
| 407 | - { |
|
| 408 | - return \array_map( |
|
| 409 | - function (string $key) { |
|
| 410 | - return $this->getTagKey($key); |
|
| 411 | - }, |
|
| 412 | - $keys |
|
| 413 | - ); |
|
| 414 | - } |
|
| 32 | + /** |
|
| 33 | + * @inheritdoc |
|
| 34 | + */ |
|
| 35 | + public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): string |
|
| 36 | + { |
|
| 37 | + $callback = static function (CacheItemInterface $item) { |
|
| 38 | + return $item->get(); |
|
| 39 | + }; |
|
| 40 | + |
|
| 41 | + return \json_encode(\array_map($callback, \array_values($this->getItemsByTags($tagNames, $strategy))), $option, $depth); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @inheritdoc |
|
| 46 | + */ |
|
| 47 | + public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 48 | + { |
|
| 49 | + $items = []; |
|
| 50 | + foreach (\array_unique($tagNames) as $tagName) { |
|
| 51 | + if (\is_string($tagName)) { |
|
| 52 | + $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
| 53 | + } else { |
|
| 54 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $items = \array_merge([], ...$items); |
|
| 59 | + |
|
| 60 | + switch ($strategy) { |
|
| 61 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
| 62 | + foreach ($items as $key => $item) { |
|
| 63 | + if (\array_diff($tagNames, $item->getTags())) { |
|
| 64 | + unset($items[$key]); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + break; |
|
| 68 | + |
|
| 69 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
| 70 | + foreach ($items as $key => $item) { |
|
| 71 | + if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
| 72 | + unset($items[$key]); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + break; |
|
| 76 | + } |
|
| 77 | + return $items; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param string $tagName |
|
| 82 | + * @return array |
|
| 83 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 84 | + */ |
|
| 85 | + protected function fetchItemsByTagFromBackend(string $tagName): array |
|
| 86 | + { |
|
| 87 | + if (\is_string($tagName)) { |
|
| 88 | + $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
| 89 | + if ($driverResponse->isHit()) { |
|
| 90 | + $tagsItems = (array)$driverResponse->get(); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * getItems() may provides expired item(s) |
|
| 94 | + * themselves provided by a cache of item |
|
| 95 | + * keys based stored the tag item. |
|
| 96 | + * Therefore we pass a filter callback |
|
| 97 | + * to remove the expired Item(s) provided by |
|
| 98 | + * the item keys passed through getItems() |
|
| 99 | + * |
|
| 100 | + * #headache |
|
| 101 | + */ |
|
| 102 | + return \array_filter( |
|
| 103 | + $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
| 104 | + static function (ExtendedCacheItemInterface $item) { |
|
| 105 | + return $item->isHit(); |
|
| 106 | + } |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + return []; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @param string $key |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + protected function getTagKey(string $key): string |
|
| 120 | + { |
|
| 121 | + return self::DRIVER_TAGS_KEY_PREFIX . $key; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @inheritdoc |
|
| 126 | + */ |
|
| 127 | + public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 128 | + { |
|
| 129 | + $return = null; |
|
| 130 | + |
|
| 131 | + foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
| 132 | + $result = $this->deleteItem($item->getKey()); |
|
| 133 | + if ($return !== false) { |
|
| 134 | + $return = $result; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + return (bool)$return; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @inheritdoc |
|
| 143 | + */ |
|
| 144 | + public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 145 | + { |
|
| 146 | + if (\is_string($tagName)) { |
|
| 147 | + $return = null; |
|
| 148 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 149 | + $result = $this->deleteItem($item->getKey()); |
|
| 150 | + if ($return !== false) { |
|
| 151 | + $return = $result; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return (bool)$return; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @inheritdoc |
|
| 163 | + */ |
|
| 164 | + public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 165 | + { |
|
| 166 | + $items = $this->fetchItemsByTagFromBackend($tagName); |
|
| 167 | + if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
| 168 | + foreach ($items as $key => $item) { |
|
| 169 | + if (\array_diff($item->getTags(), $tagName)) { |
|
| 170 | + unset($items[$key]); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + return $items; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @inheritdoc |
|
| 179 | + */ |
|
| 180 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 181 | + { |
|
| 182 | + $return = null; |
|
| 183 | + foreach ($tagNames as $tagName) { |
|
| 184 | + $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
| 185 | + if ($return !== false) { |
|
| 186 | + $return = $result; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return (bool)$return; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @inheritdoc |
|
| 195 | + */ |
|
| 196 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 197 | + { |
|
| 198 | + if (\is_string($tagName) && \is_int($step)) { |
|
| 199 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 200 | + $item->increment($step); |
|
| 201 | + $this->saveDeferred($item); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return (bool)$this->commit(); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * @inheritdoc |
|
| 212 | + */ |
|
| 213 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 214 | + { |
|
| 215 | + $return = null; |
|
| 216 | + foreach ($tagNames as $tagName) { |
|
| 217 | + $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
| 218 | + if ($return !== false) { |
|
| 219 | + $return = $result; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return (bool)$return; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @inheritdoc |
|
| 228 | + */ |
|
| 229 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 230 | + { |
|
| 231 | + if (\is_string($tagName) && \is_int($step)) { |
|
| 232 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 233 | + $item->decrement($step); |
|
| 234 | + $this->saveDeferred($item); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return (bool)$this->commit(); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @inheritdoc |
|
| 245 | + */ |
|
| 246 | + public function appendItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 247 | + { |
|
| 248 | + $return = null; |
|
| 249 | + foreach ($tagNames as $tagName) { |
|
| 250 | + $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
| 251 | + if ($return !== false) { |
|
| 252 | + $return = $result; |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + return (bool)$return; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * @inheritdoc |
|
| 261 | + */ |
|
| 262 | + public function appendItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 263 | + { |
|
| 264 | + if (\is_string($tagName)) { |
|
| 265 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 266 | + $item->append($data); |
|
| 267 | + $this->saveDeferred($item); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + return (bool)$this->commit(); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * @inheritdoc |
|
| 278 | + */ |
|
| 279 | + public function prependItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 280 | + { |
|
| 281 | + $return = null; |
|
| 282 | + foreach ($tagNames as $tagName) { |
|
| 283 | + $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
| 284 | + if ($return !== false) { |
|
| 285 | + $return = $result; |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + return (bool)$return; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * @inheritdoc |
|
| 294 | + */ |
|
| 295 | + public function prependItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 296 | + { |
|
| 297 | + if (\is_string($tagName)) { |
|
| 298 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 299 | + $item->prepend($data); |
|
| 300 | + $this->saveDeferred($item); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + return (bool)$this->commit(); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * @param array $wrapper |
|
| 311 | + * @return mixed |
|
| 312 | + */ |
|
| 313 | + protected function driverUnwrapTags(array $wrapper) |
|
| 314 | + { |
|
| 315 | + return $wrapper[self::DRIVER_TAGS_WRAPPER_INDEX]; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param ExtendedCacheItemInterface $item |
|
| 320 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 321 | + * @throws PhpfastcacheLogicException |
|
| 322 | + */ |
|
| 323 | + protected function cleanItemTags(ExtendedCacheItemInterface $item) |
|
| 324 | + { |
|
| 325 | + $this->driverWriteTags($item->removeTags($item->getTags())); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * @param ExtendedCacheItemInterface $item |
|
| 330 | + * @return bool |
|
| 331 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 332 | + * @throws PhpfastcacheLogicException |
|
| 333 | + */ |
|
| 334 | + protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
| 335 | + { |
|
| 336 | + /** |
|
| 337 | + * Do not attempt to write tags |
|
| 338 | + * on tags item, it can leads |
|
| 339 | + * to an infinite recursive calls |
|
| 340 | + */ |
|
| 341 | + if (\strpos($item->getKey(), self::DRIVER_TAGS_KEY_PREFIX) === 0) { |
|
| 342 | + throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if (!$item->getTags() && !$item->getRemovedTags()) { |
|
| 346 | + return true; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * @var $tagsItems ExtendedCacheItemInterface[] |
|
| 351 | + */ |
|
| 352 | + $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
| 353 | + |
|
| 354 | + foreach ($tagsItems as $tagsItem) { |
|
| 355 | + $data = $tagsItem->get(); |
|
| 356 | + $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Using the key will |
|
| 360 | + * avoid to use array_unique |
|
| 361 | + * that has slow performances |
|
| 362 | + */ |
|
| 363 | + |
|
| 364 | + $tagsItem->set(\array_merge((array)$data, [$item->getKey() => $expTimestamp])) |
|
| 365 | + ->expiresAt($item->getExpirationDate()); |
|
| 366 | + |
|
| 367 | + $this->driverWrite($tagsItem); |
|
| 368 | + $tagsItem->setHit(true); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Also update removed tags to |
|
| 373 | + * keep the index up to date |
|
| 374 | + */ |
|
| 375 | + $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
| 376 | + |
|
| 377 | + foreach ($tagsItems as $tagsItem) { |
|
| 378 | + $data = (array)$tagsItem->get(); |
|
| 379 | + |
|
| 380 | + unset($data[$item->getKey()]); |
|
| 381 | + $tagsItem->set($data); |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Recalculate the expiration date |
|
| 385 | + * |
|
| 386 | + * If the $tagsItem does not have |
|
| 387 | + * any cache item references left |
|
| 388 | + * then remove it from tagsItems index |
|
| 389 | + */ |
|
| 390 | + if (\count($data)) { |
|
| 391 | + $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 392 | + $this->driverWrite($tagsItem); |
|
| 393 | + $tagsItem->setHit(true); |
|
| 394 | + } else { |
|
| 395 | + $this->deleteItem($tagsItem->getKey()); |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + return true; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * @param array $keys |
|
| 404 | + * @return array |
|
| 405 | + */ |
|
| 406 | + protected function getTagKeys(array $keys): array |
|
| 407 | + { |
|
| 408 | + return \array_map( |
|
| 409 | + function (string $key) { |
|
| 410 | + return $this->getTagKey($key); |
|
| 411 | + }, |
|
| 412 | + $keys |
|
| 413 | + ); |
|
| 414 | + } |
|
| 415 | 415 | } |
@@ -50,7 +50,8 @@ discard block |
||
| 50 | 50 | foreach (\array_unique($tagNames) as $tagName) { |
| 51 | 51 | if (\is_string($tagName)) { |
| 52 | 52 | $items[] = $this->fetchItemsByTagFromBackend($tagName); |
| 53 | - } else { |
|
| 53 | + } |
|
| 54 | + else { |
|
| 54 | 55 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); |
| 55 | 56 | } |
| 56 | 57 | } |
@@ -310,8 +311,7 @@ discard block |
||
| 310 | 311 | * @param array $wrapper |
| 311 | 312 | * @return mixed |
| 312 | 313 | */ |
| 313 | - protected function driverUnwrapTags(array $wrapper) |
|
| 314 | - { |
|
| 314 | + protected function driverUnwrapTags(array $wrapper) { |
|
| 315 | 315 | return $wrapper[self::DRIVER_TAGS_WRAPPER_INDEX]; |
| 316 | 316 | } |
| 317 | 317 | |
@@ -320,8 +320,7 @@ discard block |
||
| 320 | 320 | * @throws PhpfastcacheInvalidArgumentException |
| 321 | 321 | * @throws PhpfastcacheLogicException |
| 322 | 322 | */ |
| 323 | - protected function cleanItemTags(ExtendedCacheItemInterface $item) |
|
| 324 | - { |
|
| 323 | + protected function cleanItemTags(ExtendedCacheItemInterface $item) { |
|
| 325 | 324 | $this->driverWriteTags($item->removeTags($item->getTags())); |
| 326 | 325 | } |
| 327 | 326 | |
@@ -391,7 +390,8 @@ discard block |
||
| 391 | 390 | $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
| 392 | 391 | $this->driverWrite($tagsItem); |
| 393 | 392 | $tagsItem->setHit(true); |
| 394 | - } else { |
|
| 393 | + } |
|
| 394 | + else { |
|
| 395 | 395 | $this->deleteItem($tagsItem->getKey()); |
| 396 | 396 | } |
| 397 | 397 | } |
@@ -64,9 +64,9 @@ discard block |
||
| 64 | 64 | ->setSize(Directory::dirSize($path)) |
| 65 | 65 | ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)); |
| 66 | 66 | |
| 67 | - if($this->getConfig()->isUseStaticItemCaching()){ |
|
| 67 | + if ($this->getConfig()->isUseStaticItemCaching()) { |
|
| 68 | 68 | $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
| 69 | - }else{ |
|
| 69 | + } else { |
|
| 70 | 70 | $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
| 71 | 71 | } |
| 72 | 72 | |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | throw new PhpfastcacheIOException("Cannot read file located at: {$file}"); |
| 295 | 295 | } |
| 296 | 296 | if (\function_exists('file_get_contents')) { |
| 297 | - return (string)\file_get_contents($file); |
|
| 297 | + return (string) \file_get_contents($file); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | $string = ''; |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
| 339 | 339 | \bin2hex(\random_bytes(16)) |
| 340 | 340 | ) |
| 341 | - ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
| 341 | + ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
| 342 | 342 | |
| 343 | 343 | $handle = \fopen($tmpFilename, 'w+b'); |
| 344 | 344 | if (\is_resource($handle)) { |
@@ -359,6 +359,6 @@ discard block |
||
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | - return (bool)($octetWritten ?? false); |
|
| 362 | + return (bool) ($octetWritten ?? false); |
|
| 363 | 363 | } |
| 364 | 364 | } |
@@ -37,228 +37,228 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | trait IOHelperTrait |
| 39 | 39 | { |
| 40 | - /** |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - public $tmp = []; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Provide a generic getStats() method |
|
| 47 | - * for files-based drivers |
|
| 48 | - * @return DriverStatistic |
|
| 49 | - * @throws PhpfastcacheIOException |
|
| 50 | - */ |
|
| 51 | - public function getStats(): DriverStatistic |
|
| 52 | - { |
|
| 53 | - $stat = new DriverStatistic(); |
|
| 54 | - $path = $this->getFilePath(false); |
|
| 55 | - |
|
| 56 | - if (!is_dir($path)) { |
|
| 57 | - throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
|
| 58 | - } |
|
| 59 | - $stat->setRawData( |
|
| 60 | - [ |
|
| 61 | - 'tmp' => $this->tmp, |
|
| 62 | - ] |
|
| 63 | - ) |
|
| 64 | - ->setSize(Directory::dirSize($path)) |
|
| 65 | - ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)); |
|
| 66 | - |
|
| 67 | - if($this->getConfig()->isUseStaticItemCaching()){ |
|
| 68 | - $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
|
| 69 | - }else{ |
|
| 70 | - $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return $stat; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param $keyword |
|
| 78 | - * @param bool $skip |
|
| 79 | - * @return string |
|
| 80 | - * @throws PhpfastcacheIOException |
|
| 81 | - */ |
|
| 82 | - protected function getFilePath($keyword, $skip = false): string |
|
| 83 | - { |
|
| 84 | - $path = $this->getPath(); |
|
| 85 | - |
|
| 86 | - if ($keyword === false) { |
|
| 87 | - return $path; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $filename = $this->encodeFilename($keyword); |
|
| 91 | - $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
|
| 92 | - $path = \rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Skip Create Sub Folders; |
|
| 96 | - */ |
|
| 97 | - if (!$skip && !\is_dir($path) && @!\mkdir($path, $this->getDefaultChmod(), true) && !\is_dir($path)) { |
|
| 98 | - throw new PhpfastcacheIOException( |
|
| 99 | - 'Path "' . $path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 100 | - ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param bool $readonly |
|
| 108 | - * @return string |
|
| 109 | - * @throws PhpfastcacheIOException |
|
| 110 | - */ |
|
| 111 | - public function getPath($readonly = false): string |
|
| 112 | - { |
|
| 113 | - /** |
|
| 114 | - * Get the base system temporary directory |
|
| 115 | - */ |
|
| 116 | - $tmp_dir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Calculate the security key |
|
| 120 | - */ |
|
| 121 | - { |
|
| 122 | - $securityKey = $this->getConfig()->getSecurityKey(); |
|
| 123 | - if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
|
| 124 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
| 125 | - $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
|
| 126 | - } else { |
|
| 127 | - $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if ($securityKey !== '') { |
|
| 132 | - $securityKey .= '/'; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $securityKey = static::cleanFileName($securityKey); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Extends the temporary directory |
|
| 140 | - * with the security key and the driver name |
|
| 141 | - */ |
|
| 142 | - $tmp_dir = \rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
|
| 143 | - |
|
| 144 | - if (empty($this->getConfig()->getPath())) { |
|
| 145 | - $path = $tmp_dir; |
|
| 146 | - } else { |
|
| 147 | - $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
|
| 151 | - $full_path = Directory::getAbsolutePath($path . $path_suffix); |
|
| 152 | - $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
|
| 153 | - $full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path); |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * In readonly mode we only attempt |
|
| 157 | - * to verify if the directory exists |
|
| 158 | - * or not, if it does not then we |
|
| 159 | - * return the temp dir |
|
| 160 | - */ |
|
| 161 | - if ($readonly === true) { |
|
| 162 | - if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
| 163 | - return $full_path_tmp; |
|
| 164 | - } |
|
| 165 | - return $full_path; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - if (!isset($this->tmp[$full_path_hash]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
| 169 | - if (!@\file_exists($full_path)) { |
|
| 170 | - if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path)) { |
|
| 171 | - throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
| 172 | - } |
|
| 173 | - } else { |
|
| 174 | - if (!@\is_writable($full_path)) { |
|
| 175 | - if (!@\chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
| 176 | - /** |
|
| 177 | - * Switch back to tmp dir |
|
| 178 | - * again if the path is not writable |
|
| 179 | - */ |
|
| 180 | - $full_path = $full_path_tmp; |
|
| 181 | - if (!@\file_exists($full_path)) { |
|
| 182 | - if (@\mkdir($full_path, $this->getDefaultChmod(), true) && !\is_dir($full_path)) { |
|
| 183 | - throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * In case there is no directory |
|
| 192 | - * writable including the temporary |
|
| 193 | - * one, we must throw an exception |
|
| 194 | - */ |
|
| 195 | - if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
|
| 196 | - throw new PhpfastcacheIOException( |
|
| 197 | - 'Path "' . $full_path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $this->tmp[$full_path_hash] = $full_path; |
|
| 202 | - $this->htaccessGen($full_path, $this->getConfig()->isValidOption('htaccess') ? $this->getConfig()->getHtaccess() : false); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return realpath($full_path); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @param $filename |
|
| 210 | - * @return string |
|
| 211 | - */ |
|
| 212 | - protected static function cleanFileName($filename): string |
|
| 213 | - { |
|
| 214 | - $regex = [ |
|
| 215 | - '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
| 216 | - '/\.$/', |
|
| 217 | - '/^\./', |
|
| 218 | - ]; |
|
| 219 | - $replace = ['-', '', '']; |
|
| 220 | - |
|
| 221 | - return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @return int |
|
| 226 | - */ |
|
| 227 | - protected function getDefaultChmod(): int |
|
| 228 | - { |
|
| 229 | - if (!$this->getConfig()->getDefaultChmod()) { |
|
| 230 | - return 0777; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return $this->getConfig()->getDefaultChmod(); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @param $path |
|
| 238 | - * @param bool $create |
|
| 239 | - * @throws PhpfastcacheIOException |
|
| 240 | - */ |
|
| 241 | - protected function htaccessGen($path, $create = true) |
|
| 242 | - { |
|
| 243 | - if ($create === true) { |
|
| 244 | - if (!\is_writable($path)) { |
|
| 245 | - try { |
|
| 246 | - if (!\chmod($path, 0777)) { |
|
| 247 | - throw new PhpfastcacheIOException('Chmod failed on : ' . $path); |
|
| 248 | - } |
|
| 249 | - } catch (PhpfastcacheIOException $e) { |
|
| 250 | - throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if (!\file_exists($path . '/.htaccess')) { |
|
| 255 | - $file = @\fopen($path . '/.htaccess', 'w+b'); |
|
| 256 | - if (!$file) { |
|
| 257 | - throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
|
| 258 | - } |
|
| 259 | - \fwrite( |
|
| 260 | - $file, |
|
| 261 | - <<<HTACCESS |
|
| 40 | + /** |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + public $tmp = []; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Provide a generic getStats() method |
|
| 47 | + * for files-based drivers |
|
| 48 | + * @return DriverStatistic |
|
| 49 | + * @throws PhpfastcacheIOException |
|
| 50 | + */ |
|
| 51 | + public function getStats(): DriverStatistic |
|
| 52 | + { |
|
| 53 | + $stat = new DriverStatistic(); |
|
| 54 | + $path = $this->getFilePath(false); |
|
| 55 | + |
|
| 56 | + if (!is_dir($path)) { |
|
| 57 | + throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
|
| 58 | + } |
|
| 59 | + $stat->setRawData( |
|
| 60 | + [ |
|
| 61 | + 'tmp' => $this->tmp, |
|
| 62 | + ] |
|
| 63 | + ) |
|
| 64 | + ->setSize(Directory::dirSize($path)) |
|
| 65 | + ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)); |
|
| 66 | + |
|
| 67 | + if($this->getConfig()->isUseStaticItemCaching()){ |
|
| 68 | + $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
|
| 69 | + }else{ |
|
| 70 | + $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return $stat; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param $keyword |
|
| 78 | + * @param bool $skip |
|
| 79 | + * @return string |
|
| 80 | + * @throws PhpfastcacheIOException |
|
| 81 | + */ |
|
| 82 | + protected function getFilePath($keyword, $skip = false): string |
|
| 83 | + { |
|
| 84 | + $path = $this->getPath(); |
|
| 85 | + |
|
| 86 | + if ($keyword === false) { |
|
| 87 | + return $path; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $filename = $this->encodeFilename($keyword); |
|
| 91 | + $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
|
| 92 | + $path = \rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Skip Create Sub Folders; |
|
| 96 | + */ |
|
| 97 | + if (!$skip && !\is_dir($path) && @!\mkdir($path, $this->getDefaultChmod(), true) && !\is_dir($path)) { |
|
| 98 | + throw new PhpfastcacheIOException( |
|
| 99 | + 'Path "' . $path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param bool $readonly |
|
| 108 | + * @return string |
|
| 109 | + * @throws PhpfastcacheIOException |
|
| 110 | + */ |
|
| 111 | + public function getPath($readonly = false): string |
|
| 112 | + { |
|
| 113 | + /** |
|
| 114 | + * Get the base system temporary directory |
|
| 115 | + */ |
|
| 116 | + $tmp_dir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Calculate the security key |
|
| 120 | + */ |
|
| 121 | + { |
|
| 122 | + $securityKey = $this->getConfig()->getSecurityKey(); |
|
| 123 | + if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
|
| 124 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
| 125 | + $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
|
| 126 | + } else { |
|
| 127 | + $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if ($securityKey !== '') { |
|
| 132 | + $securityKey .= '/'; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $securityKey = static::cleanFileName($securityKey); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Extends the temporary directory |
|
| 140 | + * with the security key and the driver name |
|
| 141 | + */ |
|
| 142 | + $tmp_dir = \rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
|
| 143 | + |
|
| 144 | + if (empty($this->getConfig()->getPath())) { |
|
| 145 | + $path = $tmp_dir; |
|
| 146 | + } else { |
|
| 147 | + $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
|
| 151 | + $full_path = Directory::getAbsolutePath($path . $path_suffix); |
|
| 152 | + $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
|
| 153 | + $full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path); |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * In readonly mode we only attempt |
|
| 157 | + * to verify if the directory exists |
|
| 158 | + * or not, if it does not then we |
|
| 159 | + * return the temp dir |
|
| 160 | + */ |
|
| 161 | + if ($readonly === true) { |
|
| 162 | + if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
| 163 | + return $full_path_tmp; |
|
| 164 | + } |
|
| 165 | + return $full_path; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + if (!isset($this->tmp[$full_path_hash]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
| 169 | + if (!@\file_exists($full_path)) { |
|
| 170 | + if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path)) { |
|
| 171 | + throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
| 172 | + } |
|
| 173 | + } else { |
|
| 174 | + if (!@\is_writable($full_path)) { |
|
| 175 | + if (!@\chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
| 176 | + /** |
|
| 177 | + * Switch back to tmp dir |
|
| 178 | + * again if the path is not writable |
|
| 179 | + */ |
|
| 180 | + $full_path = $full_path_tmp; |
|
| 181 | + if (!@\file_exists($full_path)) { |
|
| 182 | + if (@\mkdir($full_path, $this->getDefaultChmod(), true) && !\is_dir($full_path)) { |
|
| 183 | + throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * In case there is no directory |
|
| 192 | + * writable including the temporary |
|
| 193 | + * one, we must throw an exception |
|
| 194 | + */ |
|
| 195 | + if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
|
| 196 | + throw new PhpfastcacheIOException( |
|
| 197 | + 'Path "' . $full_path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $this->tmp[$full_path_hash] = $full_path; |
|
| 202 | + $this->htaccessGen($full_path, $this->getConfig()->isValidOption('htaccess') ? $this->getConfig()->getHtaccess() : false); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return realpath($full_path); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @param $filename |
|
| 210 | + * @return string |
|
| 211 | + */ |
|
| 212 | + protected static function cleanFileName($filename): string |
|
| 213 | + { |
|
| 214 | + $regex = [ |
|
| 215 | + '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
| 216 | + '/\.$/', |
|
| 217 | + '/^\./', |
|
| 218 | + ]; |
|
| 219 | + $replace = ['-', '', '']; |
|
| 220 | + |
|
| 221 | + return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @return int |
|
| 226 | + */ |
|
| 227 | + protected function getDefaultChmod(): int |
|
| 228 | + { |
|
| 229 | + if (!$this->getConfig()->getDefaultChmod()) { |
|
| 230 | + return 0777; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return $this->getConfig()->getDefaultChmod(); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @param $path |
|
| 238 | + * @param bool $create |
|
| 239 | + * @throws PhpfastcacheIOException |
|
| 240 | + */ |
|
| 241 | + protected function htaccessGen($path, $create = true) |
|
| 242 | + { |
|
| 243 | + if ($create === true) { |
|
| 244 | + if (!\is_writable($path)) { |
|
| 245 | + try { |
|
| 246 | + if (!\chmod($path, 0777)) { |
|
| 247 | + throw new PhpfastcacheIOException('Chmod failed on : ' . $path); |
|
| 248 | + } |
|
| 249 | + } catch (PhpfastcacheIOException $e) { |
|
| 250 | + throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if (!\file_exists($path . '/.htaccess')) { |
|
| 255 | + $file = @\fopen($path . '/.htaccess', 'w+b'); |
|
| 256 | + if (!$file) { |
|
| 257 | + throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
|
| 258 | + } |
|
| 259 | + \fwrite( |
|
| 260 | + $file, |
|
| 261 | + <<<HTACCESS |
|
| 262 | 262 | ### This .htaccess is auto-generated by PhpFastCache ### |
| 263 | 263 | <IfModule mod_authz_host> |
| 264 | 264 | Require all denied |
@@ -268,97 +268,97 @@ discard block |
||
| 268 | 268 | Deny from all |
| 269 | 269 | </IfModule> |
| 270 | 270 | HTACCESS |
| 271 | - ); |
|
| 272 | - \fclose($file); |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @param $keyword |
|
| 279 | - * @return string |
|
| 280 | - */ |
|
| 281 | - protected function encodeFilename($keyword): string |
|
| 282 | - { |
|
| 283 | - return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param $file |
|
| 288 | - * @return string |
|
| 289 | - * @throws PhpfastcacheIOException |
|
| 290 | - */ |
|
| 291 | - protected function readFile($file): string |
|
| 292 | - { |
|
| 293 | - if (!\is_readable($file)) { |
|
| 294 | - throw new PhpfastcacheIOException("Cannot read file located at: {$file}"); |
|
| 295 | - } |
|
| 296 | - if (\function_exists('file_get_contents')) { |
|
| 297 | - return (string)\file_get_contents($file); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $string = ''; |
|
| 301 | - |
|
| 302 | - $file_handle = @\fopen($file, 'rb'); |
|
| 303 | - while (!\feof($file_handle)) { |
|
| 304 | - $line = \fgets($file_handle); |
|
| 305 | - $string .= $line; |
|
| 306 | - } |
|
| 307 | - \fclose($file_handle); |
|
| 308 | - |
|
| 309 | - return $string; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /******************** |
|
| 271 | + ); |
|
| 272 | + \fclose($file); |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @param $keyword |
|
| 279 | + * @return string |
|
| 280 | + */ |
|
| 281 | + protected function encodeFilename($keyword): string |
|
| 282 | + { |
|
| 283 | + return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param $file |
|
| 288 | + * @return string |
|
| 289 | + * @throws PhpfastcacheIOException |
|
| 290 | + */ |
|
| 291 | + protected function readFile($file): string |
|
| 292 | + { |
|
| 293 | + if (!\is_readable($file)) { |
|
| 294 | + throw new PhpfastcacheIOException("Cannot read file located at: {$file}"); |
|
| 295 | + } |
|
| 296 | + if (\function_exists('file_get_contents')) { |
|
| 297 | + return (string)\file_get_contents($file); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $string = ''; |
|
| 301 | + |
|
| 302 | + $file_handle = @\fopen($file, 'rb'); |
|
| 303 | + while (!\feof($file_handle)) { |
|
| 304 | + $line = \fgets($file_handle); |
|
| 305 | + $string .= $line; |
|
| 306 | + } |
|
| 307 | + \fclose($file_handle); |
|
| 308 | + |
|
| 309 | + return $string; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /******************** |
|
| 313 | 313 | * |
| 314 | 314 | * PSR-6 Extended Methods |
| 315 | 315 | * |
| 316 | 316 | *******************/ |
| 317 | 317 | |
| 318 | - /** |
|
| 319 | - * @param string $file |
|
| 320 | - * @param string $data |
|
| 321 | - * @param bool $secureFileManipulation |
|
| 322 | - * @return bool |
|
| 323 | - * @throws PhpfastcacheIOException |
|
| 324 | - */ |
|
| 325 | - protected function writefile(string $file, string $data, bool $secureFileManipulation = false): bool |
|
| 326 | - { |
|
| 327 | - /** |
|
| 328 | - * @eventName CacheWriteFileOnDisk |
|
| 329 | - * @param ExtendedCacheItemPoolInterface $this |
|
| 330 | - * @param string $file |
|
| 331 | - * @param bool $secureFileManipulation |
|
| 332 | - * |
|
| 333 | - */ |
|
| 334 | - $this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation); |
|
| 335 | - |
|
| 336 | - if ($secureFileManipulation) { |
|
| 337 | - $tmpFilename = Directory::getAbsolutePath( |
|
| 338 | - dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
|
| 339 | - \bin2hex(\random_bytes(16)) |
|
| 340 | - ) |
|
| 341 | - ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
| 342 | - |
|
| 343 | - $handle = \fopen($tmpFilename, 'w+b'); |
|
| 344 | - if (\is_resource($handle)) { |
|
| 345 | - \flock($handle, \LOCK_EX); |
|
| 346 | - $octetWritten = fwrite($handle, $data); |
|
| 347 | - \flock($handle, \LOCK_UN); |
|
| 348 | - \fclose($handle); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - if (!\rename($tmpFilename, $file)) { |
|
| 352 | - throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
|
| 353 | - } |
|
| 354 | - } else { |
|
| 355 | - $handle = \fopen($file, 'w+b'); |
|
| 356 | - if (\is_resource($handle)) { |
|
| 357 | - $octetWritten = \fwrite($handle, $data); |
|
| 358 | - \fclose($handle); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - return (bool)($octetWritten ?? false); |
|
| 363 | - } |
|
| 318 | + /** |
|
| 319 | + * @param string $file |
|
| 320 | + * @param string $data |
|
| 321 | + * @param bool $secureFileManipulation |
|
| 322 | + * @return bool |
|
| 323 | + * @throws PhpfastcacheIOException |
|
| 324 | + */ |
|
| 325 | + protected function writefile(string $file, string $data, bool $secureFileManipulation = false): bool |
|
| 326 | + { |
|
| 327 | + /** |
|
| 328 | + * @eventName CacheWriteFileOnDisk |
|
| 329 | + * @param ExtendedCacheItemPoolInterface $this |
|
| 330 | + * @param string $file |
|
| 331 | + * @param bool $secureFileManipulation |
|
| 332 | + * |
|
| 333 | + */ |
|
| 334 | + $this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation); |
|
| 335 | + |
|
| 336 | + if ($secureFileManipulation) { |
|
| 337 | + $tmpFilename = Directory::getAbsolutePath( |
|
| 338 | + dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
|
| 339 | + \bin2hex(\random_bytes(16)) |
|
| 340 | + ) |
|
| 341 | + ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
| 342 | + |
|
| 343 | + $handle = \fopen($tmpFilename, 'w+b'); |
|
| 344 | + if (\is_resource($handle)) { |
|
| 345 | + \flock($handle, \LOCK_EX); |
|
| 346 | + $octetWritten = fwrite($handle, $data); |
|
| 347 | + \flock($handle, \LOCK_UN); |
|
| 348 | + \fclose($handle); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + if (!\rename($tmpFilename, $file)) { |
|
| 352 | + throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
|
| 353 | + } |
|
| 354 | + } else { |
|
| 355 | + $handle = \fopen($file, 'w+b'); |
|
| 356 | + if (\is_resource($handle)) { |
|
| 357 | + $octetWritten = \fwrite($handle, $data); |
|
| 358 | + \fclose($handle); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + return (bool)($octetWritten ?? false); |
|
| 363 | + } |
|
| 364 | 364 | } |
@@ -66,7 +66,8 @@ discard block |
||
| 66 | 66 | |
| 67 | 67 | if($this->getConfig()->isUseStaticItemCaching()){ |
| 68 | 68 | $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
| 69 | - }else{ |
|
| 69 | + } |
|
| 70 | + else{ |
|
| 70 | 71 | $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
| 71 | 72 | } |
| 72 | 73 | |
@@ -123,7 +124,8 @@ discard block |
||
| 123 | 124 | if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
| 124 | 125 | if (isset($_SERVER['HTTP_HOST'])) { |
| 125 | 126 | $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
| 126 | - } else { |
|
| 127 | + } |
|
| 128 | + else { |
|
| 127 | 129 | $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
| 128 | 130 | } |
| 129 | 131 | } |
@@ -143,7 +145,8 @@ discard block |
||
| 143 | 145 | |
| 144 | 146 | if (empty($this->getConfig()->getPath())) { |
| 145 | 147 | $path = $tmp_dir; |
| 146 | - } else { |
|
| 148 | + } |
|
| 149 | + else { |
|
| 147 | 150 | $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
| 148 | 151 | } |
| 149 | 152 | |
@@ -170,7 +173,8 @@ discard block |
||
| 170 | 173 | if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path)) { |
| 171 | 174 | throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
| 172 | 175 | } |
| 173 | - } else { |
|
| 176 | + } |
|
| 177 | + else { |
|
| 174 | 178 | if (!@\is_writable($full_path)) { |
| 175 | 179 | if (!@\chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
| 176 | 180 | /** |
@@ -238,15 +242,15 @@ discard block |
||
| 238 | 242 | * @param bool $create |
| 239 | 243 | * @throws PhpfastcacheIOException |
| 240 | 244 | */ |
| 241 | - protected function htaccessGen($path, $create = true) |
|
| 242 | - { |
|
| 245 | + protected function htaccessGen($path, $create = true) { |
|
| 243 | 246 | if ($create === true) { |
| 244 | 247 | if (!\is_writable($path)) { |
| 245 | 248 | try { |
| 246 | 249 | if (!\chmod($path, 0777)) { |
| 247 | 250 | throw new PhpfastcacheIOException('Chmod failed on : ' . $path); |
| 248 | 251 | } |
| 249 | - } catch (PhpfastcacheIOException $e) { |
|
| 252 | + } |
|
| 253 | + catch (PhpfastcacheIOException $e) { |
|
| 250 | 254 | throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
| 251 | 255 | } |
| 252 | 256 | } |
@@ -351,7 +355,8 @@ discard block |
||
| 351 | 355 | if (!\rename($tmpFilename, $file)) { |
| 352 | 356 | throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
| 353 | 357 | } |
| 354 | - } else { |
|
| 358 | + } |
|
| 359 | + else { |
|
| 355 | 360 | $handle = \fopen($file, 'w+b'); |
| 356 | 361 | if (\is_resource($handle)) { |
| 357 | 362 | $octetWritten = \fwrite($handle, $data); |