@@ -29,73 +29,73 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class Driver implements ExtendedCacheItemPoolInterface |
| 31 | 31 | { |
| 32 | - use TaggableCacheItemPoolTrait; |
|
| 32 | + use TaggableCacheItemPoolTrait; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @return bool |
|
| 36 | - */ |
|
| 37 | - public function driverCheck(): bool |
|
| 38 | - { |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 34 | + /** |
|
| 35 | + * @return bool |
|
| 36 | + */ |
|
| 37 | + public function driverCheck(): bool |
|
| 38 | + { |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return DriverStatistic |
|
| 44 | - */ |
|
| 45 | - public function getStats(): DriverStatistic |
|
| 46 | - { |
|
| 47 | - $stat = new DriverStatistic(); |
|
| 48 | - $stat->setInfo('[Devnull] A void info string') |
|
| 49 | - ->setSize(0) |
|
| 50 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 51 | - ->setRawData(null); |
|
| 42 | + /** |
|
| 43 | + * @return DriverStatistic |
|
| 44 | + */ |
|
| 45 | + public function getStats(): DriverStatistic |
|
| 46 | + { |
|
| 47 | + $stat = new DriverStatistic(); |
|
| 48 | + $stat->setInfo('[Devnull] A void info string') |
|
| 49 | + ->setSize(0) |
|
| 50 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 51 | + ->setRawData(null); |
|
| 52 | 52 | |
| 53 | - return $stat; |
|
| 54 | - } |
|
| 53 | + return $stat; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param ExtendedCacheItemInterface $item |
|
| 58 | - * @return mixed |
|
| 59 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 60 | - */ |
|
| 61 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 62 | - { |
|
| 56 | + /** |
|
| 57 | + * @param ExtendedCacheItemInterface $item |
|
| 58 | + * @return mixed |
|
| 59 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 60 | + */ |
|
| 61 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 62 | + { |
|
| 63 | 63 | |
| 64 | - return true; |
|
| 65 | - } |
|
| 64 | + return true; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @param ExtendedCacheItemInterface $item |
|
| 69 | - * @return ?array<string, mixed> |
|
| 70 | - */ |
|
| 71 | - protected function driverRead(CacheItemInterface $item): ?array |
|
| 72 | - { |
|
| 73 | - return null; |
|
| 74 | - } |
|
| 67 | + /** |
|
| 68 | + * @param ExtendedCacheItemInterface $item |
|
| 69 | + * @return ?array<string, mixed> |
|
| 70 | + */ |
|
| 71 | + protected function driverRead(CacheItemInterface $item): ?array |
|
| 72 | + { |
|
| 73 | + return null; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param string $key |
|
| 78 | - * @param string $encodedKey |
|
| 79 | - * @return bool |
|
| 80 | - */ |
|
| 81 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 82 | - { |
|
| 83 | - return true; |
|
| 84 | - } |
|
| 76 | + /** |
|
| 77 | + * @param string $key |
|
| 78 | + * @param string $encodedKey |
|
| 79 | + * @return bool |
|
| 80 | + */ |
|
| 81 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 82 | + { |
|
| 83 | + return true; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * @return bool |
|
| 88 | - */ |
|
| 89 | - protected function driverClear(): bool |
|
| 90 | - { |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 86 | + /** |
|
| 87 | + * @return bool |
|
| 88 | + */ |
|
| 89 | + protected function driverClear(): bool |
|
| 90 | + { |
|
| 91 | + return true; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * @return bool |
|
| 96 | - */ |
|
| 97 | - protected function driverConnect(): bool |
|
| 98 | - { |
|
| 99 | - return true; |
|
| 100 | - } |
|
| 94 | + /** |
|
| 95 | + * @return bool |
|
| 96 | + */ |
|
| 97 | + protected function driverConnect(): bool |
|
| 98 | + { |
|
| 99 | + return true; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -31,98 +31,98 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class Driver implements ExtendedCacheItemPoolInterface |
| 33 | 33 | { |
| 34 | - use TaggableCacheItemPoolTrait; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var array<string, array<string, mixed>> |
|
| 38 | - */ |
|
| 39 | - protected array $staticStack = []; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - public function driverCheck(): bool |
|
| 45 | - { |
|
| 46 | - return true; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @return bool |
|
| 51 | - */ |
|
| 52 | - protected function driverConnect(): bool |
|
| 53 | - { |
|
| 54 | - return true; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param ExtendedCacheItemInterface $item |
|
| 59 | - * @return ?array<string, mixed> |
|
| 60 | - */ |
|
| 61 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 62 | - { |
|
| 63 | - return $this->staticStack[$item->getKey()] ?? null; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @return array<int, string> |
|
| 68 | - */ |
|
| 69 | - protected function driverReadAllKeys(string $pattern = ''): iterable |
|
| 70 | - { |
|
| 71 | - return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) { |
|
| 72 | - return $pattern |
|
| 73 | - ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key) |
|
| 74 | - : true; |
|
| 75 | - }); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param ExtendedCacheItemInterface $item |
|
| 80 | - * @return bool |
|
| 81 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 82 | - * @throws PhpfastcacheLogicException |
|
| 83 | - */ |
|
| 84 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 85 | - { |
|
| 86 | - |
|
| 87 | - $this->staticStack[$item->getKey()] = $this->driverPreWrap($item); |
|
| 88 | - return true; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param string $key |
|
| 93 | - * @param string $encodedKey |
|
| 94 | - * @return bool |
|
| 95 | - */ |
|
| 96 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 97 | - { |
|
| 98 | - if (isset($this->staticStack[$key])) { |
|
| 99 | - unset($this->staticStack[$key]); |
|
| 100 | - return true; |
|
| 101 | - } |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return bool |
|
| 107 | - */ |
|
| 108 | - protected function driverClear(): bool |
|
| 109 | - { |
|
| 110 | - unset($this->staticStack); |
|
| 111 | - $this->staticStack = []; |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @return DriverStatistic |
|
| 117 | - */ |
|
| 118 | - public function getStats(): DriverStatistic |
|
| 119 | - { |
|
| 120 | - $stat = new DriverStatistic(); |
|
| 121 | - $stat->setInfo('[Memstatic] A memory static driver') |
|
| 122 | - ->setSize(mb_strlen(serialize($this->staticStack))) |
|
| 123 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 124 | - ->setRawData($this->staticStack); |
|
| 125 | - |
|
| 126 | - return $stat; |
|
| 127 | - } |
|
| 34 | + use TaggableCacheItemPoolTrait; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var array<string, array<string, mixed>> |
|
| 38 | + */ |
|
| 39 | + protected array $staticStack = []; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + public function driverCheck(): bool |
|
| 45 | + { |
|
| 46 | + return true; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @return bool |
|
| 51 | + */ |
|
| 52 | + protected function driverConnect(): bool |
|
| 53 | + { |
|
| 54 | + return true; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param ExtendedCacheItemInterface $item |
|
| 59 | + * @return ?array<string, mixed> |
|
| 60 | + */ |
|
| 61 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 62 | + { |
|
| 63 | + return $this->staticStack[$item->getKey()] ?? null; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @return array<int, string> |
|
| 68 | + */ |
|
| 69 | + protected function driverReadAllKeys(string $pattern = ''): iterable |
|
| 70 | + { |
|
| 71 | + return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) { |
|
| 72 | + return $pattern |
|
| 73 | + ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key) |
|
| 74 | + : true; |
|
| 75 | + }); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param ExtendedCacheItemInterface $item |
|
| 80 | + * @return bool |
|
| 81 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 82 | + * @throws PhpfastcacheLogicException |
|
| 83 | + */ |
|
| 84 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 85 | + { |
|
| 86 | + |
|
| 87 | + $this->staticStack[$item->getKey()] = $this->driverPreWrap($item); |
|
| 88 | + return true; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param string $key |
|
| 93 | + * @param string $encodedKey |
|
| 94 | + * @return bool |
|
| 95 | + */ |
|
| 96 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 97 | + { |
|
| 98 | + if (isset($this->staticStack[$key])) { |
|
| 99 | + unset($this->staticStack[$key]); |
|
| 100 | + return true; |
|
| 101 | + } |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return bool |
|
| 107 | + */ |
|
| 108 | + protected function driverClear(): bool |
|
| 109 | + { |
|
| 110 | + unset($this->staticStack); |
|
| 111 | + $this->staticStack = []; |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @return DriverStatistic |
|
| 117 | + */ |
|
| 118 | + public function getStats(): DriverStatistic |
|
| 119 | + { |
|
| 120 | + $stat = new DriverStatistic(); |
|
| 121 | + $stat->setInfo('[Memstatic] A memory static driver') |
|
| 122 | + ->setSize(mb_strlen(serialize($this->staticStack))) |
|
| 123 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 124 | + ->setRawData($this->staticStack); |
|
| 125 | + |
|
| 126 | + return $stat; |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -68,7 +68,7 @@ |
||
| 68 | 68 | */ |
| 69 | 69 | protected function driverReadAllKeys(string $pattern = ''): iterable |
| 70 | 70 | { |
| 71 | - return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) { |
|
| 71 | + return array_filter(array_keys($this->staticStack), function(string $key) use ($pattern) { |
|
| 72 | 72 | return $pattern |
| 73 | 73 | ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key) |
| 74 | 74 | : true; |
@@ -30,115 +30,115 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class Driver implements AggregatablePoolInterface |
| 32 | 32 | { |
| 33 | - use RedisDriverTrait, TaggableCacheItemPoolTrait { |
|
| 34 | - RedisDriverTrait::driverReadMultiple insteadof TaggableCacheItemPoolTrait; |
|
| 35 | - RedisDriverTrait::driverDeleteMultiple insteadof TaggableCacheItemPoolTrait; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @return bool |
|
| 41 | - */ |
|
| 42 | - public function driverCheck(): bool |
|
| 43 | - { |
|
| 44 | - return extension_loaded('Redis') && class_exists(RedisClient::class); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @return DriverStatistic |
|
| 49 | - */ |
|
| 50 | - public function getStats(): DriverStatistic |
|
| 51 | - { |
|
| 52 | - // used_memory |
|
| 53 | - $info = $this->instance->info(); |
|
| 54 | - $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
| 55 | - |
|
| 56 | - return (new DriverStatistic()) |
|
| 57 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 58 | - ->setRawData($info) |
|
| 59 | - ->setSize((int)$info['used_memory_dataset']) |
|
| 60 | - ->setInfo( |
|
| 61 | - sprintf( |
|
| 62 | - "The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData.", |
|
| 63 | - $info['redis_version'], |
|
| 64 | - \phpversion("redis"), |
|
| 65 | - $date->format(DATE_RFC2822) |
|
| 66 | - ) |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return bool |
|
| 72 | - * @throws PhpfastcacheLogicException |
|
| 73 | - */ |
|
| 74 | - protected function driverConnect(): bool |
|
| 75 | - { |
|
| 76 | - if (isset($this->instance) && $this->instance instanceof RedisClient) { |
|
| 77 | - throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * In case of an user-provided |
|
| 82 | - * Redis client just return here |
|
| 83 | - */ |
|
| 84 | - if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
| 85 | - /** |
|
| 86 | - * Unlike Predis, we can't test if we're connected |
|
| 87 | - * or not, so let's just assume that we are |
|
| 88 | - */ |
|
| 89 | - $this->instance = $this->getConfig()->getRedisClient(); |
|
| 90 | - return true; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $this->instance = $this->instance ?? new RedisClient(); |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * If path is provided we consider it as a UNIX Socket |
|
| 97 | - */ |
|
| 98 | - if ($this->getConfig()->getPath()) { |
|
| 99 | - $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
| 100 | - } else { |
|
| 101 | - $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - if (!$isConnected && $this->getConfig()->getPath()) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if ($this->getConfig()->getOptPrefix()) { |
|
| 109 | - $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
| 113 | - return false; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - if ($this->getConfig()->getDatabase() !== null) { |
|
| 117 | - $this->instance->select($this->getConfig()->getDatabase()); |
|
| 118 | - } |
|
| 119 | - return true; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return array<int, string> |
|
| 124 | - * @throws \RedisException |
|
| 125 | - */ |
|
| 126 | - protected function driverReadAllKeys(string $pattern = '*'): iterable |
|
| 127 | - { |
|
| 128 | - $i = -1; |
|
| 129 | - $keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
|
| 130 | - if (is_iterable($keys)) { |
|
| 131 | - return $keys; |
|
| 132 | - } else { |
|
| 133 | - return []; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - protected function driverClear(): bool |
|
| 141 | - { |
|
| 142 | - return $this->instance->flushDB(); |
|
| 143 | - } |
|
| 33 | + use RedisDriverTrait, TaggableCacheItemPoolTrait { |
|
| 34 | + RedisDriverTrait::driverReadMultiple insteadof TaggableCacheItemPoolTrait; |
|
| 35 | + RedisDriverTrait::driverDeleteMultiple insteadof TaggableCacheItemPoolTrait; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @return bool |
|
| 41 | + */ |
|
| 42 | + public function driverCheck(): bool |
|
| 43 | + { |
|
| 44 | + return extension_loaded('Redis') && class_exists(RedisClient::class); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @return DriverStatistic |
|
| 49 | + */ |
|
| 50 | + public function getStats(): DriverStatistic |
|
| 51 | + { |
|
| 52 | + // used_memory |
|
| 53 | + $info = $this->instance->info(); |
|
| 54 | + $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
| 55 | + |
|
| 56 | + return (new DriverStatistic()) |
|
| 57 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 58 | + ->setRawData($info) |
|
| 59 | + ->setSize((int)$info['used_memory_dataset']) |
|
| 60 | + ->setInfo( |
|
| 61 | + sprintf( |
|
| 62 | + "The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData.", |
|
| 63 | + $info['redis_version'], |
|
| 64 | + \phpversion("redis"), |
|
| 65 | + $date->format(DATE_RFC2822) |
|
| 66 | + ) |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return bool |
|
| 72 | + * @throws PhpfastcacheLogicException |
|
| 73 | + */ |
|
| 74 | + protected function driverConnect(): bool |
|
| 75 | + { |
|
| 76 | + if (isset($this->instance) && $this->instance instanceof RedisClient) { |
|
| 77 | + throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * In case of an user-provided |
|
| 82 | + * Redis client just return here |
|
| 83 | + */ |
|
| 84 | + if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
| 85 | + /** |
|
| 86 | + * Unlike Predis, we can't test if we're connected |
|
| 87 | + * or not, so let's just assume that we are |
|
| 88 | + */ |
|
| 89 | + $this->instance = $this->getConfig()->getRedisClient(); |
|
| 90 | + return true; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $this->instance = $this->instance ?? new RedisClient(); |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * If path is provided we consider it as a UNIX Socket |
|
| 97 | + */ |
|
| 98 | + if ($this->getConfig()->getPath()) { |
|
| 99 | + $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
| 100 | + } else { |
|
| 101 | + $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + if (!$isConnected && $this->getConfig()->getPath()) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if ($this->getConfig()->getOptPrefix()) { |
|
| 109 | + $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
| 113 | + return false; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + if ($this->getConfig()->getDatabase() !== null) { |
|
| 117 | + $this->instance->select($this->getConfig()->getDatabase()); |
|
| 118 | + } |
|
| 119 | + return true; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @return array<int, string> |
|
| 124 | + * @throws \RedisException |
|
| 125 | + */ |
|
| 126 | + protected function driverReadAllKeys(string $pattern = '*'): iterable |
|
| 127 | + { |
|
| 128 | + $i = -1; |
|
| 129 | + $keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
|
| 130 | + if (is_iterable($keys)) { |
|
| 131 | + return $keys; |
|
| 132 | + } else { |
|
| 133 | + return []; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + protected function driverClear(): bool |
|
| 141 | + { |
|
| 142 | + return $this->instance->flushDB(); |
|
| 143 | + } |
|
| 144 | 144 | } |
@@ -56,7 +56,7 @@ |
||
| 56 | 56 | return (new DriverStatistic()) |
| 57 | 57 | ->setData(implode(', ', array_keys($this->itemInstances))) |
| 58 | 58 | ->setRawData($info) |
| 59 | - ->setSize((int)$info['used_memory_dataset']) |
|
| 59 | + ->setSize((int) $info['used_memory_dataset']) |
|
| 60 | 60 | ->setInfo( |
| 61 | 61 | sprintf( |
| 62 | 62 | "The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData.", |
@@ -97,7 +97,8 @@ discard block |
||
| 97 | 97 | */ |
| 98 | 98 | if ($this->getConfig()->getPath()) { |
| 99 | 99 | $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
| 100 | - } else { |
|
| 100 | + } |
|
| 101 | + else { |
|
| 101 | 102 | $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
| 102 | 103 | } |
| 103 | 104 | |
@@ -129,7 +130,8 @@ discard block |
||
| 129 | 130 | $keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
| 130 | 131 | if (is_iterable($keys)) { |
| 131 | 132 | return $keys; |
| 132 | - } else { |
|
| 133 | + } |
|
| 134 | + else { |
|
| 133 | 135 | return []; |
| 134 | 136 | } |
| 135 | 137 | } |
@@ -28,76 +28,76 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | trait RedisDriverTrait |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * @param ExtendedCacheItemInterface $item |
|
| 33 | - * @return ?array<string, mixed> |
|
| 34 | - */ |
|
| 35 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 36 | - { |
|
| 37 | - $val = $this->instance->get($item->getKey()); |
|
| 38 | - if (!$val) { |
|
| 39 | - return null; |
|
| 40 | - } |
|
| 31 | + /** |
|
| 32 | + * @param ExtendedCacheItemInterface $item |
|
| 33 | + * @return ?array<string, mixed> |
|
| 34 | + */ |
|
| 35 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
| 36 | + { |
|
| 37 | + $val = $this->instance->get($item->getKey()); |
|
| 38 | + if (!$val) { |
|
| 39 | + return null; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - return $this->decode($val); |
|
| 43 | - } |
|
| 42 | + return $this->decode($val); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemInterface ...$items |
|
| 47 | - * @return array<array<string, mixed>> |
|
| 48 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
| 49 | - * @throws \RedisException |
|
| 50 | - */ |
|
| 51 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
| 52 | - { |
|
| 53 | - $keys = $this->getKeys($items); |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemInterface ...$items |
|
| 47 | + * @return array<array<string, mixed>> |
|
| 48 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
| 49 | + * @throws \RedisException |
|
| 50 | + */ |
|
| 51 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
| 52 | + { |
|
| 53 | + $keys = $this->getKeys($items); |
|
| 54 | 54 | |
| 55 | - return array_combine($keys, array_map( |
|
| 56 | - fn($val) => $val ? $this->decode($val) : null, |
|
| 57 | - $this->instance->mGet($keys) |
|
| 58 | - )); |
|
| 59 | - } |
|
| 55 | + return array_combine($keys, array_map( |
|
| 56 | + fn($val) => $val ? $this->decode($val) : null, |
|
| 57 | + $this->instance->mGet($keys) |
|
| 58 | + )); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param ExtendedCacheItemInterface $item |
|
| 63 | - * @return mixed |
|
| 64 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 65 | - * @throws PhpfastcacheLogicException |
|
| 66 | - */ |
|
| 67 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 68 | - { |
|
| 69 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
| 61 | + /** |
|
| 62 | + * @param ExtendedCacheItemInterface $item |
|
| 63 | + * @return mixed |
|
| 64 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 65 | + * @throws PhpfastcacheLogicException |
|
| 66 | + */ |
|
| 67 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
| 68 | + { |
|
| 69 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
| 70 | 70 | |
| 71 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 71 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * @see https://redis.io/commands/setex |
|
| 75 | - * @see https://redis.io/commands/expire |
|
| 76 | - */ |
|
| 77 | - if ($ttl <= 0) { |
|
| 78 | - return $this->instance->expire($item->getKey(), 0); |
|
| 79 | - } |
|
| 73 | + /** |
|
| 74 | + * @see https://redis.io/commands/setex |
|
| 75 | + * @see https://redis.io/commands/expire |
|
| 76 | + */ |
|
| 77 | + if ($ttl <= 0) { |
|
| 78 | + return $this->instance->expire($item->getKey(), 0); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
| 82 | - } |
|
| 81 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @param string $key |
|
| 86 | - * @param string $encodedKey |
|
| 87 | - * @return bool |
|
| 88 | - * @throws \RedisException |
|
| 89 | - */ |
|
| 90 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
| 91 | - { |
|
| 92 | - return (bool) $this->instance->del($key); |
|
| 93 | - } |
|
| 84 | + /** |
|
| 85 | + * @param string $key |
|
| 86 | + * @param string $encodedKey |
|
| 87 | + * @return bool |
|
| 88 | + * @throws \RedisException |
|
| 89 | + */ |
|
| 90 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
| 91 | + { |
|
| 92 | + return (bool) $this->instance->del($key); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @param string[] $keys |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - protected function driverDeleteMultiple(array $keys): bool |
|
| 100 | - { |
|
| 101 | - return (bool) $this->instance->del(...$keys); |
|
| 102 | - } |
|
| 95 | + /** |
|
| 96 | + * @param string[] $keys |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + protected function driverDeleteMultiple(array $keys): bool |
|
| 100 | + { |
|
| 101 | + return (bool) $this->instance->del(...$keys); |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -22,140 +22,140 @@ |
||
| 22 | 22 | |
| 23 | 23 | class Config extends ConfigurationOption |
| 24 | 24 | { |
| 25 | - protected string $host = '127.0.0.1'; |
|
| 26 | - protected int $port = 6379; |
|
| 27 | - protected string $password = ''; |
|
| 28 | - protected int $database = 0; |
|
| 29 | - protected int $timeout = 5; |
|
| 30 | - protected ?RedisClient $redisClient = null; |
|
| 31 | - protected string $optPrefix = ''; |
|
| 25 | + protected string $host = '127.0.0.1'; |
|
| 26 | + protected int $port = 6379; |
|
| 27 | + protected string $password = ''; |
|
| 28 | + protected int $database = 0; |
|
| 29 | + protected int $timeout = 5; |
|
| 30 | + protected ?RedisClient $redisClient = null; |
|
| 31 | + protected string $optPrefix = ''; |
|
| 32 | 32 | /** |
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function getHost(): string |
|
| 36 | - { |
|
| 37 | - return $this->host; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param string $host |
|
| 42 | - * @return static |
|
| 43 | - * @throws PhpfastcacheLogicException |
|
| 44 | - */ |
|
| 45 | - public function setHost(string $host): static |
|
| 46 | - { |
|
| 47 | - return $this->setProperty('host', $host); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @return int |
|
| 52 | - */ |
|
| 53 | - public function getPort(): int |
|
| 54 | - { |
|
| 55 | - return $this->port; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param int $port |
|
| 60 | - * @return static |
|
| 61 | - * @throws PhpfastcacheLogicException |
|
| 62 | - */ |
|
| 63 | - public function setPort(int $port): static |
|
| 64 | - { |
|
| 65 | - return $this->setProperty('port', $port); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function getPassword(): string |
|
| 72 | - { |
|
| 73 | - return $this->password; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $password |
|
| 78 | - * |
|
| 79 | - * @return static |
|
| 80 | - * @throws PhpfastcacheLogicException |
|
| 81 | - */ |
|
| 82 | - public function setPassword(string $password): static |
|
| 83 | - { |
|
| 84 | - return $this->setProperty('password', $password); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return null|int |
|
| 89 | - */ |
|
| 90 | - public function getDatabase(): ?int |
|
| 91 | - { |
|
| 92 | - return $this->database; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param int|null $database |
|
| 97 | - * |
|
| 98 | - * @return static |
|
| 99 | - * @throws PhpfastcacheLogicException |
|
| 100 | - */ |
|
| 101 | - public function setDatabase(int $database = null): static |
|
| 102 | - { |
|
| 103 | - return $this->setProperty('database', $database); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return int |
|
| 108 | - */ |
|
| 109 | - public function getTimeout(): int |
|
| 110 | - { |
|
| 111 | - return $this->timeout; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param int $timeout |
|
| 116 | - * @return static |
|
| 117 | - * @throws PhpfastcacheLogicException |
|
| 118 | - */ |
|
| 119 | - public function setTimeout(int $timeout): static |
|
| 120 | - { |
|
| 121 | - return $this->setProperty('timeout', $timeout); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return RedisClient|null |
|
| 126 | - */ |
|
| 127 | - public function getRedisClient(): ?RedisClient |
|
| 128 | - { |
|
| 129 | - return $this->redisClient; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @param RedisClient|null $redisClient |
|
| 134 | - * @return Config |
|
| 135 | - * @throws PhpfastcacheLogicException |
|
| 136 | - */ |
|
| 137 | - public function setRedisClient(?RedisClient $redisClient): Config |
|
| 138 | - { |
|
| 139 | - return $this->setProperty('redisClient', $redisClient); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return string |
|
| 144 | - * @since 7.0.2 |
|
| 145 | - */ |
|
| 146 | - public function getOptPrefix(): string |
|
| 147 | - { |
|
| 148 | - return $this->optPrefix; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $optPrefix |
|
| 153 | - * @return Config |
|
| 154 | - * @throws PhpfastcacheLogicException |
|
| 155 | - * @since 7.0.2 |
|
| 156 | - */ |
|
| 157 | - public function setOptPrefix(string $optPrefix): Config |
|
| 158 | - { |
|
| 159 | - return $this->setProperty('optPrefix', trim($optPrefix)); |
|
| 160 | - } |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function getHost(): string |
|
| 36 | + { |
|
| 37 | + return $this->host; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param string $host |
|
| 42 | + * @return static |
|
| 43 | + * @throws PhpfastcacheLogicException |
|
| 44 | + */ |
|
| 45 | + public function setHost(string $host): static |
|
| 46 | + { |
|
| 47 | + return $this->setProperty('host', $host); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @return int |
|
| 52 | + */ |
|
| 53 | + public function getPort(): int |
|
| 54 | + { |
|
| 55 | + return $this->port; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param int $port |
|
| 60 | + * @return static |
|
| 61 | + * @throws PhpfastcacheLogicException |
|
| 62 | + */ |
|
| 63 | + public function setPort(int $port): static |
|
| 64 | + { |
|
| 65 | + return $this->setProperty('port', $port); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function getPassword(): string |
|
| 72 | + { |
|
| 73 | + return $this->password; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $password |
|
| 78 | + * |
|
| 79 | + * @return static |
|
| 80 | + * @throws PhpfastcacheLogicException |
|
| 81 | + */ |
|
| 82 | + public function setPassword(string $password): static |
|
| 83 | + { |
|
| 84 | + return $this->setProperty('password', $password); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return null|int |
|
| 89 | + */ |
|
| 90 | + public function getDatabase(): ?int |
|
| 91 | + { |
|
| 92 | + return $this->database; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param int|null $database |
|
| 97 | + * |
|
| 98 | + * @return static |
|
| 99 | + * @throws PhpfastcacheLogicException |
|
| 100 | + */ |
|
| 101 | + public function setDatabase(int $database = null): static |
|
| 102 | + { |
|
| 103 | + return $this->setProperty('database', $database); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return int |
|
| 108 | + */ |
|
| 109 | + public function getTimeout(): int |
|
| 110 | + { |
|
| 111 | + return $this->timeout; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param int $timeout |
|
| 116 | + * @return static |
|
| 117 | + * @throws PhpfastcacheLogicException |
|
| 118 | + */ |
|
| 119 | + public function setTimeout(int $timeout): static |
|
| 120 | + { |
|
| 121 | + return $this->setProperty('timeout', $timeout); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return RedisClient|null |
|
| 126 | + */ |
|
| 127 | + public function getRedisClient(): ?RedisClient |
|
| 128 | + { |
|
| 129 | + return $this->redisClient; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @param RedisClient|null $redisClient |
|
| 134 | + * @return Config |
|
| 135 | + * @throws PhpfastcacheLogicException |
|
| 136 | + */ |
|
| 137 | + public function setRedisClient(?RedisClient $redisClient): Config |
|
| 138 | + { |
|
| 139 | + return $this->setProperty('redisClient', $redisClient); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return string |
|
| 144 | + * @since 7.0.2 |
|
| 145 | + */ |
|
| 146 | + public function getOptPrefix(): string |
|
| 147 | + { |
|
| 148 | + return $this->optPrefix; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $optPrefix |
|
| 153 | + * @return Config |
|
| 154 | + * @throws PhpfastcacheLogicException |
|
| 155 | + * @since 7.0.2 |
|
| 156 | + */ |
|
| 157 | + public function setOptPrefix(string $optPrefix): Config |
|
| 158 | + { |
|
| 159 | + return $this->setProperty('optPrefix', trim($optPrefix)); |
|
| 160 | + } |
|
| 161 | 161 | } |
@@ -24,172 +24,172 @@ |
||
| 24 | 24 | |
| 25 | 25 | class EventManager implements EventManagerInterface |
| 26 | 26 | { |
| 27 | - use UninstanciableObjectTrait; |
|
| 28 | - |
|
| 29 | - public const ON_EVERY_EVENT = '__every'; |
|
| 30 | - |
|
| 31 | - protected static EventManagerInterface $instance; |
|
| 32 | - |
|
| 33 | - protected bool $isScopedEventManager = false; |
|
| 34 | - |
|
| 35 | - protected ?ExtendedCacheItemPoolInterface $itemPoolContext = null; |
|
| 36 | - |
|
| 37 | - /** @var array<string, array<string, callable>> */ |
|
| 38 | - protected array $events = [ |
|
| 39 | - self::ON_EVERY_EVENT => [] |
|
| 40 | - ]; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @return EventManagerInterface |
|
| 44 | - */ |
|
| 45 | - public static function getInstance(): EventManagerInterface |
|
| 46 | - { |
|
| 47 | - return (self::$instance ?? self::$instance = new static()); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param EventManagerInterface $eventManagerInstance |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public static function setInstance(EventManagerInterface $eventManagerInstance): void |
|
| 55 | - { |
|
| 56 | - self::$instance = $eventManagerInstance; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function dispatch(string $eventName, ...$args): void |
|
| 60 | - { |
|
| 61 | - /** |
|
| 62 | - * Replace array_key_exists by isset |
|
| 63 | - * due to performance issue on huge |
|
| 64 | - * loop dispatching operations |
|
| 65 | - */ |
|
| 66 | - if (isset($this->events[$eventName]) && $eventName !== self::ON_EVERY_EVENT) { |
|
| 67 | - $loopArgs = array_merge($args, [$eventName]); |
|
| 68 | - foreach ($this->events[$eventName] as $event) { |
|
| 69 | - /** |
|
| 70 | - * @todo V10: BC Break: Put eventName as first parameter (like self::ON_EVERY_EVENT) |
|
| 71 | - */ |
|
| 72 | - $event(...$loopArgs); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - foreach ($this->events[self::ON_EVERY_EVENT] as $event) { |
|
| 76 | - $event($eventName, ...$args); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @inheritDoc |
|
| 82 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 83 | - * @throws PhpfastcacheEventManagerException |
|
| 84 | - */ |
|
| 85 | - public function __call(string $name, array $arguments): void |
|
| 86 | - { |
|
| 87 | - if (\str_starts_with($name, 'on')) { |
|
| 88 | - $name = \substr($name, 2); |
|
| 89 | - if (\is_callable($arguments[0])) { |
|
| 90 | - if (isset($arguments[1]) && \is_string($arguments[0])) { |
|
| 91 | - $this->events[$name][$arguments[1]] = $arguments[0]; |
|
| 92 | - } else { |
|
| 93 | - $this->events[$name][] = $arguments[0]; |
|
| 94 | - } |
|
| 95 | - } else { |
|
| 96 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[0]))); |
|
| 97 | - } |
|
| 98 | - } else { |
|
| 99 | - throw new PhpfastcacheEventManagerException('An event must start with "on" such as "onCacheGetItem"'); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param callable $callback |
|
| 105 | - * @param string $callbackName |
|
| 106 | - * @throws PhpfastcacheEventManagerException |
|
| 107 | - */ |
|
| 108 | - public function onEveryEvents(callable $callback, string $callbackName): void |
|
| 109 | - { |
|
| 110 | - if ($callbackName === '') { |
|
| 111 | - throw new PhpfastcacheEventManagerException('Callbackname cannot be empty'); |
|
| 112 | - } |
|
| 113 | - $this->events[self::ON_EVERY_EVENT][$callbackName] = $callback; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @throws PhpfastcacheEventManagerException |
|
| 119 | - */ |
|
| 120 | - public function on(array|string $events, callable $callback): void |
|
| 121 | - { |
|
| 122 | - if (is_string($events)) { |
|
| 123 | - $events = [$events]; |
|
| 124 | - } |
|
| 125 | - foreach ($events as $event) { |
|
| 126 | - if (!\preg_match('#^([a-zA-Z])*$#', $event)) { |
|
| 127 | - throw new PhpfastcacheEventManagerException(\sprintf('Invalid event name "%s"', $event)); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $this->{'on' . \ucfirst($event)}($callback); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param string $eventName |
|
| 136 | - * @param string $callbackName |
|
| 137 | - * @return bool |
|
| 138 | - */ |
|
| 139 | - public function unbindEventCallback(string $eventName, string $callbackName): bool |
|
| 140 | - { |
|
| 141 | - $return = isset($this->events[$eventName][$callbackName]); |
|
| 142 | - unset($this->events[$eventName][$callbackName]); |
|
| 143 | - |
|
| 144 | - return $return; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @return bool |
|
| 149 | - */ |
|
| 150 | - public function unbindAllEventCallbacks(): bool |
|
| 151 | - { |
|
| 152 | - $this->events = [ |
|
| 153 | - self::ON_EVERY_EVENT => [] |
|
| 154 | - ]; |
|
| 155 | - |
|
| 156 | - return true; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function __clone(): void |
|
| 160 | - { |
|
| 161 | - $this->isScopedEventManager = true; |
|
| 162 | - $this->unbindAllEventCallbacks(); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param ExtendedCacheItemPoolInterface $pool |
|
| 167 | - * @return EventManagerInterface |
|
| 168 | - * @throws PhpfastcacheEventManagerException |
|
| 169 | - */ |
|
| 170 | - public function getScopedEventManager(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
| 171 | - { |
|
| 172 | - return (clone $this)->setItemPoolContext($pool); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param ExtendedCacheItemPoolInterface $pool |
|
| 177 | - * @return EventManagerInterface |
|
| 178 | - * @throws PhpfastcacheEventManagerException |
|
| 179 | - */ |
|
| 180 | - public function setItemPoolContext(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
| 181 | - { |
|
| 182 | - if (!$this->isScopedEventManager) { |
|
| 183 | - throw new PhpfastcacheEventManagerException('Cannot set itemPool context on unscoped event manager instance.'); |
|
| 184 | - } |
|
| 185 | - $this->itemPoolContext = $pool; |
|
| 186 | - |
|
| 187 | - $this->onEveryEvents(function (string $eventName, ...$args) { |
|
| 188 | - EventManager::getInstance()->dispatch($eventName, ...$args); |
|
| 189 | - }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); |
|
| 190 | - |
|
| 191 | - return $this; |
|
| 192 | - } |
|
| 27 | + use UninstanciableObjectTrait; |
|
| 28 | + |
|
| 29 | + public const ON_EVERY_EVENT = '__every'; |
|
| 30 | + |
|
| 31 | + protected static EventManagerInterface $instance; |
|
| 32 | + |
|
| 33 | + protected bool $isScopedEventManager = false; |
|
| 34 | + |
|
| 35 | + protected ?ExtendedCacheItemPoolInterface $itemPoolContext = null; |
|
| 36 | + |
|
| 37 | + /** @var array<string, array<string, callable>> */ |
|
| 38 | + protected array $events = [ |
|
| 39 | + self::ON_EVERY_EVENT => [] |
|
| 40 | + ]; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @return EventManagerInterface |
|
| 44 | + */ |
|
| 45 | + public static function getInstance(): EventManagerInterface |
|
| 46 | + { |
|
| 47 | + return (self::$instance ?? self::$instance = new static()); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param EventManagerInterface $eventManagerInstance |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public static function setInstance(EventManagerInterface $eventManagerInstance): void |
|
| 55 | + { |
|
| 56 | + self::$instance = $eventManagerInstance; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function dispatch(string $eventName, ...$args): void |
|
| 60 | + { |
|
| 61 | + /** |
|
| 62 | + * Replace array_key_exists by isset |
|
| 63 | + * due to performance issue on huge |
|
| 64 | + * loop dispatching operations |
|
| 65 | + */ |
|
| 66 | + if (isset($this->events[$eventName]) && $eventName !== self::ON_EVERY_EVENT) { |
|
| 67 | + $loopArgs = array_merge($args, [$eventName]); |
|
| 68 | + foreach ($this->events[$eventName] as $event) { |
|
| 69 | + /** |
|
| 70 | + * @todo V10: BC Break: Put eventName as first parameter (like self::ON_EVERY_EVENT) |
|
| 71 | + */ |
|
| 72 | + $event(...$loopArgs); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + foreach ($this->events[self::ON_EVERY_EVENT] as $event) { |
|
| 76 | + $event($eventName, ...$args); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @inheritDoc |
|
| 82 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 83 | + * @throws PhpfastcacheEventManagerException |
|
| 84 | + */ |
|
| 85 | + public function __call(string $name, array $arguments): void |
|
| 86 | + { |
|
| 87 | + if (\str_starts_with($name, 'on')) { |
|
| 88 | + $name = \substr($name, 2); |
|
| 89 | + if (\is_callable($arguments[0])) { |
|
| 90 | + if (isset($arguments[1]) && \is_string($arguments[0])) { |
|
| 91 | + $this->events[$name][$arguments[1]] = $arguments[0]; |
|
| 92 | + } else { |
|
| 93 | + $this->events[$name][] = $arguments[0]; |
|
| 94 | + } |
|
| 95 | + } else { |
|
| 96 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[0]))); |
|
| 97 | + } |
|
| 98 | + } else { |
|
| 99 | + throw new PhpfastcacheEventManagerException('An event must start with "on" such as "onCacheGetItem"'); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param callable $callback |
|
| 105 | + * @param string $callbackName |
|
| 106 | + * @throws PhpfastcacheEventManagerException |
|
| 107 | + */ |
|
| 108 | + public function onEveryEvents(callable $callback, string $callbackName): void |
|
| 109 | + { |
|
| 110 | + if ($callbackName === '') { |
|
| 111 | + throw new PhpfastcacheEventManagerException('Callbackname cannot be empty'); |
|
| 112 | + } |
|
| 113 | + $this->events[self::ON_EVERY_EVENT][$callbackName] = $callback; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @throws PhpfastcacheEventManagerException |
|
| 119 | + */ |
|
| 120 | + public function on(array|string $events, callable $callback): void |
|
| 121 | + { |
|
| 122 | + if (is_string($events)) { |
|
| 123 | + $events = [$events]; |
|
| 124 | + } |
|
| 125 | + foreach ($events as $event) { |
|
| 126 | + if (!\preg_match('#^([a-zA-Z])*$#', $event)) { |
|
| 127 | + throw new PhpfastcacheEventManagerException(\sprintf('Invalid event name "%s"', $event)); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $this->{'on' . \ucfirst($event)}($callback); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param string $eventName |
|
| 136 | + * @param string $callbackName |
|
| 137 | + * @return bool |
|
| 138 | + */ |
|
| 139 | + public function unbindEventCallback(string $eventName, string $callbackName): bool |
|
| 140 | + { |
|
| 141 | + $return = isset($this->events[$eventName][$callbackName]); |
|
| 142 | + unset($this->events[$eventName][$callbackName]); |
|
| 143 | + |
|
| 144 | + return $return; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @return bool |
|
| 149 | + */ |
|
| 150 | + public function unbindAllEventCallbacks(): bool |
|
| 151 | + { |
|
| 152 | + $this->events = [ |
|
| 153 | + self::ON_EVERY_EVENT => [] |
|
| 154 | + ]; |
|
| 155 | + |
|
| 156 | + return true; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function __clone(): void |
|
| 160 | + { |
|
| 161 | + $this->isScopedEventManager = true; |
|
| 162 | + $this->unbindAllEventCallbacks(); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param ExtendedCacheItemPoolInterface $pool |
|
| 167 | + * @return EventManagerInterface |
|
| 168 | + * @throws PhpfastcacheEventManagerException |
|
| 169 | + */ |
|
| 170 | + public function getScopedEventManager(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
| 171 | + { |
|
| 172 | + return (clone $this)->setItemPoolContext($pool); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param ExtendedCacheItemPoolInterface $pool |
|
| 177 | + * @return EventManagerInterface |
|
| 178 | + * @throws PhpfastcacheEventManagerException |
|
| 179 | + */ |
|
| 180 | + public function setItemPoolContext(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
| 181 | + { |
|
| 182 | + if (!$this->isScopedEventManager) { |
|
| 183 | + throw new PhpfastcacheEventManagerException('Cannot set itemPool context on unscoped event manager instance.'); |
|
| 184 | + } |
|
| 185 | + $this->itemPoolContext = $pool; |
|
| 186 | + |
|
| 187 | + $this->onEveryEvents(function (string $eventName, ...$args) { |
|
| 188 | + EventManager::getInstance()->dispatch($eventName, ...$args); |
|
| 189 | + }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); |
|
| 190 | + |
|
| 191 | + return $this; |
|
| 192 | + } |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | // phpcs:disable |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | /** |
| 118 | 118 | * @throws PhpfastcacheEventManagerException |
| 119 | 119 | */ |
| 120 | - public function on(array|string $events, callable $callback): void |
|
| 120 | + public function on(array | string $events, callable $callback): void |
|
| 121 | 121 | { |
| 122 | 122 | if (is_string($events)) { |
| 123 | 123 | $events = [$events]; |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | $this->itemPoolContext = $pool; |
| 186 | 186 | |
| 187 | - $this->onEveryEvents(function (string $eventName, ...$args) { |
|
| 187 | + $this->onEveryEvents(function(string $eventName, ...$args) { |
|
| 188 | 188 | EventManager::getInstance()->dispatch($eventName, ...$args); |
| 189 | 189 | }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); |
| 190 | 190 | |
@@ -89,13 +89,16 @@ |
||
| 89 | 89 | if (\is_callable($arguments[0])) { |
| 90 | 90 | if (isset($arguments[1]) && \is_string($arguments[0])) { |
| 91 | 91 | $this->events[$name][$arguments[1]] = $arguments[0]; |
| 92 | - } else { |
|
| 92 | + } |
|
| 93 | + else { |
|
| 93 | 94 | $this->events[$name][] = $arguments[0]; |
| 94 | 95 | } |
| 95 | - } else { |
|
| 96 | + } |
|
| 97 | + else { |
|
| 96 | 98 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[0]))); |
| 97 | 99 | } |
| 98 | - } else { |
|
| 100 | + } |
|
| 101 | + else { |
|
| 99 | 102 | throw new PhpfastcacheEventManagerException('An event must start with "on" such as "onCacheGetItem"'); |
| 100 | 103 | } |
| 101 | 104 | } |
@@ -21,102 +21,102 @@ |
||
| 21 | 21 | |
| 22 | 22 | interface TaggableCacheItemInterface |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
| 26 | - * Default behavior |
|
| 27 | - */ |
|
| 28 | - public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
| 32 | - * The cache item *CAN* have additional tag(s) |
|
| 33 | - */ |
|
| 34 | - public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
| 38 | - * The cache item *CANNOT* have additional tag(s) |
|
| 39 | - */ |
|
| 40 | - public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param string $tagName |
|
| 44 | - * |
|
| 45 | - * @return ExtendedCacheItemInterface |
|
| 46 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 47 | - */ |
|
| 48 | - public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param string[] $tagNames |
|
| 52 | - * |
|
| 53 | - * @return ExtendedCacheItemInterface |
|
| 54 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 55 | - */ |
|
| 56 | - public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param string $tagName |
|
| 60 | - * |
|
| 61 | - * @return bool |
|
| 62 | - */ |
|
| 63 | - public function hasTag(string $tagName): bool; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return bool |
|
| 67 | - */ |
|
| 68 | - public function isTagged(): bool; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param string[] $tagNames |
|
| 72 | - * @param int $strategy |
|
| 73 | - * @return bool |
|
| 74 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 75 | - */ |
|
| 76 | - public function hasTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param string[] $tags |
|
| 80 | - * |
|
| 81 | - * @return ExtendedCacheItemInterface |
|
| 82 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 83 | - */ |
|
| 84 | - public function setTags(array $tags): ExtendedCacheItemInterface; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return string[] |
|
| 88 | - */ |
|
| 89 | - public function getTags(): array; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param string $separator |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function getTagsAsString(string $separator = ', '): string; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $tagName |
|
| 100 | - * |
|
| 101 | - * @return ExtendedCacheItemInterface |
|
| 102 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 103 | - */ |
|
| 104 | - public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param string[] $tagNames |
|
| 108 | - * |
|
| 109 | - * @return ExtendedCacheItemInterface |
|
| 110 | - */ |
|
| 111 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return string[] |
|
| 115 | - */ |
|
| 116 | - public function getRemovedTags(): array; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return ExtendedCacheItemInterface |
|
| 120 | - */ |
|
| 121 | - public function clearRemovedTags(): ExtendedCacheItemInterface; |
|
| 24 | + /** |
|
| 25 | + * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
| 26 | + * Default behavior |
|
| 27 | + */ |
|
| 28 | + public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
| 32 | + * The cache item *CAN* have additional tag(s) |
|
| 33 | + */ |
|
| 34 | + public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
| 38 | + * The cache item *CANNOT* have additional tag(s) |
|
| 39 | + */ |
|
| 40 | + public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param string $tagName |
|
| 44 | + * |
|
| 45 | + * @return ExtendedCacheItemInterface |
|
| 46 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 47 | + */ |
|
| 48 | + public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param string[] $tagNames |
|
| 52 | + * |
|
| 53 | + * @return ExtendedCacheItemInterface |
|
| 54 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 55 | + */ |
|
| 56 | + public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param string $tagName |
|
| 60 | + * |
|
| 61 | + * @return bool |
|
| 62 | + */ |
|
| 63 | + public function hasTag(string $tagName): bool; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return bool |
|
| 67 | + */ |
|
| 68 | + public function isTagged(): bool; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param string[] $tagNames |
|
| 72 | + * @param int $strategy |
|
| 73 | + * @return bool |
|
| 74 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 75 | + */ |
|
| 76 | + public function hasTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param string[] $tags |
|
| 80 | + * |
|
| 81 | + * @return ExtendedCacheItemInterface |
|
| 82 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 83 | + */ |
|
| 84 | + public function setTags(array $tags): ExtendedCacheItemInterface; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return string[] |
|
| 88 | + */ |
|
| 89 | + public function getTags(): array; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param string $separator |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function getTagsAsString(string $separator = ', '): string; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $tagName |
|
| 100 | + * |
|
| 101 | + * @return ExtendedCacheItemInterface |
|
| 102 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 103 | + */ |
|
| 104 | + public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param string[] $tagNames |
|
| 108 | + * |
|
| 109 | + * @return ExtendedCacheItemInterface |
|
| 110 | + */ |
|
| 111 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return string[] |
|
| 115 | + */ |
|
| 116 | + public function getRemovedTags(): array; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return ExtendedCacheItemInterface |
|
| 120 | + */ |
|
| 121 | + public function clearRemovedTags(): ExtendedCacheItemInterface; |
|
| 122 | 122 | } |
@@ -22,171 +22,171 @@ |
||
| 22 | 22 | |
| 23 | 23 | trait TaggableCacheItemTrait |
| 24 | 24 | { |
| 25 | - use ExtendedCacheItemTrait; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var string[] |
|
| 29 | - */ |
|
| 30 | - protected array $tags = []; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var string[] |
|
| 34 | - */ |
|
| 35 | - protected array $removedTags = []; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param string[] $tagNames |
|
| 39 | - * @return ExtendedCacheItemInterface |
|
| 40 | - */ |
|
| 41 | - public function addTags(array $tagNames): ExtendedCacheItemInterface |
|
| 42 | - { |
|
| 43 | - foreach ($tagNames as $tagName) { |
|
| 44 | - $this->addTag($tagName); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - return $this; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param string $tagName |
|
| 52 | - * @return ExtendedCacheItemInterface |
|
| 53 | - */ |
|
| 54 | - public function addTag(string $tagName): ExtendedCacheItemInterface |
|
| 55 | - { |
|
| 56 | - $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
|
| 57 | - |
|
| 58 | - return $this; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param string $tagName |
|
| 63 | - * |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - public function hasTag(string $tagName): bool |
|
| 67 | - { |
|
| 68 | - return \in_array($tagName, $this->tags, true); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return bool |
|
| 73 | - */ |
|
| 74 | - public function isTagged(): bool |
|
| 75 | - { |
|
| 76 | - return !empty($this->tags); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param string[] $tagNames |
|
| 81 | - * @param int $strategy |
|
| 82 | - * @return bool |
|
| 83 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 84 | - */ |
|
| 85 | - public function hasTags(array $tagNames, int $strategy = TaggableCacheItemInterface::TAG_STRATEGY_ONE): bool |
|
| 86 | - { |
|
| 87 | - return match ($strategy) { |
|
| 88 | - TaggableCacheItemInterface::TAG_STRATEGY_ONE => !empty(array_intersect($tagNames, $this->tags)), |
|
| 89 | - TaggableCacheItemInterface::TAG_STRATEGY_ALL => empty(\array_diff($tagNames, $this->tags)), |
|
| 90 | - TaggableCacheItemInterface::TAG_STRATEGY_ONLY => empty(\array_diff($tagNames, $this->tags)) && empty(\array_diff($this->tags, $tagNames)), |
|
| 91 | - default => throw new PhpfastcacheInvalidArgumentException('Invalid strategy constant provided.'), |
|
| 92 | - }; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param string[] $tags |
|
| 98 | - * @return ExtendedCacheItemInterface |
|
| 99 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | - */ |
|
| 101 | - public function setTags(array $tags): ExtendedCacheItemInterface |
|
| 102 | - { |
|
| 103 | - if ($tags === [] || \array_filter($tags, 'is_string')) { |
|
| 104 | - $this->tags = $tags; |
|
| 105 | - } else { |
|
| 106 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $this; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @return string[] |
|
| 114 | - */ |
|
| 115 | - public function getTags(): array |
|
| 116 | - { |
|
| 117 | - return $this->tags; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param string $separator |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function getTagsAsString(string $separator = ', '): string |
|
| 125 | - { |
|
| 126 | - return \implode($separator, $this->tags); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param string[] $tagNames |
|
| 131 | - * @return ExtendedCacheItemInterface |
|
| 132 | - */ |
|
| 133 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface |
|
| 134 | - { |
|
| 135 | - foreach ($tagNames as $tagName) { |
|
| 136 | - $this->removeTag($tagName); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $this; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param string $tagName |
|
| 144 | - * @return ExtendedCacheItemInterface |
|
| 145 | - */ |
|
| 146 | - public function removeTag(string $tagName): ExtendedCacheItemInterface |
|
| 147 | - { |
|
| 148 | - if (($key = \array_search($tagName, $this->tags, true)) !== false) { |
|
| 149 | - unset($this->tags[$key]); |
|
| 150 | - $this->removedTags[] = $tagName; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $this; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return string[] |
|
| 158 | - */ |
|
| 159 | - public function getRemovedTags(): array |
|
| 160 | - { |
|
| 161 | - return \array_diff($this->removedTags, $this->tags); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @return ExtendedCacheItemInterface |
|
| 166 | - */ |
|
| 167 | - public function clearRemovedTags(): ExtendedCacheItemInterface |
|
| 168 | - { |
|
| 169 | - $this->removedTags = []; |
|
| 170 | - |
|
| 171 | - return $this; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @throws PhpfastcacheLogicException |
|
| 176 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 177 | - */ |
|
| 178 | - public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void |
|
| 179 | - { |
|
| 180 | - $itemTarget->setEventManager($this->getEventManager()) |
|
| 181 | - ->set($this->_getData()) |
|
| 182 | - ->setHit($this->isHit()) |
|
| 183 | - ->setTags($this->getTags()) |
|
| 184 | - ->expiresAt(clone $this->getExpirationDate()) |
|
| 185 | - ->setDriver($itemPoolTarget ?? $this->driver); |
|
| 186 | - |
|
| 187 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 188 | - $itemTarget->setCreationDate(clone $this->getCreationDate()) |
|
| 189 | - ->setModificationDate(clone $this->getModificationDate()); |
|
| 190 | - } |
|
| 191 | - } |
|
| 25 | + use ExtendedCacheItemTrait; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var string[] |
|
| 29 | + */ |
|
| 30 | + protected array $tags = []; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var string[] |
|
| 34 | + */ |
|
| 35 | + protected array $removedTags = []; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param string[] $tagNames |
|
| 39 | + * @return ExtendedCacheItemInterface |
|
| 40 | + */ |
|
| 41 | + public function addTags(array $tagNames): ExtendedCacheItemInterface |
|
| 42 | + { |
|
| 43 | + foreach ($tagNames as $tagName) { |
|
| 44 | + $this->addTag($tagName); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + return $this; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param string $tagName |
|
| 52 | + * @return ExtendedCacheItemInterface |
|
| 53 | + */ |
|
| 54 | + public function addTag(string $tagName): ExtendedCacheItemInterface |
|
| 55 | + { |
|
| 56 | + $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
|
| 57 | + |
|
| 58 | + return $this; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param string $tagName |
|
| 63 | + * |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + public function hasTag(string $tagName): bool |
|
| 67 | + { |
|
| 68 | + return \in_array($tagName, $this->tags, true); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return bool |
|
| 73 | + */ |
|
| 74 | + public function isTagged(): bool |
|
| 75 | + { |
|
| 76 | + return !empty($this->tags); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param string[] $tagNames |
|
| 81 | + * @param int $strategy |
|
| 82 | + * @return bool |
|
| 83 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 84 | + */ |
|
| 85 | + public function hasTags(array $tagNames, int $strategy = TaggableCacheItemInterface::TAG_STRATEGY_ONE): bool |
|
| 86 | + { |
|
| 87 | + return match ($strategy) { |
|
| 88 | + TaggableCacheItemInterface::TAG_STRATEGY_ONE => !empty(array_intersect($tagNames, $this->tags)), |
|
| 89 | + TaggableCacheItemInterface::TAG_STRATEGY_ALL => empty(\array_diff($tagNames, $this->tags)), |
|
| 90 | + TaggableCacheItemInterface::TAG_STRATEGY_ONLY => empty(\array_diff($tagNames, $this->tags)) && empty(\array_diff($this->tags, $tagNames)), |
|
| 91 | + default => throw new PhpfastcacheInvalidArgumentException('Invalid strategy constant provided.'), |
|
| 92 | + }; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param string[] $tags |
|
| 98 | + * @return ExtendedCacheItemInterface |
|
| 99 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | + */ |
|
| 101 | + public function setTags(array $tags): ExtendedCacheItemInterface |
|
| 102 | + { |
|
| 103 | + if ($tags === [] || \array_filter($tags, 'is_string')) { |
|
| 104 | + $this->tags = $tags; |
|
| 105 | + } else { |
|
| 106 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $this; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @return string[] |
|
| 114 | + */ |
|
| 115 | + public function getTags(): array |
|
| 116 | + { |
|
| 117 | + return $this->tags; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param string $separator |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function getTagsAsString(string $separator = ', '): string |
|
| 125 | + { |
|
| 126 | + return \implode($separator, $this->tags); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param string[] $tagNames |
|
| 131 | + * @return ExtendedCacheItemInterface |
|
| 132 | + */ |
|
| 133 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface |
|
| 134 | + { |
|
| 135 | + foreach ($tagNames as $tagName) { |
|
| 136 | + $this->removeTag($tagName); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $this; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param string $tagName |
|
| 144 | + * @return ExtendedCacheItemInterface |
|
| 145 | + */ |
|
| 146 | + public function removeTag(string $tagName): ExtendedCacheItemInterface |
|
| 147 | + { |
|
| 148 | + if (($key = \array_search($tagName, $this->tags, true)) !== false) { |
|
| 149 | + unset($this->tags[$key]); |
|
| 150 | + $this->removedTags[] = $tagName; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $this; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return string[] |
|
| 158 | + */ |
|
| 159 | + public function getRemovedTags(): array |
|
| 160 | + { |
|
| 161 | + return \array_diff($this->removedTags, $this->tags); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @return ExtendedCacheItemInterface |
|
| 166 | + */ |
|
| 167 | + public function clearRemovedTags(): ExtendedCacheItemInterface |
|
| 168 | + { |
|
| 169 | + $this->removedTags = []; |
|
| 170 | + |
|
| 171 | + return $this; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @throws PhpfastcacheLogicException |
|
| 176 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 177 | + */ |
|
| 178 | + public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void |
|
| 179 | + { |
|
| 180 | + $itemTarget->setEventManager($this->getEventManager()) |
|
| 181 | + ->set($this->_getData()) |
|
| 182 | + ->setHit($this->isHit()) |
|
| 183 | + ->setTags($this->getTags()) |
|
| 184 | + ->expiresAt(clone $this->getExpirationDate()) |
|
| 185 | + ->setDriver($itemPoolTarget ?? $this->driver); |
|
| 186 | + |
|
| 187 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 188 | + $itemTarget->setCreationDate(clone $this->getCreationDate()) |
|
| 189 | + ->setModificationDate(clone $this->getModificationDate()); |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | 192 | } |
@@ -102,7 +102,8 @@ |
||
| 102 | 102 | { |
| 103 | 103 | if ($tags === [] || \array_filter($tags, 'is_string')) { |
| 104 | 104 | $this->tags = $tags; |
| 105 | - } else { |
|
| 105 | + } |
|
| 106 | + else { |
|
| 106 | 107 | throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
| 107 | 108 | } |
| 108 | 109 | |
@@ -26,282 +26,282 @@ |
||
| 26 | 26 | |
| 27 | 27 | trait ExtendedCacheItemTrait |
| 28 | 28 | { |
| 29 | - use CacheItemTrait; |
|
| 30 | - |
|
| 31 | - protected ExtendedCacheItemPoolInterface $driver; |
|
| 32 | - |
|
| 33 | - protected string $encodedKey; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Item constructor. |
|
| 37 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 38 | - * @param string $key |
|
| 39 | - * @param EventManagerInterface $em |
|
| 40 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 41 | - */ |
|
| 42 | - public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) |
|
| 43 | - { |
|
| 44 | - $this->data = null; |
|
| 45 | - $this->key = $key; |
|
| 46 | - $this->setEventManager($em); |
|
| 47 | - $this->setDriver($driver); |
|
| 48 | - if ($driver->getConfig()->isUseStaticItemCaching()) { |
|
| 49 | - $this->driver->setItem($this); |
|
| 50 | - } |
|
| 51 | - $this->expirationDate = new DateTime(); |
|
| 52 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 53 | - $this->creationDate = new DateTime(); |
|
| 54 | - $this->modificationDate = new DateTime(); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 60 | - * @return ExtendedCacheItemInterface |
|
| 61 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 62 | - */ |
|
| 63 | - public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface |
|
| 64 | - { |
|
| 65 | - $driverClass = $this->getDriverClass(); |
|
| 66 | - if ($driver instanceof $driverClass) { |
|
| 67 | - $this->driver = $driver; |
|
| 68 | - |
|
| 69 | - return $this; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid driver instance "%s" for cache item "%s"', $driver::class, static::class)); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @inheritDoc |
|
| 77 | - */ |
|
| 78 | - public function getEncodedKey(): string |
|
| 79 | - { |
|
| 80 | - if (!isset($this->encodedKey)) { |
|
| 81 | - $this->encodedKey = $this->driver->getEncodedKey($this->getKey()); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return $this->encodedKey; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @inheritDoc |
|
| 89 | - * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
| 90 | - */ |
|
| 91 | - public function _getData(): mixed // @phpcs:ignore |
|
| 92 | - { |
|
| 93 | - return $this->data; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @inheritDoc |
|
| 98 | - */ |
|
| 99 | - public function getExpirationDate(): DateTimeInterface |
|
| 100 | - { |
|
| 101 | - return $this->expirationDate; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @inheritDoc |
|
| 106 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 107 | - */ |
|
| 108 | - public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
| 109 | - { |
|
| 110 | - return $this->expiresAt($expiration); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @inheritDoc |
|
| 115 | - * @throws PhpfastcacheLogicException |
|
| 116 | - */ |
|
| 117 | - public function getCreationDate(): DateTimeInterface |
|
| 118 | - { |
|
| 119 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 120 | - return $this->creationDate; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @inheritDoc |
|
| 128 | - * @throws PhpfastcacheLogicException |
|
| 129 | - */ |
|
| 130 | - public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 131 | - { |
|
| 132 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 133 | - $this->creationDate = $this->demutateDatetime($date); |
|
| 134 | - return $this; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @inheritDoc |
|
| 142 | - * @throws PhpfastcacheLogicException |
|
| 143 | - */ |
|
| 144 | - public function getModificationDate(): DateTimeInterface |
|
| 145 | - { |
|
| 146 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 147 | - return $this->modificationDate; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @inheritDoc |
|
| 155 | - * @throws PhpfastcacheLogicException |
|
| 156 | - */ |
|
| 157 | - public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 158 | - { |
|
| 159 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 160 | - $this->modificationDate = $this->demutateDatetime($date); |
|
| 161 | - return $this; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - public function getTtl(): int |
|
| 168 | - { |
|
| 169 | - return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - public function isExpired(): bool |
|
| 173 | - { |
|
| 174 | - return $this->getTtl() <= 0; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - public function isNull(): bool |
|
| 178 | - { |
|
| 179 | - return $this->get() === null; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - public function isEmpty(): bool |
|
| 183 | - { |
|
| 184 | - return empty($this->get()); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Return the data length: |
|
| 189 | - * Either the string length if it's a string (binary mode) |
|
| 190 | - * # or the number of element (count) if it's an array |
|
| 191 | - * # or the number returned by count() if it's an object implementing \Countable interface |
|
| 192 | - * # -1 for anything else |
|
| 193 | - * @return int |
|
| 194 | - */ |
|
| 195 | - public function getLength(): int |
|
| 196 | - { |
|
| 197 | - switch (\gettype($this->data)) { |
|
| 198 | - case 'array': |
|
| 199 | - case 'object': |
|
| 200 | - if (\is_countable($this->data)) { |
|
| 201 | - return \count($this->data); |
|
| 202 | - } |
|
| 203 | - break; |
|
| 204 | - |
|
| 205 | - case 'string': |
|
| 206 | - return \strlen($this->data); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return -1; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @throws PhpfastcacheInvalidTypeException |
|
| 214 | - */ |
|
| 215 | - public function increment(int $step = 1): ExtendedCacheItemInterface |
|
| 216 | - { |
|
| 217 | - if ($this->data !== null && !\is_numeric($this->data)) { |
|
| 218 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot increment on a "%s" type.', \gettype($this->data))); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $this->data += $step; |
|
| 222 | - |
|
| 223 | - return $this; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @throws PhpfastcacheInvalidTypeException |
|
| 228 | - */ |
|
| 229 | - public function decrement(int $step = 1): ExtendedCacheItemInterface |
|
| 230 | - { |
|
| 231 | - if ($this->data !== null && !\is_numeric($this->data)) { |
|
| 232 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot decrement on a "%s" type.', \gettype($this->data))); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $this->data -= $step; |
|
| 236 | - |
|
| 237 | - return $this; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @throws PhpfastcacheInvalidTypeException |
|
| 242 | - */ |
|
| 243 | - public function append(array|string $data): ExtendedCacheItemInterface |
|
| 244 | - { |
|
| 245 | - if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
| 246 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot append on a "%s" type.', \gettype($this->data))); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if (\is_array($this->data)) { |
|
| 250 | - $this->data[] = $data; |
|
| 251 | - } else { |
|
| 252 | - $this->data .= $data; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $this; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * @throws PhpfastcacheInvalidTypeException |
|
| 260 | - */ |
|
| 261 | - public function prepend(array|string $data): ExtendedCacheItemInterface |
|
| 262 | - { |
|
| 263 | - if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
| 264 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot prepend on a "%s" type.', \gettype($this->data))); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - if (\is_array($this->data)) { |
|
| 268 | - \array_unshift($this->data, $data); |
|
| 269 | - } else { |
|
| 270 | - $this->data = $data . $this->data; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return $this; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Return the data as a well-formatted string. |
|
| 278 | - * Any scalar value will be cast to an array |
|
| 279 | - * @param int $options \json_encode() options |
|
| 280 | - * @param int $depth \json_encode() depth |
|
| 281 | - * @return string |
|
| 282 | - */ |
|
| 283 | - public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string |
|
| 284 | - { |
|
| 285 | - $data = $this->get(); |
|
| 286 | - |
|
| 287 | - if (\is_object($data) || \is_array($data)) { |
|
| 288 | - $data = \json_encode($data, $options, $depth); |
|
| 289 | - } else { |
|
| 290 | - $data = \json_encode([$data], $options, $depth); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - return \json_encode($data, $options, $depth); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - public function jsonSerialize(): mixed |
|
| 297 | - { |
|
| 298 | - return $this->get(); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
| 302 | - { |
|
| 303 | - return $driverPool::getClassNamespace() === self::getClassNamespace(); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - abstract protected function getDriverClass(): string; |
|
| 29 | + use CacheItemTrait; |
|
| 30 | + |
|
| 31 | + protected ExtendedCacheItemPoolInterface $driver; |
|
| 32 | + |
|
| 33 | + protected string $encodedKey; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Item constructor. |
|
| 37 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 38 | + * @param string $key |
|
| 39 | + * @param EventManagerInterface $em |
|
| 40 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 41 | + */ |
|
| 42 | + public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) |
|
| 43 | + { |
|
| 44 | + $this->data = null; |
|
| 45 | + $this->key = $key; |
|
| 46 | + $this->setEventManager($em); |
|
| 47 | + $this->setDriver($driver); |
|
| 48 | + if ($driver->getConfig()->isUseStaticItemCaching()) { |
|
| 49 | + $this->driver->setItem($this); |
|
| 50 | + } |
|
| 51 | + $this->expirationDate = new DateTime(); |
|
| 52 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 53 | + $this->creationDate = new DateTime(); |
|
| 54 | + $this->modificationDate = new DateTime(); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 60 | + * @return ExtendedCacheItemInterface |
|
| 61 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 62 | + */ |
|
| 63 | + public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface |
|
| 64 | + { |
|
| 65 | + $driverClass = $this->getDriverClass(); |
|
| 66 | + if ($driver instanceof $driverClass) { |
|
| 67 | + $this->driver = $driver; |
|
| 68 | + |
|
| 69 | + return $this; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid driver instance "%s" for cache item "%s"', $driver::class, static::class)); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @inheritDoc |
|
| 77 | + */ |
|
| 78 | + public function getEncodedKey(): string |
|
| 79 | + { |
|
| 80 | + if (!isset($this->encodedKey)) { |
|
| 81 | + $this->encodedKey = $this->driver->getEncodedKey($this->getKey()); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return $this->encodedKey; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @inheritDoc |
|
| 89 | + * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
| 90 | + */ |
|
| 91 | + public function _getData(): mixed // @phpcs:ignore |
|
| 92 | + { |
|
| 93 | + return $this->data; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @inheritDoc |
|
| 98 | + */ |
|
| 99 | + public function getExpirationDate(): DateTimeInterface |
|
| 100 | + { |
|
| 101 | + return $this->expirationDate; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @inheritDoc |
|
| 106 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 107 | + */ |
|
| 108 | + public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
| 109 | + { |
|
| 110 | + return $this->expiresAt($expiration); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @inheritDoc |
|
| 115 | + * @throws PhpfastcacheLogicException |
|
| 116 | + */ |
|
| 117 | + public function getCreationDate(): DateTimeInterface |
|
| 118 | + { |
|
| 119 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 120 | + return $this->creationDate; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @inheritDoc |
|
| 128 | + * @throws PhpfastcacheLogicException |
|
| 129 | + */ |
|
| 130 | + public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 131 | + { |
|
| 132 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 133 | + $this->creationDate = $this->demutateDatetime($date); |
|
| 134 | + return $this; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @inheritDoc |
|
| 142 | + * @throws PhpfastcacheLogicException |
|
| 143 | + */ |
|
| 144 | + public function getModificationDate(): DateTimeInterface |
|
| 145 | + { |
|
| 146 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 147 | + return $this->modificationDate; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @inheritDoc |
|
| 155 | + * @throws PhpfastcacheLogicException |
|
| 156 | + */ |
|
| 157 | + public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
| 158 | + { |
|
| 159 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
| 160 | + $this->modificationDate = $this->demutateDatetime($date); |
|
| 161 | + return $this; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + public function getTtl(): int |
|
| 168 | + { |
|
| 169 | + return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + public function isExpired(): bool |
|
| 173 | + { |
|
| 174 | + return $this->getTtl() <= 0; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + public function isNull(): bool |
|
| 178 | + { |
|
| 179 | + return $this->get() === null; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + public function isEmpty(): bool |
|
| 183 | + { |
|
| 184 | + return empty($this->get()); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Return the data length: |
|
| 189 | + * Either the string length if it's a string (binary mode) |
|
| 190 | + * # or the number of element (count) if it's an array |
|
| 191 | + * # or the number returned by count() if it's an object implementing \Countable interface |
|
| 192 | + * # -1 for anything else |
|
| 193 | + * @return int |
|
| 194 | + */ |
|
| 195 | + public function getLength(): int |
|
| 196 | + { |
|
| 197 | + switch (\gettype($this->data)) { |
|
| 198 | + case 'array': |
|
| 199 | + case 'object': |
|
| 200 | + if (\is_countable($this->data)) { |
|
| 201 | + return \count($this->data); |
|
| 202 | + } |
|
| 203 | + break; |
|
| 204 | + |
|
| 205 | + case 'string': |
|
| 206 | + return \strlen($this->data); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return -1; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @throws PhpfastcacheInvalidTypeException |
|
| 214 | + */ |
|
| 215 | + public function increment(int $step = 1): ExtendedCacheItemInterface |
|
| 216 | + { |
|
| 217 | + if ($this->data !== null && !\is_numeric($this->data)) { |
|
| 218 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot increment on a "%s" type.', \gettype($this->data))); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $this->data += $step; |
|
| 222 | + |
|
| 223 | + return $this; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @throws PhpfastcacheInvalidTypeException |
|
| 228 | + */ |
|
| 229 | + public function decrement(int $step = 1): ExtendedCacheItemInterface |
|
| 230 | + { |
|
| 231 | + if ($this->data !== null && !\is_numeric($this->data)) { |
|
| 232 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot decrement on a "%s" type.', \gettype($this->data))); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $this->data -= $step; |
|
| 236 | + |
|
| 237 | + return $this; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @throws PhpfastcacheInvalidTypeException |
|
| 242 | + */ |
|
| 243 | + public function append(array|string $data): ExtendedCacheItemInterface |
|
| 244 | + { |
|
| 245 | + if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
| 246 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot append on a "%s" type.', \gettype($this->data))); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if (\is_array($this->data)) { |
|
| 250 | + $this->data[] = $data; |
|
| 251 | + } else { |
|
| 252 | + $this->data .= $data; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $this; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * @throws PhpfastcacheInvalidTypeException |
|
| 260 | + */ |
|
| 261 | + public function prepend(array|string $data): ExtendedCacheItemInterface |
|
| 262 | + { |
|
| 263 | + if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
| 264 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot prepend on a "%s" type.', \gettype($this->data))); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + if (\is_array($this->data)) { |
|
| 268 | + \array_unshift($this->data, $data); |
|
| 269 | + } else { |
|
| 270 | + $this->data = $data . $this->data; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return $this; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Return the data as a well-formatted string. |
|
| 278 | + * Any scalar value will be cast to an array |
|
| 279 | + * @param int $options \json_encode() options |
|
| 280 | + * @param int $depth \json_encode() depth |
|
| 281 | + * @return string |
|
| 282 | + */ |
|
| 283 | + public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string |
|
| 284 | + { |
|
| 285 | + $data = $this->get(); |
|
| 286 | + |
|
| 287 | + if (\is_object($data) || \is_array($data)) { |
|
| 288 | + $data = \json_encode($data, $options, $depth); |
|
| 289 | + } else { |
|
| 290 | + $data = \json_encode([$data], $options, $depth); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + return \json_encode($data, $options, $depth); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + public function jsonSerialize(): mixed |
|
| 297 | + { |
|
| 298 | + return $this->get(); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
| 302 | + { |
|
| 303 | + return $driverPool::getClassNamespace() === self::getClassNamespace(); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + abstract protected function getDriverClass(): string; |
|
| 307 | 307 | } |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | /** |
| 241 | 241 | * @throws PhpfastcacheInvalidTypeException |
| 242 | 242 | */ |
| 243 | - public function append(array|string $data): ExtendedCacheItemInterface |
|
| 243 | + public function append(array | string $data): ExtendedCacheItemInterface |
|
| 244 | 244 | { |
| 245 | 245 | if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
| 246 | 246 | throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot append on a "%s" type.', \gettype($this->data))); |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | /** |
| 259 | 259 | * @throws PhpfastcacheInvalidTypeException |
| 260 | 260 | */ |
| 261 | - public function prepend(array|string $data): ExtendedCacheItemInterface |
|
| 261 | + public function prepend(array | string $data): ExtendedCacheItemInterface |
|
| 262 | 262 | { |
| 263 | 263 | if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
| 264 | 264 | throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot prepend on a "%s" type.', \gettype($this->data))); |
@@ -39,8 +39,7 @@ discard block |
||
| 39 | 39 | * @param EventManagerInterface $em |
| 40 | 40 | * @throws PhpfastcacheInvalidArgumentException |
| 41 | 41 | */ |
| 42 | - public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) |
|
| 43 | - { |
|
| 42 | + public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) { |
|
| 44 | 43 | $this->data = null; |
| 45 | 44 | $this->key = $key; |
| 46 | 45 | $this->setEventManager($em); |
@@ -248,7 +247,8 @@ discard block |
||
| 248 | 247 | |
| 249 | 248 | if (\is_array($this->data)) { |
| 250 | 249 | $this->data[] = $data; |
| 251 | - } else { |
|
| 250 | + } |
|
| 251 | + else { |
|
| 252 | 252 | $this->data .= $data; |
| 253 | 253 | } |
| 254 | 254 | |
@@ -266,7 +266,8 @@ discard block |
||
| 266 | 266 | |
| 267 | 267 | if (\is_array($this->data)) { |
| 268 | 268 | \array_unshift($this->data, $data); |
| 269 | - } else { |
|
| 269 | + } |
|
| 270 | + else { |
|
| 270 | 271 | $this->data = $data . $this->data; |
| 271 | 272 | } |
| 272 | 273 | |
@@ -286,7 +287,8 @@ discard block |
||
| 286 | 287 | |
| 287 | 288 | if (\is_object($data) || \is_array($data)) { |
| 288 | 289 | $data = \json_encode($data, $options, $depth); |
| 289 | - } else { |
|
| 290 | + } |
|
| 291 | + else { |
|
| 290 | 292 | $data = \json_encode([$data], $options, $depth); |
| 291 | 293 | } |
| 292 | 294 | |