vendor/ibexa/fieldtype-page/src/lib/Event/Subscriber/SetCacheHeadersResponseSubscriber.php line 43

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. declare(strict_types=1);
  7. namespace Ibexa\FieldTypePage\Event\Subscriber;
  8. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  9. use Ibexa\FieldTypePage\Event\BlockResponseEvent;
  10. use Ibexa\FieldTypePage\Event\BlockResponseEvents;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Context\ContentViewBlockContext;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class SetCacheHeadersResponseSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
  16.     private $configResolver;
  17.     /** @var string */
  18.     private $userHashHeaderName;
  19.     public function __construct(
  20.         ConfigResolverInterface $configResolver,
  21.         string $userHashHeaderName
  22.     ) {
  23.         $this->configResolver $configResolver;
  24.         $this->userHashHeaderName $userHashHeaderName;
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             BlockResponseEvents::BLOCK_RESPONSE => ['onBlockResponse'0],
  33.         ];
  34.     }
  35.     public function onBlockResponse(BlockResponseEvent $event): void
  36.     {
  37.         $blockContext $event->getBlockContext();
  38.         $response $event->getResponse();
  39.         if (!$blockContext instanceof ContentViewBlockContext) {
  40.             return;
  41.         }
  42.         if (!$this->configResolver->getParameter('content.ttl_cache')) {
  43.             $response->setPrivate();
  44.             return;
  45.         }
  46.         $response->setPublic();
  47.         $response->setSharedMaxAge((int) $this->configResolver->getParameter('content.default_ttl'));
  48.         $response->setVary([$this->userHashHeaderName'X-Editorial-Mode']);
  49.     }
  50. }
  51. class_alias(SetCacheHeadersResponseSubscriber::class, 'EzSystems\EzPlatformPageFieldType\Event\Subscriber\SetCacheHeadersResponseSubscriber');