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

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