Properly merge session cookie_* parameters

Prefixed following session options: 'lifetime', 'path', 'domain', 'secure',
 'httponly' because this results in better session driver code
This commit is contained in:
Miha Vrhovnik 2012-02-23 11:53:14 +01:00
parent 3de31c62d6
commit e0fba80057
5 changed files with 50 additions and 28 deletions

View File

@ -42,6 +42,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* added support for placeholders in route defaults and requirements (replaced by the value set in the service container)
* added Filesystem component as a dependency
* added support for hinclude (use ``standalone: 'js'`` in render tag)
* [BC BREAK] following session options: 'lifetime', 'path', 'domain', 'secure', 'httponly'
are now prefixed with cookie_ when dumped to the container
### MonologBundle

View File

@ -294,11 +294,17 @@ class FrameworkExtension extends Extension
// session storage
$container->setAlias('session.storage', $config['storage_id']);
$options = array();
foreach (array('name', 'lifetime', 'path', 'domain', 'secure', 'httponly', 'auto_start') as $key) {
foreach (array('name', 'auto_start') as $key) {
if (isset($config[$key])) {
$options[$key] = $config[$key];
}
}
//drivers require correct names for cookie options e.g the one with cookie_ prefix
foreach (array('lifetime', 'path', 'domain', 'secure', 'httponly') as $key) {
if (isset($config[$key])) {
$options['cookie_'.$key] = $config[$key];
}
}
$container->setParameter('session.storage.options', $options);
$this->addClassesToCompile(array(

View File

@ -82,11 +82,11 @@ abstract class FrameworkExtensionTest extends TestCase
$options = $container->getParameter('session.storage.options');
$this->assertEquals('_SYMFONY', $options['name']);
$this->assertEquals(86400, $options['lifetime']);
$this->assertEquals('/', $options['path']);
$this->assertEquals('example.com', $options['domain']);
$this->assertTrue($options['secure']);
$this->assertTrue($options['httponly']);
$this->assertEquals(86400, $options['cookie_lifetime']);
$this->assertEquals('/', $options['cookie_path']);
$this->assertEquals('example.com', $options['cookie_domain']);
$this->assertTrue($options['cookie_secure']);
$this->assertTrue($options['cookie_httponly']);
}
public function testTemplating()

View File

@ -200,28 +200,21 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
*/
protected function setOptions(array $options)
{
$cookieDefaults = session_get_cookie_params();
$this->options = array_merge(array(
'cookie_lifetime' => $cookieDefaults['lifetime'],
'cookie_path' => $cookieDefaults['path'],
'cookie_domain' => $cookieDefaults['domain'],
'cookie_secure' => $cookieDefaults['secure'],
'cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
), $options);
$this->options = $options;
// Unless session.cache_limiter has been set explicitly, disable it
// because this is managed by HeaderBag directly (if used).
if (!isset($this->options['cache_limiter'])) {
$this->options['cache_limiter'] = false;
}
// set defaults for certain values
$defaults = array(
'cache_limiter' => '', // disable by default because it's managed by HeaderBag (if used)
'auto_start' => true,
'use_cookies' => true,
'cookie_httponly' => true,
);
if (!isset($this->options['auto_start'])) {
$this->options['auto_start'] = 0;
}
if (!isset($this->options['use_cookies'])) {
$this->options['use_cookies'] = 1;
}
foreach ($defaults as $key => $value) {
if (!isset($this->options[$key])) {
$this->options[$key] = $value;
}
}
foreach ($this->options as $key => $value) {
if (in_array($key, array(

View File

@ -56,9 +56,9 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
/**
* @return AbstractSessionStorage
*/
protected function getStorage()
protected function getStorage($options = array())
{
$storage = new CustomHandlerSessionStorage();
$storage = new CustomHandlerSessionStorage($options);
$storage->registerBag(new AttributeBag);
return $storage;
@ -139,4 +139,25 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
$storage = new ConcreteSessionStorage(array('cache_limiter' => 'public'));
$this->assertEquals('public', ini_get('session.cache_limiter'));
}
public function testCookieOptions()
{
$options = array(
'cookie_lifetime' => 123456,
'cookie_path' => '/my/cookie/path',
'cookie_domain' => 'symfony2.example.com',
'cookie_secure' => true,
'cookie_httponly' => false,
);
$this->getStorage($options);
$temp = session_get_cookie_params();
$gco = array();
foreach ($temp as $key => $value) {
$gco['cookie_'.$key] = $value;
}
$this->assertEquals($options, $gco);
}
}