Skip to content

Commit

Permalink
refactored session configuration
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
fabpot committed Apr 22, 2011
1 parent f1e14f5 commit 7644e86
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 126 deletions.
Expand Up @@ -145,36 +145,16 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
->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()
Expand Down
Expand Up @@ -85,7 +85,7 @@ public function load(array $configs, ContainerBuilder $container)
if (!empty($config['test'])) {
$loader->load('test.xml');
if (isset($config['session'])) {
$config['session']['storage_id'] = 'filesystem';
$config['session']['storage_id'] = 'session.storage.filesystem';
}
}

Expand Down Expand Up @@ -281,25 +281,22 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
{
$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',
Expand Down
Expand Up @@ -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" />
Expand All @@ -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">
Expand Down
15 changes: 2 additions & 13 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml
Expand Up @@ -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>
Expand All @@ -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>
Expand Up @@ -19,9 +19,8 @@
),
'session' => array(
'auto_start' => true,
'class' => 'Session',
'default_locale' => 'fr',
'storage_id' => 'native',
'storage_id' => 'session.storage.native',
'name' => '_SYMFONY',
'lifetime' => 86400,
'path' => '/',
Expand Down

This file was deleted.

Expand Up @@ -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>
Expand Down

This file was deleted.

Expand Up @@ -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: /
Expand Down

This file was deleted.

Expand Up @@ -75,10 +75,9 @@ public function testSession()
$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']);
Expand All @@ -87,18 +86,6 @@ public function testSession()
$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');
Expand Down
Expand Up @@ -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())
{
Expand Down
Expand Up @@ -19,23 +19,32 @@
*/
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);
}
Expand Down Expand Up @@ -98,8 +107,8 @@ public function sessionClose()
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.'= ?';
Expand Down Expand Up @@ -127,8 +136,8 @@ public function sessionDestroy($id)
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);
Expand All @@ -154,10 +163,10 @@ public function sessionGC($lifetime)
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.'=?';
Expand Down Expand Up @@ -196,10 +205,10 @@ public function sessionRead($id)
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.'= ?';

Expand Down Expand Up @@ -230,10 +239,10 @@ public function sessionWrite($id, $data)
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 (?, ?, ?)';

Expand Down

0 comments on commit 7644e86

Please sign in to comment.