vendor/shopware/storefront/Framework/Routing/ResponseHeaderListener.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Routing;
  3. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  4. use Shopware\Core\PlatformRequest;
  5. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. class ResponseHeaderListener implements EventSubscriberInterface
  11. {
  12.     private const REMOVAL_HEADERS = [
  13.         PlatformRequest::HEADER_VERSION_ID,
  14.         PlatformRequest::HEADER_LANGUAGE_ID,
  15.         PlatformRequest::HEADER_CONTEXT_TOKEN,
  16.         'Access-Control-Allow-Origin',
  17.         'Access-Control-Allow-Methods',
  18.         'Access-Control-Allow-Headers',
  19.         'Access-Control-Expose-Headers',
  20.     ];
  21.     /**
  22.      * @deprecated tag:v6.5.0 - Will be removed, use onResponse() instead
  23.      */
  24.     public function __invoke(ResponseEvent $event): void
  25.     {
  26.         $this->onResponse($event);
  27.     }
  28.     /**
  29.      * @return array<string, array{0: string, 1: int}>
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ResponseEvent::class => ['onResponse', -10],
  35.         ];
  36.     }
  37.     public function onResponse(ResponseEvent $event): void
  38.     {
  39.         $response $event->getResponse();
  40.         /** @var RouteScope|list<string> $scopes */
  41.         $scopes $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  42.         if ($scopes instanceof RouteScope) {
  43.             $scopes $scopes->getScopes();
  44.         }
  45.         if (!\in_array(StorefrontRouteScope::ID$scopestrue) && !$response instanceof StorefrontResponse) {
  46.             return;
  47.         }
  48.         $this->manipulateStorefrontHeader($event->getRequest(), $response);
  49.     }
  50.     private function manipulateStorefrontHeader(Request $requestResponse $response): void
  51.     {
  52.         $this->removeHeaders($response);
  53.         $this->addNoStoreHeader($request$response);
  54.     }
  55.     private function removeHeaders(Response $response): void
  56.     {
  57.         foreach (self::REMOVAL_HEADERS as $headerKey) {
  58.             $response->headers->remove($headerKey);
  59.         }
  60.     }
  61.     private function addNoStoreHeader(Request $requestResponse $response): void
  62.     {
  63.         if (!$request->attributes->has('_' NoStore::ALIAS)) {
  64.             return;
  65.         }
  66.         $response->setMaxAge(0);
  67.         $response->headers->addCacheControlDirective('no-cache');
  68.         $response->headers->addCacheControlDirective('no-store');
  69.         $response->headers->addCacheControlDirective('must-revalidate');
  70.         $response->setExpires(new \DateTime('@0'));
  71.     }
  72. }