@@ -39,286 +39,286 @@ |
||
| 39 | 39 | |
| 40 | 40 | trait DriverBaseTrait |
| 41 | 41 | { |
| 42 | - use DriverPoolAbstractTrait; |
|
| 43 | - use ClassNamespaceResolverTrait; |
|
| 44 | - use EventManagerDispatcherTrait; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string[] |
|
| 48 | - */ |
|
| 49 | - protected static array $cacheItemClasses = []; |
|
| 50 | - |
|
| 51 | - protected ConfigurationOptionInterface $config; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var object|null |
|
| 55 | - */ |
|
| 56 | - protected ?object $instance; |
|
| 57 | - |
|
| 58 | - protected string $driverName; |
|
| 59 | - |
|
| 60 | - protected string $instanceId; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Driver constructor. |
|
| 64 | - * @param ConfigurationOptionInterface $config |
|
| 65 | - * @param string $instanceId |
|
| 66 | - * @param EventManagerInterface $em |
|
| 67 | - * @throws PhpfastcacheCoreException |
|
| 68 | - * @throws PhpfastcacheDriverCheckException |
|
| 69 | - * @throws PhpfastcacheDriverConnectException |
|
| 70 | - * @throws PhpfastcacheIOException |
|
| 71 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 72 | - */ |
|
| 73 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 74 | - { |
|
| 75 | - $this->setEventManager($em->getScopedEventManager($this)); |
|
| 76 | - $this->setConfig($config); |
|
| 77 | - $this->instanceId = $instanceId; |
|
| 78 | - |
|
| 79 | - if (!$this->driverCheck()) { |
|
| 80 | - throw new PhpfastcacheDriverCheckException( |
|
| 81 | - \sprintf( |
|
| 82 | - ExtendedCacheItemPoolInterface::DRIVER_CHECK_FAILURE, |
|
| 83 | - $this->getDriverName(), |
|
| 84 | - $this->getHelp() ? " Additionally, {$this->getHelp()}" : '' |
|
| 85 | - ) |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - $this->eventManager->dispatch(Event::CACHE_DRIVER_CHECKED, $this); |
|
| 89 | - |
|
| 90 | - try { |
|
| 91 | - $this->driverConnect(); |
|
| 92 | - $config->lock($this); // Lock the config only after a successful driver connection. |
|
| 93 | - $this->eventManager->dispatch(Event::CACHE_DRIVER_CONNECTED, $this, $this->instance ?? null); |
|
| 94 | - } catch (Throwable $e) { |
|
| 95 | - throw new PhpfastcacheDriverConnectException( |
|
| 96 | - sprintf( |
|
| 97 | - ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE, |
|
| 98 | - $this->getDriverName(), |
|
| 99 | - $e->getMessage(), |
|
| 100 | - $e->getLine() ?: 'unknown line', |
|
| 101 | - $e->getFile() ?: 'unknown file', |
|
| 102 | - ), |
|
| 103 | - 0, |
|
| 104 | - $e |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return string |
|
| 111 | - */ |
|
| 112 | - public function getDriverName(): string |
|
| 113 | - { |
|
| 114 | - if (!isset($this->driverName)) { |
|
| 115 | - $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
|
| 116 | - } |
|
| 117 | - return $this->driverName; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return ConfigurationOptionInterface |
|
| 122 | - */ |
|
| 123 | - public function getDefaultConfig(): ConfigurationOptionInterface |
|
| 124 | - { |
|
| 125 | - $className = $this::getConfigClass(); |
|
| 126 | - |
|
| 127 | - return new $className(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - public static function getConfigClass(): string |
|
| 134 | - { |
|
| 135 | - $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
|
| 136 | - if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
|
| 137 | - return $localConfigClass; |
|
| 138 | - } |
|
| 139 | - return ConfigurationOption::class; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public static function getItemClass(): string |
|
| 143 | - { |
|
| 144 | - if (!isset(self::$cacheItemClasses[static::class])) { |
|
| 145 | - self::$cacheItemClasses[static::class] = self::getClassNamespace() . '\\' . 'Item'; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return self::$cacheItemClasses[static::class]; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @inheritDoc |
|
| 153 | - */ |
|
| 154 | - public function getEncodedKey(string $key): string |
|
| 155 | - { |
|
| 156 | - $keyHashFunction = $this->getConfig()->getDefaultKeyHashFunction(); |
|
| 157 | - |
|
| 158 | - if ($keyHashFunction) { |
|
| 159 | - if (\is_callable($keyHashFunction)) { |
|
| 160 | - return $keyHashFunction($key); |
|
| 161 | - } |
|
| 162 | - throw new PhpfastcacheLogicException('Unable to build the encoded key (defaultKeyHashFunction is not callable)'); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $key; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param ExtendedCacheItemInterface $item |
|
| 172 | - * @param bool $stringifyDate |
|
| 173 | - * @return array<string, mixed> |
|
| 174 | - * @throws PhpfastcacheLogicException |
|
| 175 | - */ |
|
| 176 | - public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyDate = false): array |
|
| 177 | - { |
|
| 178 | - $wrap = [ |
|
| 179 | - ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
| 180 | - ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => $item->_getData(), |
|
| 181 | - ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
| 182 | - TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 183 | - ]; |
|
| 184 | - |
|
| 185 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 186 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();// Always on the latest date |
|
| 187 | - /** |
|
| 188 | - * If the creation date exists |
|
| 189 | - * reuse it else set a new Date |
|
| 190 | - */ |
|
| 191 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate(); |
|
| 192 | - } else { |
|
| 193 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = null; |
|
| 194 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = null; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if ($stringifyDate) { |
|
| 198 | - \array_walk($wrap, static function (mixed &$value, string $key): void { |
|
| 199 | - if ($value instanceof DateTimeInterface && $key !== ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX) { |
|
| 200 | - $value = $value->format(DateTimeInterface::W3C); |
|
| 201 | - } |
|
| 202 | - }); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return $wrap; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @return ConfigurationOptionInterface |
|
| 210 | - */ |
|
| 211 | - public function getConfig(): ConfigurationOptionInterface |
|
| 212 | - { |
|
| 213 | - return $this->config; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param ConfigurationOptionInterface $config |
|
| 218 | - * @return static |
|
| 219 | - */ |
|
| 220 | - public function setConfig(ConfigurationOptionInterface $config): static |
|
| 221 | - { |
|
| 222 | - $this->config = $config; |
|
| 223 | - |
|
| 224 | - return $this; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @param array<string, mixed> $wrapper |
|
| 229 | - * @return mixed |
|
| 230 | - * @throws \Exception |
|
| 231 | - */ |
|
| 232 | - public function driverUnwrapData(array $wrapper): mixed |
|
| 233 | - { |
|
| 234 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param array<string, mixed> $wrapper |
|
| 239 | - * @return DateTimeInterface |
|
| 240 | - */ |
|
| 241 | - public function driverUnwrapEdate(array $wrapper): \DateTimeInterface |
|
| 242 | - { |
|
| 243 | - if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 244 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param array<string, mixed> $wrapper |
|
| 252 | - * @return DateTimeInterface|null |
|
| 253 | - */ |
|
| 254 | - public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface |
|
| 255 | - { |
|
| 256 | - if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 257 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @param array<string, mixed> $wrapper |
|
| 265 | - * @return DateTimeInterface|null |
|
| 266 | - */ |
|
| 267 | - public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface |
|
| 268 | - { |
|
| 269 | - if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 270 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * @return string |
|
| 278 | - */ |
|
| 279 | - public function getInstanceId(): string |
|
| 280 | - { |
|
| 281 | - return $this->instanceId; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Encode data types such as object/array |
|
| 286 | - * for driver that does not support |
|
| 287 | - * non-scalar value |
|
| 288 | - * @param mixed $data |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - protected function encode(mixed $data): string |
|
| 292 | - { |
|
| 293 | - return \serialize($data); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Decode data stored in the cache |
|
| 298 | - * for driver that does not support |
|
| 299 | - * non-scalar value storage. |
|
| 300 | - * @param string|null $value |
|
| 301 | - * @return array<string, mixed>|null |
|
| 302 | - * @throws PhpfastcacheDriverException |
|
| 303 | - */ |
|
| 304 | - protected function decode(?string $value): ?array |
|
| 305 | - { |
|
| 306 | - $decoded = $this->unserialize($value); |
|
| 307 | - |
|
| 308 | - if ($decoded === null || is_array($decoded)) { |
|
| 309 | - return $decoded; |
|
| 310 | - } |
|
| 311 | - throw new PhpfastcacheCorruptedDataException( |
|
| 312 | - sprintf( |
|
| 313 | - 'Failed to unserialize data from the cache, expected array or null but got "%s". Stored data may be corrupted.', |
|
| 314 | - gettype($decoded) |
|
| 315 | - ), |
|
| 316 | - $value |
|
| 317 | - ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - protected function unserialize(?string $value): mixed |
|
| 321 | - { |
|
| 322 | - return $value ? \unserialize($value, ['allowed_classes' => true]) : null; |
|
| 323 | - } |
|
| 42 | + use DriverPoolAbstractTrait; |
|
| 43 | + use ClassNamespaceResolverTrait; |
|
| 44 | + use EventManagerDispatcherTrait; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string[] |
|
| 48 | + */ |
|
| 49 | + protected static array $cacheItemClasses = []; |
|
| 50 | + |
|
| 51 | + protected ConfigurationOptionInterface $config; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var object|null |
|
| 55 | + */ |
|
| 56 | + protected ?object $instance; |
|
| 57 | + |
|
| 58 | + protected string $driverName; |
|
| 59 | + |
|
| 60 | + protected string $instanceId; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Driver constructor. |
|
| 64 | + * @param ConfigurationOptionInterface $config |
|
| 65 | + * @param string $instanceId |
|
| 66 | + * @param EventManagerInterface $em |
|
| 67 | + * @throws PhpfastcacheCoreException |
|
| 68 | + * @throws PhpfastcacheDriverCheckException |
|
| 69 | + * @throws PhpfastcacheDriverConnectException |
|
| 70 | + * @throws PhpfastcacheIOException |
|
| 71 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 72 | + */ |
|
| 73 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 74 | + { |
|
| 75 | + $this->setEventManager($em->getScopedEventManager($this)); |
|
| 76 | + $this->setConfig($config); |
|
| 77 | + $this->instanceId = $instanceId; |
|
| 78 | + |
|
| 79 | + if (!$this->driverCheck()) { |
|
| 80 | + throw new PhpfastcacheDriverCheckException( |
|
| 81 | + \sprintf( |
|
| 82 | + ExtendedCacheItemPoolInterface::DRIVER_CHECK_FAILURE, |
|
| 83 | + $this->getDriverName(), |
|
| 84 | + $this->getHelp() ? " Additionally, {$this->getHelp()}" : '' |
|
| 85 | + ) |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + $this->eventManager->dispatch(Event::CACHE_DRIVER_CHECKED, $this); |
|
| 89 | + |
|
| 90 | + try { |
|
| 91 | + $this->driverConnect(); |
|
| 92 | + $config->lock($this); // Lock the config only after a successful driver connection. |
|
| 93 | + $this->eventManager->dispatch(Event::CACHE_DRIVER_CONNECTED, $this, $this->instance ?? null); |
|
| 94 | + } catch (Throwable $e) { |
|
| 95 | + throw new PhpfastcacheDriverConnectException( |
|
| 96 | + sprintf( |
|
| 97 | + ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE, |
|
| 98 | + $this->getDriverName(), |
|
| 99 | + $e->getMessage(), |
|
| 100 | + $e->getLine() ?: 'unknown line', |
|
| 101 | + $e->getFile() ?: 'unknown file', |
|
| 102 | + ), |
|
| 103 | + 0, |
|
| 104 | + $e |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return string |
|
| 111 | + */ |
|
| 112 | + public function getDriverName(): string |
|
| 113 | + { |
|
| 114 | + if (!isset($this->driverName)) { |
|
| 115 | + $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
|
| 116 | + } |
|
| 117 | + return $this->driverName; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return ConfigurationOptionInterface |
|
| 122 | + */ |
|
| 123 | + public function getDefaultConfig(): ConfigurationOptionInterface |
|
| 124 | + { |
|
| 125 | + $className = $this::getConfigClass(); |
|
| 126 | + |
|
| 127 | + return new $className(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + public static function getConfigClass(): string |
|
| 134 | + { |
|
| 135 | + $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
|
| 136 | + if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
|
| 137 | + return $localConfigClass; |
|
| 138 | + } |
|
| 139 | + return ConfigurationOption::class; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public static function getItemClass(): string |
|
| 143 | + { |
|
| 144 | + if (!isset(self::$cacheItemClasses[static::class])) { |
|
| 145 | + self::$cacheItemClasses[static::class] = self::getClassNamespace() . '\\' . 'Item'; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return self::$cacheItemClasses[static::class]; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @inheritDoc |
|
| 153 | + */ |
|
| 154 | + public function getEncodedKey(string $key): string |
|
| 155 | + { |
|
| 156 | + $keyHashFunction = $this->getConfig()->getDefaultKeyHashFunction(); |
|
| 157 | + |
|
| 158 | + if ($keyHashFunction) { |
|
| 159 | + if (\is_callable($keyHashFunction)) { |
|
| 160 | + return $keyHashFunction($key); |
|
| 161 | + } |
|
| 162 | + throw new PhpfastcacheLogicException('Unable to build the encoded key (defaultKeyHashFunction is not callable)'); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $key; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param ExtendedCacheItemInterface $item |
|
| 172 | + * @param bool $stringifyDate |
|
| 173 | + * @return array<string, mixed> |
|
| 174 | + * @throws PhpfastcacheLogicException |
|
| 175 | + */ |
|
| 176 | + public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyDate = false): array |
|
| 177 | + { |
|
| 178 | + $wrap = [ |
|
| 179 | + ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
| 180 | + ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => $item->_getData(), |
|
| 181 | + ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
| 182 | + TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 183 | + ]; |
|
| 184 | + |
|
| 185 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 186 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();// Always on the latest date |
|
| 187 | + /** |
|
| 188 | + * If the creation date exists |
|
| 189 | + * reuse it else set a new Date |
|
| 190 | + */ |
|
| 191 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate(); |
|
| 192 | + } else { |
|
| 193 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = null; |
|
| 194 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = null; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if ($stringifyDate) { |
|
| 198 | + \array_walk($wrap, static function (mixed &$value, string $key): void { |
|
| 199 | + if ($value instanceof DateTimeInterface && $key !== ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX) { |
|
| 200 | + $value = $value->format(DateTimeInterface::W3C); |
|
| 201 | + } |
|
| 202 | + }); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return $wrap; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @return ConfigurationOptionInterface |
|
| 210 | + */ |
|
| 211 | + public function getConfig(): ConfigurationOptionInterface |
|
| 212 | + { |
|
| 213 | + return $this->config; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param ConfigurationOptionInterface $config |
|
| 218 | + * @return static |
|
| 219 | + */ |
|
| 220 | + public function setConfig(ConfigurationOptionInterface $config): static |
|
| 221 | + { |
|
| 222 | + $this->config = $config; |
|
| 223 | + |
|
| 224 | + return $this; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @param array<string, mixed> $wrapper |
|
| 229 | + * @return mixed |
|
| 230 | + * @throws \Exception |
|
| 231 | + */ |
|
| 232 | + public function driverUnwrapData(array $wrapper): mixed |
|
| 233 | + { |
|
| 234 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param array<string, mixed> $wrapper |
|
| 239 | + * @return DateTimeInterface |
|
| 240 | + */ |
|
| 241 | + public function driverUnwrapEdate(array $wrapper): \DateTimeInterface |
|
| 242 | + { |
|
| 243 | + if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 244 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param array<string, mixed> $wrapper |
|
| 252 | + * @return DateTimeInterface|null |
|
| 253 | + */ |
|
| 254 | + public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface |
|
| 255 | + { |
|
| 256 | + if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 257 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @param array<string, mixed> $wrapper |
|
| 265 | + * @return DateTimeInterface|null |
|
| 266 | + */ |
|
| 267 | + public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface |
|
| 268 | + { |
|
| 269 | + if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 270 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * @return string |
|
| 278 | + */ |
|
| 279 | + public function getInstanceId(): string |
|
| 280 | + { |
|
| 281 | + return $this->instanceId; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Encode data types such as object/array |
|
| 286 | + * for driver that does not support |
|
| 287 | + * non-scalar value |
|
| 288 | + * @param mixed $data |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + protected function encode(mixed $data): string |
|
| 292 | + { |
|
| 293 | + return \serialize($data); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Decode data stored in the cache |
|
| 298 | + * for driver that does not support |
|
| 299 | + * non-scalar value storage. |
|
| 300 | + * @param string|null $value |
|
| 301 | + * @return array<string, mixed>|null |
|
| 302 | + * @throws PhpfastcacheDriverException |
|
| 303 | + */ |
|
| 304 | + protected function decode(?string $value): ?array |
|
| 305 | + { |
|
| 306 | + $decoded = $this->unserialize($value); |
|
| 307 | + |
|
| 308 | + if ($decoded === null || is_array($decoded)) { |
|
| 309 | + return $decoded; |
|
| 310 | + } |
|
| 311 | + throw new PhpfastcacheCorruptedDataException( |
|
| 312 | + sprintf( |
|
| 313 | + 'Failed to unserialize data from the cache, expected array or null but got "%s". Stored data may be corrupted.', |
|
| 314 | + gettype($decoded) |
|
| 315 | + ), |
|
| 316 | + $value |
|
| 317 | + ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + protected function unserialize(?string $value): mixed |
|
| 321 | + { |
|
| 322 | + return $value ? \unserialize($value, ['allowed_classes' => true]) : null; |
|
| 323 | + } |
|
| 324 | 324 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | ]; |
| 184 | 184 | |
| 185 | 185 | if ($this->getConfig()->isItemDetailedDate()) { |
| 186 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();// Always on the latest date |
|
| 186 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime(); // Always on the latest date |
|
| 187 | 187 | /** |
| 188 | 188 | * If the creation date exists |
| 189 | 189 | * reuse it else set a new Date |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | if ($stringifyDate) { |
| 198 | - \array_walk($wrap, static function (mixed &$value, string $key): void { |
|
| 198 | + \array_walk($wrap, static function(mixed &$value, string $key): void { |
|
| 199 | 199 | if ($value instanceof DateTimeInterface && $key !== ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX) { |
| 200 | 200 | $value = $value->format(DateTimeInterface::W3C); |
| 201 | 201 | } |
@@ -70,8 +70,7 @@ discard block |
||
| 70 | 70 | * @throws PhpfastcacheIOException |
| 71 | 71 | * @throws PhpfastcacheInvalidArgumentException |
| 72 | 72 | */ |
| 73 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 74 | - { |
|
| 73 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) { |
|
| 75 | 74 | $this->setEventManager($em->getScopedEventManager($this)); |
| 76 | 75 | $this->setConfig($config); |
| 77 | 76 | $this->instanceId = $instanceId; |
@@ -91,7 +90,8 @@ discard block |
||
| 91 | 90 | $this->driverConnect(); |
| 92 | 91 | $config->lock($this); // Lock the config only after a successful driver connection. |
| 93 | 92 | $this->eventManager->dispatch(Event::CACHE_DRIVER_CONNECTED, $this, $this->instance ?? null); |
| 94 | - } catch (Throwable $e) { |
|
| 93 | + } |
|
| 94 | + catch (Throwable $e) { |
|
| 95 | 95 | throw new PhpfastcacheDriverConnectException( |
| 96 | 96 | sprintf( |
| 97 | 97 | ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE, |
@@ -189,7 +189,8 @@ discard block |
||
| 189 | 189 | * reuse it else set a new Date |
| 190 | 190 | */ |
| 191 | 191 | $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate(); |
| 192 | - } else { |
|
| 192 | + } |
|
| 193 | + else { |
|
| 193 | 194 | $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = null; |
| 194 | 195 | $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = null; |
| 195 | 196 | } |
@@ -18,17 +18,17 @@ |
||
| 18 | 18 | |
| 19 | 19 | trait AggregatablePoolTrait |
| 20 | 20 | { |
| 21 | - protected ?ClusterPoolInterface $clusterPool = null; |
|
| 21 | + protected ?ClusterPoolInterface $clusterPool = null; |
|
| 22 | 22 | |
| 23 | - public function isAggregatedBy(): ?ClusterPoolInterface |
|
| 24 | - { |
|
| 25 | - return $this->clusterPool; |
|
| 26 | - } |
|
| 23 | + public function isAggregatedBy(): ?ClusterPoolInterface |
|
| 24 | + { |
|
| 25 | + return $this->clusterPool; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - public function setAggregatedBy(ClusterPoolInterface $clusterPool): static |
|
| 29 | - { |
|
| 30 | - $this->clusterPool = $clusterPool; |
|
| 28 | + public function setAggregatedBy(ClusterPoolInterface $clusterPool): static |
|
| 29 | + { |
|
| 30 | + $this->clusterPool = $clusterPool; |
|
| 31 | 31 | |
| 32 | - return $this; |
|
| 33 | - } |
|
| 32 | + return $this; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -26,56 +26,56 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | final class ExtensionManager |
| 28 | 28 | { |
| 29 | - use UninstanciableObjectTrait; |
|
| 29 | + use UninstanciableObjectTrait; |
|
| 30 | 30 | |
| 31 | - public const KNOWN_EXTENSION_NAMES = [ |
|
| 32 | - 'Arangodb', |
|
| 33 | - 'Couchbasev4', |
|
| 34 | - 'Couchdb', |
|
| 35 | - 'Dynamodb', |
|
| 36 | - 'Firestore', |
|
| 37 | - 'Mongodb', |
|
| 38 | - 'Ravendb', |
|
| 39 | - 'Solr' |
|
| 40 | - ]; |
|
| 31 | + public const KNOWN_EXTENSION_NAMES = [ |
|
| 32 | + 'Arangodb', |
|
| 33 | + 'Couchbasev4', |
|
| 34 | + 'Couchdb', |
|
| 35 | + 'Dynamodb', |
|
| 36 | + 'Firestore', |
|
| 37 | + 'Mongodb', |
|
| 38 | + 'Ravendb', |
|
| 39 | + 'Solr' |
|
| 40 | + ]; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @var array<string, string> |
|
| 44 | - */ |
|
| 45 | - protected static array $registeredExtensions = []; |
|
| 42 | + /** |
|
| 43 | + * @var array<string, string> |
|
| 44 | + */ |
|
| 45 | + protected static array $registeredExtensions = []; |
|
| 46 | 46 | |
| 47 | - public static function registerExtension(string $extensionName, string $driverClassName): void |
|
| 48 | - { |
|
| 49 | - if (!str_starts_with($driverClassName, ltrim('Phpfastcache\\Extensions\\', '\\'))) { |
|
| 50 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 51 | - 'Only extensions from "\\Phpfastcache\\Extensions\\" namespace are allowed. Use CacheManager::addCustomDriver() to create your own extensions.' |
|
| 52 | - ); |
|
| 53 | - } |
|
| 54 | - self::$registeredExtensions[$extensionName] = $driverClassName; |
|
| 55 | - } |
|
| 47 | + public static function registerExtension(string $extensionName, string $driverClassName): void |
|
| 48 | + { |
|
| 49 | + if (!str_starts_with($driverClassName, ltrim('Phpfastcache\\Extensions\\', '\\'))) { |
|
| 50 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 51 | + 'Only extensions from "\\Phpfastcache\\Extensions\\" namespace are allowed. Use CacheManager::addCustomDriver() to create your own extensions.' |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | + self::$registeredExtensions[$extensionName] = $driverClassName; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public static function extensionExists(string $extensionName): bool |
|
| 58 | - { |
|
| 59 | - return isset(self::$registeredExtensions[$extensionName]); |
|
| 60 | - } |
|
| 57 | + public static function extensionExists(string $extensionName): bool |
|
| 58 | + { |
|
| 59 | + return isset(self::$registeredExtensions[$extensionName]); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @param string $name |
|
| 64 | - * @return string |
|
| 65 | - * @throws PhpfastcacheExtensionNotFoundException |
|
| 66 | - */ |
|
| 67 | - public static function getExtension(string $name): string |
|
| 68 | - { |
|
| 69 | - if (isset(self::$registeredExtensions[$name])) { |
|
| 70 | - return self::$registeredExtensions[$name]; |
|
| 71 | - } else { |
|
| 72 | - throw new PhpfastcacheExtensionNotFoundException( |
|
| 73 | - sprintf( |
|
| 74 | - 'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`', |
|
| 75 | - $name, |
|
| 76 | - strtolower($name) |
|
| 77 | - ) |
|
| 78 | - ); |
|
| 79 | - } |
|
| 80 | - } |
|
| 62 | + /** |
|
| 63 | + * @param string $name |
|
| 64 | + * @return string |
|
| 65 | + * @throws PhpfastcacheExtensionNotFoundException |
|
| 66 | + */ |
|
| 67 | + public static function getExtension(string $name): string |
|
| 68 | + { |
|
| 69 | + if (isset(self::$registeredExtensions[$name])) { |
|
| 70 | + return self::$registeredExtensions[$name]; |
|
| 71 | + } else { |
|
| 72 | + throw new PhpfastcacheExtensionNotFoundException( |
|
| 73 | + sprintf( |
|
| 74 | + 'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`', |
|
| 75 | + $name, |
|
| 76 | + strtolower($name) |
|
| 77 | + ) |
|
| 78 | + ); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -68,7 +68,8 @@ |
||
| 68 | 68 | { |
| 69 | 69 | if (isset(self::$registeredExtensions[$name])) { |
| 70 | 70 | return self::$registeredExtensions[$name]; |
| 71 | - } else { |
|
| 71 | + } |
|
| 72 | + else { |
|
| 72 | 73 | throw new PhpfastcacheExtensionNotFoundException( |
| 73 | 74 | sprintf( |
| 74 | 75 | 'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`', |
@@ -21,110 +21,110 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class DriverStatistic |
| 23 | 23 | { |
| 24 | - protected string $info = ''; |
|
| 25 | - |
|
| 26 | - protected ?int $size = 0; |
|
| 27 | - |
|
| 28 | - protected ?int $count = 0; |
|
| 29 | - |
|
| 30 | - protected string $data = ''; |
|
| 31 | - |
|
| 32 | - protected mixed $rawData; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Return quick information about the driver instance |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - public function getInfo(): string |
|
| 39 | - { |
|
| 40 | - return $this->info; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function setInfo(string $info): static |
|
| 44 | - { |
|
| 45 | - $this->info = $info; |
|
| 46 | - |
|
| 47 | - return $this; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Return the approximate size taken by the driver instance (in bytes) (null if unsupported by the driver) |
|
| 52 | - * @return int|null |
|
| 53 | - */ |
|
| 54 | - public function getSize(): ?int |
|
| 55 | - { |
|
| 56 | - return $this->size; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function setSize(?int $size): static |
|
| 60 | - { |
|
| 61 | - $this->size = $size; |
|
| 62 | - |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Return the approximate count of elements stored in a driver database (or collection if applicable). Added in v9.2.3 |
|
| 68 | - * @since 9.2.3 |
|
| 69 | - * @return int|null |
|
| 70 | - */ |
|
| 71 | - public function getCount(): ?int |
|
| 72 | - { |
|
| 73 | - return $this->count; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function setCount(?int $count): static |
|
| 77 | - { |
|
| 78 | - $this->count = $count; |
|
| 79 | - return $this; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Return an array of item keys used by this driver instance (deprecated as of v9.2.3, will be removed as of v10) |
|
| 84 | - * @deprecated as of phpfastcache 9.2.3, will be removed as of v10 |
|
| 85 | - */ |
|
| 86 | - public function getData(): string |
|
| 87 | - { |
|
| 88 | - return $this->data; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @deprecated as of phpfastcache 9.2.3, will be removed as of v10 |
|
| 93 | - */ |
|
| 94 | - public function setData(string $data): static |
|
| 95 | - { |
|
| 96 | - $this->data = ($data ?: ''); |
|
| 97 | - |
|
| 98 | - return $this; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Return a bunch of random data provided by the driver. Any type can be provided, usually an array |
|
| 103 | - * @return mixed |
|
| 104 | - */ |
|
| 105 | - public function getRawData(): mixed |
|
| 106 | - { |
|
| 107 | - return $this->rawData; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - public function setRawData(mixed $raw): static |
|
| 111 | - { |
|
| 112 | - $this->rawData = $raw; |
|
| 113 | - |
|
| 114 | - return $this; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return array<string, string> |
|
| 119 | - */ |
|
| 120 | - public function getPublicDesc(): array |
|
| 121 | - { |
|
| 122 | - return [ |
|
| 123 | - 'Info' => 'Cache Information', |
|
| 124 | - 'Size' => 'Cache Size', |
|
| 125 | - 'Count' => 'Cache database/collection count', |
|
| 126 | - 'Data' => 'Cache items keys (Deprecated)', |
|
| 127 | - 'RawData' => 'Cache raw data', |
|
| 128 | - ]; |
|
| 129 | - } |
|
| 24 | + protected string $info = ''; |
|
| 25 | + |
|
| 26 | + protected ?int $size = 0; |
|
| 27 | + |
|
| 28 | + protected ?int $count = 0; |
|
| 29 | + |
|
| 30 | + protected string $data = ''; |
|
| 31 | + |
|
| 32 | + protected mixed $rawData; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Return quick information about the driver instance |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + public function getInfo(): string |
|
| 39 | + { |
|
| 40 | + return $this->info; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function setInfo(string $info): static |
|
| 44 | + { |
|
| 45 | + $this->info = $info; |
|
| 46 | + |
|
| 47 | + return $this; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Return the approximate size taken by the driver instance (in bytes) (null if unsupported by the driver) |
|
| 52 | + * @return int|null |
|
| 53 | + */ |
|
| 54 | + public function getSize(): ?int |
|
| 55 | + { |
|
| 56 | + return $this->size; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function setSize(?int $size): static |
|
| 60 | + { |
|
| 61 | + $this->size = $size; |
|
| 62 | + |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Return the approximate count of elements stored in a driver database (or collection if applicable). Added in v9.2.3 |
|
| 68 | + * @since 9.2.3 |
|
| 69 | + * @return int|null |
|
| 70 | + */ |
|
| 71 | + public function getCount(): ?int |
|
| 72 | + { |
|
| 73 | + return $this->count; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function setCount(?int $count): static |
|
| 77 | + { |
|
| 78 | + $this->count = $count; |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Return an array of item keys used by this driver instance (deprecated as of v9.2.3, will be removed as of v10) |
|
| 84 | + * @deprecated as of phpfastcache 9.2.3, will be removed as of v10 |
|
| 85 | + */ |
|
| 86 | + public function getData(): string |
|
| 87 | + { |
|
| 88 | + return $this->data; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @deprecated as of phpfastcache 9.2.3, will be removed as of v10 |
|
| 93 | + */ |
|
| 94 | + public function setData(string $data): static |
|
| 95 | + { |
|
| 96 | + $this->data = ($data ?: ''); |
|
| 97 | + |
|
| 98 | + return $this; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Return a bunch of random data provided by the driver. Any type can be provided, usually an array |
|
| 103 | + * @return mixed |
|
| 104 | + */ |
|
| 105 | + public function getRawData(): mixed |
|
| 106 | + { |
|
| 107 | + return $this->rawData; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + public function setRawData(mixed $raw): static |
|
| 111 | + { |
|
| 112 | + $this->rawData = $raw; |
|
| 113 | + |
|
| 114 | + return $this; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return array<string, string> |
|
| 119 | + */ |
|
| 120 | + public function getPublicDesc(): array |
|
| 121 | + { |
|
| 122 | + return [ |
|
| 123 | + 'Info' => 'Cache Information', |
|
| 124 | + 'Size' => 'Cache Size', |
|
| 125 | + 'Count' => 'Cache database/collection count', |
|
| 126 | + 'Data' => 'Cache items keys (Deprecated)', |
|
| 127 | + 'RawData' => 'Cache raw data', |
|
| 128 | + ]; |
|
| 129 | + } |
|
| 130 | 130 | } |
@@ -20,30 +20,30 @@ |
||
| 20 | 20 | |
| 21 | 21 | class ItemBatch |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * ItemBatch constructor. |
|
| 25 | - * @param string $itemKey |
|
| 26 | - * @param DateTimeInterface $itemDate |
|
| 27 | - */ |
|
| 28 | - public function __construct( |
|
| 29 | - protected string $itemKey, |
|
| 30 | - protected DateTimeInterface $itemDate |
|
| 31 | - ) { |
|
| 32 | - } |
|
| 23 | + /** |
|
| 24 | + * ItemBatch constructor. |
|
| 25 | + * @param string $itemKey |
|
| 26 | + * @param DateTimeInterface $itemDate |
|
| 27 | + */ |
|
| 28 | + public function __construct( |
|
| 29 | + protected string $itemKey, |
|
| 30 | + protected DateTimeInterface $itemDate |
|
| 31 | + ) { |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function getItemKey(): string |
|
| 38 | - { |
|
| 39 | - return $this->itemKey; |
|
| 40 | - } |
|
| 34 | + /** |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function getItemKey(): string |
|
| 38 | + { |
|
| 39 | + return $this->itemKey; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return DateTimeInterface |
|
| 44 | - */ |
|
| 45 | - public function getItemDate(): DateTimeInterface |
|
| 46 | - { |
|
| 47 | - return $this->itemDate; |
|
| 48 | - } |
|
| 42 | + /** |
|
| 43 | + * @return DateTimeInterface |
|
| 44 | + */ |
|
| 45 | + public function getItemDate(): DateTimeInterface |
|
| 46 | + { |
|
| 47 | + return $this->itemDate; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -18,65 +18,65 @@ |
||
| 18 | 18 | |
| 19 | 19 | class DriverIO |
| 20 | 20 | { |
| 21 | - protected int $writeHit = 0; |
|
| 22 | - |
|
| 23 | - protected int $readHit = 0; |
|
| 24 | - |
|
| 25 | - protected int $readMiss = 0; |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - public function getWriteHit(): int |
|
| 29 | - { |
|
| 30 | - return $this->writeHit; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param int $writeHit |
|
| 35 | - * @return DriverIO |
|
| 36 | - */ |
|
| 37 | - public function setWriteHit(int $writeHit): DriverIO |
|
| 38 | - { |
|
| 39 | - $this->writeHit = $writeHit; |
|
| 40 | - return $this; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function incWriteHit(): DriverIO |
|
| 44 | - { |
|
| 45 | - $this->writeHit++; |
|
| 46 | - return $this; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function getReadHit(): int |
|
| 50 | - { |
|
| 51 | - return $this->readHit; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public function setReadHit(int $readHit): DriverIO |
|
| 55 | - { |
|
| 56 | - $this->readHit = $readHit; |
|
| 57 | - return $this; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function incReadHit(): DriverIO |
|
| 61 | - { |
|
| 62 | - $this->readHit++; |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function getReadMiss(): int |
|
| 67 | - { |
|
| 68 | - return $this->readMiss; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function setReadMiss(int $readMiss): DriverIO |
|
| 72 | - { |
|
| 73 | - $this->readMiss = $readMiss; |
|
| 74 | - return $this; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function incReadMiss(): DriverIO |
|
| 78 | - { |
|
| 79 | - $this->readMiss++; |
|
| 80 | - return $this; |
|
| 81 | - } |
|
| 21 | + protected int $writeHit = 0; |
|
| 22 | + |
|
| 23 | + protected int $readHit = 0; |
|
| 24 | + |
|
| 25 | + protected int $readMiss = 0; |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + public function getWriteHit(): int |
|
| 29 | + { |
|
| 30 | + return $this->writeHit; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param int $writeHit |
|
| 35 | + * @return DriverIO |
|
| 36 | + */ |
|
| 37 | + public function setWriteHit(int $writeHit): DriverIO |
|
| 38 | + { |
|
| 39 | + $this->writeHit = $writeHit; |
|
| 40 | + return $this; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function incWriteHit(): DriverIO |
|
| 44 | + { |
|
| 45 | + $this->writeHit++; |
|
| 46 | + return $this; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function getReadHit(): int |
|
| 50 | + { |
|
| 51 | + return $this->readHit; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public function setReadHit(int $readHit): DriverIO |
|
| 55 | + { |
|
| 56 | + $this->readHit = $readHit; |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function incReadHit(): DriverIO |
|
| 61 | + { |
|
| 62 | + $this->readHit++; |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function getReadMiss(): int |
|
| 67 | + { |
|
| 68 | + return $this->readMiss; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function setReadMiss(int $readMiss): DriverIO |
|
| 72 | + { |
|
| 73 | + $this->readMiss = $readMiss; |
|
| 74 | + return $this; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function incReadMiss(): DriverIO |
|
| 78 | + { |
|
| 79 | + $this->readMiss++; |
|
| 80 | + return $this; |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -21,173 +21,173 @@ |
||
| 21 | 21 | |
| 22 | 22 | trait ClassNamespaceResolverTrait |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var array<string, string> |
|
| 26 | - */ |
|
| 27 | - protected static array $namespaces = []; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Iterate over all files in the given directory searching for classes. |
|
| 31 | - * |
|
| 32 | - * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 33 | - * deprecated the whole component as of SF4. Our thanks to them. |
|
| 34 | - * |
|
| 35 | - * @param string $dir The directory to search in or an iterator |
|
| 36 | - * |
|
| 37 | - * @return array<string, string> A class map array |
|
| 38 | - */ |
|
| 39 | - protected static function createClassMap(string $dir): array |
|
| 40 | - { |
|
| 41 | - $dirIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
|
| 42 | - |
|
| 43 | - $map = []; |
|
| 44 | - |
|
| 45 | - foreach ($dirIterator as $file) { |
|
| 46 | - if (!$file->isFile()) { |
|
| 47 | - continue; |
|
| 48 | - } |
|
| 49 | - $path = $file->getRealPath() ?: $file->getPathname(); |
|
| 50 | - if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { |
|
| 51 | - continue; |
|
| 52 | - } |
|
| 53 | - $classes = self::findClasses($path); |
|
| 54 | - gc_mem_caches(); |
|
| 55 | - foreach ($classes as $class) { |
|
| 56 | - $map[$class] = $path; |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $map; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Extract the classes in the given file. |
|
| 65 | - * |
|
| 66 | - * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 67 | - * deprecated the whole component as of SF4. Our thanks to them. |
|
| 68 | - * |
|
| 69 | - * @param string $path The file to check |
|
| 70 | - * |
|
| 71 | - * @return string[] The found classes |
|
| 72 | - * |
|
| 73 | - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 74 | - */ |
|
| 75 | - protected static function findClasses(string $path): array |
|
| 76 | - { |
|
| 77 | - $contents = file_get_contents($path); |
|
| 78 | - $tokens = token_get_all($contents); |
|
| 79 | - $classes = []; |
|
| 80 | - $namespace = ''; |
|
| 81 | - for ($i = 0; isset($tokens[$i]); ++$i) { |
|
| 82 | - $token = $tokens[$i]; |
|
| 83 | - if (!isset($token[1])) { |
|
| 84 | - continue; |
|
| 85 | - } |
|
| 86 | - $class = ''; |
|
| 87 | - switch ($token[0]) { |
|
| 88 | - case T_NAMESPACE: |
|
| 89 | - $namespace = self::buildTokenNamespace($i, $tokens); |
|
| 90 | - break; |
|
| 91 | - case T_CLASS: |
|
| 92 | - case T_INTERFACE: |
|
| 93 | - case T_TRAIT: |
|
| 94 | - $classes = self::buildTokenClasses($namespace, $class, $classes, $i, $tokens); |
|
| 95 | - break; |
|
| 96 | - default: |
|
| 97 | - break; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $classes; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param string $namespace |
|
| 106 | - * @param string $class |
|
| 107 | - * @param string[] $classes |
|
| 108 | - * @param int $index |
|
| 109 | - * @param array<array<mixed>|string> $tokens |
|
| 110 | - * @return string[] |
|
| 111 | - */ |
|
| 112 | - protected static function buildTokenClasses(string $namespace, string $class, array $classes, int $index, array $tokens): array |
|
| 113 | - { |
|
| 114 | - // Skip usage of ::class constant |
|
| 115 | - $isClassConstant = false; |
|
| 116 | - for ($j = $index - 1; $j > 0; --$j) { |
|
| 117 | - if (!isset($tokens[$j][1])) { |
|
| 118 | - break; |
|
| 119 | - } |
|
| 120 | - if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
| 121 | - $isClassConstant = true; |
|
| 122 | - break; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
| 126 | - break; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - if ($isClassConstant) { |
|
| 130 | - return $classes; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // Find the classname |
|
| 134 | - while (isset($tokens[++$index][1])) { |
|
| 135 | - $t = $tokens[$index]; |
|
| 136 | - if (T_STRING === $t[0]) { |
|
| 137 | - $class .= $t[1]; |
|
| 138 | - } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 139 | - break; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return \array_merge($classes, [\ltrim($namespace . $class, '\\')]); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @param int $index |
|
| 148 | - * @param array<array<mixed>|string> $tokens |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 151 | - protected static function buildTokenNamespace(int $index, array $tokens): string |
|
| 152 | - { |
|
| 153 | - $namespace = ''; |
|
| 154 | - |
|
| 155 | - // If there is a namespace, extract it (PHP 8 test) |
|
| 156 | - if (\defined('T_NAME_QUALIFIED')) { |
|
| 157 | - while (isset($tokens[++$index][1])) { |
|
| 158 | - if ($tokens[$index][0] === T_NAME_QUALIFIED) { |
|
| 159 | - $namespace = $tokens[$index][1]; |
|
| 160 | - break; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } else { |
|
| 164 | - while (isset($tokens[++$index][1])) { |
|
| 165 | - if (\in_array($tokens[$index][0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
| 166 | - $namespace .= $tokens[$index][1]; |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $namespace . '\\'; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 177 | - public static function getClassNamespace(): string |
|
| 178 | - { |
|
| 179 | - if (!isset(self::$namespaces[static::class])) { |
|
| 180 | - self::$namespaces[static::class] = substr(static::class, 0, strrpos(static::class, '\\')); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return self::$namespaces[static::class]; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @return string |
|
| 188 | - */ |
|
| 189 | - public function getClassName(): string |
|
| 190 | - { |
|
| 191 | - return static::class; |
|
| 192 | - } |
|
| 24 | + /** |
|
| 25 | + * @var array<string, string> |
|
| 26 | + */ |
|
| 27 | + protected static array $namespaces = []; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Iterate over all files in the given directory searching for classes. |
|
| 31 | + * |
|
| 32 | + * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 33 | + * deprecated the whole component as of SF4. Our thanks to them. |
|
| 34 | + * |
|
| 35 | + * @param string $dir The directory to search in or an iterator |
|
| 36 | + * |
|
| 37 | + * @return array<string, string> A class map array |
|
| 38 | + */ |
|
| 39 | + protected static function createClassMap(string $dir): array |
|
| 40 | + { |
|
| 41 | + $dirIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
|
| 42 | + |
|
| 43 | + $map = []; |
|
| 44 | + |
|
| 45 | + foreach ($dirIterator as $file) { |
|
| 46 | + if (!$file->isFile()) { |
|
| 47 | + continue; |
|
| 48 | + } |
|
| 49 | + $path = $file->getRealPath() ?: $file->getPathname(); |
|
| 50 | + if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { |
|
| 51 | + continue; |
|
| 52 | + } |
|
| 53 | + $classes = self::findClasses($path); |
|
| 54 | + gc_mem_caches(); |
|
| 55 | + foreach ($classes as $class) { |
|
| 56 | + $map[$class] = $path; |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $map; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Extract the classes in the given file. |
|
| 65 | + * |
|
| 66 | + * NOTICE: This method has been borrowed from Symfony ClassLoader 3.4 since they |
|
| 67 | + * deprecated the whole component as of SF4. Our thanks to them. |
|
| 68 | + * |
|
| 69 | + * @param string $path The file to check |
|
| 70 | + * |
|
| 71 | + * @return string[] The found classes |
|
| 72 | + * |
|
| 73 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 74 | + */ |
|
| 75 | + protected static function findClasses(string $path): array |
|
| 76 | + { |
|
| 77 | + $contents = file_get_contents($path); |
|
| 78 | + $tokens = token_get_all($contents); |
|
| 79 | + $classes = []; |
|
| 80 | + $namespace = ''; |
|
| 81 | + for ($i = 0; isset($tokens[$i]); ++$i) { |
|
| 82 | + $token = $tokens[$i]; |
|
| 83 | + if (!isset($token[1])) { |
|
| 84 | + continue; |
|
| 85 | + } |
|
| 86 | + $class = ''; |
|
| 87 | + switch ($token[0]) { |
|
| 88 | + case T_NAMESPACE: |
|
| 89 | + $namespace = self::buildTokenNamespace($i, $tokens); |
|
| 90 | + break; |
|
| 91 | + case T_CLASS: |
|
| 92 | + case T_INTERFACE: |
|
| 93 | + case T_TRAIT: |
|
| 94 | + $classes = self::buildTokenClasses($namespace, $class, $classes, $i, $tokens); |
|
| 95 | + break; |
|
| 96 | + default: |
|
| 97 | + break; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $classes; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param string $namespace |
|
| 106 | + * @param string $class |
|
| 107 | + * @param string[] $classes |
|
| 108 | + * @param int $index |
|
| 109 | + * @param array<array<mixed>|string> $tokens |
|
| 110 | + * @return string[] |
|
| 111 | + */ |
|
| 112 | + protected static function buildTokenClasses(string $namespace, string $class, array $classes, int $index, array $tokens): array |
|
| 113 | + { |
|
| 114 | + // Skip usage of ::class constant |
|
| 115 | + $isClassConstant = false; |
|
| 116 | + for ($j = $index - 1; $j > 0; --$j) { |
|
| 117 | + if (!isset($tokens[$j][1])) { |
|
| 118 | + break; |
|
| 119 | + } |
|
| 120 | + if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
| 121 | + $isClassConstant = true; |
|
| 122 | + break; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
|
| 126 | + break; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + if ($isClassConstant) { |
|
| 130 | + return $classes; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // Find the classname |
|
| 134 | + while (isset($tokens[++$index][1])) { |
|
| 135 | + $t = $tokens[$index]; |
|
| 136 | + if (T_STRING === $t[0]) { |
|
| 137 | + $class .= $t[1]; |
|
| 138 | + } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 139 | + break; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return \array_merge($classes, [\ltrim($namespace . $class, '\\')]); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @param int $index |
|
| 148 | + * @param array<array<mixed>|string> $tokens |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | + protected static function buildTokenNamespace(int $index, array $tokens): string |
|
| 152 | + { |
|
| 153 | + $namespace = ''; |
|
| 154 | + |
|
| 155 | + // If there is a namespace, extract it (PHP 8 test) |
|
| 156 | + if (\defined('T_NAME_QUALIFIED')) { |
|
| 157 | + while (isset($tokens[++$index][1])) { |
|
| 158 | + if ($tokens[$index][0] === T_NAME_QUALIFIED) { |
|
| 159 | + $namespace = $tokens[$index][1]; |
|
| 160 | + break; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } else { |
|
| 164 | + while (isset($tokens[++$index][1])) { |
|
| 165 | + if (\in_array($tokens[$index][0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
| 166 | + $namespace .= $tokens[$index][1]; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $namespace . '\\'; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | + public static function getClassNamespace(): string |
|
| 178 | + { |
|
| 179 | + if (!isset(self::$namespaces[static::class])) { |
|
| 180 | + self::$namespaces[static::class] = substr(static::class, 0, strrpos(static::class, '\\')); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return self::$namespaces[static::class]; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @return string |
|
| 188 | + */ |
|
| 189 | + public function getClassName(): string |
|
| 190 | + { |
|
| 191 | + return static::class; |
|
| 192 | + } |
|
| 193 | 193 | } |
@@ -135,7 +135,8 @@ discard block |
||
| 135 | 135 | $t = $tokens[$index]; |
| 136 | 136 | if (T_STRING === $t[0]) { |
| 137 | 137 | $class .= $t[1]; |
| 138 | - } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 138 | + } |
|
| 139 | + elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
| 139 | 140 | break; |
| 140 | 141 | } |
| 141 | 142 | } |
@@ -160,7 +161,8 @@ discard block |
||
| 160 | 161 | break; |
| 161 | 162 | } |
| 162 | 163 | } |
| 163 | - } else { |
|
| 164 | + } |
|
| 165 | + else { |
|
| 164 | 166 | while (isset($tokens[++$index][1])) { |
| 165 | 167 | if (\in_array($tokens[$index][0], [T_STRING, T_NS_SEPARATOR], true)) { |
| 166 | 168 | $namespace .= $tokens[$index][1]; |
@@ -18,13 +18,13 @@ |
||
| 18 | 18 | |
| 19 | 19 | interface ClassNamespaceResolverInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - public static function getClassNamespace(): string; |
|
| 21 | + /** |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + public static function getClassNamespace(): string; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function getClassName(): string; |
|
| 26 | + /** |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function getClassName(): string; |
|
| 30 | 30 | } |
@@ -23,126 +23,126 @@ |
||
| 23 | 23 | |
| 24 | 24 | class Directory |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Get the directory size |
|
| 28 | - * @param string $directory |
|
| 29 | - * @param bool $includeDirAllocSize |
|
| 30 | - * @return int |
|
| 31 | - */ |
|
| 32 | - public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
| 33 | - { |
|
| 34 | - $size = 0; |
|
| 35 | - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
| 36 | - /** |
|
| 37 | - * @var SplFileInfo $file |
|
| 38 | - */ |
|
| 39 | - if ($file->isFile()) { |
|
| 40 | - $size += filesize($file->getRealPath()); |
|
| 41 | - } elseif ($includeDirAllocSize) { |
|
| 42 | - $size += $file->getSize(); |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - return $size; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $path |
|
| 51 | - * @return int |
|
| 52 | - */ |
|
| 53 | - public static function getFileCount(string $path): int |
|
| 54 | - { |
|
| 55 | - $count = 0; |
|
| 56 | - $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
| 57 | - foreach ($objects as $object) { |
|
| 58 | - /** |
|
| 59 | - * @var SplFileInfo $object |
|
| 60 | - */ |
|
| 61 | - if ($object->isFile()) { |
|
| 62 | - $count++; |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return $count; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Recursively delete a directory and all of its contents - e.g.the equivalent of `rm -r` on the command-line. |
|
| 71 | - * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
| 72 | - * |
|
| 73 | - * @param string $source absolute path to directory or file to delete. |
|
| 74 | - * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
| 75 | - * |
|
| 76 | - * @return bool true on success; false on failure |
|
| 77 | - */ |
|
| 78 | - public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
| 79 | - { |
|
| 80 | - if (empty($source) || file_exists($source) === false) { |
|
| 81 | - return false; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if (is_file($source) || is_link($source)) { |
|
| 85 | - clearstatcache(true, $source); |
|
| 86 | - return unlink($source); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $files = new RecursiveIteratorIterator( |
|
| 90 | - new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), |
|
| 91 | - RecursiveIteratorIterator::CHILD_FIRST |
|
| 92 | - ); |
|
| 93 | - |
|
| 94 | - foreach ($files as $fileInfo) { |
|
| 95 | - /** |
|
| 96 | - * @var SplFileInfo $fileInfo |
|
| 97 | - */ |
|
| 98 | - $realpath = $fileInfo->getRealPath(); |
|
| 99 | - if ($realpath) { |
|
| 100 | - if ($fileInfo->isDir()) { |
|
| 101 | - if (self::rrmdir($fileInfo->getRealPath()) === false) { |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - } elseif (unlink($realpath) === false) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - } else { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($removeOnlyChildren === false) { |
|
| 113 | - return rmdir($source); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return true; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Alias of realpath() but work |
|
| 121 | - * on non-existing files |
|
| 122 | - * |
|
| 123 | - * @param string $path |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - public static function getAbsolutePath(string $path): string |
|
| 127 | - { |
|
| 128 | - $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
| 129 | - $absolutes = []; |
|
| 130 | - foreach ($parts as $part) { |
|
| 131 | - if ('.' === $part) { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - if ('..' === $part) { |
|
| 135 | - array_pop($absolutes); |
|
| 136 | - } else { |
|
| 137 | - $absolutes[] = $part; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Allows dereferencing char |
|
| 143 | - */ |
|
| 144 | - $file = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 145 | - $prefix = $file[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
| 146 | - return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 147 | - } |
|
| 26 | + /** |
|
| 27 | + * Get the directory size |
|
| 28 | + * @param string $directory |
|
| 29 | + * @param bool $includeDirAllocSize |
|
| 30 | + * @return int |
|
| 31 | + */ |
|
| 32 | + public static function dirSize(string $directory, bool $includeDirAllocSize = false): int |
|
| 33 | + { |
|
| 34 | + $size = 0; |
|
| 35 | + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { |
|
| 36 | + /** |
|
| 37 | + * @var SplFileInfo $file |
|
| 38 | + */ |
|
| 39 | + if ($file->isFile()) { |
|
| 40 | + $size += filesize($file->getRealPath()); |
|
| 41 | + } elseif ($includeDirAllocSize) { |
|
| 42 | + $size += $file->getSize(); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + return $size; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $path |
|
| 51 | + * @return int |
|
| 52 | + */ |
|
| 53 | + public static function getFileCount(string $path): int |
|
| 54 | + { |
|
| 55 | + $count = 0; |
|
| 56 | + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); |
|
| 57 | + foreach ($objects as $object) { |
|
| 58 | + /** |
|
| 59 | + * @var SplFileInfo $object |
|
| 60 | + */ |
|
| 61 | + if ($object->isFile()) { |
|
| 62 | + $count++; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return $count; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Recursively delete a directory and all of its contents - e.g.the equivalent of `rm -r` on the command-line. |
|
| 71 | + * Consistent with `rmdir()` and `unlink()`, an E_WARNING level error will be generated on failure. |
|
| 72 | + * |
|
| 73 | + * @param string $source absolute path to directory or file to delete. |
|
| 74 | + * @param bool $removeOnlyChildren set to true will only remove content inside directory. |
|
| 75 | + * |
|
| 76 | + * @return bool true on success; false on failure |
|
| 77 | + */ |
|
| 78 | + public static function rrmdir(string $source, bool $removeOnlyChildren = false): bool |
|
| 79 | + { |
|
| 80 | + if (empty($source) || file_exists($source) === false) { |
|
| 81 | + return false; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if (is_file($source) || is_link($source)) { |
|
| 85 | + clearstatcache(true, $source); |
|
| 86 | + return unlink($source); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $files = new RecursiveIteratorIterator( |
|
| 90 | + new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), |
|
| 91 | + RecursiveIteratorIterator::CHILD_FIRST |
|
| 92 | + ); |
|
| 93 | + |
|
| 94 | + foreach ($files as $fileInfo) { |
|
| 95 | + /** |
|
| 96 | + * @var SplFileInfo $fileInfo |
|
| 97 | + */ |
|
| 98 | + $realpath = $fileInfo->getRealPath(); |
|
| 99 | + if ($realpath) { |
|
| 100 | + if ($fileInfo->isDir()) { |
|
| 101 | + if (self::rrmdir($fileInfo->getRealPath()) === false) { |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + } elseif (unlink($realpath) === false) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + } else { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($removeOnlyChildren === false) { |
|
| 113 | + return rmdir($source); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Alias of realpath() but work |
|
| 121 | + * on non-existing files |
|
| 122 | + * |
|
| 123 | + * @param string $path |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + public static function getAbsolutePath(string $path): string |
|
| 127 | + { |
|
| 128 | + $parts = preg_split('~[/\\\\]+~', $path, 0, PREG_SPLIT_NO_EMPTY); |
|
| 129 | + $absolutes = []; |
|
| 130 | + foreach ($parts as $part) { |
|
| 131 | + if ('.' === $part) { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + if ('..' === $part) { |
|
| 135 | + array_pop($absolutes); |
|
| 136 | + } else { |
|
| 137 | + $absolutes[] = $part; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Allows dereferencing char |
|
| 143 | + */ |
|
| 144 | + $file = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 145 | + $prefix = $file[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
|
| 146 | + return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 147 | + } |
|
| 148 | 148 | } |
@@ -141,7 +141,7 @@ |
||
| 141 | 141 | /** |
| 142 | 142 | * Allows dereferencing char |
| 143 | 143 | */ |
| 144 | - $file = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__);// remove file protocols such as "phar://" etc. |
|
| 144 | + $file = preg_replace('~^(([a-z0-9\-]+)://)~', '', __FILE__); // remove file protocols such as "phar://" etc. |
|
| 145 | 145 | $prefix = $file[0] === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : ''; |
| 146 | 146 | return $prefix . implode(DIRECTORY_SEPARATOR, $absolutes); |
| 147 | 147 | } |
@@ -38,7 +38,8 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | if ($file->isFile()) { |
| 40 | 40 | $size += filesize($file->getRealPath()); |
| 41 | - } elseif ($includeDirAllocSize) { |
|
| 41 | + } |
|
| 42 | + elseif ($includeDirAllocSize) { |
|
| 42 | 43 | $size += $file->getSize(); |
| 43 | 44 | } |
| 44 | 45 | } |
@@ -101,10 +102,12 @@ discard block |
||
| 101 | 102 | if (self::rrmdir($fileInfo->getRealPath()) === false) { |
| 102 | 103 | return false; |
| 103 | 104 | } |
| 104 | - } elseif (unlink($realpath) === false) { |
|
| 105 | + } |
|
| 106 | + elseif (unlink($realpath) === false) { |
|
| 105 | 107 | return false; |
| 106 | 108 | } |
| 107 | - } else { |
|
| 109 | + } |
|
| 110 | + else { |
|
| 108 | 111 | return false; |
| 109 | 112 | } |
| 110 | 113 | } |
@@ -133,7 +136,8 @@ discard block |
||
| 133 | 136 | } |
| 134 | 137 | if ('..' === $part) { |
| 135 | 138 | array_pop($absolutes); |
| 136 | - } else { |
|
| 139 | + } |
|
| 140 | + else { |
|
| 137 | 141 | $absolutes[] = $part; |
| 138 | 142 | } |
| 139 | 143 | } |