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

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