vendor/symfony/cache/CacheItem.php line 150

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