refactored session configuration

* made the options array only for "global" options that are valid for all session storages
 * changed the PDO session storage constructor signature to accept an array of options for DB configuration
 * changed the storage_id to be the full service id, instead of just part of it
 * removed the class parameter for session as it can be changed via the .class parameter (it was the only example in the framework)
 * removed the configuration for the PDO session storage for now
This commit is contained in:
Fabien Potencier 2011-04-22 12:35:09 +02:00
parent f1e14f5ad3
commit 7644e86683
13 changed files with 51 additions and 126 deletions

View File

@ -145,36 +145,16 @@ class Configuration implements ConfigurationInterface
->children()
->arrayNode('session')
->canBeUnset()
// Strip "pdo." prefix from option keys, since dots cannot appear in node names
->beforeNormalization()
->ifArray()
->then(function($v){
foreach ($v as $key => $value) {
if (0 === strncmp('pdo.', $key, 4)) {
$v[substr($key, 4)] = $value;
unset($v[$key]);
}
}
return $v;
})
->end()
->children()
->booleanNode('auto_start')->end()
->scalarNode('class')->end()
->scalarNode('default_locale')->defaultValue('en')->end()
->scalarNode('storage_id')->defaultValue('native')->end()
// NativeSessionStorage options
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
->scalarNode('name')->end()
->scalarNode('lifetime')->end()
->scalarNode('path')->end()
->scalarNode('domain')->end()
->booleanNode('secure')->end()
->booleanNode('httponly')->end()
// PdoSessionStorage options
->scalarNode('db_table')->end()
->scalarNode('db_id_col')->end()
->scalarNode('db_data_col')->end()
->scalarNode('db_time_col')->end()
->end()
->end()
->end()

View File

