vendor/shopware/core/Content/Flow/Dispatching/CachedFlowLoader.php line 84

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Content\Flow\FlowEvents;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Contracts\Service\ResetInterface;
  10. /**
  11.  * @internal not intended for decoration or replacement
  12.  */
  13. class CachedFlowLoader extends AbstractFlowLoader implements EventSubscriberInterfaceResetInterface
  14. {
  15.     public const KEY 'flow-loader';
  16.     private array $flows = [];
  17.     private AbstractFlowLoader $decorated;
  18.     private TagAwareAdapterInterface $cache;
  19.     private LoggerInterface $logger;
  20.     public function __construct(
  21.         AbstractFlowLoader $decorated,
  22.         TagAwareAdapterInterface $cache,
  23.         LoggerInterface $logger
  24.     ) {
  25.         $this->decorated $decorated;
  26.         $this->cache $cache;
  27.         $this->logger $logger;
  28.     }
  29.     /**
  30.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  31.      */
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             FlowEvents::FLOW_WRITTEN_EVENT => 'invalidate',
  36.         ];
  37.     }
  38.     public function getDecorated(): AbstractFlowLoader
  39.     {
  40.         return $this->decorated;
  41.     }
  42.     public function load(): array
  43.     {
  44.         if (!empty($this->flows)) {
  45.             return $this->flows;
  46.         }
  47.         $item $this->cache->getItem(self::KEY);
  48.         try {
  49.             if ($item->isHit() && $item->get()) {
  50.                 $this->logger->info('cache-hit: ' self::KEY);
  51.                 return $this->flows CacheCompressor::uncompress($item);
  52.             }
  53.         } catch (\Throwable $e) {
  54.             $this->logger->error($e->getMessage());
  55.         }
  56.         $this->logger->info('cache-miss: ' self::KEY);
  57.         $flows $this->getDecorated()->load();
  58.         $item CacheCompressor::compress($item$flows);
  59.         $item->tag([self::KEY]);
  60.         $this->cache->save($item);
  61.         return $this->flows $flows;
  62.     }
  63.     public function invalidate(EntityWrittenEvent $event): void
  64.     {
  65.         $this->reset();
  66.         $this->cache->deleteItem(self::KEY);
  67.     }
  68.     public function reset(): void
  69.     {
  70.         $this->flows = [];
  71.     }
  72. }