merged branch drak/session_interface (PR #3342)
Commits
-------
388a9c2
[HttpFoundation] Make SessionHandlerInterface compatible with PHP 5.4's SessionHandlerInterface
Discussion
----------
[HttpFoundation] Make Sessions compatible with PHP 5.4's SessioHandlerInterface
Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
This commit is contained in:
commit
a375c1289e
@ -240,9 +240,10 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
||||
attributes. `Session->getFlashBag()->all()` clears flashes now.
|
||||
* Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` base class for
|
||||
session storage drivers.
|
||||
* Added `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` interface
|
||||
* Added `Symfony\Component\HttpFoundation\Session\Storage\SessionHandlerInterface` interface
|
||||
which storage drivers should implement after inheriting from
|
||||
`Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` when writing custom session save handlers.
|
||||
`Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` when writing custom
|
||||
session save handlers. This interface is also compatible with PHP 5.4's SessionHandlerInterface.
|
||||
* [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and `remove()`. Added
|
||||
`getBag()`, `registerBag()`.
|
||||
* Moved attribute storage to `Symfony\Component\HttpFoundation\Attribute\AttributeBagInterface`.
|
||||
|
@ -4,7 +4,7 @@ namespace Symfony\Bridge\Doctrine\HttpFoundation;
|
||||
|
||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionHandlerInterface;
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
|
||||
/**
|
||||
@ -13,7 +13,7 @@ use Doctrine\DBAL\Driver\Connection;
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
class DbalSessionStorage extends AbstractSessionStorage implements SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
@ -47,7 +47,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
*
|
||||
* @return Boolean true, if the session was opened, otherwise an exception is thrown
|
||||
*/
|
||||
public function openSession($path = null, $name = null)
|
||||
public function open($path = null, $name = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -57,7 +57,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
*
|
||||
* @return Boolean true, if the session was closed, otherwise false
|
||||
*/
|
||||
public function closeSession()
|
||||
public function close()
|
||||
{
|
||||
// do nothing
|
||||
return true;
|
||||
@ -72,7 +72,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
*
|
||||
* @throws \RuntimeException If the session cannot be destroyed
|
||||
*/
|
||||
public function destroySession($id)
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
$this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_id = :id", array(
|
||||
@ -94,7 +94,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
*
|
||||
* @throws \RuntimeException If any old sessions cannot be cleaned
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
public function gc($lifetime)
|
||||
{
|
||||
try {
|
||||
$this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array(
|
||||
@ -116,7 +116,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
*
|
||||
* @throws \RuntimeException If the session cannot be read
|
||||
*/
|
||||
public function readSession($id)
|
||||
public function read($id)
|
||||
{
|
||||
try {
|
||||
$data = $this->con->executeQuery("SELECT sess_data FROM {$this->tableName} WHERE sess_id = :id", array(
|
||||
@ -146,7 +146,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
*
|
||||
* @throws \RuntimeException If the session data cannot be written
|
||||
*/
|
||||
public function writeSession($id, $data)
|
||||
public function write($id, $data)
|
||||
{
|
||||
$platform = $this->con->getDatabasePlatform();
|
||||
|
||||
|
@ -248,18 +248,18 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
|
||||
* When the session starts, PHP will call the sessionRead() handler which should return an array
|
||||
* of any session attributes. PHP will then populate these into $_SESSION.
|
||||
*
|
||||
* When PHP shuts down, the sessionWrite() handler is called and will pass the $_SESSION contents
|
||||
* When PHP shuts down, the write() handler is called and will pass the $_SESSION contents
|
||||
* to be stored.
|
||||
*
|
||||
* When a session is specifically destroyed, PHP will call the sessionDestroy() handler with the
|
||||
* When a session is specifically destroyed, PHP will call the destroy() handler with the
|
||||
* session ID. This happens when the session is regenerated for example and th handler
|
||||
* MUST delete the session by ID from the persistent storage immediately.
|
||||
*
|
||||
* PHP will call sessionGc() from time to time to expire any session records according to the
|
||||
* PHP will call gc() from time to time to expire any session records according to the
|
||||
* set max lifetime of a session. This routine should delete all records from persistent
|
||||
* storage which were last accessed longer than the $lifetime.
|
||||
*
|
||||
* PHP sessionOpen() and sessionClose() are pretty much redundant and can just return true.
|
||||
* PHP open() and close() are pretty much redundant and can just return true.
|
||||
*
|
||||
* NOTE:
|
||||
*
|
||||
@ -270,20 +270,21 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
|
||||
* ini_set('session.save_path', /tmp');
|
||||
*
|
||||
* @see http://php.net/manual/en/function.session-set-save-handler.php
|
||||
* @see SessionSaveHandlerInterface
|
||||
* @see \SessionHandlerInterface
|
||||
* @see http://php.net/manual/en/class.sessionhandlerinterface.php
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
// note this can be reset to PHP's control using ini_set('session.save_handler', 'files');
|
||||
// so long as ini_set() is called before the session is started.
|
||||
if ($this instanceof SessionSaveHandlerInterface) {
|
||||
if ($this instanceof \SessionHandlerInterface || $this instanceof SessionHandlerInterface) {
|
||||
session_set_save_handler(
|
||||
array($this, 'openSession'),
|
||||
array($this, 'closeSession'),
|
||||
array($this, 'readSession'),
|
||||
array($this, 'writeSession'),
|
||||
array($this, 'destroySession'),
|
||||
array($this, 'gcSession')
|
||||
array($this, 'open'),
|
||||
array($this, 'close'),
|
||||
array($this, 'read'),
|
||||
array($this, 'write'),
|
||||
array($this, 'destroy'),
|
||||
array($this, 'gc')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
class MemcacheSessionStorage extends AbstractSessionStorage implements SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Memcache driver.
|
||||
@ -85,7 +85,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($savePath, $sessionName)
|
||||
public function open($savePath, $sessionName)
|
||||
{
|
||||
foreach ($this->memcacheOptions['serverpool'] as $server) {
|
||||
$this->memcache->addServer($server);
|
||||
@ -97,7 +97,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function closeSession()
|
||||
public function close()
|
||||
{
|
||||
return $this->memcache->close();
|
||||
}
|
||||
@ -105,7 +105,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($sessionId)
|
||||
public function read($sessionId)
|
||||
{
|
||||
return $this->memcache->get($this->prefix.$sessionId) ?: '';
|
||||
}
|
||||
@ -113,7 +113,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($sessionId, $data)
|
||||
public function write($sessionId, $data)
|
||||
{
|
||||
return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']);
|
||||
}
|
||||
@ -121,7 +121,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($sessionId)
|
||||
public function destroy($sessionId)
|
||||
{
|
||||
return $this->memcache->delete($this->prefix.$sessionId);
|
||||
}
|
||||
@ -129,7 +129,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
public function gc($lifetime)
|
||||
{
|
||||
// not required here because memcache will auto expire the records anyhow.
|
||||
return true;
|
||||
|
@ -16,7 +16,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class MemcachedSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
class MemcachedSessionStorage extends AbstractSessionStorage implements SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Memcached driver.
|
||||
@ -65,7 +65,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($savePath, $sessionName)
|
||||
public function open($savePath, $sessionName)
|
||||
{
|
||||
return $this->memcached->addServers($this->memcachedOptions['serverpool']);
|
||||
}
|
||||
@ -73,7 +73,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function closeSession()
|
||||
public function close()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -81,7 +81,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($sessionId)
|
||||
public function read($sessionId)
|
||||
{
|
||||
return $this->memcached->get($sessionId) ?: '';
|
||||
}
|
||||
@ -89,7 +89,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($sessionId, $data)
|
||||
public function write($sessionId, $data)
|
||||
{
|
||||
return $this->memcached->set($sessionId, $data, false, $this->memcachedOptions['expiretime']);
|
||||
}
|
||||
@ -97,7 +97,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($sessionId)
|
||||
public function destroy($sessionId)
|
||||
{
|
||||
return $this->memcached->delete($sessionId);
|
||||
}
|
||||
@ -105,7 +105,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
public function gc($lifetime)
|
||||
{
|
||||
// not required here because memcached will auto expire the records anyhow.
|
||||
return true;
|
||||
|
@ -20,12 +20,12 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
class NullSessionStorage extends AbstractSessionStorage implements SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($savePath, $sessionName)
|
||||
public function open($savePath, $sessionName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -33,7 +33,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function closeSession()
|
||||
public function close()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -41,7 +41,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($sessionId)
|
||||
public function read($sessionId)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
@ -49,7 +49,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($sessionId, $data)
|
||||
public function write($sessionId, $data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -57,7 +57,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($sessionId)
|
||||
public function destroy($sessionId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -65,7 +65,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
public function gc($lifetime)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Michael Williams <michael.williams@funsational.com>
|
||||
*/
|
||||
class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
class PdoSessionStorage extends AbstractSessionStorage implements SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* PDO instance.
|
||||
@ -65,7 +65,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openSession($path, $name)
|
||||
public function open($path, $name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -73,7 +73,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function closeSession()
|
||||
public function close()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -81,7 +81,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroySession($id)
|
||||
public function destroy($id)
|
||||
{
|
||||
// get table/column
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -104,7 +104,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gcSession($lifetime)
|
||||
public function gc($lifetime)
|
||||
{
|
||||
// get table/column
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -127,7 +127,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readSession($id)
|
||||
public function read($id)
|
||||
{
|
||||
// get table/columns
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
@ -161,7 +161,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeSession($id, $data)
|
||||
public function write($id, $data)
|
||||
{
|
||||
// get table/column
|
||||
$dbTable = $this->dbOptions['db_table'];
|
||||
|
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace
|
||||
{
|
||||
if (!interface_exists('SessionHandlerInterface', false)) {
|
||||
/**
|
||||
* Session Savehandler Interface.
|
||||
*
|
||||
* This interface is for implementing methods required for the
|
||||
* session_set_save_handler() function.
|
||||
*
|
||||
* @see http://php.net/session_set_save_handler
|
||||
*
|
||||
* These are methods called by PHP when the session is started
|
||||
* and closed and for various house-keeping tasks required
|
||||
* by session management.
|
||||
*
|
||||
* PHP requires session save handlers. There are some defaults set
|
||||
* automatically when PHP starts, but these can be overriden using
|
||||
* this command if you need anything other than PHP's default handling.
|
||||
*
|
||||
* When the session starts, PHP will call the read() handler
|
||||
* which should return a string extactly as stored (which will have
|
||||
* been encoded by PHP using a special session serializer session_decode()
|
||||
* which is different to the serialize() function. PHP will then populate
|
||||
* these into $_SESSION.
|
||||
*
|
||||
* When PHP shuts down, the write() handler is called and will pass
|
||||
* the $_SESSION contents already serialized (using session_encode()) to
|
||||
* be stored.
|
||||
*
|
||||
* When a session is specifically destroyed, PHP will call the
|
||||
* destroy() handler with the session ID. This happens when the
|
||||
* session is regenerated for example and th handler MUST delete the
|
||||
* session by ID from the persistent storage immediately.
|
||||
*
|
||||
* PHP will call gc() from time to time to expire any session
|
||||
* records according to the set max lifetime of a session. This routine
|
||||
* should delete all records from persistent storage which were last
|
||||
* accessed longer than the $lifetime.
|
||||
*
|
||||
* PHP open() and close() are pretty much redundant and
|
||||
* can return true.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Open session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* @param string $savePath Save path.
|
||||
* @param string $sessionName Session Name.
|
||||
*
|
||||
* @throws \RuntimeException If something goes wrong starting the session.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function open($savePath, $sessionName);
|
||||
|
||||
/**
|
||||
* Close session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function close();
|
||||
|
||||
/**
|
||||
* Read session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* This method is called by PHP itself when the session is started.
|
||||
* This method should retrieve the session data from storage by the
|
||||
* ID provided by PHP. Return the string directly as is from storage.
|
||||
* If the record was not found you must return an empty string.
|
||||
*
|
||||
* The returned data will be unserialized automatically by PHP using a
|
||||
* special unserializer method session_decode() and the result will be used
|
||||
* to populate the $_SESSION superglobal. This is done automatically and
|
||||
* is not configurable.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error but not "record not found".
|
||||
*
|
||||
* @return string String as stored in persistent storage or empty string in all other cases.
|
||||
*/
|
||||
function read($sessionId);
|
||||
|
||||
/**
|
||||
* Commit session to storage.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* PHP will call this method when the session is closed. It sends
|
||||
* the session ID and the contents of $_SESSION to be saved in a lightweight
|
||||
* serialized format (which PHP does automatically using session_encode()
|
||||
* which should be stored exactly as is given in $data.
|
||||
*
|
||||
* Note this method is normally called by PHP after the output buffers
|
||||
* have been closed.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
* @param string $data Session serialized data to save.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function write($sessionId, $data);
|
||||
|
||||
/**
|
||||
* Destroys this session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* PHP will call this method when the session data associated
|
||||
* with the session ID provided needs to be immediately
|
||||
* deleted from the permanent storage.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function destroy($sessionId);
|
||||
|
||||
/**
|
||||
* Garbage collection for storage.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* This method is called by PHP periodically and passes the maximum
|
||||
* time a session can exist for before being deleted from permanent storage.
|
||||
*
|
||||
* @param integer $lifetime Max lifetime in seconds to keep sessions stored.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function gc($lifetime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage
|
||||
{
|
||||
interface SessionHandlerInterface extends \SessionHandlerInterface
|
||||
{
|
||||
}
|
||||
}
|
@ -1,157 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
/**
|
||||
* Session Savehandler Interface.
|
||||
*
|
||||
* This interface is for implementing methods required for the
|
||||
* session_set_save_handler() function.
|
||||
*
|
||||
* @see http://php.net/session_set_save_handler
|
||||
*
|
||||
* These are methods called by PHP when the session is started
|
||||
* and closed and for various house-keeping tasks required
|
||||
* by session management.
|
||||
*
|
||||
* PHP requires session save handlers. There are some defaults set
|
||||
* automatically when PHP starts, but these can be overriden using
|
||||
* this command if you need anything other than PHP's default handling.
|
||||
*
|
||||
* When the session starts, PHP will call the readSession() handler
|
||||
* which should return a string extactly as stored (which will have
|
||||
* been encoded by PHP using a special session serializer session_decode()
|
||||
* which is different to the serialize() function. PHP will then populate
|
||||
* these into $_SESSION.
|
||||
*
|
||||
* When PHP shuts down, the sessionWrite() handler is called and will pass
|
||||
* the $_SESSION contents already serialized (using session_encode()) to
|
||||
* be stored.
|
||||
*
|
||||
* When a session is specifically destroyed, PHP will call the
|
||||
* sessionSession() handler with the session ID. This happens when the
|
||||
* session is regenerated for example and th handler MUST delete the
|
||||
* session by ID from the persistent storage immediately.
|
||||
*
|
||||
* PHP will call sessionSession() from time to time to expire any session
|
||||
* records according to the set max lifetime of a session. This routine
|
||||
* should delete all records from persistent storage which were last
|
||||
* accessed longer than the $lifetime.
|
||||
*
|
||||
* PHP openSession() and closeSession() are pretty much redundant and
|
||||
* can return true.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
interface SessionSaveHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Open session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* @param string $savePath Save path.
|
||||
* @param string $sessionName Session Name.
|
||||
*
|
||||
* @throws \RuntimeException If something goes wrong starting the session.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function openSession($savePath, $sessionName);
|
||||
|
||||
/**
|
||||
* Close session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function closeSession();
|
||||
|
||||
/**
|
||||
* Read session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* This method is called by PHP itself when the session is started.
|
||||
* This method should retrieve the session data from storage by the
|
||||
* ID provided by PHP. Return the string directly as is from storage.
|
||||
* If the record was not found you must return an empty string.
|
||||
*
|
||||
* The returned data will be unserialized automatically by PHP using a
|
||||
* special unserializer method session_decode() and the result will be used
|
||||
* to populate the $_SESSION superglobal. This is done automatically and
|
||||
* is not configurable.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error but not "record not found".
|
||||
*
|
||||
* @return string String as stored in persistent storage or empty string in all other cases.
|
||||
*/
|
||||
function readSession($sessionId);
|
||||
|
||||
/**
|
||||
* Commit session to storage.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* PHP will call this method when the session is closed. It sends
|
||||
* the session ID and the contents of $_SESSION to be saved in a lightweight
|
||||
* serialized format (which PHP does automatically using session_encode()
|
||||
* which should be stored exactly as is given in $data.
|
||||
*
|
||||
* Note this method is normally called by PHP after the output buffers
|
||||
* have been closed.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
* @param string $data Session serialized data to save.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function writeSession($sessionId, $data);
|
||||
|
||||
/**
|
||||
* Destroys this session.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* PHP will call this method when the session data associated
|
||||
* with the session ID provided needs to be immediately
|
||||
* deleted from the permanent storage.
|
||||
*
|
||||
* @param string $sessionId Session ID.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function destroySession($sessionId);
|
||||
|
||||
/**
|
||||
* Garbage collection for storage.
|
||||
*
|
||||
* This method is for internal use by PHP and must not be called manually.
|
||||
*
|
||||
* This method is called by PHP periodically and passes the maximum
|
||||
* time a session can exist for before being deleted from permanent storage.
|
||||
*
|
||||
* @param integer $lifetime Max lifetime in seconds to keep sessions stored.
|
||||
*
|
||||
* @throws \RuntimeException On fatal error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function gcSession($lifetime);
|
||||
}
|
@ -5,7 +5,7 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\SessionHandlerInterface;
|
||||
|
||||
/**
|
||||
* Turn AbstractSessionStorage into something concrete because
|
||||
@ -16,29 +16,29 @@ class ConcreteSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
}
|
||||
|
||||
class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
|
||||
class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionHandlerInterface
|
||||
{
|
||||
public function openSession($path, $id)
|
||||
public function open($path, $id)
|
||||
{
|
||||
}
|
||||
|
||||
public function closeSession()
|
||||
public function close()
|
||||
{
|
||||
}
|
||||
|
||||
public function readSession($id)
|
||||
public function read($id)
|
||||
{
|
||||
}
|
||||
|
||||
public function writeSession($id, $data)
|
||||
public function write($id, $data)
|
||||
{
|
||||
}
|
||||
|
||||
public function destroySession($id)
|
||||
public function destroy($id)
|
||||
{
|
||||
}
|
||||
|
||||
public function gcSession($lifetime)
|
||||
public function gc($lifetime)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class MemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
$this->memcache->expects($this->atLeastOnce())
|
||||
->method('addServer');
|
||||
|
||||
$this->assertTrue($this->storage->openSession('', ''));
|
||||
$this->assertTrue($this->storage->open('', ''));
|
||||
}
|
||||
|
||||
public function testCloseSession()
|
||||
@ -43,7 +43,7 @@ class MemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
->method('close')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($this->storage->closeSession());
|
||||
$this->assertTrue($this->storage->close());
|
||||
}
|
||||
|
||||
public function testReadSession()
|
||||
@ -51,7 +51,7 @@ class MemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
$this->memcache->expects($this->once())
|
||||
->method('get');
|
||||
|
||||
$this->assertEquals('', $this->storage->readSession(''));
|
||||
$this->assertEquals('', $this->storage->read(''));
|
||||
}
|
||||
|
||||
public function testWriteSession()
|
||||
@ -60,7 +60,7 @@ class MemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($this->storage->writeSession('', ''));
|
||||
$this->assertTrue($this->storage->write('', ''));
|
||||
}
|
||||
|
||||
public function testDestroySession()
|
||||
@ -69,12 +69,12 @@ class MemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
->method('delete')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($this->storage->destroySession(''));
|
||||
$this->assertTrue($this->storage->destroy(''));
|
||||
}
|
||||
|
||||
public function testGcSession()
|
||||
{
|
||||
$this->assertTrue($this->storage->gcSession(123));
|
||||
$this->assertTrue($this->storage->gc(123));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,12 +35,12 @@ class MemcacheddSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
->method('addServers')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($this->storage->openSession('', ''));
|
||||
$this->assertTrue($this->storage->open('', ''));
|
||||
}
|
||||
|
||||
public function testCloseSession()
|
||||
{
|
||||
$this->assertTrue($this->storage->closeSession());
|
||||
$this->assertTrue($this->storage->close());
|
||||
}
|
||||
|
||||
public function testReadSession()
|
||||
@ -48,7 +48,7 @@ class MemcacheddSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
$this->memcached->expects($this->once())
|
||||
->method('get');
|
||||
|
||||
$this->assertEquals('', $this->storage->readSession(''));
|
||||
$this->assertEquals('', $this->storage->read(''));
|
||||
}
|
||||
|
||||
public function testWriteSession()
|
||||
@ -57,7 +57,7 @@ class MemcacheddSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($this->storage->writeSession('', ''));
|
||||
$this->assertTrue($this->storage->write('', ''));
|
||||
}
|
||||
|
||||
public function testDestroySession()
|
||||
@ -66,12 +66,12 @@ class MemcacheddSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
->method('delete')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($this->storage->destroySession(''));
|
||||
$this->assertTrue($this->storage->destroy(''));
|
||||
}
|
||||
|
||||
public function testGcSession()
|
||||
{
|
||||
$this->assertTrue($this->storage->gcSession(123));
|
||||
$this->assertTrue($this->storage->gc(123));
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user