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

  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(
  19.         private readonly AbstractFlowLoader $decorated,
  20.         private readonly CacheInterface $cache
  21.     ) {
  22.     }
  23.     /**
  24.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             FlowEvents::FLOW_WRITTEN_EVENT => 'invalidate',
  30.         ];
  31.     }
  32.     public function load(): array
  33.     {
  34.         if (!empty($this->flows)) {
  35.             return $this->flows;
  36.         }
  37.         $value $this->cache->get(self::KEY, function (ItemInterface $item) {
  38.             $item->tag([self::KEY]);
  39.             return CacheValueCompressor::compress($this->decorated->load());
  40.         });
  41.         return $this->flows CacheValueCompressor::uncompress($value);
  42.     }
  43.     public function invalidate(): void
  44.     {
  45.         $this->reset();
  46.         $this->cache->delete(self::KEY);
  47.     }
  48.     public function reset(): void
  49.     {
  50.         $this->flows = [];
  51.     }
  52. }