vendor/ibexa/http-cache/src/lib/EventSubscriber/UserContextSubscriber.php line 48

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\EventSubscriber;
  7. use FOS\HttpCache\TagHeaderFormatter\TagHeaderFormatter;
  8. use Ibexa\HttpCache\RepositoryTagPrefix;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. /**
  13.  * Tag /_fos_user_context_hash responses, so we can expire/clear it by tag.
  14.  */
  15. class UserContextSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var \Ibexa\HttpCache\RepositoryTagPrefix
  19.      */
  20.     private $prefixService;
  21.     /**
  22.      * @var string
  23.      */
  24.     private $tagHeader;
  25.     public function __construct(
  26.         RepositoryTagPrefix $prefixService,
  27.         string $tagHeader TagHeaderFormatter::DEFAULT_HEADER_NAME
  28.     ) {
  29.         $this->prefixService $prefixService;
  30.         $this->tagHeader $tagHeader;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [KernelEvents::RESPONSE => ['tagUserContext'10]];
  35.     }
  36.     /**
  37.      * Tag vnd.fos.user-context-hash responses if they are set to cached.
  38.      *
  39.      * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
  40.      */
  41.     public function tagUserContext(ResponseEvent $event)
  42.     {
  43.         $response $event->getResponse();
  44.         if (!$response->isCacheable()) {
  45.             return;
  46.         }
  47.         if ($response->headers->get('Content-Type') !== 'application/vnd.fos.user-context-hash') {
  48.             return;
  49.         }
  50.         if (!$response->getTtl()) {
  51.             return;
  52.         }
  53.         // We need to set tag directly on response here to make sure this does not also get applied to the main request
  54.         // when using Symfony Proxy, as tag handler does not clear tags between requests.
  55.         // NB: We prefix this even if route is not SA aware, but this is the same as with REST. As doc states,
  56.         // it's expected that each repo needs to have own domain so requests against base domain represent same repo.
  57.         $response->headers->set($this->tagHeader$this->prefixService->getRepositoryPrefix() . 'ez-user-context-hash');
  58.     }
  59. }
  60. class_alias(UserContextSubscriber::class, 'EzSystems\PlatformHttpCacheBundle\EventSubscriber\UserContextSubscriber');