[HttpFoundation] Free bags from session storage and move classes to their own namespaces.

This commit is contained in:
Drak 2012-02-08 15:31:38 +05:45
parent d64939aeee
commit 468391525a
44 changed files with 385 additions and 383 deletions

View File

@ -232,7 +232,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* Flashes are now stored as a bucket of messages per `$type` so there can be multiple messages per type.
There are four interface constants for type, `FlashBagInterface::INFO`, `FlashBagInterface::NOTICE`,
`FlashBagInterface::WARNING` and `FlashBagInterface::ERROR`.
* Added `FlashBag` (default). Flashes expire when retrieved by `popFlashes()`.
* Added `FlashBag` (default). Flashes expire when retrieved by `pop()` or `popAll()`.
This makes the implementation ESI compatible.
* Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring
after one page page load. Messages must be retrived by `pop()` or `popAll()`.

View File

@ -259,7 +259,7 @@ UPGRADE FROM 2.0 to 2.1
</div>
<?php endif; ?>
If You wanted to process all flash types you could also make use of the `popAllFlashes()` API:
If you wanted to process all flash types you could also make use of the `getFlashes()->all()` API:
<?php foreach ($view['session']->getFlashes()->all() as $type => $flash): ?>
<div class="flash-$type">
@ -270,8 +270,8 @@ UPGRADE FROM 2.0 to 2.1
.. note::
The Flash Message API provides constants which you can optionally use. For example
`Symfony\Component\HttpFoundation\FlashBag::NOTICE`, which can also be abbreviated to
`FlashBag::NOTICE` providing you declare `<?php use Symfony\Component\HttpFoundation\FlashBag; ?>`
`Symfony\Component\HttpFoundation\SessionFlash\FlashBag::NOTICE`, which can also be abbreviated to
`FlashBag::NOTICE` providing you declare `<?php use Symfony\Component\HttpFoundation\SessionFlash\FlashBag; ?>`
at the beginning of the PHP template.
Before (Twig):
@ -301,7 +301,7 @@ UPGRADE FROM 2.0 to 2.1
.. note::
You can optionally use constants in Twig templates using `constant()` e.g.
`constant('Symfony\Component\HttpFoundation\FlashBag::NOTICE')`.
`constant('Symfony\Component\HttpFoundation\SessionFlash\FlashBag::NOTICE')`.
* Session object

View File

