@@ -36,196 +36,196 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | interface ExtendedCacheItemPoolInterface extends CacheItemPoolInterface, EventManagerDispatcherInterface, ClassNamespaceResolverInterface, TaggableCacheItemPoolInterface |
| 38 | 38 | { |
| 39 | - public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue. |
|
| 39 | + public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue. |
|
| 40 | 40 | Also, please verify the suggested dependencies in composer because as of the V6, 3rd party libraries are no longer required.%s'; |
| 41 | 41 | |
| 42 | - public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s.'; |
|
| 43 | - |
|
| 44 | - public const DRIVER_KEY_WRAPPER_INDEX = 'k'; |
|
| 45 | - |
|
| 46 | - public const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Expiration date Index |
|
| 50 | - */ |
|
| 51 | - public const DRIVER_EDATE_WRAPPER_INDEX = 'e'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Creation date Index |
|
| 55 | - */ |
|
| 56 | - public const DRIVER_CDATE_WRAPPER_INDEX = 'c'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Modification date Index |
|
| 60 | - */ |
|
| 61 | - public const DRIVER_MDATE_WRAPPER_INDEX = 'm'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Hard-limit count of items returns by getAllItems() |
|
| 65 | - */ |
|
| 66 | - public const MAX_ALL_KEYS_COUNT = 9999; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Return the config class name |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public static function getConfigClass(): string; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Return the item class name |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public static function getItemClass(): string; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param string $key |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 84 | - public function getEncodedKey(string $key): string; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return ConfigurationOptionInterface |
|
| 88 | - */ |
|
| 89 | - public function getConfig(): ConfigurationOptionInterface; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @return ConfigurationOptionInterface |
|
| 93 | - */ |
|
| 94 | - public function getDefaultConfig(): ConfigurationOptionInterface; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - public function getDriverName(): string; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return mixed |
|
| 103 | - */ |
|
| 104 | - public function getInstanceId(): string; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * [Phpfastcache phpDoc Override] |
|
| 108 | - * Returns a Cache Item representing the specified key. |
|
| 109 | - * |
|
| 110 | - * This method must always return a CacheItemInterface object, even in case of |
|
| 111 | - * a cache miss. It MUST NOT return null. |
|
| 112 | - * |
|
| 113 | - * @param string $key |
|
| 114 | - * The key for which to return the corresponding Cache Item. |
|
| 115 | - * |
|
| 116 | - * @return ExtendedCacheItemInterface |
|
| 117 | - * The corresponding Cache Item. |
|
| 118 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 119 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 120 | - * MUST be thrown. |
|
| 121 | - * |
|
| 122 | - */ |
|
| 123 | - public function getItem(string $key): ExtendedCacheItemInterface; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * [Phpfastcache phpDoc Override] |
|
| 127 | - * Returns a traversable set of cache items. |
|
| 128 | - * |
|
| 129 | - * @param string[] $keys |
|
| 130 | - * An indexed array of keys of items to retrieve. |
|
| 131 | - * |
|
| 132 | - * @return iterable<ExtendedCacheItemInterface> |
|
| 133 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
| 134 | - * each item. A Cache item will be returned for each key, even if that |
|
| 135 | - * key is not found. However, if no keys are specified then an empty |
|
| 136 | - * traversable MUST be returned instead. |
|
| 137 | - * @throws InvalidArgumentException |
|
| 138 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 139 | - * MUST be thrown. |
|
| 140 | - * |
|
| 141 | - */ |
|
| 142 | - public function getItems(array $keys = []): iterable; |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Returns the WHOLE cache as a traversable set of cache items. |
|
| 146 | - * A hard-limit of 9999 items is defined internally to prevent |
|
| 147 | - * serious performances issues of your application. |
|
| 148 | - * @see ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT |
|
| 149 | - * |
|
| 150 | - * @param string $pattern |
|
| 151 | - * An optional pattern supported by a limited range of drivers. |
|
| 152 | - * If this parameter is unsupported by the driver, a PhpfastcacheInvalidArgumentException will be thrown. |
|
| 153 | - * |
|
| 154 | - * @return iterable<ExtendedCacheItemInterface> |
|
| 155 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
| 156 | - * each item. However, if no keys are returned by the backend then an empty |
|
| 157 | - * traversable WILL be returned instead. |
|
| 158 | - * |
|
| 159 | - * @throws PhpfastcacheInvalidArgumentException If the driver does not support the $pattern argument |
|
| 160 | - * @throws PhpfastcacheUnsupportedMethodException If the driver does not permit to list all the keys through this implementation. |
|
| 161 | - */ |
|
| 162 | - public function getAllItems(string $pattern = ''): iterable; |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Returns A json string that represents an array of items. |
|
| 166 | - * |
|
| 167 | - * @param array<string> $keys An indexed array of keys of items to retrieve. |
|
| 168 | - * @param int $options \json_encode() options |
|
| 169 | - * @param int $depth \json_encode() depth |
|
| 170 | - * |
|
| 171 | - * @return string |
|
| 172 | - * @throws InvalidArgumentException |
|
| 173 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 174 | - * MUST be thrown. |
|
| 175 | - * |
|
| 176 | - */ |
|
| 177 | - public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string; |
|
| 178 | - |
|
| 179 | - public function setItem(CacheItemInterface $item): static; |
|
| 180 | - |
|
| 181 | - public function getStats(): DriverStatistic; |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Get a quick help guide |
|
| 185 | - * about the current driver |
|
| 186 | - */ |
|
| 187 | - public function getHelp(): string; |
|
| 188 | - |
|
| 189 | - public function detachItem(CacheItemInterface $item): static; |
|
| 190 | - |
|
| 191 | - public function detachAllItems(): static; |
|
| 192 | - |
|
| 193 | - public function attachItem(CacheItemInterface $item): static; |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Returns true if the item exists, is attached and the Spl Hash matches |
|
| 197 | - * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
| 198 | - * Returns null if the item does not exist |
|
| 199 | - * |
|
| 200 | - * @param CacheItemInterface $item |
|
| 201 | - * @return bool |
|
| 202 | - * @throws PhpfastcacheLogicException |
|
| 203 | - */ |
|
| 204 | - public function isAttached(CacheItemInterface $item): bool; |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Persists a cache item immediately. |
|
| 208 | - * |
|
| 209 | - * @param ExtendedCacheItemInterface|CacheItemInterface $item |
|
| 210 | - * The cache item to save. |
|
| 211 | - * |
|
| 212 | - * @return bool |
|
| 213 | - * True if the item was successfully persisted. False if there was an error. |
|
| 214 | - */ |
|
| 215 | - public function save(ExtendedCacheItemInterface|CacheItemInterface $item): bool; |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Save multiple items, possible uses: |
|
| 219 | - * saveMultiple([$item1, $item2, $item3]); |
|
| 220 | - * saveMultiple($item1, $item2, $item3); |
|
| 221 | - * |
|
| 222 | - * @param ExtendedCacheItemInterface[] $items |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - public function saveMultiple(ExtendedCacheItemInterface ...$items): bool; |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @return DriverIO |
|
| 229 | - */ |
|
| 230 | - public function getIO(): DriverIO; |
|
| 42 | + public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s.'; |
|
| 43 | + |
|
| 44 | + public const DRIVER_KEY_WRAPPER_INDEX = 'k'; |
|
| 45 | + |
|
| 46 | + public const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Expiration date Index |
|
| 50 | + */ |
|
| 51 | + public const DRIVER_EDATE_WRAPPER_INDEX = 'e'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Creation date Index |
|
| 55 | + */ |
|
| 56 | + public const DRIVER_CDATE_WRAPPER_INDEX = 'c'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Modification date Index |
|
| 60 | + */ |
|
| 61 | + public const DRIVER_MDATE_WRAPPER_INDEX = 'm'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Hard-limit count of items returns by getAllItems() |
|
| 65 | + */ |
|
| 66 | + public const MAX_ALL_KEYS_COUNT = 9999; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Return the config class name |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public static function getConfigClass(): string; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Return the item class name |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public static function getItemClass(): string; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param string $key |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | + public function getEncodedKey(string $key): string; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return ConfigurationOptionInterface |
|
| 88 | + */ |
|
| 89 | + public function getConfig(): ConfigurationOptionInterface; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @return ConfigurationOptionInterface |
|
| 93 | + */ |
|
| 94 | + public function getDefaultConfig(): ConfigurationOptionInterface; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + public function getDriverName(): string; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return mixed |
|
| 103 | + */ |
|
| 104 | + public function getInstanceId(): string; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * [Phpfastcache phpDoc Override] |
|
| 108 | + * Returns a Cache Item representing the specified key. |
|
| 109 | + * |
|
| 110 | + * This method must always return a CacheItemInterface object, even in case of |
|
| 111 | + * a cache miss. It MUST NOT return null. |
|
| 112 | + * |
|
| 113 | + * @param string $key |
|
| 114 | + * The key for which to return the corresponding Cache Item. |
|
| 115 | + * |
|
| 116 | + * @return ExtendedCacheItemInterface |
|
| 117 | + * The corresponding Cache Item. |
|
| 118 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 119 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 120 | + * MUST be thrown. |
|
| 121 | + * |
|
| 122 | + */ |
|
| 123 | + public function getItem(string $key): ExtendedCacheItemInterface; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * [Phpfastcache phpDoc Override] |
|
| 127 | + * Returns a traversable set of cache items. |
|
| 128 | + * |
|
| 129 | + * @param string[] $keys |
|
| 130 | + * An indexed array of keys of items to retrieve. |
|
| 131 | + * |
|
| 132 | + * @return iterable<ExtendedCacheItemInterface> |
|
| 133 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
| 134 | + * each item. A Cache item will be returned for each key, even if that |
|
| 135 | + * key is not found. However, if no keys are specified then an empty |
|
| 136 | + * traversable MUST be returned instead. |
|
| 137 | + * @throws InvalidArgumentException |
|
| 138 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 139 | + * MUST be thrown. |
|
| 140 | + * |
|
| 141 | + */ |
|
| 142 | + public function getItems(array $keys = []): iterable; |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Returns the WHOLE cache as a traversable set of cache items. |
|
| 146 | + * A hard-limit of 9999 items is defined internally to prevent |
|
| 147 | + * serious performances issues of your application. |
|
| 148 | + * @see ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT |
|
| 149 | + * |
|
| 150 | + * @param string $pattern |
|
| 151 | + * An optional pattern supported by a limited range of drivers. |
|
| 152 | + * If this parameter is unsupported by the driver, a PhpfastcacheInvalidArgumentException will be thrown. |
|
| 153 | + * |
|
| 154 | + * @return iterable<ExtendedCacheItemInterface> |
|
| 155 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
| 156 | + * each item. However, if no keys are returned by the backend then an empty |
|
| 157 | + * traversable WILL be returned instead. |
|
| 158 | + * |
|
| 159 | + * @throws PhpfastcacheInvalidArgumentException If the driver does not support the $pattern argument |
|
| 160 | + * @throws PhpfastcacheUnsupportedMethodException If the driver does not permit to list all the keys through this implementation. |
|
| 161 | + */ |
|
| 162 | + public function getAllItems(string $pattern = ''): iterable; |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Returns A json string that represents an array of items. |
|
| 166 | + * |
|
| 167 | + * @param array<string> $keys An indexed array of keys of items to retrieve. |
|
| 168 | + * @param int $options \json_encode() options |
|
| 169 | + * @param int $depth \json_encode() depth |
|
| 170 | + * |
|
| 171 | + * @return string |
|
| 172 | + * @throws InvalidArgumentException |
|
| 173 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 174 | + * MUST be thrown. |
|
| 175 | + * |
|
| 176 | + */ |
|
| 177 | + public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string; |
|
| 178 | + |
|
| 179 | + public function setItem(CacheItemInterface $item): static; |
|
| 180 | + |
|
| 181 | + public function getStats(): DriverStatistic; |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Get a quick help guide |
|
| 185 | + * about the current driver |
|
| 186 | + */ |
|
| 187 | + public function getHelp(): string; |
|
| 188 | + |
|
| 189 | + public function detachItem(CacheItemInterface $item): static; |
|
| 190 | + |
|
| 191 | + public function detachAllItems(): static; |
|
| 192 | + |
|
| 193 | + public function attachItem(CacheItemInterface $item): static; |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Returns true if the item exists, is attached and the Spl Hash matches |
|
| 197 | + * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
| 198 | + * Returns null if the item does not exist |
|
| 199 | + * |
|
| 200 | + * @param CacheItemInterface $item |
|
| 201 | + * @return bool |
|
| 202 | + * @throws PhpfastcacheLogicException |
|
| 203 | + */ |
|
| 204 | + public function isAttached(CacheItemInterface $item): bool; |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Persists a cache item immediately. |
|
| 208 | + * |
|
| 209 | + * @param ExtendedCacheItemInterface|CacheItemInterface $item |
|
| 210 | + * The cache item to save. |
|
| 211 | + * |
|
| 212 | + * @return bool |
|
| 213 | + * True if the item was successfully persisted. False if there was an error. |
|
| 214 | + */ |
|
| 215 | + public function save(ExtendedCacheItemInterface|CacheItemInterface $item): bool; |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Save multiple items, possible uses: |
|
| 219 | + * saveMultiple([$item1, $item2, $item3]); |
|
| 220 | + * saveMultiple($item1, $item2, $item3); |
|
| 221 | + * |
|
| 222 | + * @param ExtendedCacheItemInterface[] $items |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + public function saveMultiple(ExtendedCacheItemInterface ...$items): bool; |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @return DriverIO |
|
| 229 | + */ |
|
| 230 | + public function getIO(): DriverIO; |
|
| 231 | 231 | } |
@@ -30,442 +30,442 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | trait TaggableCacheItemPoolTrait |
| 32 | 32 | { |
| 33 | - use ExtendedCacheItemPoolTrait; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @inheritDoc |
|
| 37 | - * @throws PhpfastcacheCoreException |
|
| 38 | - * @throws PhpfastcacheDriverException |
|
| 39 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 40 | - * @throws PhpfastcacheLogicException |
|
| 41 | - */ |
|
| 42 | - public function getItemsByTagsAsJsonString( |
|
| 43 | - array $tagNames, |
|
| 44 | - int $option = \JSON_THROW_ON_ERROR, |
|
| 45 | - int $depth = 512, |
|
| 46 | - int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE |
|
| 47 | - ): string { |
|
| 48 | - return \json_encode( |
|
| 49 | - \array_map( |
|
| 50 | - static fn(CacheItemInterface $item) => $item->get(), |
|
| 51 | - \array_values($this->getItemsByTags($tagNames, $strategy)) |
|
| 52 | - ), |
|
| 53 | - $option, |
|
| 54 | - $depth, |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @inheritDoc |
|
| 60 | - * @throws PhpfastcacheCoreException |
|
| 61 | - * @throws PhpfastcacheDriverException |
|
| 62 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 63 | - * @throws PhpfastcacheLogicException |
|
| 64 | - */ |
|
| 65 | - public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 66 | - { |
|
| 67 | - $items = []; |
|
| 68 | - foreach (\array_unique($tagNames) as $tagName) { |
|
| 69 | - $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $items = \array_merge([], ...$items); |
|
| 73 | - |
|
| 74 | - switch ($strategy) { |
|
| 75 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
| 76 | - foreach ($items as $key => $item) { |
|
| 77 | - if (\array_diff($tagNames, $item->getTags())) { |
|
| 78 | - unset($items[$key]); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - break; |
|
| 82 | - |
|
| 83 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
| 84 | - foreach ($items as $key => $item) { |
|
| 85 | - if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
| 86 | - unset($items[$key]); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - break; |
|
| 90 | - } |
|
| 91 | - return $items; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $tagName |
|
| 96 | - * @return ExtendedCacheItemInterface[] |
|
| 97 | - * @throws PhpfastcacheCoreException |
|
| 98 | - * @throws PhpfastcacheDriverException |
|
| 99 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | - * @throws PhpfastcacheLogicException |
|
| 101 | - */ |
|
| 102 | - protected function fetchItemsByTagFromBackend(string $tagName): array |
|
| 103 | - { |
|
| 104 | - $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
| 105 | - |
|
| 106 | - if ($driverResponse->isHit()) { |
|
| 107 | - $tagsItems = (array)$driverResponse->get(); |
|
| 108 | - /** |
|
| 109 | - * getItems() may provide expired item(s) |
|
| 110 | - * themselves provided by a cache of item |
|
| 111 | - * keys based stored the tag item. |
|
| 112 | - * Therefore, we pass a filter callback |
|
| 113 | - * to remove the expired Item(s) provided by |
|
| 114 | - * the item keys passed through getItems() |
|
| 115 | - * |
|
| 116 | - * #headache |
|
| 117 | - */ |
|
| 118 | - return \array_filter( |
|
| 119 | - $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
| 120 | - static fn (ExtendedCacheItemInterface $item) => $item->isHit(), |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - return []; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $key |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - protected function getTagKey(string $key): string |
|
| 131 | - { |
|
| 132 | - return TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX . $key; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @inheritDoc |
|
| 137 | - * @throws PhpfastcacheCoreException |
|
| 138 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 139 | - * @throws PhpfastcacheLogicException |
|
| 140 | - * @throws PhpfastcacheDriverException |
|
| 141 | - */ |
|
| 142 | - public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 143 | - { |
|
| 144 | - $return = true; |
|
| 145 | - |
|
| 146 | - foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
| 147 | - $result = $this->deleteItem($item->getKey()); |
|
| 148 | - if ($result !== true) { |
|
| 149 | - $return = $result; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @inheritDoc |
|
| 158 | - * @throws PhpfastcacheCoreException |
|
| 159 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 160 | - * @throws PhpfastcacheLogicException |
|
| 161 | - * @throws PhpfastcacheDriverException |
|
| 162 | - */ |
|
| 163 | - public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 164 | - { |
|
| 165 | - $return = true; |
|
| 166 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 167 | - $result = $this->deleteItem($item->getKey()); |
|
| 168 | - if ($result !== true) { |
|
| 169 | - $return = $result; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $return; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @inheritDoc |
|
| 178 | - * @throws PhpfastcacheCoreException |
|
| 179 | - * @throws PhpfastcacheDriverException |
|
| 180 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 181 | - * @throws PhpfastcacheLogicException |
|
| 182 | - */ |
|
| 183 | - public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 184 | - { |
|
| 185 | - $items = $this->fetchItemsByTagFromBackend($tagName); |
|
| 186 | - |
|
| 187 | - if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
| 188 | - foreach ($items as $key => $item) { |
|
| 189 | - if (\array_diff($item->getTags(), [$tagName])) { |
|
| 190 | - unset($items[$key]); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - return $items; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @inheritDoc |
|
| 199 | - * @throws PhpfastcacheCoreException |
|
| 200 | - * @throws PhpfastcacheDriverException |
|
| 201 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 202 | - * @throws PhpfastcacheLogicException |
|
| 203 | - * @throws \ReflectionException |
|
| 204 | - */ |
|
| 205 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 206 | - { |
|
| 207 | - $return = true; |
|
| 208 | - foreach ($tagNames as $tagName) { |
|
| 209 | - $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
| 210 | - if ($result !== true) { |
|
| 211 | - $return = $result; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return $return; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @inheritDoc |
|
| 220 | - * @throws PhpfastcacheCoreException |
|
| 221 | - * @throws PhpfastcacheDriverException |
|
| 222 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 223 | - * @throws PhpfastcacheLogicException |
|
| 224 | - * @throws \ReflectionException |
|
| 225 | - */ |
|
| 226 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 227 | - { |
|
| 228 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 229 | - $item->increment($step); |
|
| 230 | - $this->saveDeferred($item); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return $this->commit(); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @inheritDoc |
|
| 238 | - * @throws PhpfastcacheCoreException |
|
| 239 | - * @throws PhpfastcacheDriverException |
|
| 240 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 241 | - * @throws PhpfastcacheLogicException |
|
| 242 | - * @throws \ReflectionException |
|
| 243 | - */ |
|
| 244 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 245 | - { |
|
| 246 | - $return = true; |
|
| 247 | - foreach ($tagNames as $tagName) { |
|
| 248 | - $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
| 249 | - if ($result !== true) { |
|
| 250 | - $return = $result; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @inheritDoc |
|
| 259 | - * @throws PhpfastcacheCoreException |
|
| 260 | - * @throws PhpfastcacheDriverException |
|
| 261 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 262 | - * @throws PhpfastcacheLogicException |
|
| 263 | - * @throws \ReflectionException |
|
| 264 | - */ |
|
| 265 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 266 | - { |
|
| 267 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 268 | - $item->decrement($step); |
|
| 269 | - $this->saveDeferred($item); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return $this->commit(); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @inheritDoc |
|
| 277 | - * @throws PhpfastcacheCoreException |
|
| 278 | - * @throws PhpfastcacheDriverException |
|
| 279 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 280 | - * @throws PhpfastcacheLogicException |
|
| 281 | - * @throws \ReflectionException |
|
| 282 | - */ |
|
| 283 | - public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 284 | - { |
|
| 285 | - $return = true; |
|
| 286 | - foreach ($tagNames as $tagName) { |
|
| 287 | - $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
| 288 | - if ($result !== true) { |
|
| 289 | - $return = $result; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - return $return; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * @inheritDoc |
|
| 298 | - * @throws PhpfastcacheCoreException |
|
| 299 | - * @throws PhpfastcacheDriverException |
|
| 300 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 301 | - * @throws PhpfastcacheLogicException |
|
| 302 | - * @throws \ReflectionException |
|
| 303 | - */ |
|
| 304 | - public function appendItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 305 | - { |
|
| 306 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 307 | - $item->append($data); |
|
| 308 | - $this->saveDeferred($item); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - return $this->commit(); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @inheritDoc |
|
| 316 | - * @throws PhpfastcacheCoreException |
|
| 317 | - * @throws PhpfastcacheDriverException |
|
| 318 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 319 | - * @throws PhpfastcacheLogicException |
|
| 320 | - * @throws \ReflectionException |
|
| 321 | - */ |
|
| 322 | - public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 323 | - { |
|
| 324 | - $return = true; |
|
| 325 | - foreach ($tagNames as $tagName) { |
|
| 326 | - $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
| 327 | - if ($result !== true) { |
|
| 328 | - $return = $result; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return $return; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @inheritDoc |
|
| 337 | - * @throws PhpfastcacheCoreException |
|
| 338 | - * @throws PhpfastcacheDriverException |
|
| 339 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 340 | - * @throws PhpfastcacheLogicException |
|
| 341 | - * @throws \ReflectionException |
|
| 342 | - */ |
|
| 343 | - public function prependItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 344 | - { |
|
| 345 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 346 | - $item->prepend($data); |
|
| 347 | - $this->saveDeferred($item); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - return $this->commit(); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @param array<mixed> $wrapper |
|
| 355 | - * @return string[] |
|
| 356 | - */ |
|
| 357 | - protected function driverUnwrapTags(array $wrapper): array |
|
| 358 | - { |
|
| 359 | - return $wrapper[TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX]; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * @param ExtendedCacheItemInterface $item |
|
| 364 | - * @throws PhpfastcacheCoreException |
|
| 365 | - * @throws PhpfastcacheDriverException |
|
| 366 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 367 | - * @throws PhpfastcacheLogicException |
|
| 368 | - */ |
|
| 369 | - protected function cleanItemTags(ExtendedCacheItemInterface $item): void |
|
| 370 | - { |
|
| 371 | - if (!empty($item->getTags()) || !empty($item->getRemovedTags())) { |
|
| 372 | - $this->driverWriteTags($item->removeTags($item->getTags())); |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * @param ExtendedCacheItemInterface $item |
|
| 378 | - * @return bool |
|
| 379 | - * @throws PhpfastcacheCoreException |
|
| 380 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 381 | - * @throws PhpfastcacheLogicException |
|
| 382 | - * @throws PhpfastcacheDriverException |
|
| 383 | - */ |
|
| 384 | - protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
| 385 | - { |
|
| 386 | - /** |
|
| 387 | - * Do not attempt to write tags |
|
| 388 | - * on tags item, it can lead |
|
| 389 | - * to an infinite recursive calls |
|
| 390 | - */ |
|
| 391 | - if (str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 392 | - throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - if (!$item->getTags() && !$item->getRemovedTags()) { |
|
| 396 | - return true; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * @var $tagsItems ExtendedCacheItemInterface[] |
|
| 401 | - */ |
|
| 402 | - $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
| 403 | - |
|
| 404 | - foreach ($tagsItems as $tagsItem) { |
|
| 405 | - $data = $tagsItem->get(); |
|
| 406 | - $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Using the key will |
|
| 410 | - * avoid to use array_unique |
|
| 411 | - * that has slow performances |
|
| 412 | - */ |
|
| 413 | - $data = \array_merge((array)$data, [$item->getKey() => $expTimestamp]); |
|
| 414 | - $tagsItem->set($data); |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Recalculate the expiration date |
|
| 418 | - * |
|
| 419 | - * If the $tagsItem does not have |
|
| 420 | - * any cache item references left |
|
| 421 | - * then remove it from tagsItems index |
|
| 422 | - */ |
|
| 423 | - $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 424 | - |
|
| 425 | - $this->driverWrite($tagsItem); |
|
| 426 | - $tagsItem->setHit(true); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Also update removed tags to |
|
| 431 | - * keep the index up to date |
|
| 432 | - */ |
|
| 433 | - $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
| 434 | - |
|
| 435 | - foreach ($tagsItems as $tagsItem) { |
|
| 436 | - $data = (array)$tagsItem->get(); |
|
| 437 | - |
|
| 438 | - unset($data[$item->getKey()]); |
|
| 439 | - $tagsItem->set($data); |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Recalculate the expiration date |
|
| 443 | - * |
|
| 444 | - * If the $tagsItem does not have |
|
| 445 | - * any cache item references left |
|
| 446 | - * then remove it from tagsItems index |
|
| 447 | - */ |
|
| 448 | - if (\count($data)) { |
|
| 449 | - $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 450 | - $this->driverWrite($tagsItem); |
|
| 451 | - $tagsItem->setHit(true); |
|
| 452 | - } else { |
|
| 453 | - $this->deleteItem($tagsItem->getKey()); |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - return true; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * @param array<string> $keys |
|
| 462 | - * @return array<string> |
|
| 463 | - */ |
|
| 464 | - protected function getTagKeys(array $keys): array |
|
| 465 | - { |
|
| 466 | - return \array_map( |
|
| 467 | - fn (string $key) => $this->getTagKey($key), |
|
| 468 | - $keys |
|
| 469 | - ); |
|
| 470 | - } |
|
| 33 | + use ExtendedCacheItemPoolTrait; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @inheritDoc |
|
| 37 | + * @throws PhpfastcacheCoreException |
|
| 38 | + * @throws PhpfastcacheDriverException |
|
| 39 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 40 | + * @throws PhpfastcacheLogicException |
|
| 41 | + */ |
|
| 42 | + public function getItemsByTagsAsJsonString( |
|
| 43 | + array $tagNames, |
|
| 44 | + int $option = \JSON_THROW_ON_ERROR, |
|
| 45 | + int $depth = 512, |
|
| 46 | + int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE |
|
| 47 | + ): string { |
|
| 48 | + return \json_encode( |
|
| 49 | + \array_map( |
|
| 50 | + static fn(CacheItemInterface $item) => $item->get(), |
|
| 51 | + \array_values($this->getItemsByTags($tagNames, $strategy)) |
|
| 52 | + ), |
|
| 53 | + $option, |
|
| 54 | + $depth, |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @inheritDoc |
|
| 60 | + * @throws PhpfastcacheCoreException |
|
| 61 | + * @throws PhpfastcacheDriverException |
|
| 62 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 63 | + * @throws PhpfastcacheLogicException |
|
| 64 | + */ |
|
| 65 | + public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 66 | + { |
|
| 67 | + $items = []; |
|
| 68 | + foreach (\array_unique($tagNames) as $tagName) { |
|
| 69 | + $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $items = \array_merge([], ...$items); |
|
| 73 | + |
|
| 74 | + switch ($strategy) { |
|
| 75 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
| 76 | + foreach ($items as $key => $item) { |
|
| 77 | + if (\array_diff($tagNames, $item->getTags())) { |
|
| 78 | + unset($items[$key]); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + break; |
|
| 82 | + |
|
| 83 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
| 84 | + foreach ($items as $key => $item) { |
|
| 85 | + if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
| 86 | + unset($items[$key]); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + break; |
|
| 90 | + } |
|
| 91 | + return $items; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $tagName |
|
| 96 | + * @return ExtendedCacheItemInterface[] |
|
| 97 | + * @throws PhpfastcacheCoreException |
|
| 98 | + * @throws PhpfastcacheDriverException |
|
| 99 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | + * @throws PhpfastcacheLogicException |
|
| 101 | + */ |
|
| 102 | + protected function fetchItemsByTagFromBackend(string $tagName): array |
|
| 103 | + { |
|
| 104 | + $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
| 105 | + |
|
| 106 | + if ($driverResponse->isHit()) { |
|
| 107 | + $tagsItems = (array)$driverResponse->get(); |
|
| 108 | + /** |
|
| 109 | + * getItems() may provide expired item(s) |
|
| 110 | + * themselves provided by a cache of item |
|
| 111 | + * keys based stored the tag item. |
|
| 112 | + * Therefore, we pass a filter callback |
|
| 113 | + * to remove the expired Item(s) provided by |
|
| 114 | + * the item keys passed through getItems() |
|
| 115 | + * |
|
| 116 | + * #headache |
|
| 117 | + */ |
|
| 118 | + return \array_filter( |
|
| 119 | + $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
| 120 | + static fn (ExtendedCacheItemInterface $item) => $item->isHit(), |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + return []; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $key |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + protected function getTagKey(string $key): string |
|
| 131 | + { |
|
| 132 | + return TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX . $key; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @inheritDoc |
|
| 137 | + * @throws PhpfastcacheCoreException |
|
| 138 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 139 | + * @throws PhpfastcacheLogicException |
|
| 140 | + * @throws PhpfastcacheDriverException |
|
| 141 | + */ |
|
| 142 | + public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 143 | + { |
|
| 144 | + $return = true; |
|
| 145 | + |
|
| 146 | + foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
| 147 | + $result = $this->deleteItem($item->getKey()); |
|
| 148 | + if ($result !== true) { |
|
| 149 | + $return = $result; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @inheritDoc |
|
| 158 | + * @throws PhpfastcacheCoreException |
|
| 159 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 160 | + * @throws PhpfastcacheLogicException |
|
| 161 | + * @throws PhpfastcacheDriverException |
|
| 162 | + */ |
|
| 163 | + public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 164 | + { |
|
| 165 | + $return = true; |
|
| 166 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 167 | + $result = $this->deleteItem($item->getKey()); |
|
| 168 | + if ($result !== true) { |
|
| 169 | + $return = $result; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $return; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @inheritDoc |
|
| 178 | + * @throws PhpfastcacheCoreException |
|
| 179 | + * @throws PhpfastcacheDriverException |
|
| 180 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 181 | + * @throws PhpfastcacheLogicException |
|
| 182 | + */ |
|
| 183 | + public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 184 | + { |
|
| 185 | + $items = $this->fetchItemsByTagFromBackend($tagName); |
|
| 186 | + |
|
| 187 | + if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
| 188 | + foreach ($items as $key => $item) { |
|
| 189 | + if (\array_diff($item->getTags(), [$tagName])) { |
|
| 190 | + unset($items[$key]); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + return $items; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @inheritDoc |
|
| 199 | + * @throws PhpfastcacheCoreException |
|
| 200 | + * @throws PhpfastcacheDriverException |
|
| 201 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 202 | + * @throws PhpfastcacheLogicException |
|
| 203 | + * @throws \ReflectionException |
|
| 204 | + */ |
|
| 205 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 206 | + { |
|
| 207 | + $return = true; |
|
| 208 | + foreach ($tagNames as $tagName) { |
|
| 209 | + $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
| 210 | + if ($result !== true) { |
|
| 211 | + $return = $result; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return $return; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @inheritDoc |
|
| 220 | + * @throws PhpfastcacheCoreException |
|
| 221 | + * @throws PhpfastcacheDriverException |
|
| 222 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 223 | + * @throws PhpfastcacheLogicException |
|
| 224 | + * @throws \ReflectionException |
|
| 225 | + */ |
|
| 226 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 227 | + { |
|
| 228 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 229 | + $item->increment($step); |
|
| 230 | + $this->saveDeferred($item); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return $this->commit(); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @inheritDoc |
|
| 238 | + * @throws PhpfastcacheCoreException |
|
| 239 | + * @throws PhpfastcacheDriverException |
|
| 240 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 241 | + * @throws PhpfastcacheLogicException |
|
| 242 | + * @throws \ReflectionException |
|
| 243 | + */ |
|
| 244 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 245 | + { |
|
| 246 | + $return = true; |
|
| 247 | + foreach ($tagNames as $tagName) { |
|
| 248 | + $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
| 249 | + if ($result !== true) { |
|
| 250 | + $return = $result; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @inheritDoc |
|
| 259 | + * @throws PhpfastcacheCoreException |
|
| 260 | + * @throws PhpfastcacheDriverException |
|
| 261 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 262 | + * @throws PhpfastcacheLogicException |
|
| 263 | + * @throws \ReflectionException |
|
| 264 | + */ |
|
| 265 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 266 | + { |
|
| 267 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 268 | + $item->decrement($step); |
|
| 269 | + $this->saveDeferred($item); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return $this->commit(); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @inheritDoc |
|
| 277 | + * @throws PhpfastcacheCoreException |
|
| 278 | + * @throws PhpfastcacheDriverException |
|
| 279 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 280 | + * @throws PhpfastcacheLogicException |
|
| 281 | + * @throws \ReflectionException |
|
| 282 | + */ |
|
| 283 | + public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 284 | + { |
|
| 285 | + $return = true; |
|
| 286 | + foreach ($tagNames as $tagName) { |
|
| 287 | + $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
| 288 | + if ($result !== true) { |
|
| 289 | + $return = $result; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + return $return; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * @inheritDoc |
|
| 298 | + * @throws PhpfastcacheCoreException |
|
| 299 | + * @throws PhpfastcacheDriverException |
|
| 300 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 301 | + * @throws PhpfastcacheLogicException |
|
| 302 | + * @throws \ReflectionException |
|
| 303 | + */ |
|
| 304 | + public function appendItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 305 | + { |
|
| 306 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 307 | + $item->append($data); |
|
| 308 | + $this->saveDeferred($item); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + return $this->commit(); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @inheritDoc |
|
| 316 | + * @throws PhpfastcacheCoreException |
|
| 317 | + * @throws PhpfastcacheDriverException |
|
| 318 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 319 | + * @throws PhpfastcacheLogicException |
|
| 320 | + * @throws \ReflectionException |
|
| 321 | + */ |
|
| 322 | + public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 323 | + { |
|
| 324 | + $return = true; |
|
| 325 | + foreach ($tagNames as $tagName) { |
|
| 326 | + $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
| 327 | + if ($result !== true) { |
|
| 328 | + $return = $result; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return $return; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @inheritDoc |
|
| 337 | + * @throws PhpfastcacheCoreException |
|
| 338 | + * @throws PhpfastcacheDriverException |
|
| 339 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 340 | + * @throws PhpfastcacheLogicException |
|
| 341 | + * @throws \ReflectionException |
|
| 342 | + */ |
|
| 343 | + public function prependItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 344 | + { |
|
| 345 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 346 | + $item->prepend($data); |
|
| 347 | + $this->saveDeferred($item); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + return $this->commit(); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @param array<mixed> $wrapper |
|
| 355 | + * @return string[] |
|
| 356 | + */ |
|
| 357 | + protected function driverUnwrapTags(array $wrapper): array |
|
| 358 | + { |
|
| 359 | + return $wrapper[TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX]; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * @param ExtendedCacheItemInterface $item |
|
| 364 | + * @throws PhpfastcacheCoreException |
|
| 365 | + * @throws PhpfastcacheDriverException |
|
| 366 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 367 | + * @throws PhpfastcacheLogicException |
|
| 368 | + */ |
|
| 369 | + protected function cleanItemTags(ExtendedCacheItemInterface $item): void |
|
| 370 | + { |
|
| 371 | + if (!empty($item->getTags()) || !empty($item->getRemovedTags())) { |
|
| 372 | + $this->driverWriteTags($item->removeTags($item->getTags())); |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * @param ExtendedCacheItemInterface $item |
|
| 378 | + * @return bool |
|
| 379 | + * @throws PhpfastcacheCoreException |
|
| 380 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 381 | + * @throws PhpfastcacheLogicException |
|
| 382 | + * @throws PhpfastcacheDriverException |
|
| 383 | + */ |
|
| 384 | + protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
| 385 | + { |
|
| 386 | + /** |
|
| 387 | + * Do not attempt to write tags |
|
| 388 | + * on tags item, it can lead |
|
| 389 | + * to an infinite recursive calls |
|
| 390 | + */ |
|
| 391 | + if (str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 392 | + throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + if (!$item->getTags() && !$item->getRemovedTags()) { |
|
| 396 | + return true; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * @var $tagsItems ExtendedCacheItemInterface[] |
|
| 401 | + */ |
|
| 402 | + $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
| 403 | + |
|
| 404 | + foreach ($tagsItems as $tagsItem) { |
|
| 405 | + $data = $tagsItem->get(); |
|
| 406 | + $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Using the key will |
|
| 410 | + * avoid to use array_unique |
|
| 411 | + * that has slow performances |
|
| 412 | + */ |
|
| 413 | + $data = \array_merge((array)$data, [$item->getKey() => $expTimestamp]); |
|
| 414 | + $tagsItem->set($data); |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Recalculate the expiration date |
|
| 418 | + * |
|
| 419 | + * If the $tagsItem does not have |
|
| 420 | + * any cache item references left |
|
| 421 | + * then remove it from tagsItems index |
|
| 422 | + */ |
|
| 423 | + $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 424 | + |
|
| 425 | + $this->driverWrite($tagsItem); |
|
| 426 | + $tagsItem->setHit(true); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Also update removed tags to |
|
| 431 | + * keep the index up to date |
|
| 432 | + */ |
|
| 433 | + $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
| 434 | + |
|
| 435 | + foreach ($tagsItems as $tagsItem) { |
|
| 436 | + $data = (array)$tagsItem->get(); |
|
| 437 | + |
|
| 438 | + unset($data[$item->getKey()]); |
|
| 439 | + $tagsItem->set($data); |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Recalculate the expiration date |
|
| 443 | + * |
|
| 444 | + * If the $tagsItem does not have |
|
| 445 | + * any cache item references left |
|
| 446 | + * then remove it from tagsItems index |
|
| 447 | + */ |
|
| 448 | + if (\count($data)) { |
|
| 449 | + $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 450 | + $this->driverWrite($tagsItem); |
|
| 451 | + $tagsItem->setHit(true); |
|
| 452 | + } else { |
|
| 453 | + $this->deleteItem($tagsItem->getKey()); |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + return true; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * @param array<string> $keys |
|
| 462 | + * @return array<string> |
|
| 463 | + */ |
|
| 464 | + protected function getTagKeys(array $keys): array |
|
| 465 | + { |
|
| 466 | + return \array_map( |
|
| 467 | + fn (string $key) => $this->getTagKey($key), |
|
| 468 | + $keys |
|
| 469 | + ); |
|
| 470 | + } |
|
| 471 | 471 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |