vendor/ibexa/http-cache/src/lib/EventSubscriber/AddContentLanguageHeaderSubscriber.php line 30

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\HttpCache\EventSubscriber;
  8. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  9. use Ibexa\Core\MVC\Symfony\View\CachableView;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. final class AddContentLanguageHeaderSubscriber implements EventSubscriberInterface
  15. {
  16. public const CONTENT_LANGUAGE_HEADER = 'x-lang';
  17. /** @var bool */
  18. private $isTranslationAware;
  19. public function __construct(bool $isTranslationAware)
  20. {
  21. $this->isTranslationAware = $isTranslationAware;
  22. }
  23. public function onKernelResponse(ResponseEvent $event)
  24. {
  25. if (!$this->isTranslationAware || HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
  26. return;
  27. }
  28. $request = $event->getRequest();
  29. $view = $request->attributes->get('view');
  30. if (!$view instanceof CachableView || !$view->isCacheEnabled()) {
  31. return;
  32. }
  33. $content = $request->attributes->get('content');
  34. if ($content instanceof Content) {
  35. $event->getResponse()->headers->add([self::CONTENT_LANGUAGE_HEADER => $content->getDefaultLanguageCode()]);
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function getSubscribedEvents()
  42. {
  43. return [
  44. KernelEvents::RESPONSE => 'onKernelResponse',
  45. ];
  46. }
  47. }