@ -3,8 +3,6 @@
namespace Symfony\Bridge\Doctrine\HttpFoundation;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface;
use Doctrine\DBAL\Driver\Connection;
@ -32,15 +30,13 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa
* @param Connection $con An instance of Connection.
* @param string $tableName Table name.
* @param array $options Session configuration options
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*/
public function __construct(Connection $con, $tableName = 'sessions', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct(Connection $con, $tableName = 'sessions', array $options = array())
{
$this->con = $con;
$this->tableName = $tableName;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -6,8 +6,8 @@
<parameters>
<parameter key="session.class">Symfony\Component\HttpFoundation\Session</parameter>
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\FlashBag</parameter>
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\AttributeBag</parameter>
<parameter key="session.flashbag.class">Symfony\Component\HttpFoundation\SessionFlash\FlashBag</parameter>
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag</parameter>
<parameter key="session.storage.native_file.class">Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage</parameter>
<parameter key="session.storage.null.class">Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage</parameter>
<parameter key="session.storage.native_memcache.class">Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage</parameter>
@ -25,6 +25,8 @@
<services>
<service id="session" class="%session.class%">
<argument type="service" id="session.storage" />
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.flash_bag" class="%session.flashbag.class%" public="false" />
@ -35,58 +37,42 @@
<service id="session.storage.mock_file" class="%session.storage.mock_file.class%" public="false">
<argument>%kernel.cache_dir%/sessions</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.native_file" class="%session.storage.native_file.class%" public="false">
<argument>%kernel.cache_dir%/sessions</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.native_memcache" class="%session.storage.native_memcache.class%" public="false">
<argument>tcp://127.0.0.1:11211?persistent=0</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.native_memcached" class="%session.storage.native_memcached.class%" public="false">
<argument>127.0.0.1:11211</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.memcache" class="%session.storage.memcache.class%" public="false">
<argument type="service" id="session.memcache" />
<argument>tcp://127.0.0.1:11211?persistent=0</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.memcached" class="%session.storage.memcached.class%" public="false">
<argument type="service" id="session.memcached" />
<argument>tcp://127.0.0.1:11211?persistent=0</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.native_sqlite" class="%session.storage.native_sqlite.class%" public="false">
<argument>%kernel.cache_dir%/sf2_sqlite_sess.db</argument>
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session.storage.null" class="%session.storage.null.class%" public="false">
<argument>%session.storage.options%</argument>
<argument type="service" id="session.attribute_bag" />
<argument type="service" id="session.flash_bag" />
</service>
<service id="session_listener" class="%session_listener.class%">

View File

@ -13,7 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
/**
* SessionHelper provides read-only access to the session attributes.

View File

@ -15,8 +15,8 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
class SessionHelperTest extends \PHPUnit_Framework_TestCase
{

View File

@ -15,7 +15,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpFoundation\AutoExpireFlashBag;
use Symfony\Component\HttpFoundation\SessionFlash\AutoExpireFlashBag;
/**
* WebDebugToolbarListener injects the Web Debug Toolbar.

View File

@ -1,24 +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;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionStorage\AttributeInterface;
/**
* Attributes store.
*
* @author Drak <drak@zikula.org>
*/
interface AttributeBagInterface extends SessionBagInterface, AttributeInterface
{
}

View File

@ -12,7 +12,11 @@
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionBagInterface;
/**
* Session.
@ -35,10 +39,14 @@ class Session implements SessionInterface
* Constructor.
*
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*/
public function __construct(SessionStorageInterface $storage)
public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
{
$this->storage = $storage;
$this->registerBag($attributes ?: new AttributeBag());
$this->registerBag($flashes ?: new FlashBag());
}
/**
@ -64,7 +72,7 @@ class Session implements SessionInterface
*/
public function has($name)
{
return $this->storage->getAttributes()->has($name);
return $this->storage->getBag('attributes')->has($name);
}
/**
@ -79,7 +87,7 @@ class Session implements SessionInterface
*/
public function get($name, $default = null)
{
return $this->storage->getAttributes()->get($name, $default);
return $this->storage->getBag('attributes')->get($name, $default);
}
/**
@ -92,7 +100,7 @@ class Session implements SessionInterface
*/
public function set($name, $value)
{
$this->storage->getAttributes()->set($name, $value);
$this->storage->getBag('attributes')->set($name, $value);
}
/**
@ -104,7 +112,7 @@ class Session implements SessionInterface
*/
public function all()
{
return $this->storage->getAttributes()->all();
return $this->storage->getBag('attributes')->all();
}
/**
@ -116,7 +124,7 @@ class Session implements SessionInterface
*/
public function replace(array $attributes)
{
$this->storage->getAttributes()->replace($attributes);
$this->storage->getBag('attributes')->replace($attributes);
}
/**
@ -128,7 +136,7 @@ class Session implements SessionInterface
*/
public function remove($name)
{
return $this->storage->getAttributes()->remove($name);
return $this->storage->getBag('attributes')->remove($name);
}
/**
@ -138,7 +146,7 @@ class Session implements SessionInterface
*/
public function clear()
{
$this->storage->getAttributes()->clear();
$this->storage->getBag('attributes')->clear();
}
/**
@ -218,13 +226,23 @@ class Session implements SessionInterface
$this->storage = $storage;
}
public function registerBag(SessionBagInterface $bag)
{
$this->storage->registerBag($bag);
}
public function getBag($name)
{
return $this->storage->getBag($name);
}
/**
* Gets all flash messages.
* Gets the flashbag interface.
*
* @return FlashBagInterface
*/
public function getFlashes()
{
return $this->storage->getFlashes();
return $this->getBag('flashes');
}
}

View File

@ -9,13 +9,15 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
namespace Symfony\Component\HttpFoundation\SessionAttribute;
/**
* This class relates to session attribute storage
*/
class AttributeBag implements AttributeBagInterface
{
private $name = 'attributes';
/**
* @var string
*/
@ -36,6 +38,19 @@ class AttributeBag implements AttributeBagInterface
$this->storageKey = $storageKey;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
/**
* {@inheritdoc}
*/
@ -114,6 +129,9 @@ class AttributeBag implements AttributeBagInterface
*/
public function clear()
{
$return = $this->attributes;
$this->attributes = array();
return $return;
}
}

View File

@ -9,14 +9,16 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\SessionStorage;
namespace Symfony\Component\HttpFoundation\SessionAttribute;
use Symfony\Component\HttpFoundation\SessionBagInterface;
/**
* Interface for the session.
* Attributes store.
*
* @author Drak <drak@zikula.org>
*/
interface AttributeInterface
interface AttributeBagInterface extends SessionBagInterface
{
/**
* Checks if an attribute is defined.
@ -65,9 +67,4 @@ interface AttributeInterface
* @param string $name
*/
function remove($name);
/**
* Clears all attributes.
*/
function clear();
}

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
namespace Symfony\Component\HttpFoundation\SessionAttribute;
/**
* This class provides structured storage of session attributes using
@ -110,7 +110,10 @@ class NamespacedAttributeBag extends AttributeBag
*/
public function clear()
{
$return = $this->attributes;
$this->attributes = array();
return $return;
}
/**

View File

@ -18,6 +18,13 @@ namespace Symfony\Component\HttpFoundation;
*/
interface SessionBagInterface
{
/**
* Gets this bag's name
*
* @return string
*/
function getName();
/**
* Initializes the Bag
*
@ -31,4 +38,11 @@ interface SessionBagInterface
* @return string
*/
function getStorageKey();
/**
* Clears out data from bag.
*
* @return mixed Whatever data was contained.
*/
function clear();
}

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
namespace Symfony\Component\HttpFoundation\SessionFlash;
/**
* AutoExpireFlashBag flash message container.
@ -18,6 +18,8 @@ namespace Symfony\Component\HttpFoundation;
*/
class AutoExpireFlashBag implements FlashBagInterface
{
private $name = 'flashes';
/**
* Flash messages.
*
@ -43,6 +45,19 @@ class AutoExpireFlashBag implements FlashBagInterface
$this->flashes = array('display' => array(), 'new' => array());
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
/**
* {@inheritdoc}
*/
@ -149,4 +164,15 @@ class AutoExpireFlashBag implements FlashBagInterface
{
return $this->storageKey;
}
/**
* {@inheritdoc}
*/
public function clear()
{
$return = $this->popAll();
$this->flashes = array('display' => array(), 'new' => array());
return $return;
}
}

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
namespace Symfony\Component\HttpFoundation\SessionFlash;
/**
* FlashBag flash message container.
@ -18,6 +18,8 @@ namespace Symfony\Component\HttpFoundation;
*/
class FlashBag implements FlashBagInterface
{
private $name = 'flashes';
/**
* Flash messages.
*
@ -42,6 +44,19 @@ class FlashBag implements FlashBagInterface
$this->storageKey = $storageKey;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
/**
* {@inheritdoc}
*/
@ -135,4 +150,12 @@ class FlashBag implements FlashBagInterface
{
return $this->storageKey;
}
/**
* {@inheritdoc}
*/
public function clear()
{
return $this->popAll();
}
}

View File

@ -9,7 +9,9 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
namespace Symfony\Component\HttpFoundation\SessionFlash;
use Symfony\Component\HttpFoundation\SessionBagInterface;
/**
* FlashBagInterface.

View File

@ -12,14 +12,14 @@
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\SessionStorage\AttributeInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
/**
* Interface for the session.
*
* @author Drak <drak@zikula.org>
*/
interface SessionInterface extends AttributeInterface, \Serializable
interface SessionInterface extends \Serializable
{
/**
* Starts the session storage.
@ -56,6 +56,59 @@ interface SessionInterface extends AttributeInterface, \Serializable
*/
function save();
/**
* Checks if an attribute is defined.
*
* @param string $name The attribute name
*
* @return Boolean true if the attribute is defined, false otherwise
*/
function has($name);
/**
* Returns an attribute.
*
* @param string $name The attribute name
* @param mixed $default The default value if not found.
*
* @return mixed
*/
function get($name, $default = null);
/**
* Sets an attribute.
*
* @param string $name
* @param mixed $value
*/
function set($name, $value);
/**
* Returns attributes.
*
* @return array Attributes
*/
function all();
/**
* Sets attributes.
*
* @param array $attributes Attributes
*/
function replace(array $attributes);
/**
* Removes an attribute.
*
* @param string $name
*/
function remove($name);
/**
* Clears all attributes.
*/
function clear();
/**
* Gets the flashbag interface.
*

View File

@ -11,10 +11,10 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionBagInterface;
/**
@ -25,14 +25,11 @@ use Symfony\Component\HttpFoundation\SessionBagInterface;
abstract class AbstractSessionStorage implements SessionStorageInterface
{
/**
* @var \Symfony\Component\HttpFoundation\FlashBagInterface
* Array of SessionBagInterface
*
* @var array
*/
protected $flashBag;
/**
* @var \Symfony\Component\HttpFoundation\AttributeBagInterface
*/
protected $attributeBag;
protected $bags;
/**
* @var array
@ -87,43 +84,15 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
* upload_progress.min-freq, "1"
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
*
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
* @param array $options Session configuration options.
*/
public function __construct(AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, array $options = array())
public function __construct(array $options = array())
{
$this->attributeBag = $attributes ?: new AttributeBag();
$this->flashBag = $flashes ?: new FlashBag();
$this->setOptions($options);
$this->registerSaveHandlers();
$this->registerShutdownFunction();
}
/**
* {@inheritdoc}
*/
public function getFlashes()
{
if ($this->options['auto_start'] && !$this->started) {
$this->start();
}
return $this->flashBag;
}
/**
* {@inheritdoc}
*/
public function getAttributes()
{
if ($this->options['auto_start'] && !$this->started) {
$this->start();
}
return $this->attributeBag;
}
/**
* {@inheritdoc}
*/
@ -194,8 +163,9 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
public function clear()
{
// clear out the bags
$this->attributeBag->clear();
$this->flashBag->popAll();
foreach ($this->bags as $bag) {
$bag->clear();
}
// clear out the session
$_SESSION = array();
@ -204,6 +174,38 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
$this->loadSession();
}
/**
* Register a SessionBagInterface for use.
*
* @param SessionBagInterface $bag
*/
public function registerBag(SessionBagInterface $bag)
{
$this->bags[$bag->getName()] = $bag;
}
/**
* Gets a bag by name.
*
* @param string $name
*
* @return SessionBagInterface
*
* @throws \InvalidArgumentException
*/
public function getBag($name)
{
if (!isset($this->bags[$name])) {
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
}
if ($this->options['auto_start'] && !$this->started) {
$this->start();
}
return $this->bags[$name];
}
/**
* Sets session.* ini variables.
*
@ -326,22 +328,16 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
* PHP takes the return value from the sessionRead() handler, unserializes it
* and populates $_SESSION with the result automatically.
*/
protected function loadSession()
protected function loadSession(array &$session = null)
{
$this->link($this->attributeBag, $_SESSION);
$this->link($this->flashBag, $_SESSION);
}
if (null === $session) {
$session = &$_SESSION;
}
/**
* Link a bag to the session.
*
* @param SessionBagInterface $bag
* @param array &$array
*/
protected function link(SessionBagInterface $bag, array &$array)
{
$key = $bag->getStorageKey();
$array[$key] = isset($array[$key]) ? $array[$key] : array();
$bag->initialize($array[$key]);
foreach ($this->bags as $bag) {
$key = $bag->getStorageKey();
$session[$key] = isset($session[$key]) ? $session[$key] : array();
$bag->initialize($session[$key]);
}
}
}

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* MemcacheSessionStorage.
*
@ -48,12 +45,10 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
* @param \Memcache $memcache A \Memcache instance
* @param array $memcacheOptions An associative array of Memcachge options
* @param array $options Session configuration options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array())
{
$this->memcache = $memcache;
@ -72,7 +67,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa
$this->memcacheOptions = $memcacheOptions;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
protected function addServer(array $server)

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* MemcachedSessionStorage.
*
@ -41,12 +38,10 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
* @param \Memcached $memcached A \Memcached instance
* @param array $memcachedOptions An associative array of Memcached options
* @param array $options Session configuration options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct(\Memcached $memcache, array $memcachedOptions = array(), array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct(\Memcached $memcache, array $memcachedOptions = array(), array $options = array())
{
$this->memcached = $memcached;
@ -66,7 +61,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS
$this->memcacheOptions = $memcachedOptions;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* MockArraySessionStorage mocks the session for unit tests.
*
@ -36,31 +33,11 @@ class MockArraySessionStorage extends AbstractSessionStorage
/**
* @var array
*/
private $attributes = array();
private $sessionData = array();
/**
* @var array
*/
private $flashes = array();
/**
* Injects array of attributes to simulate retrieval of existing session.
*
* @param array $array
*/
public function setAttributes(array $array)
public function setSessionData(array $array)
{
$this->attributes = $array;
}
/**
* Injects array of flashes to simulate retrieval of existing session.
*
* @param array $array
*/
public function setFlashes(array $array)
{
$this->flashes = $array;
$this->sessionData = $array;
}
/**
@ -73,14 +50,15 @@ class MockArraySessionStorage extends AbstractSessionStorage
}
$this->started = true;
$this->attributeBag->initialize($this->attributes);
$this->flashBag->initialize($this->flashes);
$this->loadSession($this->sessionData);
$this->sessionId = $this->generateSessionId();
session_id($this->sessionId);
return true;
}
/**
* {@inheritdoc}
*/
@ -117,6 +95,23 @@ class MockArraySessionStorage extends AbstractSessionStorage
$this->closed = false;
}
/**
* {@inheritdoc}
*/
public function clear()
{
// clear out the bags
foreach ($this->bags as $bag) {
$bag->clear();
}
// clear out the session
$this->sessionData = array();
// reconnect the bags to the session
$this->loadSession($this->sessionData);
}
/**
* Generates a session ID.
*

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* MockFileSessionStorage is used to mock sessions for
* functional testing when done in a single PHP process.
@ -25,11 +22,6 @@ use Symfony\Component\HttpFoundation\FlashBagInterface;
*/
class MockFileSessionStorage extends MockArraySessionStorage
{
/**
* @var array
*/
private $sessionData = array();
/**
* @var string
*/
@ -40,12 +32,10 @@ class MockFileSessionStorage extends MockArraySessionStorage
*
* @param string $savePath Path of directory to save session files.
* @param array $options Session options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct($savePath = null, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct($savePath = null, array $options = array())
{
if (null === $savePath) {
$savePath = sys_get_temp_dir();
@ -57,7 +47,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
$this->savePath = $savePath;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**
@ -141,8 +131,6 @@ class MockFileSessionStorage extends MockArraySessionStorage
$filePath = $this->getFilePath();
$this->sessionData = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
$this->link($this->attributeBag, $this->sessionData);
$this->link($this->flashBag, $this->sessionData);
$this->loadSession($this->sessionData);
}
}

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* NativeFileSessionStorage.
*
@ -33,12 +30,10 @@ class NativeFileSessionStorage extends AbstractSessionStorage
*
* @param string $savePath Path of directory to save session files.
* @param array $options Session configuration options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct($savePath = null, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct($savePath = null, array $options = array())
{
if (null === $savePath) {
$savePath = sys_get_temp_dir();
@ -50,7 +45,7 @@ class NativeFileSessionStorage extends AbstractSessionStorage
$this->savePath = $savePath;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* NativeMemcacheSessionStorage.
*
@ -33,19 +30,17 @@ class NativeMemcacheSessionStorage extends AbstractSessionStorage
*
* @param string $savePath Path of memcache server.
* @param array $options Session configuration options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array())
{
if (!extension_loaded('memcache')) {
throw new \RuntimeException('PHP does not have "memcache" session module registered');
}
$this->savePath = $savePath;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* NativeMemcachedSessionStorage.
*
@ -33,19 +30,17 @@ class NativeMemcachedSessionStorage extends AbstractSessionStorage
*
* @param string $savePath Comma separated list of servers: e.g. memcache1.example.com:11211,memcache2.example.com:11211
* @param array $options Session configuration options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for defaul FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct($savePath = '127.0.0.1:11211', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct($savePath = '127.0.0.1:11211', array $options = array())
{
if (!extension_loaded('memcached')) {
throw new \RuntimeException('PHP does not have "memcached" session module registered');
}
$this->savePath = $savePath;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* NativeSqliteSessionStorage.
*
@ -33,19 +30,17 @@ class NativeSqliteSessionStorage extends AbstractSessionStorage
*
* @param string $dbPath Path to SQLite database file.
* @param array $options Session configuration options.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for defaul FlashBag)
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct($dbPath, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct($dbPath, array $options = array())
{
if (!extension_loaded('sqlite')) {
throw new \RuntimeException('PHP does not have "sqlite" session module registered');
}
$this->dbPath = $dbPath;
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* NullSessionStorage.
*

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
/**
* PdoSessionStorage.
*
@ -44,14 +41,12 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
* @param \PDO $pdo A \PDO instance
* @param array $dbOptions An associative array of DB options
* @param array $options Session configuration options
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for defaul FlashBag)
*
* @throws \InvalidArgumentException When "db_table" option is not provided
*
* @see AbstractSessionStorage::__construct()
*/
public function __construct(\PDO $pdo, array $dbOptions = array(), array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
public function __construct(\PDO $pdo, array $dbOptions = array(), array $options = array())
{
if (!array_key_exists('db_table', $dbOptions)) {
throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
@ -64,7 +59,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan
'db_time_col' => 'sess_time',
), $dbOptions);
parent::__construct($attributes, $flashes, $options);
parent::__construct($options);
}
/**

View File

@ -27,7 +27,7 @@ namespace Symfony\Component\HttpFoundation\SessionStorage;
* 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 sessionRead() handler
* 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
@ -38,16 +38,16 @@ namespace Symfony\Component\HttpFoundation\SessionStorage;
* be stored.
*
* When a session is specifically destroyed, PHP will call the
* sessionDestroy() handler with the session ID. This happens when 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 sessionGc() from time to time to expire any session
* 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 sessionOpen() and sessionClose() are pretty much redundant and
* PHP openSession() and closeSession() are pretty much redundant and
* can return true.
*
* @author Drak <drak@zikula.org>

View File

@ -11,8 +11,7 @@
namespace Symfony\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionBagInterface;
/**
* SessionStorageInterface.
@ -78,16 +77,14 @@ interface SessionStorageInterface
function clear();
/**
* Gets the FlashBagInterface driver.
* Gets a SessionBagInterface by name.
*
* @return FlashBagInterface
* @return SessionBagInterface
*/
function getFlashes();
function getBag($name);
/**
* Gets the AttributeBagInterface driver.
*
* @return AttributeBagInterface
* Registers a SessionBagInterface for use.
*/
function getAttributes();
function registerBag(SessionBagInterface $bag);
}

View File

@ -15,8 +15,6 @@ namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\AttributeBag;
class RequestTest extends \PHPUnit_Framework_TestCase
{
@ -848,7 +846,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request = new Request;
$this->assertFalse($request->hasSession());
$request->setSession(new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag())));
$request->setSession(new Session(new MockArraySessionStorage()));
$this->assertTrue($request->hasSession());
}
@ -859,7 +857,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($request->hasPreviousSession());
$request->cookies->set(session_name(), 'foo');
$this->assertFalse($request->hasPreviousSession());
$request->setSession(new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag())));
$request->setSession(new Session(new MockArraySessionStorage()));
$this->assertTrue($request->hasPreviousSession());
}

View File

@ -1,7 +1,7 @@
<?php
namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
/**

View File

@ -2,7 +2,7 @@
namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\SessionAttribute\NamespacedAttributeBag;
/**
* Tests NamespacedAttributeBag

View File

@ -11,8 +11,8 @@
namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\AutoExpireFlashBag as FlashBag;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\AutoExpireFlashBag as FlashBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
/**
* AutoExpireFlashBagTest
@ -22,7 +22,7 @@ use Symfony\Component\HttpFoundation\FlashBagInterface;
class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\HttpFoundation\FlashBagInterface
* @var \Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface
*/
private $bag;

View File

@ -11,8 +11,8 @@
namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
/**
* FlashBagTest
@ -22,7 +22,7 @@ use Symfony\Component\HttpFoundation\FlashBagInterface;
class FlashBagTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\HttpFoundation\FlashBagInterface
* @var \Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface
*/
private $bag;

View File

@ -3,6 +3,8 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface;
/**
@ -57,19 +59,27 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
*/
protected function getStorage()
{
return new CustomHandlerSessionStorage();
$storage = new CustomHandlerSessionStorage();
$storage->registerBag(new AttributeBag);
return $storage;
}
public function testGetFlashBag()
public function testBag()
{
$storage = $this->getStorage();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
$bag = new FlashBag();
$storage->registerBag($bag);
$this->assertSame($bag, $storage->getBag($bag->getName()));
}
public function testGetAttributeBag()
/**
* @expectedException \InvalidArgumentException
*/
public function testRegisterBagException()
{
$storage = $this->getStorage();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
$storage->getBag('non_existing');
}
public function testGetId()
@ -85,10 +95,10 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$storage->getAttributes()->set('lucky', 7);
$storage->getBag('attributes')->set('lucky', 7);
$storage->regenerate();
$this->assertNotEquals($id, $storage->getId());
$this->assertEquals(7, $storage->getAttributes()->get('lucky'));
$this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
}
@ -97,10 +107,10 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$storage->getAttributes()->set('legs', 11);
$storage->getBag('attributes')->set('legs', 11);
$storage->regenerate(true);
$this->assertNotEquals($id, $storage->getId());
$this->assertEquals(11, $storage->getAttributes()->get('legs'));
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
}
public function testCustomSaveHandlers()

View File

@ -3,8 +3,8 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
/**
@ -29,17 +29,27 @@ class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase
*/
private $flashes;
private $data;
protected function setUp()
{
$this->attributes = array('foo' => 'bar');
$this->flashes = array('notice' => 'hello');
$this->storage = new MockArraySessionStorage(new AttributeBag(), new FlashBag());
$this->storage->setFlashes($this->flashes);
$this->storage->setAttributes($this->attributes);
$this->attributes = new AttributeBag();
$this->flashes = new FlashBag();
$this->data = array(
$this->attributes->getStorageKey() => array('foo' => 'bar'),
$this->flashes->getStorageKey() => array('notice' => 'hello'),
);
$this->storage = new MockArraySessionStorage();
$this->storage->registerBag($this->flashes);
$this->storage->registerBag($this->attributes);
$this->storage->setSessionData($this->data);
}
protected function tearDown()
{
$this->data = null;
$this->flashes = null;
$this->attributes = null;
$this->storage = null;
@ -61,14 +71,14 @@ class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase
$id = $this->storage->getId();
$this->storage->regenerate();
$this->assertNotEquals($id, $this->storage->getId());
$this->assertEquals($this->attributes, $this->storage->getAttributes()->all());
$this->assertEquals($this->flashes, $this->storage->getFlashes()->all());
$this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all());
$this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->all());
$id = $this->storage->getId();
$this->storage->regenerate(true);
$this->assertNotEquals($id, $this->storage->getId());
$this->assertEquals($this->attributes, $this->storage->getAttributes()->all());
$this->assertEquals($this->flashes, $this->storage->getFlashes()->all());
$this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all());
$this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->all());
}
public function testGetId()

