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:
Fabien Potencier 2019-01-02 11:31:36 +01:00
commit 556a9206ca
3 changed files with 32 additions and 5 deletions

View File

@ -5,6 +5,7 @@ CHANGELOG
-----
* increased the priority of `Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener`
* made `Symfony\Component\HttpKernel\EventListenerLocaleListener` set the default locale early
4.2.0
-----

View File

@ -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)),
);
}

View File

@ -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());
}