vendor/shopware/core/Framework/Adapter/Cache/CacheDecorator.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Cache;
  3. use Psr\Cache\CacheItemInterface;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  6. use Symfony\Component\Cache\CacheItem;
  7. use Symfony\Contracts\Cache\CacheTrait;
  8. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  9. #[Package('core')]
  10. class CacheDecorator implements TagAwareAdapterInterfaceTagAwareCacheInterface
  11. {
  12.     use CacheTrait;
  13.     /**
  14.      * @var TagAwareCacheInterface&TagAwareAdapterInterface
  15.      */
  16.     private $decorated;
  17.     private CacheTagCollection $collection;
  18.     private \ReflectionProperty $property;
  19.     /**
  20.      * @internal
  21.      *
  22.      * @param TagAwareCacheInterface&TagAwareAdapterInterface $decorated
  23.      */
  24.     public function __construct($decoratedCacheTagCollection $collection)
  25.     {
  26.         $this->decorated $decorated;
  27.         $this->collection $collection;
  28.         // hack to get access to tags in save() - https://github.com/symfony/symfony/issues/36697
  29.         $this->property = (new \ReflectionClass(CacheItem::class))->getProperty('newMetadata');
  30.         $this->property->setAccessible(true);
  31.     }
  32.     /**
  33.      * @param string $key
  34.      *
  35.      * @return CacheItem
  36.      */
  37.     public function getItem($key)
  38.     {
  39.         $item $this->decorated->getItem($key);
  40.         $this->collection->add($this->getTags($item));
  41.         return $item;
  42.     }
  43.     /**
  44.      * @return \Generator<CacheItem>
  45.      */
  46.     public function getItems(array $keys = [])
  47.     {
  48.         foreach ($this->decorated->getItems($keys) as $item) {
  49.             $this->collection->add($this->getTags($item));
  50.             yield $item;
  51.         }
  52.     }
  53.     /**
  54.      * @return bool
  55.      */
  56.     public function clear(string $prefix '')
  57.     {
  58.         return $this->decorated->clear($prefix);
  59.     }
  60.     /**
  61.      * @param string $key
  62.      *
  63.      * @return bool
  64.      */
  65.     public function hasItem($key)
  66.     {
  67.         return $this->decorated->hasItem($key);
  68.     }
  69.     /**
  70.      * @param string $key
  71.      *
  72.      * @return bool
  73.      */
  74.     public function deleteItem($key)
  75.     {
  76.         return $this->decorated->deleteItem($key);
  77.     }
  78.     /**
  79.      * @param array<string> $keys
  80.      *
  81.      * @return bool
  82.      */
  83.     public function deleteItems(array $keys)
  84.     {
  85.         return $this->decorated->deleteItems($keys);
  86.     }
  87.     /**
  88.      * @return bool
  89.      */
  90.     public function save(CacheItemInterface $item)
  91.     {
  92.         $this->collection->add($this->getTags($item));
  93.         return $this->decorated->save($item);
  94.     }
  95.     /**
  96.      * @return bool
  97.      */
  98.     public function saveDeferred(CacheItemInterface $item)
  99.     {
  100.         $this->collection->add($this->getTags($item));
  101.         return $this->decorated->saveDeferred($item);
  102.     }
  103.     /**
  104.      * @return bool
  105.      */
  106.     public function commit()
  107.     {
  108.         return $this->decorated->commit();
  109.     }
  110.     /**
  111.      * @param array<string> $tags
  112.      *
  113.      * @return bool
  114.      */
  115.     public function invalidateTags(array $tags)
  116.     {
  117.         return $this->decorated->invalidateTags($tags);
  118.     }
  119.     private function getTags(CacheItemInterface $item): array
  120.     {
  121.         if (!$item instanceof CacheItem) {
  122.             return [];
  123.         }
  124.         $metaData $item->getMetadata();
  125.         $new $this->property->getValue($item);
  126.         return array_merge(
  127.             $metaData['tags'] ?? [],
  128.             $new['tags'] ?? []
  129.         );
  130.     }
  131. }