vendor/shopware/storefront/Framework/Cache/CacheResponseSubscriber.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Cache;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheStateSubscriber;
  6. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  11. use Shopware\Storefront\Framework\Routing\MaintenanceModeResolver;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. class CacheResponseSubscriber implements EventSubscriberInterface
  20. {
  21.     public const STATE_LOGGED_IN CacheStateSubscriber::STATE_LOGGED_IN;
  22.     public const STATE_CART_FILLED CacheStateSubscriber::STATE_CART_FILLED;
  23.     public const CURRENCY_COOKIE 'sw-currency';
  24.     public const CONTEXT_CACHE_COOKIE 'sw-cache-hash';
  25.     public const SYSTEM_STATE_COOKIE 'sw-states';
  26.     public const INVALIDATION_STATES_HEADER 'sw-invalidation-states';
  27.     private const CORE_HTTP_CACHED_ROUTES = [
  28.         'api.acl.privileges.get',
  29.     ];
  30.     private bool $reverseProxyEnabled;
  31.     private CartService $cartService;
  32.     private int $defaultTtl;
  33.     private bool $httpCacheEnabled;
  34.     private MaintenanceModeResolver $maintenanceResolver;
  35.     /**
  36.      * @internal
  37.      */
  38.     public function __construct(
  39.         CartService $cartService,
  40.         int $defaultTtl,
  41.         bool $httpCacheEnabled,
  42.         MaintenanceModeResolver $maintenanceModeResolver,
  43.         bool $reverseProxyEnabled
  44.     ) {
  45.         $this->cartService $cartService;
  46.         $this->defaultTtl $defaultTtl;
  47.         $this->httpCacheEnabled $httpCacheEnabled;
  48.         $this->maintenanceResolver $maintenanceModeResolver;
  49.         $this->reverseProxyEnabled $reverseProxyEnabled;
  50.     }
  51.     /**
  52.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  53.      */
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             KernelEvents::REQUEST => 'addHttpCacheToCoreRoutes',
  58.             KernelEvents::RESPONSE => [
  59.                 ['setResponseCache', -1500],
  60.             ],
  61.             BeforeSendResponseEvent::class => 'updateCacheControlForBrowser',
  62.         ];
  63.     }
  64.     public function addHttpCacheToCoreRoutes(RequestEvent $event): void
  65.     {
  66.         $request $event->getRequest();
  67.         $route $request->attributes->get('_route');
  68.         if (\in_array($routeself::CORE_HTTP_CACHED_ROUTEStrue)) {
  69.             $request->attributes->set('_' HttpCache::ALIAS, [new HttpCache([])]);
  70.         }
  71.     }
  72.     public function setResponseCache(ResponseEvent $event): void
  73.     {
  74.         if (!$this->httpCacheEnabled) {
  75.             return;
  76.         }
  77.         $response $event->getResponse();
  78.         $request $event->getRequest();
  79.         if ($this->maintenanceResolver->isMaintenanceRequest($request)) {
  80.             return;
  81.         }
  82.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  83.         if (!$context instanceof SalesChannelContext) {
  84.             return;
  85.         }
  86.         $route $request->attributes->get('_route');
  87.         if ($route === 'frontend.checkout.configure') {
  88.             $this->setCurrencyCookie($request$response);
  89.         }
  90.         $cart $this->cartService->getCart($context->getToken(), $context);
  91.         $states $this->updateSystemState($cart$context$request$response);
  92.         if ($request->getMethod() !== Request::METHOD_GET) {
  93.             return;
  94.         }
  95.         if ($context->getCustomer() || $cart->getLineItems()->count() > 0) {
  96.             $newValue $this->buildCacheHash($context);
  97.             if ($request->cookies->get(self::CONTEXT_CACHE_COOKIE'') !== $newValue) {
  98.                 $cookie Cookie::create(self::CONTEXT_CACHE_COOKIE$newValue);
  99.                 $cookie->setSecureDefault($request->isSecure());
  100.                 $response->headers->setCookie($cookie);
  101.             }
  102.         } elseif ($request->cookies->has(self::CONTEXT_CACHE_COOKIE)) {
  103.             $response->headers->removeCookie(self::CONTEXT_CACHE_COOKIE);
  104.             $response->headers->clearCookie(self::CONTEXT_CACHE_COOKIE);
  105.         }
  106.         $config $request->attributes->get('_' HttpCache::ALIAS);
  107.         if (empty($config)) {
  108.             return;
  109.         }
  110.         /** @var HttpCache $cache */
  111.         $cache array_shift($config);
  112.         if ($this->hasInvalidationState($cache$states)) {
  113.             return;
  114.         }
  115.         $maxAge $cache->getMaxAge() ?? $this->defaultTtl;
  116.         $response->setSharedMaxAge($maxAge);
  117.         $response->headers->addCacheControlDirective('must-revalidate');
  118.         $response->headers->set(
  119.             self::INVALIDATION_STATES_HEADER,
  120.             implode(','$cache->getStates())
  121.         );
  122.     }
  123.     /**
  124.      * In the default HttpCache implementation the reverse proxy cache is implemented too in PHP and triggered before the response is send to the client. We don't need to send the "real" cache-control headers to the end client (browser/cloudflare).
  125.      * If a external reverse proxy cache is used we still need to provide the actual cache-control, so the external system can cache the system correctly and set the cache-control again to
  126.      */
  127.     public function updateCacheControlForBrowser(BeforeSendResponseEvent $event): void
  128.     {
  129.         if ($this->reverseProxyEnabled) {
  130.             return;
  131.         }
  132.         $response $event->getResponse();
  133.         $noStore $response->headers->getCacheControlDirective('no-store');
  134.         // We don't want that the client will cache the website, if no reverse proxy is configured
  135.         $response->headers->remove('cache-control');
  136.         $response->setPrivate();
  137.         if ($noStore) {
  138.             $response->headers->addCacheControlDirective('no-store');
  139.         } else {
  140.             $response->headers->addCacheControlDirective('no-cache');
  141.         }
  142.     }
  143.     private function hasInvalidationState(HttpCache $cache, array $states): bool
  144.     {
  145.         foreach ($states as $state) {
  146.             if (\in_array($state$cache->getStates(), true)) {
  147.                 return true;
  148.             }
  149.         }
  150.         return false;
  151.     }
  152.     private function buildCacheHash(SalesChannelContext $context): string
  153.     {
  154.         return md5(json_encode([
  155.             $context->getRuleIds(),
  156.             $context->getContext()->getVersionId(),
  157.             $context->getCurrency()->getId(),
  158.             $context->getCustomer() ? 'logged-in' 'not-logged-in',
  159.         ]));
  160.     }
  161.     /**
  162.      * System states can be used to stop caching routes at certain states. For example,
  163.      * the checkout routes are no longer cached if the customer has products in the cart or is logged in.
  164.      */
  165.     private function updateSystemState(Cart $cartSalesChannelContext $contextRequest $requestResponse $response): array
  166.     {
  167.         $states $this->getSystemStates($request$context$cart);
  168.         if (empty($states)) {
  169.             if ($request->cookies->has(self::SYSTEM_STATE_COOKIE)) {
  170.                 $response->headers->removeCookie(self::SYSTEM_STATE_COOKIE);
  171.                 $response->headers->clearCookie(self::SYSTEM_STATE_COOKIE);
  172.             }
  173.             return [];
  174.         }
  175.         $newStates implode(','$states);
  176.         if ($request->cookies->get(self::SYSTEM_STATE_COOKIE) !== $newStates) {
  177.             $cookie Cookie::create(self::SYSTEM_STATE_COOKIE$newStates);
  178.             $cookie->setSecureDefault($request->isSecure());
  179.             $response->headers->setCookie($cookie);
  180.         }
  181.         return $states;
  182.     }
  183.     private function getSystemStates(Request $requestSalesChannelContext $contextCart $cart): array
  184.     {
  185.         $states = [];
  186.         $swStates = (string) $request->cookies->get(self::SYSTEM_STATE_COOKIE);
  187.         if ($swStates !== '') {
  188.             $states array_flip(explode(','$swStates));
  189.         }
  190.         $states $this->switchState($statesself::STATE_LOGGED_IN$context->getCustomer() !== null);
  191.         $states $this->switchState($statesself::STATE_CART_FILLED$cart->getLineItems()->count() > 0);
  192.         return array_keys($states);
  193.     }
  194.     private function switchState(array $statesstring $keybool $match): array
  195.     {
  196.         if ($match) {
  197.             $states[$key] = true;
  198.             return $states;
  199.         }
  200.         unset($states[$key]);
  201.         return $states;
  202.     }
  203.     private function setCurrencyCookie(Request $requestResponse $response): void
  204.     {
  205.         $currencyId $request->get(SalesChannelContextService::CURRENCY_ID);
  206.         if (!$currencyId) {
  207.             return;
  208.         }
  209.         $cookie Cookie::create(self::CURRENCY_COOKIE$currencyId);
  210.         $cookie->setSecureDefault($request->isSecure());
  211.         $response->headers->setCookie($cookie);
  212.     }
  213. }