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

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