From 02c9f352d99d277c82ba368e3e45c65036d53fe5 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 13 Dec 2018 10:16:14 +0000 Subject: [PATCH] Set the default locale early --- src/Symfony/Component/HttpKernel/CHANGELOG.md | 5 +++++ .../EventListener/LocaleListener.php | 14 +++++++++--- .../EventListener/LocaleListenerTest.php | 22 +++++++++++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index b96b4aefa1..3b40d8ae71 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.3.0 +----- + + * made `Symfony\Component\HttpKernel\EventListenerLocaleListener` set the default locale early + 4.2.0 ----- diff --git a/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php b/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php index 1067e8a0a5..2c8e7744af 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Event\FinishRequestEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Event\KernelEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Routing\RequestContextAwareInterface; @@ -42,10 +43,14 @@ class LocaleListener implements EventSubscriberInterface $this->router = $router; } + public function setDefaultLocale(KernelEvent $event) + { + $event->getRequest()->setDefaultLocale($this->defaultLocale); + } + public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); - $request->setDefaultLocale($this->defaultLocale); $this->setLocale($request); $this->setRouterContext($request); @@ -75,8 +80,11 @@ class LocaleListener implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - // must be registered after the Router to have access to the _locale - KernelEvents::REQUEST => array(array('onKernelRequest', 16)), + KernelEvents::REQUEST => array( + array('setDefaultLocale', 100), + // must be registered after the Router to have access to the _locale + array('onKernelRequest', 16), + ), KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)), ); } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleListenerTest.php index f442235ad0..20282766a8 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleListenerTest.php @@ -12,10 +12,12 @@ namespace Symfony\Component\HttpKernel\Tests\EventListener; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\EventListener\LocaleListener; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\KernelEvents; class LocaleListenerTest extends TestCase { @@ -26,12 +28,28 @@ class LocaleListenerTest extends TestCase $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock(); } - public function testDefaultLocaleWithoutSession() + public function testIsAnEventSubscriber() + { + $this->assertInstanceOf(EventSubscriberInterface::class, new LocaleListener($this->requestStack)); + } + + public function testRegisteredEvent() + { + $this->assertEquals( + array( + KernelEvents::REQUEST => array(array('setDefaultLocale', 100), array('onKernelRequest', 16)), + KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)), + ), + LocaleListener::getSubscribedEvents() + ); + } + + public function testDefaultLocale() { $listener = new LocaleListener($this->requestStack, 'fr'); $event = $this->getEvent($request = Request::create('/')); - $listener->onKernelRequest($event); + $listener->setDefaultLocale($event); $this->assertEquals('fr', $request->getLocale()); }