Permalink
Browse files

Cookie was now configurable (name, lifetime, path, domain, secure, ht…

…tponly)
  • Loading branch information...
jpierront committed Oct 7, 2012
1 parent aff49a7 commit 164c4b4286d9757be9485926f83449dfe5d5028f
@@ -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')
@@ -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()
;
@@ -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(),
@@ -66,11 +67,18 @@ public function load(array $configs, ContainerBuilder $container)
$container
->getDefinition('jms_i18n_routing.locale_resolver.default')
+ ->addArgument($config['cookie']['enabled'])
->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))
;
@@ -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()) {
@@ -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));
}
}
}
@@ -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" />
@@ -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;
}
@@ -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;
@@ -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;
@@ -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',
));

0 comments on commit 164c4b4

Please sign in to comment.