View File

@ -3,6 +3,8 @@
namespace Symfony\Test\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
/**
* Test class for MockFileSessionStorage.
@ -50,11 +52,11 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
public function testRegenerate()
{
$this->storage->start();
$this->storage->getAttributes()->set('regenerate', 1234);
$this->storage->getBag('attributes')->set('regenerate', 1234);
$this->storage->regenerate();
$this->assertEquals(1234, $this->storage->getAttributes()->get('regenerate'));
$this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
$this->storage->regenerate(true);
$this->assertEquals(1234, $this->storage->getAttributes()->get('regenerate'));
$this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
}
public function testGetId()
@ -67,33 +69,37 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
public function testSave()
{
$this->storage->start();
$this->assertNotEquals('108', $this->storage->getAttributes()->get('new'));
$this->assertFalse($this->storage->getFlashes()->has('newkey'));
$this->storage->getAttributes()->set('new', '108');
$this->storage->getFlashes()->set('newkey', 'test');
$this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
$this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
$this->storage->getBag('attributes')->set('new', '108');
$this->storage->getBag('flashes')->set('newkey', 'test');
$this->storage->save();
$storage = $this->getStorage();
$storage->start();
$this->assertEquals('108', $storage->getAttributes()->get('new'));
$this->assertTrue($storage->getFlashes()->has('newkey'));
$this->assertEquals('test', $storage->getFlashes()->get('newkey'));
$this->assertEquals('108', $storage->getBag('attributes')->get('new'));
$this->assertTrue($storage->getBag('flashes')->has('newkey'));
$this->assertEquals('test', $storage->getBag('flashes')->get('newkey'));
}
public function testMultipleInstances()
{
$storage1 = $this->getStorage();
$storage1->start();
$storage1->getAttributes()->set('foo', 'bar');
$storage1->getBag('attributes')->set('foo', 'bar');
$storage1->save();
$storage2 = $this->getStorage();
$storage2->start();
$this->assertEquals('bar', $storage2->getAttributes()->get('foo'), 'values persist between instances');
$this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
}
private function getStorage(array $options = array())
{
return new MockFileSessionStorage($this->sessionDir, $options);
$storage = new MockFileSessionStorage($this->sessionDir, $options);
$storage->registerBag(new FlashBag);
$storage->registerBag(new AttributeBag);
return $storage;
}
}

View File

@ -3,8 +3,8 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
/**
* Test class for NativeFileSessionStorage.
@ -15,23 +15,11 @@ use Symfony\Component\HttpFoundation\FlashBag;
*/
class NativeFileSessionStorageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructDefaults()
{
$storage = new NativeFileSessionStorage();
$this->assertEquals('files', ini_get('session.save_handler'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
}
public function testSaveHandlers()
{
$attributeBag = new AttributeBag();
$flashBag = new FlashBag();
$storage = new NativeFileSessionStorage(sys_get_temp_dir(), array('name' => 'TESTING'), $attributeBag, $flashBag);
$storage = new NativeFileSessionStorage(sys_get_temp_dir(), array('name' => 'TESTING'));
$this->assertEquals('files', ini_get('session.save_handler'));
$this->assertEquals(sys_get_temp_dir(), ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
$this->assertSame($attributeBag, $storage->getAttributes());
$this->assertSame($flashBag, $storage->getFlashes());
}
}

View File

@ -3,8 +3,8 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
/**
* Test class for NativeMemcacheSessionStorage.
@ -15,31 +15,15 @@ use Symfony\Component\HttpFoundation\FlashBag;
*/
class NativeMemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructDefaults()
{
if (!extension_loaded('memcache')) {
$this->markTestSkipped('Skipped tests SQLite extension is not present');
}
$storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0');
$this->assertEquals('memcache', ini_get('session.save_handler'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
}
public function testSaveHandlers()
{
if (!extension_loaded('memcache')) {
$this->markTestSkipped('Skipped tests SQLite extension is not present');
}
$attributeBag = new AttributeBag();
$flashBag = new FlashBag();
$storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0', array('name' => 'TESTING'), $attributeBag, $flashBag);
$storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0', array('name' => 'TESTING'));
$this->assertEquals('memcache', ini_get('session.save_handler'));
$this->assertEquals('tcp://127.0.0.1:11211?persistent=0', ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
$this->assertSame($attributeBag, $storage->getAttributes());
$this->assertSame($flashBag, $storage->getFlashes());
}
}

View File

@ -3,8 +3,8 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\NativeMemcachedSessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
/**
* Test class for NativeMemcachedSessionStorage.
@ -15,40 +15,20 @@ use Symfony\Component\HttpFoundation\FlashBag;
*/
class NativeMemcachedSessionStorageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructDefaults()
{
if (!extension_loaded('memcached')) {
$this->markTestSkipped('Skipped tests SQLite extension is not present');
}
// test takes too long if memcached server is not running
ini_set('memcached.sess_locking', '0');
$storage = new NativeMemcachedSessionStorage('127.0.0.1:11211');
$this->assertEquals('memcached', ini_get('session.save_handler'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
}
public function testSaveHandlers()
{
if (!extension_loaded('memcached')) {
$this->markTestSkipped('Skipped tests SQLite extension is not present');
}
$attributeBag = new AttributeBag();
$flashBag = new FlashBag();
// test takes too long if memcached server is not running
ini_set('memcached.sess_locking', '0');
$storage = new NativeMemcachedSessionStorage('127.0.0.1:11211', array('name' => 'TESTING'), $attributeBag, $flashBag);
$storage = new NativeMemcachedSessionStorage('127.0.0.1:11211', array('name' => 'TESTING'));
$this->assertEquals('memcached', ini_get('session.save_handler'));
$this->assertEquals('127.0.0.1:11211', ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
$this->assertSame($attributeBag, $storage->getAttributes());
$this->assertSame($flashBag, $storage->getFlashes());
}
}

View File

@ -3,8 +3,8 @@
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSqliteSessionStorage;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
/**
* Test class for NativeSqliteSessionStorage.
@ -15,32 +15,16 @@ use Symfony\Component\HttpFoundation\FlashBag;
*/
class NativeSqliteSessionStorageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructDefaults()
{
if (!extension_loaded('sqlite')) {
$this->markTestSkipped('Skipped tests SQLite extension is not present');
}
$storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db');
$this->assertEquals('sqlite', ini_get('session.save_handler'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
}
public function testSaveHandlers()
{
if (!extension_loaded('sqlite')) {
$this->markTestSkipped('Skipped tests SQLite extension is not present');
}
$attributeBag = new AttributeBag();
$flashBag = new FlashBag();
$storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db', array('name' => 'TESTING'), $attributeBag, $flashBag);
$storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db', array('name' => 'TESTING'));
$this->assertEquals('sqlite', ini_get('session.save_handler'));
$this->assertEquals(sys_get_temp_dir().'/sqlite.db', ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
$this->assertSame($attributeBag, $storage->getAttributes());
$this->assertSame($flashBag, $storage->getFlashes());
}
}

View File

@ -13,14 +13,6 @@ use Symfony\Component\HttpFoundation\Session;
*/
class NullSessionStorageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructDefaults()
{
$storage = new NullSessionStorage();
$this->assertEquals('user', ini_get('session.save_handler'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
}
public function testSaveHandlers()
{
$storage = new NullSessionStorage();

View File

@ -12,10 +12,10 @@
namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\FlashBag;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\AttributeBag;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage;
/**
@ -39,8 +39,8 @@ class SessionTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->storage = new MockArraySessionStorage(new AttributeBag(), new FlashBag());
$this->session = new Session($this->storage);
$this->storage = new MockArraySessionStorage();
$this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
}
protected function tearDown()