vendor/ezsystems/ezplatform-http-cache/src/EventSubscriber/UserContextSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace EzSystems\PlatformHttpCacheBundle\EventSubscriber;
  7. use EzSystems\PlatformHttpCacheBundle\RepositoryTagPrefix;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. /**
  12.  * Tag /_fos_user_context_hash responses, so we can expire/clear it by tag.
  13.  */
  14. class UserContextSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var \EzSystems\PlatformHttpCacheBundle\RepositoryTagPrefix
  18.      */
  19.     private $prefixService;
  20.     /**
  21.      * @var string
  22.      */
  23.     private $tagHeader 'xkey';
  24.     public function __construct(RepositoryTagPrefix $prefixService$tagHeader)
  25.     {
  26.         $this->prefixService $prefixService;
  27.         $this->tagHeader $tagHeader;
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [KernelEvents::RESPONSE => ['tagUserContext'10]];
  32.     }
  33.     /**
  34.      * Tag vnd.fos.user-context-hash responses if they are set to cached.
  35.      *
  36.      * @param FilterResponseEvent $event
  37.      */
  38.     public function tagUserContext(FilterResponseEvent $event)
  39.     {
  40.         $response $event->getResponse();
  41.         if (!$response->isCacheable()) {
  42.             return;
  43.         }
  44.         if ($response->headers->get('Content-Type') !== 'application/vnd.fos.user-context-hash') {
  45.             return;
  46.         }
  47.         if (!$response->getTtl()) {
  48.             return;
  49.         }
  50.         // We need to set tag directly on response here to make sure this does not also get applied to the main request
  51.         // when using Symfony Proxy, as tag handler does not clear tags between requests.
  52.         // NB: We prefix this even if route is not SA aware, but this is the same as with REST. As doc states,
  53.         // it's expected that each repo needs to have own domain so requests against base domain represent same repo.
  54.         $response->headers->set($this->tagHeader$this->prefixService->getRepositoryPrefix() . 'ez-user-context-hash');
  55.     }
  56. }