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

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