Skip to content

Commit

Permalink
Merge pull request #40 from lunetics/fix_listener
Browse files Browse the repository at this point in the history
Using EventSubscriber instead of Event Listener, because the LocaleListener must be called before the Symfony LocaleListener, which sets the locale for the routing.
  • Loading branch information
lunetics committed Oct 27, 2012
2 parents a5d8238 + 0908fd5 commit 7caf98f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
33 changes: 27 additions & 6 deletions EventListener/LocaleListener.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager;
use Lunetics\LocaleBundle\Event\FilterLocaleSwitchEvent;
Expand All @@ -26,7 +27,7 @@
* @author Christophe Willemsen <willemsen.christophe@gmail.com>
* @author Matthias Breddin <mb@lunetics.com>
*/
class LocaleListener
class LocaleListener implements EventSubscriberInterface
{
/**
* @var string Default framework locale
Expand Down Expand Up @@ -77,6 +78,8 @@ public function onKernelRequest(GetResponseEvent $event)
/** @var $request \Symfony\Component\HttpFoundation\Request */
$request = $event->getRequest();

$request->setDefaultLocale($this->defaultLocale);

if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST && !$request->isXmlHttpRequest()) {
$this->logEvent('Request is not a "MASTER_REQUEST" : SKIPPING...');

Expand All @@ -93,15 +96,21 @@ public function onKernelRequest(GetResponseEvent $event)
$this->dispatcher->dispatch(LocaleBundleEvents::onLocaleChange, $localeSwitchEvent);
}

$this->dispatcher->addListener(KernelEvents::RESPONSE, function(FilterResponseEvent $event) {
return $event->getResponse()->setVary('Accept-Language');
});

return;
}
$request->setDefaultLocale($this->defaultLocale);
}

/**
* This Listener adds a vary header to all responses.
*
* @param FilterResponseEvent $event
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function onLocaleDetectedSetVaryHeader(FilterResponseEvent $event)
{
return $event->getResponse()->setVary('Accept-Language');
}
/**
* DI Setter for the EventDispatcher
*
Expand All @@ -124,4 +133,16 @@ private function logEvent($logMessage, $parameters = null)
$this->logger->info(sprintf($logMessage, $parameters));
}
}

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
// must be registered after the Router to have access to the _locale and before the Symfony LocaleListener
KernelEvents::REQUEST => array(array('onKernelRequest', 24)),
KernelEvents::RESPONSE => array('onLocaleDetectedSetVaryHeader')
);
}
}
19 changes: 18 additions & 1 deletion EventListener/LocaleUpdateListener.php
Expand Up @@ -15,17 +15,19 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Lunetics\LocaleBundle\Cookie\LocaleCookie;
use Lunetics\LocaleBundle\Event\FilterLocaleSwitchEvent;
use Lunetics\LocaleBundle\Session\LocaleSession;
use Lunetics\LocaleBundle\LocaleBundleEvents;

/**
* Locale Update Listener
*
* @author Matthias Breddin <mb@lunetics.com>
*/
class LocaleUpdateListener
class LocaleUpdateListener implements EventSubscriberInterface
{
/**
* @var string
Expand Down Expand Up @@ -96,6 +98,8 @@ public function onLocaleChange(FilterLocaleSwitchEvent $event)
* Update Cookie Section
*
* @param bool $update If cookie should be updated
*
* @return bool
*/
public function updateCookie($update)
{
Expand Down Expand Up @@ -132,6 +136,8 @@ public function updateCookieOnResponse(FilterResponseEvent $event)

/**
* Update Session section
*
* @return bool
*/
public function updateSession()
{
Expand All @@ -158,4 +164,15 @@ private function checkGuesser($guesser)
{
return in_array($guesser, $this->registeredGuessers);
}

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
// must be registered after the Router to have access to the _locale and before the Symfony LocaleListener
LocaleBundleEvents::onLocaleChange => array('onLocaleChange')
);
}
}
6 changes: 3 additions & 3 deletions Resources/config/services.xml
Expand Up @@ -35,13 +35,13 @@
</service>

<service id="lunetics_locale.locale_listener" class="%lunetics_locale.request_listener.class%">
<argument>%locale%</argument>
<argument>%kernel.default_locale%</argument>
<argument type="service" id="lunetics_locale.guesser_manager" />
<argument type="service" id="logger" />
<call method="setEventDispatcher">
<argument type="service" id="event_dispatcher"/>
</call>
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest"/>
<tag name="kernel.event_subscriber"/>
</service>

<service id="lunetics_locale.locale_update_listener" class="Lunetics\LocaleBundle\EventListener\LocaleUpdateListener" scope="request">
Expand All @@ -51,7 +51,7 @@
<argument type="service" id="event_dispatcher" />
<argument>%lunetics_locale.guessing_order%</argument>
<argument type="service" id="logger" />
<tag name="kernel.event_listener" event="lunetics_locale.change" method="onLocaleChange" />
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>

0 comments on commit 7caf98f

Please sign in to comment.