Permalink
Browse files

LocaleUpdateListener now gets the Request instance from FilterLocaleS…

…witchEvent
  • Loading branch information...
1 parent f1862cc commit 85b293ca15feaadd2db7f15410068c4cbb7a2af6 @lsmith77 lsmith77 committed Dec 7, 2012
@@ -10,13 +10,19 @@
namespace Lunetics\LocaleBundle\Event;
use Symfony\Component\EventDispatcher\Event;
+use Symfony\Component\HttpFoundation\Request;
/**
* Filter for the LocaleSwitchEvent
*/
class FilterLocaleSwitchEvent extends Event
{
/**
+ * @var Request
+ */
+ protected $request;
+
+ /**
* @var string
*/
protected $locale;
@@ -28,16 +34,27 @@ class FilterLocaleSwitchEvent extends Event
*
* @throws \InvalidArgumentException
*/
- public function __construct($locale)
+ public function __construct(Request $request, $locale)
{
if (!is_string($locale) || null == $locale || '' == $locale) {
throw new \InvalidArgumentException(sprintf('Wrong type, expected \'string\' got \'%s\'', $locale));
}
+ $this->request = $request;
$this->locale = $locale;
}
/**
+ * Returns the request
+ *
+ * @return Request
+ */
+ public function getRequest()
+ {
+ return $this->request;
+ }
+
+ /**
* Returns the locale string
*
* @return string
@@ -92,7 +92,7 @@ public function onKernelRequest(GetResponseEvent $event)
$this->logEvent('Setting [ %s ] as defaultLocale for the Request', $locale);
$request->setLocale($locale);
if ($manager->getGuesser('session') || $manager->getGuesser('cookie')) {
- $localeSwitchEvent = new FilterLocaleSwitchEvent($locale);
+ $localeSwitchEvent = new FilterLocaleSwitchEvent($request, $locale);
$this->dispatcher->dispatch(LocaleBundleEvents::onLocaleChange, $localeSwitchEvent);
}
@@ -10,11 +10,11 @@
namespace Lunetics\LocaleBundle\EventListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelEvents;
-use Symfony\Component\EventDispatcher\EventDispatcher;
-use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Lunetics\LocaleBundle\Cookie\LocaleCookie;
@@ -43,11 +43,6 @@ class LocaleUpdateListener implements EventSubscriberInterface
private $localeCookie;
/**
- * @var Request
- */
- private $request;
-
- /**
* @var array
*/
private $registeredGuessers;
@@ -60,23 +55,20 @@ class LocaleUpdateListener implements EventSubscriberInterface
/**
* Construct the Locale Update Listener
*
- * @param LocaleCookie $localeCookie Locale Cookie
- * @param LocaleSession $session Locale Session
- * @param Request $request Request
- * @param EventDispatcher $dispatcher Event Dispatcher
- * @param array $registeredGuessers List of registered guessers
- * @param LoggerInterface $logger Logger
+ * @param LocaleCookie $localeCookie Locale Cookie
+ * @param LocaleSession $session Locale Session
+ * @param EventDispatcher $dispatcher Event Dispatcher
+ * @param array $registeredGuessers List of registered guessers
+ * @param LoggerInterface $logger Logger
*/
public function __construct(LocaleCookie $localeCookie,
LocaleSession $session,
- Request $request,
EventDispatcher $dispatcher,
$registeredGuessers = array(),
LoggerInterface $logger = null)
{
$this->localeCookie = $localeCookie;
$this->session = $session;
- $this->request = $request;
$this->dispatcher = $dispatcher;
$this->logger = $logger;
$this->registeredGuessers = $registeredGuessers;
@@ -90,7 +82,7 @@ public function __construct(LocaleCookie $localeCookie,
public function onLocaleChange(FilterLocaleSwitchEvent $event)
{
$this->locale = $event->getLocale();
- $this->updateCookie($this->localeCookie->setCookieOnChange());
+ $this->updateCookie($event->getRequest(), $this->localeCookie->setCookieOnChange());
$this->updateSession();
}
@@ -101,11 +93,11 @@ public function onLocaleChange(FilterLocaleSwitchEvent $event)
*
* @return bool
*/
- public function updateCookie($update)
+ public function updateCookie(Request $request, $update)
{
if ($this->checkGuesser('cookie')
&& $update === true
- && $this->request->cookies->get($this->localeCookie->getName()) !== $this->locale
+ && $request->cookies->get($this->localeCookie->getName()) !== $this->locale
) {
$this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'updateCookieOnResponse'));
@@ -44,10 +44,9 @@
<tag name="kernel.event_subscriber"/>
</service>
- <service id="lunetics_locale.locale_update_listener" class="Lunetics\LocaleBundle\EventListener\LocaleUpdateListener" scope="request">
+ <service id="lunetics_locale.locale_update_listener" class="Lunetics\LocaleBundle\EventListener\LocaleUpdateListener">
<argument type="service" id="lunetics_locale.locale_cookie" />
<argument type="service" id="lunetics_locale.locale_session" />
- <argument type="service" id="request" />
<argument type="service" id="event_dispatcher" />
<argument>%lunetics_locale.guessing_order%</argument>
<argument type="service" id="logger" />
@@ -11,13 +11,16 @@
namespace Lunetics\LocaleBundle\Tests\Event;
use Lunetics\LocaleBundle\Event\FilterLocaleSwitchEvent;
+use Symfony\Component\HttpFoundation\Request;
class FilterLocaleSwitchEventTest extends \PHPUnit_Framework_TestCase
{
public function testFilterLocaleSwitchEvent()
{
+ $request = Request::create('/');
$locale = 'de';
- $filter = new FilterLocaleSwitchEvent($locale);
+ $filter = new FilterLocaleSwitchEvent($request, $locale);
+ $this->assertEquals('/', $filter->getRequest()->getPathInfo());
$this->assertEquals('de', $filter->getLocale());
}
@@ -27,7 +30,7 @@ public function testFilterLocaleSwitchEvent()
public function testThrowsInvalidTypeException($locale)
{
$this->setExpectedException('\InvalidArgumentException');
- new FilterLocaleSwitchEvent($locale);
+ new FilterLocaleSwitchEvent(Request::create('/'), $locale);
}
public function invalidType()
@@ -19,6 +19,7 @@
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Response;
+use Lunetics\LocaleBundle\Event\FilterLocaleSwitchEvent;
use Lunetics\LocaleBundle\EventListener\LocaleUpdateListener;
use Lunetics\LocaleBundle\Session\LocaleSession;
use Lunetics\LocaleBundle\Cookie\LocaleCookie;
@@ -42,38 +43,39 @@ public function setUp()
public function testCookieIsNotUpdatedNoGuesser()
{
- $listener = $this->getLocaleUpdateListener(array('session'), false, true);
+ $request = $this->getRequest(false);
+ $listener = $this->getLocaleUpdateListener(array('session'), true);
- $this->assertFalse($listener->updateCookie(true));
- $this->assertFalse($listener->updateCookie(false));
+ $this->assertFalse($listener->updateCookie($request, true));
+ $this->assertFalse($listener->updateCookie($request, false));
- $listener->onLocaleChange($this->getFilterLocaleSwitchEvent());
+ $listener->onLocaleChange($this->getFilterLocaleSwitchEvent(false));
$addedListeners = $this->dispatcher->getListeners(KernelEvents::RESPONSE);
$this->assertSame(array(), $addedListeners);
}
public function testCookieIsNotUpdatedOnSameLocale()
{
- $listener = $this->getLocaleUpdateListener(array('cookie'), true, true);
- $listener->onLocaleChange($this->getFilterLocaleSwitchEvent('de'));
+ $listener = $this->getLocaleUpdateListener(array('cookie'), true);
+ $listener->onLocaleChange($this->getFilterLocaleSwitchEvent(true, 'de'));
$addedListeners = $this->dispatcher->getListeners(KernelEvents::RESPONSE);
$this->assertSame(array(), $addedListeners);
}
public function testCookieIsUpdatedOnChange()
{
- $listener = $this->getLocaleUpdateListener(array('cookie'), false, true);
- $listener->onLocaleChange($this->getFilterLocaleSwitchEvent());
+ $listener = $this->getLocaleUpdateListener(array('cookie'), true);
+ $listener->onLocaleChange($this->getFilterLocaleSwitchEvent(false));
$addedListeners = $this->dispatcher->getListeners(KernelEvents::RESPONSE);
$this->assertContains('updateCookieOnResponse', $addedListeners[0]);
}
public function testCookieIsNotUpdatedWithFalseSetCookieOnChange()
{
- $listener = $this->getLocaleUpdateListener(array('cookie'), false, false);
- $listener->onLocaleChange($this->getFilterLocaleSwitchEvent());
+ $listener = $this->getLocaleUpdateListener(array('cookie'), false);
+ $listener->onLocaleChange($this->getFilterLocaleSwitchEvent(false));
$addedListeners = $this->dispatcher->getListeners(KernelEvents::RESPONSE);
$this->assertSame(array(), $addedListeners);
}
@@ -137,16 +139,15 @@ public function testNotUpdateSessionSameLocale()
$this->assertFalse($listener->updateSession());
}
- private function getFilterLocaleSwitchEvent($locale = 'fr')
+ private function getFilterLocaleSwitchEvent($withCookieSet = true, $locale = 'fr')
{
- return new \Lunetics\LocaleBundle\Event\FilterLocaleSwitchEvent($locale);
+ return new FilterLocaleSwitchEvent($this->getRequest($withCookieSet), $locale);
}
- private function getLocaleUpdateListener($registeredGuessers = array(), $withCookieSet = true, $updateCookie = false)
+ private function getLocaleUpdateListener($registeredGuessers = array(), $updateCookie = false)
{
$listener = new LocaleUpdateListener($this->getLocaleCookie($updateCookie),
$this->session,
- $this->getRequest($withCookieSet),
$this->dispatcher,
$registeredGuessers);

0 comments on commit 85b293c

Please sign in to comment.