@ -85,7 +85,7 @@ class FrameworkExtension extends Extension
if (!empty($config['test'])) {
$loader->load('test.xml');
if (isset($config['session'])) {
$config['session']['storage_id'] = 'filesystem';
$config['session']['storage_id'] = 'session.storage.filesystem';
}
}
@ -281,25 +281,22 @@ class FrameworkExtension extends Extension
{
$loader->load('session.xml');
// session
$session = $container->getDefinition('session');
if (!empty($config['auto_start'])) {
$container->getDefinition('session')->addMethodCall('start');
$session->addMethodCall('start');
}
$session->replaceArgument(1, $config['default_locale']);
if (isset($config['class'])) {
$container->setParameter('session.class', $config['class']);
}
$container->getDefinition('session')->replaceArgument(1, $config['default_locale']);
$container->setAlias('session.storage', 'session.storage.'.$config['storage_id']);
$options = $container->getParameter('session.storage.'.$config['storage_id'].'.options');
foreach (array('name', 'lifetime', 'path', 'domain', 'secure', 'httponly', 'db_table', 'db_id_col', 'db_data_col', 'db_time_col') as $key) {
// session storage
$container->setAlias('session.storage', $config['storage_id']);
$options = array();
foreach (array('name', 'lifetime', 'path', 'domain', 'secure', 'httponly') as $key) {
if (isset($config[$key])) {
$options[$key] = $config[$key];
}
}
$container->setParameter('session.storage.'.$config['storage_id'].'.options', $options);
$container->setParameter('session.storage.options', $options);
$this->addClassesToCompile(array(
'Symfony\\Component\\HttpFoundation\\Session',

View File

@ -74,7 +74,6 @@
</xsd:complexType>
<xsd:complexType name="session">
<xsd:attribute name="class" type="xsd:string" />
<xsd:attribute name="storage-id" type="xsd:string" />
<xsd:attribute name="default-locale" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
@ -85,10 +84,6 @@
<xsd:attribute name="httponly" type="xsd:boolean" />
<xsd:attribute name="cache-limiter" type="xsd:string" />
<xsd:attribute name="auto-start" type="xsd:boolean" />
<xsd:attribute name="pdo.db-table" type="xsd:string" />
<xsd:attribute name="pdo.db-id-col" type="xsd:string" />
<xsd:attribute name="pdo.db-data-col" type="xsd:string" />
<xsd:attribute name="pdo.db-time-col" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="templating">

View File

@ -7,11 +7,7 @@
<parameters>
<parameter key="session.class">Symfony\Component\HttpFoundation\Session</parameter>
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage</parameter>
<parameter key="session.storage.native.options" type="collection" />
<parameter key="session.storage.pdo.class">Symfony\Component\HttpFoundation\SessionStorage\PdoSessionStorage</parameter>
<parameter key="session.storage.pdo.options" type="collection" />
<parameter key="session.storage.filesystem.class">Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage</parameter>
<parameter key="session.storage.filesystem.options" type="collection" />
</parameters>
<services>
@ -21,19 +17,12 @@
</service>
<service id="session.storage.native" class="%session.storage.native.class%" public="false">
<argument>%session.storage.native.options%</argument>
</service>
<service id="session.storage.pdo" class="%session.storage.pdo.class%" public="false">
<argument type="service" id="pdo_connection" />
<argument>%session.storage.pdo.options%</argument>
<argument>%session.storage.options%</argument>
</service>
<service id="session.storage.filesystem" class="%session.storage.filesystem.class%" public="false">
<argument>%kernel.cache_dir%/sessions</argument>
<argument>%session.storage.filesystem.options%</argument>
<argument>%session.storage.options%</argument>
</service>
<service id="session.storage" alias="session.storage.native" public="false" />
</services>
</container>

View File

@ -19,9 +19,8 @@ $container->loadFromExtension('framework', array(
),
'session' => array(
'auto_start' => true,
'class' => 'Session',
'default_locale' => 'fr',
'storage_id' => 'native',
'storage_id' => 'session.storage.native',
'name' => '_SYMFONY',
'lifetime' => 86400,
'path' => '/',

View File

@ -1,11 +0,0 @@
<?php
$container->loadFromExtension('framework', array(
'session' => array(
'storage_id' => 'pdo',
'pdo.db_table' => 'table',
'pdo.db_id_col' => 'id',
'pdo.db_data_col' => 'data',
'pdo.db_time_col' => 'time',
),
));

View File

@ -11,7 +11,7 @@
<framework:esi enabled="true" />
<framework:profiler only-exceptions="true" />
<framework:router cache-warmer="true" resource="%kernel.root_dir%/config/routing.xml" type="xml" />
<framework:session auto-start="true" class="Session" default-locale="fr" storage-id="native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
<framework:session auto-start="true" default-locale="fr" storage-id="session.storage.native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
<framework:templating assets-version="SomeVersionScheme" cache-warmer="true" cache="/path/to/cache" >
<framework:loader>loader.foo</framework:loader>
<framework:loader>loader.bar</framework:loader>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:session storage-id="pdo" pdo.db-table="table" pdo.db-id-col="id" pdo.db-data-col="data" pdo.db-time-col="time" />
</framework:config>
</container>

View File

@ -13,9 +13,8 @@ framework:
type: xml
session:
auto_start: true
class: Session
default_locale: fr
storage_id: native
storage_id: session.storage.native
name: _SYMFONY
lifetime: 86400
path: /

View File

@ -1,7 +0,0 @@
framework:
session:
storage_id: pdo
pdo.db_table: table
pdo.db_id_col: id
pdo.db_data_col: data
pdo.db_time_col: time

View File

@ -75,10 +75,9 @@ abstract class FrameworkExtensionTest extends TestCase
$arguments = $container->getDefinition('session')->getArguments();
$this->assertEquals('fr', $arguments[1]);
$this->assertTrue($container->getDefinition('session')->hasMethodCall('start'));
$this->assertEquals('Session', $container->getParameter('session.class'));
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
$options = $container->getParameter('session.storage.native.options');
$options = $container->getParameter('session.storage.options');
$this->assertEquals('_SYMFONY', $options['name']);
$this->assertEquals(86400, $options['lifetime']);
$this->assertEquals('/', $options['path']);
@ -87,18 +86,6 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertTrue($options['httponly']);
}
public function testSessionPdo()
{
$container = $this->createContainerFromFile('session_pdo');
$options = $container->getParameter('session.storage.pdo.options');
$this->assertEquals('session.storage.pdo', (string) $container->getAlias('session.storage'));
$this->assertEquals('table', $options['db_table']);
$this->assertEquals('id', $options['db_id_col']);
$this->assertEquals('data', $options['db_data_col']);
$this->assertEquals('time', $options['db_time_col']);
}
public function testTemplating()
{
$container = $this->createContainerFromFile('full');

View File

@ -36,7 +36,7 @@ class NativeSessionStorage implements SessionStorageInterface
*
* The default values for most options are those returned by the session_get_cookie_params() function
*
* @param array $options An associative array of options
* @param array $options An associative array of session options
*/
public function __construct(array $options = array())
{

View File

@ -19,23 +19,32 @@ namespace Symfony\Component\HttpFoundation\SessionStorage;
*/
class PdoSessionStorage extends NativeSessionStorage
{
protected $db;
private $db;
private $dbOptions;
/**
* Constructor.
*
* @param \PDO $pdo A PDO instance
* @param array $options An associative array of session options
* @param array $dbOptions An associative array of DB options
*
* @throws \InvalidArgumentException When "db_table" option is not provided
*
* @see NativeSessionStorage::__construct()
*/
public function __construct(\PDO $db, $options = null)
public function __construct(\PDO $db, array $options = array(), array $dbOptions = array())
{
if (!array_key_exists('db_table', $dbOptions)) {
throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
}
$this->db = $db;
$options = array_merge(array(
$this->dbOptions = array_merge(array(
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
), $options);
if (!array_key_exists('db_table', $options)) {
throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
}
), $dbOptions);
parent::__construct($options);
}
@ -98,8 +107,8 @@ class PdoSessionStorage extends NativeSessionStorage
public function sessionDestroy($id)
{
// get table/column
$dbTable = $this->options['db_table'];
$dbIdCol = $this->options['db_id_col'];
$dbTable = $this->dbOptions['db_table'];
$dbIdCol = $this->dbOptions['db_id_col'];
// delete the record associated with this id
$sql = 'DELETE FROM '.$dbTable.' WHERE '.$dbIdCol.'= ?';
@ -127,8 +136,8 @@ class PdoSessionStorage extends NativeSessionStorage
public function sessionGC($lifetime)
{
// get table/column
$dbTable = $this->options['db_table'];
$dbTimeCol = $this->options['db_time_col'];
$dbTable = $this->dbOptions['db_table'];
$dbTimeCol = $this->dbOptions['db_time_col'];
// delete the record associated with this id
$sql = 'DELETE FROM '.$dbTable.' WHERE '.$dbTimeCol.' < '.(time() - $lifetime);
@ -154,10 +163,10 @@ class PdoSessionStorage extends NativeSessionStorage
public function sessionRead($id)
{
// get table/columns
$dbTable = $this->options['db_table'];
$dbDataCol = $this->options['db_data_col'];
$dbIdCol = $this->options['db_id_col'];
$dbTimeCol = $this->options['db_time_col'];
$dbTable = $this->dbOptions['db_table'];
$dbDataCol = $this->dbOptions['db_data_col'];
$dbIdCol = $this->dbOptions['db_id_col'];
$dbTimeCol = $this->dbOptions['db_time_col'];
try {
$sql = 'SELECT '.$dbDataCol.' FROM '.$dbTable.' WHERE '.$dbIdCol.'=?';
@ -196,10 +205,10 @@ class PdoSessionStorage extends NativeSessionStorage
public function sessionWrite($id, $data)
{
// get table/column
$dbTable = $this->options['db_table'];
$dbDataCol = $this->options['db_data_col'];
$dbIdCol = $this->options['db_id_col'];
$dbTimeCol = $this->options['db_time_col'];
$dbTable = $this->dbOptions['db_table'];
$dbDataCol = $this->dbOptions['db_data_col'];
$dbIdCol = $this->dbOptions['db_id_col'];
$dbTimeCol = $this->dbOptions['db_time_col'];
$sql = 'UPDATE '.$dbTable.' SET '.$dbDataCol.' = ?, '.$dbTimeCol.' = '.time().' WHERE '.$dbIdCol.'= ?';
@ -230,10 +239,10 @@ class PdoSessionStorage extends NativeSessionStorage
private function createNewSession($id, $data = '')
{
// get table/column
$dbTable = $this->options['db_table'];
$dbDataCol = $this->options['db_data_col'];
$dbIdCol = $this->options['db_id_col'];
$dbTimeCol = $this->options['db_time_col'];
$dbTable = $this->dbOptions['db_table'];
$dbDataCol = $this->dbOptions['db_data_col'];
$dbIdCol = $this->dbOptions['db_id_col'];
$dbTimeCol = $this->dbOptions['db_time_col'];
$sql = 'INSERT INTO '.$dbTable.'('.$dbIdCol.', '.$dbDataCol.', '.$dbTimeCol.') VALUES (?, ?, ?)';