bug #12181 [HttpKernel] properly handle exceptions in translator listener (xabbuh)
This PR was merged into the 2.6-dev branch.
Discussion
----------
[HttpKernel] properly handle exceptions in translator listener
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
The `setLocale()` method can throw an `\InvalidArgumentException` when
an invalid locale has been passed. These exceptions must be handled by
the `TranslatorListener` which should then pass the request's default
locale instead.
Commits
-------
d3a0a55
properly handle exceptions in translator listener
This commit is contained in:
commit
ade0db7fac
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\HttpKernel\EventListener;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
@ -36,14 +37,16 @@ class TranslatorListener implements EventSubscriberInterface
|
||||
|
||||
public function onKernelRequest(GetResponseEvent $event)
|
||||
{
|
||||
$this->translator->setLocale($event->getRequest()->getLocale());
|
||||
$this->setLocale($event->getRequest());
|
||||
}
|
||||
|
||||
public function onKernelFinishRequest(FinishRequestEvent $event)
|
||||
{
|
||||
if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
|
||||
$this->translator->setLocale($parentRequest->getLocale());
|
||||
if (null === $parentRequest = $this->requestStack->getParentRequest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setLocale($parentRequest);
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
@ -54,4 +57,13 @@ class TranslatorListener implements EventSubscriberInterface
|
||||
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
|
||||
);
|
||||
}
|
||||
|
||||
private function setLocale(Request $request)
|
||||
{
|
||||
try {
|
||||
$this->translator->setLocale($request->getLocale());
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->translator->setLocale($request->getDefaultLocale());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,21 @@ class TranslatorListenerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->listener->onKernelRequest($event);
|
||||
}
|
||||
|
||||
public function testDefaultLocaleIsUsedOnExceptionsInOnKernelRequest()
|
||||
{
|
||||
$this->translator
|
||||
->expects($this->at(0))
|
||||
->method('setLocale')
|
||||
->will($this->throwException(new \InvalidArgumentException()));
|
||||
$this->translator
|
||||
->expects($this->at(1))
|
||||
->method('setLocale')
|
||||
->with($this->equalTo('en'));
|
||||
|
||||
$event = new GetResponseEvent($this->createHttpKernel(), $this->createRequest('fr'), HttpKernelInterface::MASTER_REQUEST);
|
||||
$this->listener->onKernelRequest($event);
|
||||
}
|
||||
|
||||
public function testLocaleIsSetInOnKernelFinishRequestWhenParentRequestExists()
|
||||
{
|
||||
$this->translator
|
||||
@ -63,6 +78,22 @@ class TranslatorListenerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->listener->onKernelFinishRequest($event);
|
||||
}
|
||||
|
||||
public function testDefaultLocaleIsUsedOnExceptionsInOnKernelFinishRequest()
|
||||
{
|
||||
$this->translator
|
||||
->expects($this->at(0))
|
||||
->method('setLocale')
|
||||
->will($this->throwException(new \InvalidArgumentException()));
|
||||
$this->translator
|
||||
->expects($this->at(1))
|
||||
->method('setLocale')
|
||||
->with($this->equalTo('en'));
|
||||
|
||||
$this->setMasterRequest($this->createRequest('fr'));
|
||||
$event = new FinishRequestEvent($this->createHttpKernel(), $this->createRequest('de'), HttpKernelInterface::SUB_REQUEST);
|
||||
$this->listener->onKernelFinishRequest($event);
|
||||
}
|
||||
|
||||
private function createHttpKernel()
|
||||
{
|
||||
return $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
|
||||
|
Reference in New Issue
Block a user