vendor/symfony/http-kernel/EventListener/LocaleListener.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  15. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  16. use Symfony\Component\HttpKernel\Event\KernelEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Routing\RequestContextAwareInterface;
  19. /**
  20.  * Initializes the locale based on the current request.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @final since Symfony 4.3
  25.  */
  26. class LocaleListener implements EventSubscriberInterface
  27. {
  28.     private $router;
  29.     private $defaultLocale;
  30.     private $requestStack;
  31.     public function __construct(RequestStack $requestStackstring $defaultLocale 'en'RequestContextAwareInterface $router null)
  32.     {
  33.         $this->defaultLocale $defaultLocale;
  34.         $this->requestStack $requestStack;
  35.         $this->router $router;
  36.     }
  37.     public function setDefaultLocale(KernelEvent $event)
  38.     {
  39.         $event->getRequest()->setDefaultLocale($this->defaultLocale);
  40.     }
  41.     public function onKernelRequest(GetResponseEvent $event)
  42.     {
  43.         $request $event->getRequest();
  44.         $this->setLocale($request);
  45.         $this->setRouterContext($request);
  46.     }
  47.     public function onKernelFinishRequest(FinishRequestEvent $event)
  48.     {
  49.         if (null !== $parentRequest $this->requestStack->getParentRequest()) {
  50.             $this->setRouterContext($parentRequest);
  51.         }
  52.     }
  53.     private function setLocale(Request $request)
  54.     {
  55.         if ($locale $request->attributes->get('_locale')) {
  56.             $request->setLocale($locale);
  57.         }
  58.     }
  59.     private function setRouterContext(Request $request)
  60.     {
  61.         if (null !== $this->router) {
  62.             $this->router->getContext()->setParameter('_locale'$request->getLocale());
  63.         }
  64.     }
  65.     public static function getSubscribedEvents()
  66.     {
  67.         return [
  68.             KernelEvents::REQUEST => [
  69.                 ['setDefaultLocale'100],
  70.                 // must be registered after the Router to have access to the _locale
  71.                 ['onKernelRequest'16],
  72.             ],
  73.             KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest'0]],
  74.         ];
  75.     }
  76. }