vendor/shopware/core/System/SalesChannel/Context/CachedBaseContextFactory.php line 71

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\System\SalesChannel\BaseContext;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Symfony\Contracts\Cache\CacheInterface;
  8. use Symfony\Contracts\Cache\ItemInterface;
  9. /**
  10.  * @internal
  11.  */
  12. class CachedBaseContextFactory extends AbstractBaseContextFactory
  13. {
  14.     private AbstractBaseContextFactory $decorated;
  15.     private CacheInterface $cache;
  16.     /**
  17.      * @var AbstractCacheTracer<SalesChannelContext>
  18.      */
  19.     private AbstractCacheTracer $tracer;
  20.     /**
  21.      * @param AbstractCacheTracer<SalesChannelContext> $tracer
  22.      */
  23.     public function __construct(
  24.         AbstractBaseContextFactory $decorated,
  25.         CacheInterface $cache,
  26.         AbstractCacheTracer $tracer
  27.     ) {
  28.         $this->decorated $decorated;
  29.         $this->cache $cache;
  30.         $this->tracer $tracer;
  31.     }
  32.     public function getDecorated(): AbstractBaseContextFactory
  33.     {
  34.         return $this->decorated;
  35.     }
  36.     public function create(string $salesChannelId, array $options = []): BaseContext
  37.     {
  38.         if (isset($options[SalesChannelContextService::ORIGINAL_CONTEXT])) {
  39.             return $this->getDecorated()->create($salesChannelId$options);
  40.         }
  41.         if (isset($options[SalesChannelContextService::PERMISSIONS])) {
  42.             return $this->getDecorated()->create($salesChannelId$options);
  43.         }
  44.         $name self::buildName($salesChannelId);
  45.         ksort($options);
  46.         $keys \array_intersect_key($options, [
  47.             SalesChannelContextService::CURRENCY_ID => true,
  48.             SalesChannelContextService::LANGUAGE_ID => true,
  49.             SalesChannelContextService::DOMAIN_ID => true,
  50.             SalesChannelContextService::PAYMENT_METHOD_ID => true,
  51.             SalesChannelContextService::SHIPPING_METHOD_ID => true,
  52.             SalesChannelContextService::VERSION_ID => true,
  53.             SalesChannelContextService::COUNTRY_ID => true,
  54.             SalesChannelContextService::COUNTRY_STATE_ID => true,
  55.         ]);
  56.         $key implode('-', [$namemd5(json_encode($keys\JSON_THROW_ON_ERROR))]);
  57.         $value $this->cache->get($key, function (ItemInterface $item) use ($name$salesChannelId$options) {
  58.             $context $this->tracer->trace($name, function () use ($salesChannelId$options) {
  59.                 return $this->getDecorated()->create($salesChannelId$options);
  60.             });
  61.             $keys array_unique(array_merge(
  62.                 $this->tracer->get($name),
  63.                 [$nameCachedSalesChannelContextFactory::ALL_TAG]
  64.             ));
  65.             $item->tag($keys);
  66.             return CacheValueCompressor::compress($context);
  67.         });
  68.         return CacheValueCompressor::uncompress($value);
  69.     }
  70.     public static function buildName(string $salesChannelId): string
  71.     {
  72.         return 'base-context-factory-' $salesChannelId;
  73.     }
  74. }