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

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.     /**
  32.      * @param RequestStack                      $requestStack  A RequestStack instance
  33.      * @param string                            $defaultLocale The default locale
  34.      * @param RequestContextAwareInterface|null $router        The router
  35.      */
  36.     public function __construct(RequestStack $requestStackstring $defaultLocale 'en'RequestContextAwareInterface $router null)
  37.     {
  38.         $this->defaultLocale $defaultLocale;
  39.         $this->requestStack $requestStack;
  40.         $this->router $router;
  41.     }
  42.     public function setDefaultLocale(KernelEvent $event)
  43.     {
  44.         $event->getRequest()->setDefaultLocale($this->defaultLocale);
  45.     }
  46.     public function onKernelRequest(GetResponseEvent $event)
  47.     {
  48.         $request $event->getRequest();
  49.         $this->setLocale($request);
  50.         $this->setRouterContext($request);
  51.     }
  52.     public function onKernelFinishRequest(FinishRequestEvent $event)
  53.     {
  54.         if (null !== $parentRequest $this->requestStack->getParentRequest()) {
  55.             $this->setRouterContext($parentRequest);
  56.         }
  57.     }
  58.     private function setLocale(Request $request)
  59.     {
  60.         if ($locale $request->attributes->get('_locale')) {
  61.             $request->setLocale($locale);
  62.         }
  63.     }
  64.     private function setRouterContext(Request $request)
  65.     {
  66.         if (null !== $this->router) {
  67.             $this->router->getContext()->setParameter('_locale'$request->getLocale());
  68.         }
  69.     }
  70.     public static function getSubscribedEvents()
  71.     {
  72.         return [
  73.             KernelEvents::REQUEST => [
  74.                 ['setDefaultLocale'100],
  75.                 // must be registered after the Router to have access to the _locale
  76.                 ['onKernelRequest'16],
  77.             ],
  78.             KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest'0]],
  79.         ];
  80.     }
  81. }