vendor/ibexa/http-cache/src/lib/EventListener/ConditionallyRemoveVaryHeaderListener.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace Ibexa\HttpCache\EventListener;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\HttpKernel\HttpKernelInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. /**
  12.  * Class ConditionallyRemoveVaryHeaderListener
  13.  * Unfortunately, FOS\HttpCacheBundle\EventListener\UserContextSubscriber will set Vary header on all requests.
  14.  * This event listeners removes the $userIdentifierHeaders headers again in responses to any of the given $routes.
  15.  * For such routes, the controller should instead set the Vary header explicitly.
  16.  */
  17. class ConditionallyRemoveVaryHeaderListener implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var string[]
  21.      */
  22.     private $routes;
  23.     /**
  24.      * @var string[]
  25.      */
  26.     private $userIdentifierHeaders;
  27.     /**
  28.      * ConditionallyRemoveVaryHeaderListener constructor.
  29.      *
  30.      * @param array $routes List of routes which will not have default vary headers
  31.      * @param array $userIdentifierHeaders
  32.      */
  33.     public function __construct(array $routes, array $userIdentifierHeaders = ['Cookie''Authorization'])
  34.     {
  35.         $this->routes $routes;
  36.         $this->userIdentifierHeaders array_map('strtolower'$userIdentifierHeaders);
  37.     }
  38.     /**
  39.      * Remove Vary headers for matched routes.
  40.      *
  41.      * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
  42.      */
  43.     public function onKernelResponse(ResponseEvent $event)
  44.     {
  45.         if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
  46.             return;
  47.         }
  48.         if (!\in_array($event->getRequest()->get('_route'), $this->routes)) {
  49.             return;
  50.         }
  51.         $response $event->getResponse();
  52.         $varyHeaders array_map('strtolower'$response->headers->all('vary'));
  53.         foreach ($this->userIdentifierHeaders as $removableVary) {
  54.             $key array_search($removableVary$varyHeaders);
  55.             if ($key !== false) {
  56.                 unset($varyHeaders[$key]);
  57.             }
  58.         }
  59.         $response->setVary($varyHeaderstrue);
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             KernelEvents::RESPONSE => 'onKernelResponse',
  68.         ];
  69.     }
  70. }
  71. class_alias(ConditionallyRemoveVaryHeaderListener::class, 'EzSystems\PlatformHttpCacheBundle\EventListener\ConditionallyRemoveVaryHeaderListener');