diff options
Diffstat (limited to 'vendor/symfony/cache/CacheItem.php')
| -rw-r--r-- | vendor/symfony/cache/CacheItem.php | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/vendor/symfony/cache/CacheItem.php b/vendor/symfony/cache/CacheItem.php new file mode 100644 index 0000000..1a81706 --- /dev/null +++ b/vendor/symfony/cache/CacheItem.php | |||
| @@ -0,0 +1,198 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | /* | ||
| 4 | * This file is part of the Symfony package. | ||
| 5 | * | ||
| 6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
| 7 | * | ||
| 8 | * For the full copyright and license information, please view the LICENSE | ||
| 9 | * file that was distributed with this source code. | ||
| 10 | */ | ||
| 11 | |||
| 12 | namespace Symfony\Component\Cache; | ||
| 13 | |||
| 14 | use Psr\Log\LoggerInterface; | ||
| 15 | use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
| 16 | use Symfony\Component\Cache\Exception\LogicException; | ||
| 17 | use Symfony\Contracts\Cache\ItemInterface; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @author Nicolas Grekas <p@tchwork.com> | ||
| 21 | */ | ||
| 22 | final class CacheItem implements ItemInterface | ||
| 23 | { | ||
| 24 | private const METADATA_EXPIRY_OFFSET = 1527506807; | ||
| 25 | private const VALUE_WRAPPER = "\xA9"; | ||
| 26 | |||
| 27 | protected string $key; | ||
| 28 | protected mixed $value = null; | ||
| 29 | protected bool $isHit = false; | ||
| 30 | protected float|int|null $expiry = null; | ||
| 31 | protected array $metadata = []; | ||
| 32 | protected array $newMetadata = []; | ||
| 33 | protected ?ItemInterface $innerItem = null; | ||
| 34 | protected ?string $poolHash = null; | ||
| 35 | protected bool $isTaggable = false; | ||
| 36 | |||
| 37 | public function getKey(): string | ||
| 38 | { | ||
| 39 | return $this->key; | ||
| 40 | } | ||
| 41 | |||
| 42 | public function get(): mixed | ||
| 43 | { | ||
| 44 | return $this->value; | ||
| 45 | } | ||
| 46 | |||
| 47 | public function isHit(): bool | ||
| 48 | { | ||
| 49 | return $this->isHit; | ||
| 50 | } | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @return $this | ||
| 54 | */ | ||
| 55 | public function set($value): static | ||
| 56 | { | ||
| 57 | $this->value = $value; | ||
| 58 | |||
| 59 | return $this; | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * @return $this | ||
| 64 | */ | ||
| 65 | public function expiresAt(?\DateTimeInterface $expiration): static | ||
| 66 | { | ||
| 67 | $this->expiry = null !== $expiration ? (float) $expiration->format('U.u') : null; | ||
| 68 | |||
| 69 | return $this; | ||
| 70 | } | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @return $this | ||
| 74 | */ | ||
| 75 | public function expiresAfter(mixed $time): static | ||
| 76 | { | ||
| 77 | if (null === $time) { | ||
| 78 | $this->expiry = null; | ||
| 79 | } elseif ($time instanceof \DateInterval) { | ||
| 80 | $this->expiry = microtime(true) + \DateTimeImmutable::createFromFormat('U', 0)->add($time)->format('U.u'); | ||
| 81 | } elseif (\is_int($time)) { | ||
| 82 | $this->expiry = $time + microtime(true); | ||
| 83 | } else { | ||
| 84 | throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given.', get_debug_type($time))); | ||
| 85 | } | ||
| 86 | |||
| 87 | return $this; | ||
| 88 | } | ||
| 89 | |||
| 90 | public function tag(mixed $tags): static | ||
| 91 | { | ||
| 92 | if (!$this->isTaggable) { | ||
| 93 | throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key)); | ||
| 94 | } | ||
| 95 | if (!\is_array($tags) && !$tags instanceof \Traversable) { // don't use is_iterable(), it's slow | ||
| 96 | $tags = [$tags]; | ||
| 97 | } | ||
| 98 | foreach ($tags as $tag) { | ||
| 99 | if (!\is_string($tag) && !$tag instanceof \Stringable) { | ||
| 100 | throw new InvalidArgumentException(sprintf('Cache tag must be string or object that implements __toString(), "%s" given.', get_debug_type($tag))); | ||
| 101 | } | ||
| 102 | $tag = (string) $tag; | ||
| 103 | if (isset($this->newMetadata[self::METADATA_TAGS][$tag])) { | ||
| 104 | continue; | ||
| 105 | } | ||
| 106 | if ('' === $tag) { | ||
| 107 | throw new InvalidArgumentException('Cache tag length must be greater than zero.'); | ||
| 108 | } | ||
| 109 | if (false !== strpbrk($tag, self::RESERVED_CHARACTERS)) { | ||
| 110 | throw new InvalidArgumentException(sprintf('Cache tag "%s" contains reserved characters "%s".', $tag, self::RESERVED_CHARACTERS)); | ||
| 111 | } | ||
| 112 | $this->newMetadata[self::METADATA_TAGS][$tag] = $tag; | ||
| 113 | } | ||
| 114 | |||
| 115 | return $this; | ||
| 116 | } | ||
| 117 | |||
| 118 | public function getMetadata(): array | ||
| 119 | { | ||
| 120 | return $this->metadata; | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * Validates a cache key according to PSR-6. | ||
| 125 | * | ||
| 126 | * @param mixed $key The key to validate | ||
| 127 | * | ||
| 128 | * @throws InvalidArgumentException When $key is not valid | ||
| 129 | */ | ||
| 130 | public static function validateKey($key): string | ||
| 131 | { | ||
| 132 | if (!\is_string($key)) { | ||
| 133 | throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key))); | ||
| 134 | } | ||
| 135 | if ('' === $key) { | ||
| 136 | throw new InvalidArgumentException('Cache key length must be greater than zero.'); | ||
| 137 | } | ||
| 138 | if (false !== strpbrk($key, self::RESERVED_CHARACTERS)) { | ||
| 139 | throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS)); | ||
| 140 | } | ||
| 141 | |||
| 142 | return $key; | ||
| 143 | } | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Internal logging helper. | ||
| 147 | * | ||
| 148 | * @internal | ||
| 149 | */ | ||
| 150 | public static function log(?LoggerInterface $logger, string $message, array $context = []): void | ||
| 151 | { | ||
| 152 | if ($logger) { | ||
| 153 | $logger->warning($message, $context); | ||
| 154 | } else { | ||
| 155 | $replace = []; | ||
| 156 | foreach ($context as $k => $v) { | ||
| 157 | if (\is_scalar($v)) { | ||
| 158 | $replace['{'.$k.'}'] = $v; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | @trigger_error(strtr($message, $replace), \E_USER_WARNING); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | private function pack(): mixed | ||
| 166 | { | ||
| 167 | if (!$m = $this->newMetadata) { | ||
| 168 | return $this->value; | ||
| 169 | } | ||
| 170 | $valueWrapper = self::VALUE_WRAPPER; | ||
| 171 | |||
| 172 | return new $valueWrapper($this->value, $m + ['expiry' => $this->expiry]); | ||
| 173 | } | ||
| 174 | |||
| 175 | private function unpack(): bool | ||
| 176 | { | ||
| 177 | $v = $this->value; | ||
| 178 | $valueWrapper = self::VALUE_WRAPPER; | ||
| 179 | |||
| 180 | if ($v instanceof $valueWrapper) { | ||
| 181 | $this->value = $v->value; | ||
| 182 | $this->metadata = $v->metadata; | ||
| 183 | |||
| 184 | return true; | ||
| 185 | } | ||
| 186 | |||
| 187 | if (!\is_array($v) || 1 !== \count($v) || 10 !== \strlen($k = (string) array_key_first($v)) || "\x9D" !== $k[0] || "\0" !== $k[5] || "\x5F" !== $k[9]) { | ||
| 188 | return false; | ||
| 189 | } | ||
| 190 | |||
| 191 | // BC with pools populated before v6.1 | ||
| 192 | $this->value = $v[$k]; | ||
| 193 | $this->metadata = unpack('Vexpiry/Nctime', substr($k, 1, -1)); | ||
| 194 | $this->metadata['expiry'] += self::METADATA_EXPIRY_OFFSET; | ||
| 195 | |||
| 196 | return true; | ||
| 197 | } | ||
| 198 | } | ||
