Skip to content

Commit c98c2ef

Browse files
committed
merged the two Locale listener hooks (this hack was needed when the Firewall was registered before the Routing; this is not needed anymore)
1 parent e886d73 commit c98c2ef

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,22 @@ class LocaleListener implements EventSubscriberInterface
2727
private $router;
2828
private $defaultLocale;
2929

30-
public function __construct($defaultLocale = 'en', RouterInterface $router
31-
= null)
30+
public function __construct($defaultLocale = 'en', RouterInterface $router = null)
3231
{
3332
$this->defaultLocale = $defaultLocale;
3433
$this->router = $router;
3534
}
3635

37-
public function onEarlyKernelRequest(GetResponseEvent $event)
36+
public function onKernelRequest(GetResponseEvent $event)
3837
{
3938
$request = $event->getRequest();
39+
4040
if ($request->hasPreviousSession()) {
41-
$request->setDefaultLocale($request->getSession()->get('_locale',
42-
$this->defaultLocale));
41+
$request->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale));
4342
} else {
4443
$request->setDefaultLocale($this->defaultLocale);
4544
}
4645

47-
if (null !== $this->router && ($context = $this->router->getContext())
48-
&& !$context->hasParameter('_locale')) {
49-
$this->router->getContext()->setParameter('_locale',
50-
$request->getLocale());
51-
}
52-
}
53-
54-
public function onKernelRequest(GetResponseEvent $event)
55-
{
56-
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
57-
return;
58-
}
59-
60-
$request = $event->getRequest();
6146
if ($locale = $request->attributes->get('_locale')) {
6247
$request->setLocale($locale);
6348

@@ -67,21 +52,15 @@ public function onKernelRequest(GetResponseEvent $event)
6752
}
6853

6954
if (null !== $this->router) {
70-
$this->router->getContext()->setParameter('_locale',
71-
$request->getLocale());
55+
$this->router->getContext()->setParameter('_locale', $request->getLocale());
7256
}
7357
}
7458

7559
static public function getSubscribedEvents()
7660
{
7761
return array(
78-
KernelEvents::REQUEST => array(
79-
// must be registered after the session listener
80-
array('onEarlyKernelRequest', 255),
81-
82-
// must be registered after the Router to have access to the _locale
83-
array('onKernelRequest', 16),
84-
),
62+
// must be registered after the Router to have access to the _locale
63+
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
8564
);
8665
}
8766
}

tests/Symfony/Tests/Component/HttpKernel/EventListener/LocaleListenerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testDefaultLocaleWithoutSession()
2323
$listener = new LocaleListener('fr');
2424
$event = $this->getEvent($request = Request::create('/'));
2525

26-
$listener->onEarlyKernelRequest($event);
26+
$listener->onKernelRequest($event);
2727
$this->assertEquals('fr', $request->getLocale());
2828
}
2929

@@ -40,7 +40,7 @@ public function testDefaultLocaleWithSession()
4040
$listener = new LocaleListener('fr');
4141
$event = $this->getEvent($request);
4242

43-
$listener->onEarlyKernelRequest($event);
43+
$listener->onKernelRequest($event);
4444
$this->assertEquals('es', $request->getLocale());
4545
}
4646

@@ -55,8 +55,9 @@ public function testLocaleFromRequestAttribute()
5555
$event = $this->getEvent($request);
5656

5757
// also updates the session _locale value
58-
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array('set'), array(), '', false);
58+
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array('set', 'get'), array(), '', false);
5959
$session->expects($this->once())->method('set')->with('_locale', 'es');
60+
$session->expects($this->once())->method('get')->with('_locale')->will($this->returnValue('es'));
6061
$request->setSession($session);
6162

6263
$listener->onKernelRequest($event);

0 commit comments

Comments
 (0)