feature #29483 [HttpKernel] Set the default locale early (thewilkybarkid)
This PR was merged into the 4.3-dev branch.
Discussion
----------
[HttpKernel] Set the default locale early
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
Similar to #29186, the default locale isn't set till after the router so isn't available when trying to handle errors there (well, only the _default_ default locale is).
Commits
-------
02c9f352d9
Set the default locale early
This commit is contained in:
commit
556a9206ca
@ -5,6 +5,7 @@ CHANGELOG
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
* increased the priority of `Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener`
|
* increased the priority of `Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener`
|
||||||
|
* made `Symfony\Component\HttpKernel\EventListenerLocaleListener` set the default locale early
|
||||||
|
|
||||||
4.2.0
|
4.2.0
|
||||||
-----
|
-----
|
||||||
|
@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
|
use Symfony\Component\HttpKernel\Event\KernelEvent;
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
use Symfony\Component\Routing\RequestContextAwareInterface;
|
use Symfony\Component\Routing\RequestContextAwareInterface;
|
||||||
|
|
||||||
@ -42,10 +43,14 @@ class LocaleListener implements EventSubscriberInterface
|
|||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setDefaultLocale(KernelEvent $event)
|
||||||
|
{
|
||||||
|
$event->getRequest()->setDefaultLocale($this->defaultLocale);
|
||||||
|
}
|
||||||
|
|
||||||
public function onKernelRequest(GetResponseEvent $event)
|
public function onKernelRequest(GetResponseEvent $event)
|
||||||
{
|
{
|
||||||
$request = $event->getRequest();
|
$request = $event->getRequest();
|
||||||
$request->setDefaultLocale($this->defaultLocale);
|
|
||||||
|
|
||||||
$this->setLocale($request);
|
$this->setLocale($request);
|
||||||
$this->setRouterContext($request);
|
$this->setRouterContext($request);
|
||||||
@ -75,8 +80,11 @@ class LocaleListener implements EventSubscriberInterface
|
|||||||
public static function getSubscribedEvents()
|
public static function getSubscribedEvents()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
// must be registered after the Router to have access to the _locale
|
KernelEvents::REQUEST => array(
|
||||||
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
|
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)),
|
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,12 @@
|
|||||||
namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
|
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
|
||||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
|
||||||
class LocaleListenerTest extends TestCase
|
class LocaleListenerTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -26,12 +28,28 @@ class LocaleListenerTest extends TestCase
|
|||||||
$this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock();
|
$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');
|
$listener = new LocaleListener($this->requestStack, 'fr');
|
||||||
$event = $this->getEvent($request = Request::create('/'));
|
$event = $this->getEvent($request = Request::create('/'));
|
||||||
|
|
||||||
$listener->onKernelRequest($event);
|
$listener->setDefaultLocale($event);
|
||||||
$this->assertEquals('fr', $request->getLocale());
|
$this->assertEquals('fr', $request->getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user