src/Sylius/Bundle/LocaleBundle/Listener/RequestLocaleSetter.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\LocaleBundle\Listener;
  12. use Sylius\Component\Locale\Context\LocaleContextInterface;
  13. use Sylius\Component\Locale\Context\LocaleNotFoundException;
  14. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  15. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  16. final class RequestLocaleSetter
  17. {
  18.     /** @var LocaleContextInterface */
  19.     private $localeContext;
  20.     /** @var LocaleProviderInterface */
  21.     private $localeProvider;
  22.     public function __construct(
  23.         LocaleContextInterface $localeContext,
  24.         LocaleProviderInterface $localeProvider
  25.     ) {
  26.         $this->localeContext $localeContext;
  27.         $this->localeProvider $localeProvider;
  28.     }
  29.     /**
  30.      * @throws LocaleNotFoundException
  31.      */
  32.     public function onKernelRequest(GetResponseEvent $event): void
  33.     {
  34.         $request $event->getRequest();
  35.         $request->setLocale($this->localeContext->getLocaleCode());
  36.         $request->setDefaultLocale($this->localeProvider->getDefaultLocaleCode());
  37.     }
  38. }