vendor/shopware/platform/src/Core/Content/Flow/Dispatching/CachedFlowLoader.php line 63

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching;
  3. use Shopware\Core\Content\Flow\FlowEvents;
  4. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Contracts\Cache\CacheInterface;
  7. use Symfony\Contracts\Cache\ItemInterface;
  8. use Symfony\Contracts\Service\ResetInterface;
  9. /**
  10.  * @internal not intended for decoration or replacement
  11.  */
  12. class CachedFlowLoader extends AbstractFlowLoader implements EventSubscriberInterfaceResetInterface
  13. {
  14.     public const KEY 'flow-loader';
  15.     private array $flows = [];
  16.     private AbstractFlowLoader $decorated;
  17.     private CacheInterface $cache;
  18.     public function __construct(
  19.         AbstractFlowLoader $decorated,
  20.         CacheInterface $cache
  21.     ) {
  22.         $this->decorated $decorated;
  23.         $this->cache $cache;
  24.     }
  25.     /**
  26.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             FlowEvents::FLOW_WRITTEN_EVENT => 'invalidate',
  32.         ];
  33.     }
  34.     public function getDecorated(): AbstractFlowLoader
  35.     {
  36.         return $this->decorated;
  37.     }
  38.     public function load(): array
  39.     {
  40.         if (!empty($this->flows)) {
  41.             return $this->flows;
  42.         }
  43.         $value $this->cache->get(self::KEY, function (ItemInterface $item) {
  44.             $item->tag([self::KEY]);
  45.             return CacheValueCompressor::compress($this->getDecorated()->load());
  46.         });
  47.         return $this->flows CacheValueCompressor::uncompress($value);
  48.     }
  49.     public function invalidate(): void
  50.     {
  51.         $this->reset();
  52.         $this->cache->delete(self::KEY);
  53.     }
  54.     public function reset(): void
  55.     {
  56.         $this->flows = [];
  57.     }
  58. }