Skip to content

Commit

Permalink
Merge pull request #70 from jpierront/master
Browse files Browse the repository at this point in the history
Configurable cookie (name, lifetime, path, domain, secure, httponly)
  • Loading branch information
schmittjoh committed Oct 8, 2012
2 parents aff49a7 + fcfdf0e commit c1be05b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
25 changes: 24 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public function getConfigTreeBuilder()
return $v;
})
->end()
->beforeNormalization()
->always()
->then(function($v) {
if (isset($v['use_cookie'])) {
$v['cookie']['enabled'] = $v['use_cookie'];
unset($v['use_cookie']);
}

return $v;
})
->end()
->children()
->scalarNode('default_locale')->isRequired()->end()
->arrayNode('locales')
Expand Down Expand Up @@ -90,7 +101,19 @@ public function getConfigTreeBuilder()
->prototype('scalar')->end()
->end()
->booleanNode('redirect_to_host')->defaultTrue()->end()
->booleanNode('use_cookie')->defaultTrue()->end()
->booleanNode('use_cookie')->defaultTrue()->info('DEPRECATED! Please use: cookie.enabled')->end()
->arrayNode('cookie')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultTrue()->end()
->scalarNode('name')->defaultValue('hl')->cannotBeEmpty()->end()
->scalarNode('lifetime')->defaultValue(31536000)->end()
->scalarNode('path')->defaultNull('/')->end()
->scalarNode('domain')->defaultNull('')->end()
->booleanNode('secure')->defaultFalse()->end()
->booleanNode('httponly')->defaultFalse()->end()
->end()
->end()
->end()
->end()
;
Expand Down
9 changes: 8 additions & 1 deletion DependencyInjection/JMSI18nRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('jms_i18n_routing.catalogue', $config['catalogue']);
$container->setParameter('jms_i18n_routing.strategy', $config['strategy']);
$container->setParameter('jms_i18n_routing.redirect_to_host', $config['redirect_to_host']);
$container->setParameter('jms_i18n_routing.cookie.name', $config['cookie']['name']);

$this->addClassesToCompile(array(
$container->getDefinition('jms_i18n_routing.router')->getClass(),
Expand All @@ -68,9 +69,15 @@ public function load(array $configs, ContainerBuilder $container)
->getDefinition('jms_i18n_routing.locale_resolver.default')
->addArgument(array_flip($config['hosts']))
;
} elseif ($config['use_cookie']) {
} elseif ($config['cookie']['enabled']) {
$container
->getDefinition('jms_i18n_routing.cookie_setting_listener')
->addArgument($config['cookie']['name'])
->addArgument($config['cookie']['lifetime'])
->addArgument($config['cookie']['path'])
->addArgument($config['cookie']['domain'])
->addArgument($config['cookie']['secure'])
->addArgument($config['cookie']['httponly'])
->setPublic(true)
->addTag('kernel.event_listener', array('event' => 'kernel.response', 'priority' => 256))
;
Expand Down
23 changes: 20 additions & 3 deletions EventListener/CookieSettingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@
*/
class CookieSettingListener
{
private $cookieName;
private $cookieLifetime;
private $cookiePath;
private $cookieDomain;
private $cookieSecure;
private $cookieHttponly;

public function __construct($cookieName, $cookieLifetime, $cookiePath, $cookieDomain, $cookieSecure, $cookieHttponly)
{
$this->cookieName = $cookieName;
$this->cookieLifetime = $cookieLifetime;
$this->cookiePath = $cookiePath;
$this->cookieDomain = $cookieDomain;
$this->cookieSecure = $cookieSecure;
$this->cookieHttponly = $cookieHttponly;
}

public function onKernelResponse(FilterResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
Expand All @@ -24,9 +41,9 @@ public function onKernelResponse(FilterResponseEvent $event)

$request = $event->getRequest();

if (!$request->cookies->has('hl')
|| $request->cookies->get('hl') !== $request->getLocale()) {
$event->getResponse()->headers->setCookie(new Cookie('hl', $request->getLocale(), time() + 86400 * 365));
if (!$request->cookies->has($this->cookieName)
|| $request->cookies->get($this->cookieName) !== $request->getLocale()) {
$event->getResponse()->headers->setCookie(new Cookie($this->cookieName, $request->getLocale(), time() + $this->cookieLifetime, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttponly));
}
}
}
6 changes: 4 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
</parameters>

<services>
<service id="jms_i18n_routing.locale_resolver.default" class="%jms_i18n_routing.locale_resolver.class%" public="false" />
<service id="jms_i18n_routing.locale_resolver.default" class="%jms_i18n_routing.locale_resolver.class%" public="false">
<argument>%jms_i18n_routing.cookie.name%</argument>
</service>
<service id="jms_i18n_routing.locale_resolver" alias="jms_i18n_routing.locale_resolver.default" public="false" />

<service id="jms_i18n_routing.router" class="%jms_i18n_routing.router.class%" parent="router.default" public="false">
<call method="setLocaleResolver">
<argument type="service" id="jms_i18n_routing.locale_resolver" />
Expand Down
8 changes: 5 additions & 3 deletions Router/DefaultLocaleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/
class DefaultLocaleResolver implements LocaleResolverInterface
{
private $cookieName;
private $hostMap;

public function __construct(array $hostMap = array())
public function __construct($cookieName, array $hostMap = array())
{
$this->cookieName = $cookieName;
$this->hostMap = $hostMap;
}

Expand Down Expand Up @@ -53,8 +55,8 @@ public function resolveLocale(Request $request, array $availableLocales)
}

// if user sends a cookie, use it
if ($request->cookies->has('hl')) {
$hostLanguage = $request->cookies->get('hl');
if ($request->cookies->has($this->cookieName)) {
$hostLanguage = $request->cookies->get($this->cookieName);

if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) {
return $hostLanguage;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Router/DefaultLocaleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace JMS\I18nRoutingBundle\Tests\Router;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Kernel;

use JMS\I18nRoutingBundle\Router\DefaultLocaleResolver;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function getResolutionTests()

protected function setUp()
{
$this->resolver = new DefaultLocaleResolver(array(
$this->resolver = new DefaultLocaleResolver('hl', array(
'foo' => 'en',
'bar' => 'de',
));
Expand Down

0 comments on commit c1be05b

Please sign in to comment.