vendor/shopware/core/System/SalesChannel/Context/CachedSalesChannelContextFactory.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\Context;
  3. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  4. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Symfony\Contracts\Cache\CacheInterface;
  8. use Symfony\Contracts\Cache\ItemInterface;
  9. #[Package('core')]
  10. class CachedSalesChannelContextFactory extends AbstractSalesChannelContextFactory
  11. {
  12.     public const ALL_TAG 'sales-channel-context';
  13.     private AbstractSalesChannelContextFactory $decorated;
  14.     private CacheInterface $cache;
  15.     /**
  16.      * @var AbstractCacheTracer<SalesChannelContext>
  17.      */
  18.     private AbstractCacheTracer $tracer;
  19.     /**
  20.      * @internal
  21.      *
  22.      * @param AbstractCacheTracer<SalesChannelContext> $tracer
  23.      */
  24.     public function __construct(
  25.         AbstractSalesChannelContextFactory $decorated,
  26.         CacheInterface $cache,
  27.         AbstractCacheTracer $tracer
  28.     ) {
  29.         $this->decorated $decorated;
  30.         $this->cache $cache;
  31.         $this->tracer $tracer;
  32.     }
  33.     public function getDecorated(): AbstractSalesChannelContextFactory
  34.     {
  35.         return $this->decorated;
  36.     }
  37.     public function create(string $tokenstring $salesChannelId, array $options = []): SalesChannelContext
  38.     {
  39.         $name self::buildName($salesChannelId);
  40.         if (!$this->isCacheable($options)) {
  41.             return $this->getDecorated()->create($token$salesChannelId$options);
  42.         }
  43.         ksort($options);
  44.         $key implode('-', [$namemd5(json_encode($options\JSON_THROW_ON_ERROR))]);
  45.         $value $this->cache->get($key, function (ItemInterface $item) use ($name$token$salesChannelId$options) {
  46.             $context $this->tracer->trace($name, function () use ($token$salesChannelId$options) {
  47.                 return $this->getDecorated()->create($token$salesChannelId$options);
  48.             });
  49.             $keys array_unique(array_merge(
  50.                 $this->tracer->get($name),
  51.                 [$nameself::ALL_TAG]
  52.             ));
  53.             $item->tag($keys);
  54.             return CacheValueCompressor::compress($context);
  55.         });
  56.         $context CacheValueCompressor::uncompress($value);
  57.         $context->assign(['token' => $token]);
  58.         return $context;
  59.     }
  60.     public static function buildName(string $salesChannelId): string
  61.     {
  62.         return 'context-factory-' $salesChannelId;
  63.     }
  64.     /**
  65.      * @param array<string, mixed> $options
  66.      */
  67.     private function isCacheable(array $options): bool
  68.     {
  69.         return !isset($options[SalesChannelContextService::CUSTOMER_ID])
  70.             && !isset($options[SalesChannelContextService::BILLING_ADDRESS_ID])
  71.             && !isset($options[SalesChannelContextService::SHIPPING_ADDRESS_ID]);
  72.     }
  73. }