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

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