vendor/shopware/storefront/Framework/Cache/HttpCacheKeyGenerator.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Cache;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  5. use Shopware\Core\SalesChannelRequest;
  6. use Shopware\Storefront\Framework\Cache\Event\HttpCacheGenerateKeyEvent;
  7. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  10. #[Package('storefront')]
  11. class HttpCacheKeyGenerator extends AbstractHttpCacheKeyGenerator
  12. {
  13.     private string $cacheHash;
  14.     private EventDispatcherInterface $eventDispatcher;
  15.     /**
  16.      * @var string[]
  17.      */
  18.     private array $ignoredParameters;
  19.     /**
  20.      * @param string[] $ignoredParameters
  21.      *
  22.      * @internal
  23.      */
  24.     public function __construct(
  25.         string $cacheHash,
  26.         EventDispatcherInterface $eventDispatcher,
  27.         array $ignoredParameters
  28.     ) {
  29.         $this->cacheHash $cacheHash;
  30.         $this->eventDispatcher $eventDispatcher;
  31.         $this->ignoredParameters $ignoredParameters;
  32.     }
  33.     public function getDecorated(): AbstractHttpCacheKeyGenerator
  34.     {
  35.         throw new DecorationPatternException(self::class);
  36.     }
  37.     public function generate(Request $request): string
  38.     {
  39.         $uri $this->getRequestUri($request) . $this->cacheHash;
  40.         $event = new HttpCacheGenerateKeyEvent($request'md' hash('sha256'$uri));
  41.         $this->eventDispatcher->dispatch($event);
  42.         $hash $event->getHash();
  43.         if ($request->cookies->has(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE)) {
  44.             return 'http-cache-' hash('sha256'$hash '-' $request->cookies->get(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE));
  45.         }
  46.         if ($request->cookies->has(CacheResponseSubscriber::CURRENCY_COOKIE)) {
  47.             return 'http-cache-' hash('sha256'$hash '-' $request->cookies->get(CacheResponseSubscriber::CURRENCY_COOKIE));
  48.         }
  49.         if ($request->attributes->has(SalesChannelRequest::ATTRIBUTE_DOMAIN_CURRENCY_ID)) {
  50.             return 'http-cache-' hash('sha256'$hash '-' $request->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_CURRENCY_ID));
  51.         }
  52.         return 'http-cache-' $hash;
  53.     }
  54.     private function getRequestUri(Request $request): string
  55.     {
  56.         $params $request->query->all();
  57.         foreach (array_keys($params) as $key) {
  58.             if (\in_array($key$this->ignoredParameterstrue)) {
  59.                 unset($params[$key]);
  60.             }
  61.         }
  62.         ksort($params);
  63.         $params http_build_query($params);
  64.         return sprintf(
  65.             '%s%s%s%s',
  66.             $request->getSchemeAndHttpHost(),
  67.             $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL),
  68.             $request->getPathInfo(),
  69.             '?' $params
  70.         );
  71.     }
  72. }