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

  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.     /**
  17.      * @param AbstractCacheTracer<SalesChannelContext> $tracer
  18.      */
  19.     public function __construct(private readonly AbstractBaseContextFactory $decorated, private readonly CacheInterface $cache, private readonly AbstractCacheTracer $tracer)
  20.     {
  21.     }
  22.     public function getDecorated(): AbstractBaseContextFactory
  23.     {
  24.         return $this->decorated;
  25.     }
  26.     public function create(string $salesChannelId, array $options = []): BaseContext
  27.     {
  28.         if (isset($options[SalesChannelContextService::ORIGINAL_CONTEXT])) {
  29.             return $this->getDecorated()->create($salesChannelId$options);
  30.         }
  31.         if (isset($options[SalesChannelContextService::PERMISSIONS])) {
  32.             return $this->getDecorated()->create($salesChannelId$options);
  33.         }
  34.         $name self::buildName($salesChannelId);
  35.         ksort($options);
  36.         $keys \array_intersect_key($options, [
  37.             SalesChannelContextService::CURRENCY_ID => true,
  38.             SalesChannelContextService::LANGUAGE_ID => true,
  39.             SalesChannelContextService::DOMAIN_ID => true,
  40.             SalesChannelContextService::PAYMENT_METHOD_ID => true,
  41.             SalesChannelContextService::SHIPPING_METHOD_ID => true,
  42.             SalesChannelContextService::VERSION_ID => true,
  43.             SalesChannelContextService::COUNTRY_ID => true,
  44.             SalesChannelContextService::COUNTRY_STATE_ID => true,
  45.         ]);
  46.         $key implode('-', [$namemd5(json_encode($keys\JSON_THROW_ON_ERROR))]);
  47.         $value $this->cache->get($key, function (ItemInterface $item) use ($name$salesChannelId$options) {
  48.             $context $this->tracer->trace($name, fn () => $this->getDecorated()->create($salesChannelId$options));
  49.             $keys array_unique(array_merge(
  50.                 $this->tracer->get($name),
  51.                 [$nameCachedSalesChannelContextFactory::ALL_TAG]
  52.             ));
  53.             $item->tag($keys);
  54.             return CacheValueCompressor::compress($context);
  55.         });
  56.         return CacheValueCompressor::uncompress($value);
  57.     }
  58.     public static function buildName(string $salesChannelId): string
  59.     {
  60.         return 'base-context-factory-' $salesChannelId;
  61.     }
  62. }