src/Core/Content/Flow/Dispatching/CachedFlowLoader.php line 52

  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 Shopware\Core\Framework\Log\Package;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Contracts\Cache\CacheInterface;
  8. use Symfony\Contracts\Cache\ItemInterface;
  9. use Symfony\Contracts\Service\ResetInterface;
  10. /**
  11.  * @internal not intended for decoration or replacement
  12.  */
  13. #[Package('business-ops')]
  14. class CachedFlowLoader extends AbstractFlowLoader implements EventSubscriberInterfaceResetInterface
  15. {
  16.     final public const KEY 'flow-loader';
  17.     private array $flows = [];
  18.     public function __construct(private readonly AbstractFlowLoader $decorated, private readonly CacheInterface $cache)
  19.     {
  20.     }
  21.     /**
  22.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             FlowEvents::FLOW_WRITTEN_EVENT => 'invalidate',
  28.         ];
  29.     }
  30.     public function load(): array
  31.     {
  32.         if (!empty($this->flows)) {
  33.             return $this->flows;
  34.         }
  35.         $value $this->cache->get(self::KEY, function (ItemInterface $item) {
  36.             $item->tag([self::KEY]);
  37.             return CacheValueCompressor::compress($this->decorated->load());
  38.         });
  39.         return $this->flows CacheValueCompressor::uncompress($value);
  40.     }
  41.     public function invalidate(): void
  42.     {
  43.         $this->reset();
  44.         $this->cache->delete(self::KEY);
  45.     }
  46.     public function reset(): void
  47.     {
  48.         $this->flows = [];
  49.     }
  50. }