custom/plugins/AcrisShopSwitchCS/src/Subscriber/ResponseCacheSubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Core\PlatformRequest;
  5. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
  8. use Shopware\Storefront\Framework\Routing\MaintenanceModeResolver;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Cookie;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class ResponseCacheSubscriber implements EventSubscriberInterface
  15. {
  16.     public const CACHE_HASH_EXTENSION 'acris_cache_hash';
  17.     public const CACHE_HASH_STAY_ON_PAGE '90605113f9cb402284c6afbb3d408731';
  18.     private bool $httpCacheEnabled;
  19.     private MaintenanceModeResolver $maintenanceModeResolver;
  20.     public function __construct(
  21.         bool $httpCacheEnabled,
  22.         MaintenanceModeResolver $maintenanceModeResolver)
  23.     {
  24.         $this->httpCacheEnabled $httpCacheEnabled;
  25.         $this->maintenanceModeResolver $maintenanceModeResolver;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             KernelEvents::RESPONSE => [
  31.                 ['setResponseCache', -2000]
  32.             ]
  33.         ];
  34.     }
  35.     public function addToCacheHash($valueSalesChannelContext $context)
  36.     {
  37.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) === true) {
  38.             /** @var ArrayStruct $cacheHashExtension */
  39.             $cacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  40.         } else {
  41.             $cacheHashExtension = new ArrayStruct();
  42.         }
  43.         $context->addExtension(self::CACHE_HASH_EXTENSION$this->getCacheHashExtension($value$cacheHashExtension));
  44.     }
  45.     public function setResponseCache(ResponseEvent $event)
  46.     {
  47.         if (!$this->httpCacheEnabled) {
  48.             return;
  49.         }
  50.         $response $event->getResponse();
  51.         $request $event->getRequest();
  52.         if ($this->maintenanceModeResolver->isMaintenanceRequest($request)) {
  53.             return;
  54.         }
  55.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  56.         if (!$context instanceof SalesChannelContext) {
  57.             return;
  58.         }
  59.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) !== true) {
  60.             return;
  61.         }
  62.         /** @var ArrayStruct $acrisCacheHashExtension */
  63.         $acrisCacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  64.         $acrisCacheHash $this->generateCacheHashFromExtension($acrisCacheHashExtension);
  65.         $cacheHash $this->buildCacheHash($context$acrisCacheHash$this->getCurrencyIdChanging($request));
  66.         $response->headers->setCookie(new Cookie(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE$cacheHash));
  67.     }
  68.     public function generateCacheHash($value)
  69.     {
  70.         return $this->generateCacheHashFromExtension($this->getCacheHashExtension($value));
  71.     }
  72.     private function getCacheHashExtension($value, ?ArrayStruct $cacheHashExtension null): ArrayStruct
  73.     {
  74.         if($cacheHashExtension === null) {
  75.             $cacheHashExtension = new ArrayStruct();
  76.         }
  77.         $encodedValue md5(json_encode($value));
  78.         $cacheHashExtension->set($encodedValue$encodedValue);
  79.         return $cacheHashExtension;
  80.     }
  81.     private function generateCacheHashFromExtension(ArrayStruct $cacheHashExtension): string
  82.     {
  83.         return md5(json_encode($cacheHashExtension->all()));
  84.     }
  85.     private function buildCacheHash(SalesChannelContext $context$acrisCacheHash, ?string $currencyId): string
  86.     {
  87.         if(empty($currencyId) == true) {
  88.             $currencyId $context->getCurrency()->getId();
  89.         }
  90.         return md5(json_encode([
  91.             $context->getRuleIds(),
  92.             $context->getContext()->getVersionId(),
  93.             $currencyId,
  94.             $context->getCustomer() ? 'logged-in' 'not-logged-in',
  95.             $acrisCacheHash
  96.         ]));
  97.     }
  98.     private function getCurrencyIdChanging(Request $request): ?string
  99.     {
  100.         $route $request->attributes->get('_route');
  101.         if ($route === 'frontend.checkout.configure') {
  102.             $currencyId $request->get(SalesChannelContextService::CURRENCY_ID);
  103.             if (!$currencyId) {
  104.                 return null;
  105.             }
  106.             return $currencyId;
  107.         }
  108.         return null;
  109.     }
  110. }