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

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