From 39288bcdaac16e1aeef96cdb5c4980fe24e807a2 Mon Sep 17 00:00:00 2001 From: Drak Date: Tue, 22 Nov 2011 08:32:43 +0545 Subject: [PATCH 01/31] [HttpFoundation] Added AttributesInterface and AttributesBagInterface and concrete implementations. This commit outsources session attribute storage to it's own class. There are two concrete implementations, one with structured namespace storage and the other without. --- .../Component/HttpFoundation/AttributeBag.php | 119 ++++++++++++ .../HttpFoundation/AttributeBagInterface.php | 37 ++++ .../HttpFoundation/NamespacedAttributeBag.php | 181 ++++++++++++++++++ .../SessionStorage/AttributeInterface.php | 73 +++++++ .../HttpFoundation/AttributeBagTest.php | 155 +++++++++++++++ .../NamespacedAttributeBagTest.php | 162 ++++++++++++++++ 6 files changed, 727 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/AttributeBag.php create mode 100644 src/Symfony/Component/HttpFoundation/AttributeBagInterface.php create mode 100644 src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/NamespacedAttributeBagTest.php diff --git a/src/Symfony/Component/HttpFoundation/AttributeBag.php b/src/Symfony/Component/HttpFoundation/AttributeBag.php new file mode 100644 index 0000000000..3db1650426 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/AttributeBag.php @@ -0,0 +1,119 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * This class relates to session attribute storage + */ +class AttributeBag implements AttributeBagInterface +{ + /** + * @var string + */ + private $storageKey; + + /** + * @var array + */ + protected $attributes = array(); + + /** + * Constructor. + * + * @param type $storageKey The key used to store flashes in the session. + */ + public function __construct($storageKey = '_sf2_attributes') + { + $this->storageKey = $storageKey; + } + + /** + * {@inheritdoc} + */ + public function initialize(array &$attributes) + { + $this->attributes = &$attributes; + } + + /** + * {@inheritdoc} + */ + public function getStorageKey() + { + return $this->storageKey; + } + + /** + * {@inheritdoc} + */ + public function has($name) + { + return array_key_exists($name, $this->attributes); + } + + /** + * {@inheritdoc} + */ + public function get($name, $default = null) + { + return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default; + } + + /** + * {@inheritdoc} + */ + public function set($name, $value) + { + $this->attributes[$name] = $value; + } + + /** + * {@inheritdoc} + */ + public function all() + { + return $this->attributes; + } + + /** + * {@inheritdoc} + */ + public function replace(array $attributes) + { + $this->attributes = array(); + foreach ($attributes as $key => $value) { + $this->set($key, $value); + } + } + + /** + * {@inheritdoc} + */ + public function remove($name) + { + $retval = null; + if (array_key_exists($name, $this->attributes)) { + $retval = $this->attributes[$name]; + unset($this->attributes[$name]); + } + + return $retval; + } + + /** + * {@inheritdoc} + */ + public function clear() + { + $this->attributes = array(); + } +} diff --git a/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php b/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php new file mode 100644 index 0000000000..5938a55573 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php @@ -0,0 +1,37 @@ + + * + * 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 + */ +interface AttributeBagInterface extends AttributeInterface +{ + /** + * Initializes the AttributeBag + * + * @param array $attributes + */ + function initialize(array &$attributes); + + /** + * Gets the storage key for this bag. + * + * @return string + */ + function getStorageKey(); +} diff --git a/src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php b/src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php new file mode 100644 index 0000000000..b175b5f233 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php @@ -0,0 +1,181 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * This class provides structured storage of session attributes using + * a name spacing character in the key. + * + * @author Drak + */ +class NamespacedAttributeBag extends AttributeBag +{ + /** + * Namespace character. + * + * @var string + */ + private $namespaceCharacter; + + /** + * Constructor. + * + * @param type $storageKey Session storage key. + * @param type $namespaceCharacter Namespace character to use in keys. + */ + public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/') + { + $this->namespaceCharacter = $namespaceCharacter; + parent::__construct($storageKey); + } + + /** + * {@inheritdoc} + */ + public function has($name) + { + $attributes = $this->resolveAttributePath($name); + $name = $this->resolveKey($name); + + return array_key_exists($name, $attributes); + } + + /** + * {@inheritdoc} + */ + public function get($name, $default = null) + { + $attributes = $this->resolveAttributePath($name); + $name = $this->resolveKey($name); + + return array_key_exists($name, $attributes) ? $attributes[$name] : $default; + } + + /** + * {@inheritdoc} + */ + public function set($name, $value) + { + $attributes = & $this->resolveAttributePath($name, true); + $name = $this->resolveKey($name); + $attributes[$name] = $value; + } + + /** + * {@inheritdoc} + */ + public function all() + { + return $this->attributes; + } + + /** + * {@inheritdoc} + */ + public function replace(array $attributes) + { + $this->attributes = array(); + foreach ($attributes as $key => $value) { + $this->set($key, $value); + } + } + + /** + * {@inheritdoc} + */ + public function remove($name) + { + $retval = null; + $attributes = & $this->resolveAttributePath($name); + $name = $this->resolveKey($name); + if (array_key_exists($name, $attributes)) { + $retval = $attributes[$name]; + unset($attributes[$name]); + } + + return $retval; + } + + /** + * {@inheritdoc} + */ + public function clear() + { + $this->attributes = array(); + } + + /** + * Resolves a path in attributes property and returns it as a reference. + * + * This method allows structured namespacing of session attributes. + * + * @param string $name Key name + * @param boolean $writeContext Write context, default false + * + * @return array + */ + protected function &resolveAttributePath($name, $writeContext = false) + { + $array = & $this->attributes; + $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name; + + // Check if there is anything to do, else return + if (!$name) { + return $array; + } + + $parts = explode($this->namespaceCharacter, $name); + if (count($parts) < 2) { + if (!$writeContext) { + return $array; + } + + $array[$parts[0]] = array(); + + return $array; + } + + unset($parts[count($parts)-1]); + + foreach ($parts as $part) { + if (!array_key_exists($part, $array)) { + if (!$writeContext) { + return $array; + } + + $array[$part] = array(); + } + + $array = & $array[$part]; + } + + return $array; + } + + /** + * Resolves the key from the name. + * + * This is the last part in a dot separated string. + * + * @param string $name + * + * @return string + */ + protected function resolveKey($name) + { + if (strpos($name, $this->namespaceCharacter) !== false) { + $name = substr($name, strrpos($name, $this->namespaceCharacter)+1, strlen($name)); + } + + return $name; + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php b/src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php new file mode 100644 index 0000000000..176cb8bc8b --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +/** + * Interface for the session. + * + * @author Drak + */ +interface AttributeInterface +{ + /** + * 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(); +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php new file mode 100644 index 0000000000..60e13794e0 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php @@ -0,0 +1,155 @@ + + */ +class AttributeBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var array + */ + private $array; + + /** + * @var AttributeBag + */ + private $bag; + + protected function setUp() + { + $this->array = array( + 'hello' => 'world', + 'always' => 'be happy', + 'user.login' => 'drak', + 'csrf.token' => array( + 'a' => '1234', + 'b' => '4321', + ), + 'category' => array( + 'fishing' => array( + 'first' => 'cod', + 'second' => 'sole') + ), + ); + $this->bag = new AttributeBag('_sf2'); + $this->bag->initialize($this->array); + } + + protected function tearDown() + { + $this->bag = null; + $this->array = array(); + } + + public function testInitialize() + { + $bag = new AttributeBag(); + $bag->initialize($this->array); + $this->assertEquals($this->array, $bag->all()); + $array = array('should' => 'change'); + $bag->initialize($array); + $this->assertEquals($array, $bag->all()); + } + + public function testGetStorageKey() + { + $this->assertEquals('_sf2', $this->bag->getStorageKey()); + $attributeBag = new AttributeBag('test'); + $this->assertEquals('test', $attributeBag->getStorageKey()); + } + + /** + * @dataProvider attributesProvider + */ + public function testHas($key, $value, $exists) + { + $this->assertEquals($exists, $this->bag->has($key)); + } + + /** + * @dataProvider attributesProvider + */ + public function testGet($key, $value, $expected) + { + $this->assertEquals($value, $this->bag->get($key)); + } + + public function testGetDefaults() + { + $this->assertNull($this->bag->get('user2.login')); + $this->assertEquals('default', $this->bag->get('user2.login', 'default')); + } + + /** + * @dataProvider attributesProvider + */ + public function testSet($key, $value, $expected) + { + $this->bag->set($key, $value); + $this->assertEquals($value, $this->bag->get($key)); + } + + public function testAll() + { + $this->assertEquals($this->array, $this->bag->all()); + + $this->bag->set('hello', 'fabien'); + $array = $this->array; + $array['hello'] = 'fabien'; + $this->assertEquals($array, $this->bag->all()); + } + + public function testReplace() + { + $array = array(); + $array['name'] = 'jack'; + $array['foo.bar'] = 'beep'; + $this->bag->replace($array); + $this->assertEquals($array, $this->bag->all()); + $this->assertNull($this->bag->get('hello')); + $this->assertNull($this->bag->get('always')); + $this->assertNull($this->bag->get('user.login')); + } + + public function testRemove() + { + $this->assertEquals('world', $this->bag->get('hello')); + $this->bag->remove('hello'); + $this->assertNull($this->bag->get('hello')); + + $this->assertEquals('be happy', $this->bag->get('always')); + $this->bag->remove('always'); + $this->assertNull($this->bag->get('always')); + + $this->assertEquals('drak', $this->bag->get('user.login')); + $this->bag->remove('user.login'); + $this->assertNull($this->bag->get('user.login')); + } + + public function testClear() + { + $this->bag->clear(); + $this->assertEquals(array(), $this->bag->all()); + } + + public function attributesProvider() + { + return array( + array('hello', 'world', true), + array('always', 'be happy', true), + array('user.login', 'drak', true), + array('csrf.token', array('a' => '1234', 'b' => '4321'), true), + array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true), + array('user2.login', null, false), + array('never', null, false), + array('bye', null, false), + array('bye/for/now', null, false), + ); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/NamespacedAttributeBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/NamespacedAttributeBagTest.php new file mode 100644 index 0000000000..55d614f13f --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/NamespacedAttributeBagTest.php @@ -0,0 +1,162 @@ + + */ +class NamespacedAttributeBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var array + */ + private $array; + + /** + * @var NamespacedAttributeBag + */ + private $bag; + + protected function setUp() + { + $this->array = array( + 'hello' => 'world', + 'always' => 'be happy', + 'user.login' => 'drak', + 'csrf.token' => array( + 'a' => '1234', + 'b' => '4321', + ), + 'category' => array( + 'fishing' => array( + 'first' => 'cod', + 'second' => 'sole') + ), + ); + $this->bag = new NamespacedAttributeBag('_sf2', '/'); + $this->bag->initialize($this->array); + } + + protected function tearDown() + { + $this->bag = null; + $this->array = array(); + } + + public function testInitialize() + { + $bag = new NamespacedAttributeBag(); + $bag->initialize($this->array); + $this->assertEquals($this->array, $this->bag->all()); + $array = array('should' => 'not stick'); + $bag->initialize($array); + + // should have remained the same + $this->assertEquals($this->array, $this->bag->all()); + } + + public function testGetStorageKey() + { + $this->assertEquals('_sf2', $this->bag->getStorageKey()); + $attributeBag = new NamespacedAttributeBag('test'); + $this->assertEquals('test', $attributeBag->getStorageKey()); + } + + /** + * @dataProvider attributesProvider + */ + public function testHas($key, $value, $exists) + { + $this->assertEquals($exists, $this->bag->has($key)); + } + + /** + * @dataProvider attributesProvider + */ + public function testGet($key, $value, $expected) + { + $this->assertEquals($value, $this->bag->get($key)); + } + + public function testGetDefaults() + { + $this->assertNull($this->bag->get('user2.login')); + $this->assertEquals('default', $this->bag->get('user2.login', 'default')); + } + + /** + * @dataProvider attributesProvider + */ + public function testSet($key, $value, $expected) + { + $this->bag->set($key, $value); + $this->assertEquals($value, $this->bag->get($key)); + } + + public function testAll() + { + $this->assertEquals($this->array, $this->bag->all()); + + $this->bag->set('hello', 'fabien'); + $array = $this->array; + $array['hello'] = 'fabien'; + $this->assertEquals($array, $this->bag->all()); + } + + public function testReplace() + { + $array = array(); + $array['name'] = 'jack'; + $array['foo.bar'] = 'beep'; + $this->bag->replace($array); + $this->assertEquals($array, $this->bag->all()); + $this->assertNull($this->bag->get('hello')); + $this->assertNull($this->bag->get('always')); + $this->assertNull($this->bag->get('user.login')); + } + + public function testRemove() + { + $this->assertEquals('world', $this->bag->get('hello')); + $this->bag->remove('hello'); + $this->assertNull($this->bag->get('hello')); + + $this->assertEquals('be happy', $this->bag->get('always')); + $this->bag->remove('always'); + $this->assertNull($this->bag->get('always')); + + $this->assertEquals('drak', $this->bag->get('user.login')); + $this->bag->remove('user.login'); + $this->assertNull($this->bag->get('user.login')); + } + + public function testClear() + { + $this->bag->clear(); + $this->assertEquals(array(), $this->bag->all()); + } + + public function attributesProvider() + { + return array( + array('hello', 'world', true), + array('always', 'be happy', true), + array('user.login', 'drak', true), + array('csrf.token', array('a' => '1234', 'b' => '4321'), true), + array('csrf.token/a', '1234', true), + array('csrf.token/b', '4321', true), + array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true), + array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true), + array('category/fishing/first', 'cod', true), + array('category/fishing/second', 'sole', true), + array('user2.login', null, false), + array('never', null, false), + array('bye', null, false), + array('bye/for/now', null, false), + ); + } +} From c9694237d2e59109fc121148e124e0f4139d073e Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 24 Nov 2011 10:52:08 +0545 Subject: [PATCH 02/31] [HttpFoundation] Added FlashBagInterface and concrete implementation. This commit outsources the flash message processing to it's own interface. Overall flash messages now can have multiple flash types and each type can store multiple messages. For convenience there are now four flash types by default, INFO, NOTICE, WARNING and ERROR. There are two concrete implementations: one preserving the old behaviour of flash messages expiring exactly after one page load, regardless of being displayed or not; and the other where flash messages persist until explicitly popped. --- .../HttpFoundation/AutoExpireFlashBag.php | 168 ++++++++++++++++++ .../Component/HttpFoundation/FlashBag.php | 157 ++++++++++++++++ .../HttpFoundation/FlashBagInterface.php | 121 +++++++++++++ .../HttpFoundation/AutoExpireFlashBagTest.php | 152 ++++++++++++++++ .../Component/HttpFoundation/FlashBagTest.php | 148 +++++++++++++++ 5 files changed, 746 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php create mode 100644 src/Symfony/Component/HttpFoundation/FlashBag.php create mode 100644 src/Symfony/Component/HttpFoundation/FlashBagInterface.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php diff --git a/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php new file mode 100644 index 0000000000..5c435a2821 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php @@ -0,0 +1,168 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * AutoExpireFlashBag flash message container. + * + * @author Drak + */ +class AutoExpireFlashBag implements FlashBagInterface +{ + /** + * Flash messages. + * + * @var array + */ + private $flashes = array(); + + /** + * The storage key for flashes in the session + * + * @var string + */ + private $storageKey; + + /** + * Constructor. + * + * @param type $storageKey The key used to store flashes in the session. + */ + public function __construct($storageKey = '_sf2_flashes') + { + $this->storageKey = $storageKey; + $this->flashes = array('display' => array(), 'new' => array()); + } + + /** + * {@inheritdoc} + */ + public function initialize(array &$flashes) + { + $this->flashes = &$flashes; + + // The logic: messages from the last request will be stored in new, so we move them to previous + // This request we will show what is in 'display'. What is placed into 'new' this time round will + // be moved to display next time round. + $this->flashes['display'] = array_key_exists('new', $this->flashes) ? $this->flashes['new'] : array(); + $this->flashes['new'] = array(); + } + + /** + * {@inheritdoc} + */ + public function add($message, $type = self::NOTICE) + { + $this->flashes['new'][$type][] = $message; + } + + /** + * {@inheritdoc} + */ + public function get($type) + { + if (!$this->has($type)) { + return array(); + } + + return $this->flashes['display'][$type]; + } + + /** + * {@inheritdoc} + */ + public function pop($type) + { + if (!$this->has($type)) { + return array(); + } + + return $this->clear($type); + } + + /** + * {@inheritdoc} + */ + public function popAll() + { + return $this->clearAll(); + } + + /** + * {@inheritdoc} + */ + public function set($type, array $array) + { + $this->flashes['new'][$type] = $array; + } + + /** + * {@inheritdoc} + */ + public function has($type) + { + return array_key_exists($type, $this->flashes['display']); + } + + /** + * {@inheritdoc} + */ + public function keys() + { + return array_keys($this->flashes['display']); + } + + /** + * {@inheritdoc} + */ + public function all() + { + return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array(); + } + + /** + * {@inheritdoc} + */ + public function clear($type) + { + $return = array(); + if (isset($this->flashes['new'][$type])) { + unset($this->flashes['new'][$type]); + } + + if (isset($this->flashes['display'][$type])) { + $return = $this->flashes['display'][$type]; + unset($this->flashes['display'][$type]); + } + + return $return; + } + + /** + * {@inheritdoc} + */ + public function clearAll() + { + $return = $this->flashes['display']; + $this->flashes = array('new' => array(), 'display' => array()); + + return $return; + } + + /** + * {@inheritdoc} + */ + public function getStorageKey() + { + return $this->storageKey; + } +} diff --git a/src/Symfony/Component/HttpFoundation/FlashBag.php b/src/Symfony/Component/HttpFoundation/FlashBag.php new file mode 100644 index 0000000000..89d7dfffc2 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/FlashBag.php @@ -0,0 +1,157 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * FlashBag flash message container. + * + * @author Drak + */ +class FlashBag implements FlashBagInterface +{ + /** + * Flash messages. + * + * @var array + */ + private $flashes = array(); + + /** + * The storage key for flashes in the session + * + * @var string + */ + private $storageKey; + + /** + * Constructor. + * + * @param type $storageKey The key used to store flashes in the session. + */ + public function __construct($storageKey = '_sf2_flashes') + { + $this->storageKey = $storageKey; + } + + /** + * {@inheritdoc} + */ + public function initialize(array &$flashes) + { + $this->flashes = &$flashes; + } + + /** + * {@inheritdoc} + */ + public function add($message, $type = self::NOTICE) + { + $this->flashes[$type][] = $message; + } + + /** + * {@inheritdoc} + */ + public function get($type) + { + if (!$this->has($type)) { + return array(); + } + + return $this->flashes[$type]; + } + + /** + * {@inheritdoc} + */ + public function pop($type) + { + if (!$this->has($type)) { + return array(); + } + + return $this->clear($type); + } + + /** + * {@inheritdoc} + */ + public function popAll() + { + return $this->clearAll(); + } + + /** + * {@inheritdoc} + */ + public function set($type, array $array) + { + $this->flashes[$type] = $array; + } + + /** + * {@inheritdoc} + */ + public function has($type) + { + return array_key_exists($type, $this->flashes); + } + + /** + * {@inheritdoc} + */ + public function keys() + { + return array_keys($this->flashes); + } + + /** + * {@inheritdoc} + */ + public function all() + { + return $this->flashes; + } + + /** + * {@inheritdoc} + */ + public function clear($type) + { + $return = array(); + if (isset($this->flashes[$type])) { + $return = $this->flashes[$type]; + unset($this->flashes[$type]); + } + + return $return; + } + + /** + * {@inheritdoc} + */ + public function clearAll() + { + $return = $this->flashes; + $this->flashes = array(); + + return $return; + } + + /** + * {@inheritdoc} + */ + public function getStorageKey() + { + return $this->storageKey; + } +} diff --git a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php new file mode 100644 index 0000000000..89ea8e197b --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php @@ -0,0 +1,121 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * FlashBagInterface. + * + * @author Drak + */ +interface FlashBagInterface +{ + const INFO = 'info'; + const NOTICE = 'notice'; + const WARNING = 'warning'; + const ERROR = 'error'; + + /** + * Initializes the FlashBag. + * + * @param array &$flashes + */ + function initialize(array &$flashes); + + /** + * Adds a flash to the stack for a given type. + * + * @param string $message + * @param string $type + */ + function add($message, $type = self::NOTICE); + + /** + * Gets flash messages for a given type. + * + * @param string $type Message category type. + * + * @return array + */ + function get($type); + + /** + * Pops and clears flashes from the stack. + * + * @param string $type + * + * @return array + */ + function pop($type); + + /** + * Pops all flashes from the stacl and clears flashes. + * + * @param string $type + * + * @return array Empty array, or indexed array of arrays. + */ + function popAll(); + + /** + * Sets an array of flash messages for a given type. + * + * @param string $type + * @param array $array + */ + function set($type, array $array); + + /** + * Has flash messages for a given type? + * + * @param string $type + * + * @return boolean + */ + function has($type); + + /** + * Returns a list of all defined types. + * + * @return array + */ + function keys(); + + /** + * Gets all flash messages. + * + * @return array + */ + function all(); + + /** + * Clears flash messages for a given type. + * + * @param string $type + * + * @return array Returns an array of what was just cleared. + */ + function clear($type); + + /** + * Clears all flash messages. + * + * @return array Empty array or indexed arrays or array if none. + */ + function clearAll(); + + /** + * Gets the storage key for this bag. + * + * @return string + */ + function getStorageKey(); +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php new file mode 100644 index 0000000000..d3c21c6c06 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php @@ -0,0 +1,152 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpFoundation; + +use Symfony\Component\HttpFoundation\AutoExpireFlashBag as FlashBag; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * AutoExpireFlashBagTest + * + * @author Drak + */ +class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Symfony\Component\HttpFoundation\FlashBagInterface + */ + private $bag; + + /** + * @var array + */ + protected $array = array(); + + public function setUp() + { + parent::setUp(); + $this->bag = new FlashBag(); + $this->array = array('new' => array(FlashBag::NOTICE => array('A previous flash message'))); + $this->bag->initialize($this->array); + } + + public function tearDown() + { + $this->bag = null; + parent::tearDown(); + } + + public function testInitialize() + { + $bag = new FlashBag(); + $array = array('new' => array(FlashBag::NOTICE => array('A previous flash message'))); + $bag->initialize($array); + $this->assertEquals(array('A previous flash message'), $bag->get(FlashBag::NOTICE)); + $array = array('new' => array( + FlashBag::NOTICE => array('Something else'), + FlashBag::ERROR => array('a', 'b'), + )); + $bag->initialize($array); + $this->assertEquals(array('Something else'), $bag->get(FlashBag::NOTICE)); + $this->assertEquals(array('a', 'b'), $bag->get(FlashBag::ERROR)); + } + + public function testAdd() + { + $this->bag->add('Something new', FlashBag::NOTICE); + $this->bag->add('Smile, it might work next time', FlashBag::ERROR); + $this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->get(FlashBag::ERROR)); + } + + public function testGet() + { + $this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->get('non_existing_type')); + } + + public function testSet() + { + $this->bag->set(FlashBag::NOTICE, array('Foo', 'Bar')); + $this->assertNotEquals(array('Foo', 'Bar'), $this->bag->get(FlashBag::NOTICE)); + } + + public function testHas() + { + $this->assertFalse($this->bag->has('nothing')); + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + } + + public function testKeys() + { + $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); + } + + public function testAll() + { + $array = array( + 'new' => array( + FlashBag::NOTICE => array('Foo'), + FlashBag::ERROR => array('Bar'), + ), + ); + + $this->bag->initialize($array); + $this->assertEquals(array( + FlashBag::NOTICE => array('Foo'), + FlashBag::ERROR => array('Bar'), + ), $this->bag->all() + ); + } + + public function testPop() + { + $this->assertEquals(array('A previous flash message'), $this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->pop('non_existing_type')); + } + + public function testPopAll() + { + $this->bag->set(FlashBag::NOTICE, array('Foo')); + $this->bag->set(FlashBag::ERROR, array('Bar')); + $this->assertEquals(array( + FlashBag::NOTICE => array('A previous flash message'), + ), $this->bag->popAll() + ); + + $this->assertEquals(array(), $this->bag->popAll()); + } + + public function testClear() + { + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertEquals(array('A previous flash message'), $this->bag->clear(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->clear(FlashBag::NOTICE)); + $this->assertFalse($this->bag->has(FlashBag::NOTICE)); + } + + public function testClearAll() + { + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->bag->add('Smile, it might work next time', FlashBag::ERROR); + $this->assertFalse($this->bag->has(FlashBag::ERROR)); + $this->assertEquals(array( + FlashBag::NOTICE => array('A previous flash message'), + ), $this->bag->clearAll() + ); + $this->assertEquals(array(), $this->bag->clearAll()); + $this->assertFalse($this->bag->has(FlashBag::NOTICE)); + $this->assertFalse($this->bag->has(FlashBag::ERROR)); + } + +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php new file mode 100644 index 0000000000..0d5a8a1ac5 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php @@ -0,0 +1,148 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpFoundation; + +use Symfony\Component\HttpFoundation\FlashBag; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * FlashBagTest + * + * @author Drak + */ +class FlashBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Symfony\Component\HttpFoundation\FlashBagInterface + */ + private $bag; + + /** + * @var array + */ + protected $array = array(); + + public function setUp() + { + parent::setUp(); + $this->bag = new FlashBag(); + $this->array = array(FlashBag::NOTICE => array('A previous flash message')); + $this->bag->initialize($this->array); + } + + public function tearDown() + { + $this->bag = null; + parent::tearDown(); + } + + public function testInitialize() + { + $bag = new FlashBag(); + $bag->initialize($this->array); + $this->assertEquals($this->array, $bag->all()); + $array = array('should' => array('change')); + $bag->initialize($array); + $this->assertEquals($array, $bag->all()); + } + + public function testAdd() + { + $this->bag->add('Something new', FlashBag::NOTICE); + $this->bag->add('Smile, it might work next time', FlashBag::ERROR); + $this->assertEquals(array('A previous flash message', 'Something new'), $this->bag->get(FlashBag::NOTICE)); + $this->assertEquals(array('Smile, it might work next time'), $this->bag->get(FlashBag::ERROR)); + } + + public function testGet() + { + $this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->get('non_existing_type')); + } + + public function testPop() + { + $this->assertEquals(array('A previous flash message'), $this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->pop('non_existing_type')); + } + + public function testPopAll() + { + $this->bag->set(FlashBag::NOTICE, array('Foo')); + $this->bag->set(FlashBag::ERROR, array('Bar')); + $this->assertEquals(array( + FlashBag::NOTICE => array('Foo'), + FlashBag::ERROR => array('Bar')), $this->bag->popAll() + ); + + $this->assertEquals(array(), $this->bag->popAll()); + } + + public function testSet() + { + $this->bag->set(FlashBag::NOTICE, array('Foo', 'Bar')); + $this->assertEquals(array('Foo', 'Bar'), $this->bag->get(FlashBag::NOTICE)); + } + + public function testHas() + { + $this->assertFalse($this->bag->has('nothing')); + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + } + + public function testKeys() + { + $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); + } + + public function testAll() + { + $this->bag->set(FlashBag::NOTICE, array('Foo')); + $this->bag->set(FlashBag::ERROR, array('Bar')); + $this->assertEquals(array( + FlashBag::NOTICE => array('Foo'), + FlashBag::ERROR => array('Bar')), $this->bag->all() + ); + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertTrue($this->bag->has(FlashBag::ERROR)); + $this->assertEquals(array( + FlashBag::NOTICE => array('Foo'), + FlashBag::ERROR => array('Bar'), + ), $this->bag->all() + ); + } + + public function testClear() + { + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertEquals(array('A previous flash message'), $this->bag->clear(FlashBag::NOTICE)); + $this->assertEquals(array(), $this->bag->clear(FlashBag::NOTICE)); + $this->assertFalse($this->bag->has(FlashBag::NOTICE)); + } + + public function testClearAll() + { + $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->bag->add('Smile, it might work next time', FlashBag::ERROR); + $this->assertTrue($this->bag->has(FlashBag::ERROR)); + $this->assertEquals(array( + FlashBag::NOTICE => array('A previous flash message'), + FlashBag::ERROR => array('Smile, it might work next time'), + ), $this->bag->clearAll() + ); + $this->assertEquals(array(), $this->bag->clearAll()); + $this->assertFalse($this->bag->has(FlashBag::NOTICE)); + $this->assertFalse($this->bag->has(FlashBag::ERROR)); + } + +} From 3a263dc0886827ab0d12937c99b85caf126d9e06 Mon Sep 17 00:00:00 2001 From: Drak Date: Tue, 29 Nov 2011 10:20:09 +0545 Subject: [PATCH 03/31] [HttpFoundation] Introduced session storage base class and interfaces. Session object now implements SessionInterface to make it more portable. AbstractSessionStorage and SessionSaveHandlerInterface now makes implementation of session storage drivers simple and easy to write for both custom save handlers and native php save handlers and respect the PHP session workflow. --- .../Component/HttpFoundation/Session.php | 358 ++++++++--------- .../HttpFoundation/SessionInterface.php | 138 +++++++ .../SessionStorage/AbstractSessionStorage.php | 326 +++++++++++++++ .../SessionSaveHandlerInterface.php | 157 ++++++++ .../SessionStorageInterface.php | 90 ++--- .../AbstractSessionStorageTest.php | 118 ++++++ .../Component/HttpFoundation/SessionTest.php | 372 ++++++++++++------ 7 files changed, 1188 insertions(+), 371 deletions(-) create mode 100644 src/Symfony/Component/HttpFoundation/SessionInterface.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 721a6c7240..e0961c411f 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -12,62 +12,45 @@ namespace Symfony\Component\HttpFoundation; use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; /** * Session. * * @author Fabien Potencier + * @author Drak * * @api */ -class Session implements \Serializable +class Session implements SessionInterface { + /** + * Storage driver. + * + * @var SessionStorageInterface + */ protected $storage; - protected $started; - protected $attributes; - protected $flashes; - protected $oldFlashes; - protected $closed; /** * Constructor. * - * @param SessionStorageInterface $storage A SessionStorageInterface instance + * @param SessionStorageInterface $storage A SessionStorageInterface instance. */ public function __construct(SessionStorageInterface $storage) { $this->storage = $storage; - $this->flashes = array(); - $this->oldFlashes = array(); - $this->attributes = array(); - $this->started = false; - $this->closed = false; } /** * Starts the session storage. * + * @return boolean True if session started. + * * @api */ public function start() { - if (true === $this->started) { - return; - } - - $this->storage->start(); - - $attributes = $this->storage->read('_symfony2'); - - if (isset($attributes['attributes'])) { - $this->attributes = $attributes['attributes']; - $this->flashes = $attributes['flashes']; - - // flag current flash messages to be removed at shutdown - $this->oldFlashes = $this->flashes; - } - - $this->started = true; + return $this->storage->start(); } /** @@ -81,7 +64,7 @@ class Session implements \Serializable */ public function has($name) { - return array_key_exists($name, $this->attributes); + return $this->storage->getAttributes()->has($name); } /** @@ -96,7 +79,7 @@ class Session implements \Serializable */ public function get($name, $default = null) { - return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default; + return $this->storage->getAttributes()->get($name, $default); } /** @@ -109,11 +92,7 @@ class Session implements \Serializable */ public function set($name, $value) { - if (false === $this->started) { - $this->start(); - } - - $this->attributes[$name] = $value; + $this->storage->getAttributes()->set($name, $value); } /** @@ -125,7 +104,7 @@ class Session implements \Serializable */ public function all() { - return $this->attributes; + return $this->storage->getAttributes()->all(); } /** @@ -137,11 +116,7 @@ class Session implements \Serializable */ public function replace(array $attributes) { - if (false === $this->started) { - $this->start(); - } - - $this->attributes = $attributes; + $this->storage->getAttributes()->replace($attributes); } /** @@ -153,13 +128,7 @@ class Session implements \Serializable */ public function remove($name) { - if (false === $this->started) { - $this->start(); - } - - if (array_key_exists($name, $this->attributes)) { - unset($this->attributes[$name]); - } + return $this->storage->getAttributes()->remove($name); } /** @@ -169,190 +138,193 @@ class Session implements \Serializable */ public function clear() { - if (false === $this->started) { - $this->start(); - } - - $this->attributes = array(); - $this->flashes = array(); + $this->storage->getAttributes()->clear(); } /** * Invalidates the current session. * + * Clears all session attributes and flashes and regenerates the + * session and deletes the old session from persistence. + * + * @return boolean True if session invalidated, false if error. + * * @api */ public function invalidate() { - $this->clear(); - $this->storage->regenerate(true); + $this->storage->clear(); + + return $this->storage->regenerate(true); } /** * Migrates the current session to a new session id while maintaining all * session attributes. * + * @param boolean $destroy Whether to delete the old session or leave it to garbage collection. + * + * @return boolean True if session migrated, false if error + * * @api */ - public function migrate() + public function migrate($destroy = false) { - $this->storage->regenerate(); + return $this->storage->regenerate($destroy); + } + + /** + * {@inheritdoc} + */ + public function save() + { + $this->storage->save(); } /** * Returns the session ID * - * @return mixed The session ID + * @return mixed The session ID * * @api */ public function getId() { - if (false === $this->started) { - $this->start(); - } - return $this->storage->getId(); } /** - * Gets the flash messages. + * Implements the \Serialize interface. * - * @return array + * @return SessionStorageInterface */ - public function getFlashes() - { - return $this->flashes; - } - - /** - * Sets the flash messages. - * - * @param array $values - */ - public function setFlashes($values) - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes = $values; - $this->oldFlashes = array(); - } - - /** - * Gets a flash message. - * - * @param string $name - * @param string|null $default - * - * @return string - */ - public function getFlash($name, $default = null) - { - return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default; - } - - /** - * Sets a flash message. - * - * @param string $name - * @param string $value - */ - public function setFlash($name, $value) - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes[$name] = $value; - unset($this->oldFlashes[$name]); - } - - /** - * Checks whether a flash message exists. - * - * @param string $name - * - * @return Boolean - */ - public function hasFlash($name) - { - if (false === $this->started) { - $this->start(); - } - - return array_key_exists($name, $this->flashes); - } - - /** - * Removes a flash message. - * - * @param string $name - */ - public function removeFlash($name) - { - if (false === $this->started) { - $this->start(); - } - - unset($this->flashes[$name]); - } - - /** - * Removes the flash messages. - */ - public function clearFlashes() - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes = array(); - $this->oldFlashes = array(); - } - - public function save() - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes = array_diff_key($this->flashes, $this->oldFlashes); - - $this->storage->write('_symfony2', array( - 'attributes' => $this->attributes, - 'flashes' => $this->flashes, - )); - } - - /** - * This method should be called when you don't want the session to be saved - * when the Session object is garbaged collected (useful for instance when - * you want to simulate the interaction of several users/sessions in a single - * PHP process). - */ - public function close() - { - $this->closed = true; - } - - public function __destruct() - { - if (true === $this->started && !$this->closed) { - $this->save(); - } - } - public function serialize() { return serialize($this->storage); } + /** + * Implements the \Serialize interface. + * + * @throws \InvalidArgumentException If the passed string does not unserialize to an instance of SessionStorageInterface + */ public function unserialize($serialized) { - $this->storage = unserialize($serialized); - $this->attributes = array(); - $this->started = false; + $storage = unserialize($serialized); + if (!$storage instanceof SessionStorageInterface) { + throw new \InvalidArgumentException('Serialized data did not return a valid instance of SessionStorageInterface'); + } + + $this->storage = $storage; + } + + /** + * Adds a flash to the stack for a given type. + * + * @param string $message + * @param string $type + */ + public function addFlash($message, $type = FlashBagInterface::NOTICE) + { + $this->storage->getFlashes()->add($message, $type); + } + + /** + * Gets flash messages for a given type. + * + * @param string $type Message category type. + * + * @return array + */ + public function getFlashes($type = FlashBagInterface::NOTICE) + { + return $this->storage->getFlashes()->get($type); + } + + /** + * Pops flash messages off th stack for a given type. + * + * @param string $type Message category type. + * + * @return array + */ + public function popFlashes($type = FlashBagInterface::NOTICE) + { + return $this->storage->getFlashes()->pop($type); + } + + /** + * Pop all flash messages from the stack. + * + * @return array Empty array or indexed array of arrays. + */ + public function popAllFlashes() + { + return $this->storage->getFlashes()->popAll(); + } + + /** + * Sets an array of flash messages for a given type. + * + * @param string $type + * @param array $array + */ + public function setFlashes($type, array $array) + { + $this->storage->getFlashes()->set($type, $array); + } + + /** + * Has flash messages for a given type? + * + * @param string $type + * + * @return boolean + */ + public function hasFlashes($type) + { + return $this->storage->getFlashes()->has($type); + } + + /** + * Returns a list of all defined types. + * + * @return array + */ + public function getFlashKeys() + { + return $this->storage->getFlashes()->keys(); + } + + /** + * Gets all flash messages. + * + * @return array + */ + public function getAllFlashes() + { + return $this->storage->getFlashes()->all(); + } + + /** + * Clears flash messages for a given type. + * + * @param string $type + * + * @return array Returns an array of what was just cleared. + */ + public function clearFlashes($type) + { + return $this->storage->getFlashes()->clear($type); + } + + /** + * Clears all flash messages. + * + * @return array Empty array or indexed arrays or array if none. + */ + public function clearAllFlashes() + { + return $this->storage->getFlashes()->clearAll(); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionInterface.php b/src/Symfony/Component/HttpFoundation/SessionInterface.php new file mode 100644 index 0000000000..acc53635fc --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionInterface.php @@ -0,0 +1,138 @@ + + * + * 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\SessionStorage\AttributeInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * Interface for the session. + * + * @author Drak + */ +interface SessionInterface extends AttributeInterface, \Serializable +{ + /** + * Starts the session storage. + * + * @throws \RuntimeException If session fails to start. + */ + function start(); + + /** + * Invalidates the current session. + * + * @return boolean True if session invalidated, false if error. + */ + function invalidate(); + + /** + * Migrates the current session to a new session id while maintaining all + * session attributes. + * + * @param boolean $destroy Whether to delete the old session or leave it to garbage collection. + * + * @return boolean True if session migrated, false if error. + * + * @api + */ + function migrate($destroy = false); + + /** + * Force the session to be saved and closed. + * + * This method is generally not required for real sessions as + * the session will be automatically saved at the end of + * code execution. + */ + function save(); + + /** + * Adds a flash to the stack for a given type. + * + * @param string $message + * @param string $type + */ + function addFlash($message, $type = FlashBagInterface::NOTICE); + + /** + * Gets flash messages for a given type. + * + * @param string $type Message category type. + * + * @return array + */ + function getFlashes($type = FlashBagInterface::NOTICE); + + /** + * Pops flash messages off th stack for a given type. + * + * @param string $type Message category type. + * + * @return array + */ + function popFlashes($type = FlashBagInterface::NOTICE); + + /** + * Pop all flash messages from the stack. + * + * @return array Empty array or indexed array of arrays. + */ + function popAllFlashes(); + + /** + * Sets an array of flash messages for a given type. + * + * @param string $type + * @param array $array + */ + function setFlashes($type, array $array); + + /** + * Has flash messages for a given type? + * + * @param string $type + * + * @return boolean + */ + function hasFlashes($type); + + /** + * Returns a list of all defined types. + * + * @return array + */ + function getFlashKeys(); + + /** + * Gets all flash messages. + * + * @return array + */ + function getAllFlashes(); + + /** + * Clears flash messages for a given type. + * + * @param string $type + * + * @return array Returns an array of what was just cleared. + */ + function clearFlashes($type); + + /** + * Clears all flash messages. + * + * @return array Array of arrays or array if none. + */ + function clearAllFlashes(); +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php new file mode 100644 index 0000000000..db0ecca327 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php @@ -0,0 +1,326 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +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; + +/** + * This provides a base class for session attribute storage. + * + * @author Drak + */ +abstract class AbstractSessionStorage implements SessionStorageInterface +{ + /** + * @var \Symfony\Component\HttpFoundation\FlashBagInterface + */ + protected $flashBag; + + /** + * @var \Symfony\Component\HttpFoundation\AttributeBagInterface + */ + protected $attributeBag; + + /** + * @var array + */ + protected $options; + + /** + * @var boolean + */ + protected $started = false; + + /** + * @var boolean + */ + protected $closed = false; + + /** + * Constructor. + * + * Depending on how you want the storage driver to behave you probably + * want top override this constructor entirely. + * + * List of options for $options array with their defaults. + * @see http://www.php.net/manual/en/session.configuration.php for options + * but we omit 'session.' from the beginning of the keys. + * + * auto_start, "0" + * cookie_domain, "" + * cookie_httponly, "" + * cookie_lifetime, "0" + * cookie_path, "/" + * cookie_secure, "" + * entropy_file, "" + * entropy_length, "0" + * gc_divisor, "100" + * gc_maxlifetime, "1440" + * gc_probability, "1" + * hash_bits_per_character, "4" + * hash_function, "0" + * name, "PHPSESSID" + * referer_check, "" + * save_path, "" + * serialize_handler, "php" + * use_cookies, "1" + * use_only_cookies, "1" + * use_trans_sid, "0" + * upload_progress.enabled, "1" + * upload_progress.cleanup, "1" + * upload_progress.prefix, "upload_progress_" + * upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS" + * upload_progress.freq, "1%" + * 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()) + { + $this->attributeBag = $attributes ? $attributes : new AttributeBag(); + $this->flashBag = $flashes ? $flashes : new FlashBag(); + $this->setOptions($options); + $this->registerSaveHandlers(); + $this->registerShutdownFunction(); + } + + /** + * {@inheritdoc} + */ + public function getFlashes() + { + if (!$this->started) { + $this->start(); + } + + return $this->flashBag; + } + + /** + * {@inheritdoc} + */ + public function getAttributes() + { + if (!$this->started) { + $this->start(); + } + + return $this->attributeBag; + } + + /** + * {@inheritdoc} + */ + public function start() + { + if ($this->started && !$this->closed) { + return true; + } + + // start the session + if (!session_start()) { + throw new \RuntimeException('Failed to start the session'); + } + + $this->loadSession(); + + $this->started = true; + $this->closed = false; + + return true; + } + + /** + * {@inheritdoc} + */ + public function getId() + { + if (!$this->started) { + return ''; // returning empty is consistent with session_id() behaviour + } + + return session_id(); + } + + /** + * Regenerates the session. + * + * This method will regenerate the session ID and optionally + * destroy the old ID. Session regeneration should be done + * periodically and for example, should be done when converting + * an anonymous session to a logged in user session. + * + * @param boolean $destroy + * + * @return boolean Returns true on success or false on failure. + */ + public function regenerate($destroy = false) + { + return session_regenerate_id($destroy); + } + + /** + * {@inheritdoc} + */ + public function save() + { + session_write_close(); + $this->closed = true; + } + + /** + * {@inheritdoc} + */ + public function clear() + { + // clear out the bags + $this->attributeBag->clear(); + $this->flashBag->clearAll(); + + // clear out the session + $_SESSION = array(); + + // reconnect the bags to the session + $this->loadSession(); + } + + /** + * Sets session.* ini variables. + * + * For convenience we omit 'session.' from the beginning of the keys. + * Explicitly ignores other ini keys. + * + * session_get_cookie_params() overrides values. + * + * @param array $options + * + * @see http://www.php.net/manual/en/session.configuration.php + */ + protected function setOptions(array $options) + { + $cookieDefaults = session_get_cookie_params(); + $this->options = array_merge(array( + 'cookie_lifetime' => $cookieDefaults['lifetime'], + 'cookie_path' => $cookieDefaults['path'], + 'cookie_domain' => $cookieDefaults['domain'], + 'cookie_secure' => $cookieDefaults['secure'], + 'cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false, + ), $options); + + // Unless session.cache_limiter has been set explicitly, disable it + // because this is managed by HeaderBag directly (if used). + if (!array_key_exists('cache_limiter', $this->options)) { + $this->options['cache_limiter'] = 0; + } + + foreach ($this->options as $key => $value) { + if (in_array($key, array( + 'auto_start', 'cookie_domain', 'cookie_httponly', + 'cookie_lifetime', 'cookie_path', 'cookie_secure', + 'entropy_file', 'entropy_length', 'gc_divisor', + 'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character', + 'hash_function', 'name', 'referer_check', + 'save_path', 'serialize_handler', 'use_cookies', + 'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled', + 'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name', + 'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags'))) { + ini_set('session.'.$key, $value); + } + } + } + + /** + * Registers this storage device for PHP session handling. + * + * PHP requires session save handlers to be set, either it's own, or custom ones. + * 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 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 + * to be stored. + * + * When a session is specifically destroyed, PHP will call the sessionDestroy() 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 + * 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. + * + * NOTE: + * + * To use PHP native save handlers, override this method using ini_set with + * session.save_handlers and session.save_path e.g. + * + * ini_set('session.save_handlers', 'files'); + * ini_set('session.save_path', /tmp'); + * + * @see http://php.net/manual/en/function.session-set-save-handler.php + * @see SessionSaveHandlerInterface + */ + 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) { + session_set_save_handler( + array($this, 'sessionOpen'), + array($this, 'sessionClose'), + array($this, 'sessionRead'), + array($this, 'sessionWrite'), + array($this, 'sessionDestroy'), + array($this, 'sessionGc') + ); + } + } + + /** + * Registers PHP shutdown function. + * + * This method is required to avoid strange issues when using PHP objects as + * session save handlers. + */ + protected function registerShutdownFunction() + { + register_shutdown_function('session_write_close'); + } + + /** + * Load the session with attributes. + * + * After starting the session, PHP retrieves the session from whatever handlers + * are set to (either PHP's internal, custom set with session_set_save_handler()). + * PHP takes the return value from the sessionRead() handler, unserializes it + * and populates $_SESSION with the result automatically. + */ + protected function loadSession() + { + $key = $this->attributeBag->getStorageKey(); + $_SESSION[$key] = isset($_SESSION[$key]) ? $_SESSION[$key] : array(); + $this->attributeBag->initialize($_SESSION[$key]); + + $key = $this->flashBag->getStorageKey(); + $_SESSION[$key] = isset($_SESSION[$key]) ? $_SESSION[$key] : array(); + $this->flashBag->initialize($_SESSION[$key]); + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php new file mode 100644 index 0000000000..c45b9d22dc --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php @@ -0,0 +1,157 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +/** + * 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 sessionRead() 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 + * sessionDestroy() 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 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 return true. + * + * @author Drak + */ +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 sessionOpen($savePath, $sessionName); + + /** + * Close session. + * + * This method is for internal use by PHP and must not be called manually. + * + * @return boolean + */ + function sessionClose(); + + /** + * 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 sessionRead($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 sessionWrite($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 sessionDestroy($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 sessionGc($lifetime); +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php index b61a2557b2..8624e52ec6 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php @@ -11,10 +11,14 @@ namespace Symfony\Component\HttpFoundation\SessionStorage; +use Symfony\Component\HttpFoundation\FlashBagInterface; +use Symfony\Component\HttpFoundation\AttributeBagInterface; + /** * SessionStorageInterface. * * @author Fabien Potencier + * @author Drak * * @api */ @@ -23,6 +27,10 @@ interface SessionStorageInterface /** * Starts the session. * + * @throws \RuntimeException If something goes wrong starting the session. + * + * @return boolean True if started. + * * @api */ function start(); @@ -30,61 +38,20 @@ interface SessionStorageInterface /** * Returns the session ID * - * @return mixed The session ID - * - * @throws \RuntimeException If the session was not started yet + * @return mixed The session ID or false if the session has not started. * * @api */ function getId(); - /** - * Reads data from this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * - * @return mixed Data associated with the key - * - * @throws \RuntimeException If an error occurs while reading data from this storage - * - * @api - */ - function read($key); - - /** - * Removes data from this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * - * @return mixed Data associated with the key - * - * @throws \RuntimeException If an error occurs while removing data from this storage - * - * @api - */ - function remove($key); - - /** - * Writes data to this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * @param mixed $data Data associated with your key - * - * @throws \RuntimeException If an error occurs while writing to this storage - * - * @api - */ - function write($key, $data); - /** * Regenerates id that represents this storage. * + * This method must invoke session_regenerate_id($destroy) unless + * this interface is used for a storage object designed for unit + * or functional testing where a real PHP session would interfere + * with testing. + * * @param Boolean $destroy Destroy session when regenerating? * * @return Boolean True if session regenerated, false if error @@ -94,4 +61,33 @@ interface SessionStorageInterface * @api */ function regenerate($destroy = false); + + /** + * Force the session to be saved and closed. + * + * This method must invoke session_write_close() unless this interface is + * used for a storage object design for unit or functional testing where + * a real PHP session would interfere with testing, in which case it + * it should actually persist the session data if required. + */ + function save(); + + /** + * Clear all session data in memory. + */ + function clear(); + + /** + * Gets the FlashBagInterface driver. + * + * @return FlashBagInterface + */ + function getFlashes(); + + /** + * Gets the AttributeBagInterface driver. + * + * @return AttributeBagInterface + */ + function getAttributes(); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php new file mode 100644 index 0000000000..c98d03cd87 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php @@ -0,0 +1,118 @@ + + * + * These tests require separate processes. + * + * @runTestsInSeparateProcesses + */ +class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @return AbstractSessionStorage + */ + protected function getStorage() + { + return new CustomHandlerSessionStorage(new AttributeBag(), new FlashBag()); + } + + public function testGetFlashBag() + { + $storage = $this->getStorage(); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes()); + } + + public function testGetAttributeBag() + { + $storage = $this->getStorage(); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes()); + } + + public function testGetId() + { + $storage = $this->getStorage(); + $this->assertEquals('', $storage->getId()); + $storage->start(); + $this->assertNotEquals('', $storage->getId()); + } + + public function testRegenerate() + { + $storage = $this->getStorage(); + $storage->start(); + $id = $storage->getId(); + $storage->getAttributes()->set('lucky', 7); + $storage->regenerate(); + $this->assertNotEquals($id, $storage->getId()); + $this->assertEquals(7, $storage->getAttributes()->get('lucky')); + + } + + public function testRegenerateDestroy() + { + $storage = $this->getStorage(); + $storage->start(); + $id = $storage->getId(); + $storage->getAttributes()->set('legs', 11); + $storage->regenerate(true); + $this->assertNotEquals($id, $storage->getId()); + $this->assertEquals(11, $storage->getAttributes()->get('legs')); + } + + public function testCustomSaveHandlers() + { + $storage = new CustomHandlerSessionStorage(new AttributeBag(), new FlashBag()); + $this->assertEquals('user', ini_get('session.save_handler')); + } + + public function testNativeSaveHandlers() + { + $storage = new ConcreteSessionStorage(new AttributeBag(), new FlashBag()); + $this->assertNotEquals('user', ini_get('session.save_handler')); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index 8318101e66..892672e9e0 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -12,6 +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\SessionStorage\ArraySessionStorage; /** @@ -19,16 +23,24 @@ use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; * * @author Fabien Potencier * @author Robert Schönthal + * @author Drak */ class SessionTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface + */ protected $storage; + + /** + * @var \Symfony\Component\HttpFoundation\SessionInterface + */ protected $session; public function setUp() { - $this->storage = new ArraySessionStorage(); - $this->session = $this->getSession(); + $this->storage = new ArraySessionStorage(new AttributeBag(), new FlashBag()); + $this->session = new Session($this->storage); } protected function tearDown() @@ -37,99 +49,106 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->session = null; } - public function testFlash() + public function testStart() { - $this->session->clearFlashes(); - - $this->assertSame(array(), $this->session->getFlashes()); - - $this->assertFalse($this->session->hasFlash('foo')); - - $this->session->setFlash('foo', 'bar'); - - $this->assertTrue($this->session->hasFlash('foo')); - $this->assertSame('bar', $this->session->getFlash('foo')); - - $this->session->removeFlash('foo'); - - $this->assertFalse($this->session->hasFlash('foo')); - - $flashes = array('foo' => 'bar', 'bar' => 'foo'); - - $this->session->setFlashes($flashes); - - $this->assertSame($flashes, $this->session->getFlashes()); + $this->assertEquals('', $this->session->getId()); + $this->assertTrue($this->session->start()); + $this->assertNotEquals('', $this->session->getId()); } - public function testFlashesAreFlushedWhenNeeded() + public function testGet() { - $this->session->setFlash('foo', 'bar'); - $this->session->save(); - - $this->session = $this->getSession(); - $this->assertTrue($this->session->hasFlash('foo')); - $this->session->save(); - - $this->session = $this->getSession(); - $this->assertFalse($this->session->hasFlash('foo')); - } - - public function testAll() - { - $this->assertFalse($this->session->has('foo')); + // tests defaults $this->assertNull($this->session->get('foo')); - - $this->session->set('foo', 'bar'); - - $this->assertTrue($this->session->has('foo')); - $this->assertSame('bar', $this->session->get('foo')); - - $this->session = $this->getSession(); - - $this->session->remove('foo'); - $this->session->set('foo', 'bar'); - - $this->session->remove('foo'); - - $this->assertFalse($this->session->has('foo')); - - $attrs = array('foo' => 'bar', 'bar' => 'foo'); - - $this->session = $this->getSession(); - - $this->session->replace($attrs); - - $this->assertSame($attrs, $this->session->all()); - - $this->session->clear(); - - $this->assertSame(array(), $this->session->all()); + $this->assertEquals(1, $this->session->get('foo', 1)); } - public function testMigrateAndInvalidate() + /** + * @dataProvider setProvider + */ + public function testSet($key, $value) { - $this->session->set('foo', 'bar'); - $this->session->setFlash('foo', 'bar'); + $this->session->set($key, $value); + $this->assertEquals($value, $this->session->get($key)); + } - $this->assertSame('bar', $this->session->get('foo')); - $this->assertSame('bar', $this->session->getFlash('foo')); + public function testReplace() + { + $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome')); + $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all()); + $this->session->replace(array()); + $this->assertEquals(array(), $this->session->all()); + } - $this->session->migrate(); + /** + * @dataProvider setProvider + */ + public function testAll($key, $value, $result) + { + $this->session->set($key, $value); + $this->assertEquals($result, $this->session->all()); + } - $this->assertSame('bar', $this->session->get('foo')); - $this->assertSame('bar', $this->session->getFlash('foo')); + /** + * @dataProvider setProvider + */ + public function testClear($key, $value) + { + $this->session->set('hi', 'fabien'); + $this->session->set($key, $value); + $this->session->clear(); + $this->assertEquals(array(), $this->session->all()); + } - $this->session = $this->getSession(); + public function setProvider() + { + return array( + array('foo', 'bar', array('foo' => 'bar')), + array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')), + array('great', 'symfony2 is great', array('great' => 'symfony2 is great')), + ); + } + + /** + * @dataProvider setProvider + */ + public function testRemove($key, $value) + { + $this->session->set('hi.world', 'have a nice day'); + $this->session->set($key, $value); + $this->session->remove($key); + $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all()); + } + + public function testInvalidate() + { + $this->session->set('invalidate', 123); + $this->session->addFlash('OK'); $this->session->invalidate(); + $this->assertEquals(array(), $this->session->all()); + $this->assertEquals(array(), $this->session->getAllFlashes()); + } - $this->assertSame(array(), $this->session->all()); - $this->assertSame(array(), $this->session->getFlashes()); + public function testMigrate() + { + $this->session->set('migrate', 321); + $this->session->addFlash('HI'); + $this->session->migrate(); + $this->assertEquals(321, $this->session->get('migrate')); + $this->assertEquals(array('HI'), $this->session->getFlashes(FlashBag::NOTICE)); + } + + public function testMigrateDestroy() + { + $this->session->set('migrate', 333); + $this->session->addFlash('Bye'); + $this->session->migrate(true); + $this->assertEquals(333, $this->session->get('migrate')); + $this->assertEquals(array('Bye'), $this->session->getFlashes(FlashBag::NOTICE)); } public function testSerialize() { - $this->session = new Session($this->storage); - $compare = serialize($this->storage); $this->assertSame($compare, $this->session->serialize()); @@ -142,91 +161,182 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match'); } - public function testSave() + /** + * @expectedException \InvalidArgumentException + */ + public function testUnserializeException() { - $this->storage = new ArraySessionStorage(); - $this->session = new Session($this->storage); - $this->session->set('foo', 'bar'); - - $this->session->save(); - $compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array())); - - $r = new \ReflectionObject($this->storage); - $p = $r->getProperty('data'); - $p->setAccessible(true); - - $this->assertSame($p->getValue($this->storage), $compare); + $serialized = serialize(new \ArrayObject()); + $this->session->unserialize($serialized); } public function testGetId() { - $this->assertNull($this->session->getId()); - } - - public function testStart() - { + $this->assertEquals('', $this->session->getId()); $this->session->start(); - - $this->assertSame(array(), $this->session->getFlashes()); - $this->assertSame(array(), $this->session->all()); + $this->assertNotEquals('', $this->session->getId()); } - public function testSavedOnDestruct() + /** + * @dataProvider provideFlashes + */ + public function testAddFlash($type, $flashes) { - $this->session->set('foo', 'bar'); + foreach ($flashes as $message) { + $this->session->addFlash($message, $type); + } - $this->session->__destruct(); + $this->assertEquals($flashes, $this->session->getFlashes($type)); + } + + /** + * @dataProvider provideFlashes + */ + public function testGetFlashes($type, $flashes) + { + $this->session->setFlashes($type, $flashes); + $this->assertEquals($flashes, $this->session->getFlashes($type)); + } + + /** + * @dataProvider provideFlashes + */ + public function testPopFlashes($type, $flashes) + { + $this->session->setFlashes($type, $flashes); + $this->assertEquals($flashes, $this->session->popFlashes($type)); + $this->assertEquals(array(), $this->session->popFlashes($type)); + } + + /** + * @dataProvider provideFlashes + */ + public function testPopAllFlashes($type, $flashes) + { + $this->session->setFlashes(FlashBag::NOTICE, array('First', 'Second')); + $this->session->setFlashes(FlashBag::ERROR, array('Third')); $expected = array( - 'attributes'=>array('foo'=>'bar'), - 'flashes'=>array(), + FlashBag::NOTICE => array('First', 'Second'), + FlashBag::ERROR => array('Third'), ); - $saved = $this->storage->read('_symfony2'); - $this->assertSame($expected, $saved); + + $this->assertEquals($expected, $this->session->popAllFlashes()); + $this->assertEquals(array(), $this->session->popAllFlashes()); } - public function testSavedOnDestructAfterManualSave() + public function testSetFlashes() { - $this->session->set('foo', 'nothing'); - $this->session->save(); - $this->session->set('foo', 'bar'); + $this->session->setFlashes(FlashBag::NOTICE, array('First', 'Second')); + $this->session->setFlashes(FlashBag::ERROR, array('Third')); + $this->assertEquals(array('First', 'Second'), $this->session->getFlashes(FlashBag::NOTICE, false)); + $this->assertEquals(array('Third'), $this->session->getFlashes(FlashBag::ERROR, false)); + } - $this->session->__destruct(); + /** + * @dataProvider provideFlashes + */ + public function testHasFlashes($type, $flashes) + { + $this->assertFalse($this->session->hasFlashes($type)); + $this->session->setFlashes($type, $flashes); + $this->assertTrue($this->session->hasFlashes($type)); + } - $expected = array( - 'attributes'=>array('foo'=>'bar'), - 'flashes'=>array(), + /** + * @dataProvider provideFlashes + */ + public function testGetFlashKeys($type, $flashes) + { + $this->assertEquals(array(), $this->session->getFlashKeys()); + $this->session->setFlashes($type, $flashes); + $this->assertEquals(array($type), $this->session->getFlashKeys()); + } + + public function testGetFlashKeysBulk() + { + $this->loadFlashes(); + $this->assertEquals(array( + FlashBag::NOTICE, FlashBag::ERROR, FlashBag::WARNING, FlashBag::INFO), $this->session->getFlashKeys() ); - $saved = $this->storage->read('_symfony2'); - $this->assertSame($expected, $saved); } - public function testStorageRegenerate() + public function testGetAllFlashes() { - $this->storage->write('foo', 'bar'); + $this->assertEquals(array(), $this->session->getAllFlashes()); - $this->assertTrue($this->storage->regenerate()); + $this->session->addFlash('a', FlashBag::NOTICE); + $this->assertEquals(array( + FlashBag::NOTICE => array('a') + ), $this->session->getAllFlashes() + ); - $this->assertEquals('bar', $this->storage->read('foo')); + $this->session->addFlash('a', FlashBag::ERROR); + $this->assertEquals(array( + FlashBag::NOTICE => array('a'), + FlashBag::ERROR => array('a'), + ), $this->session->getAllFlashes()); - $this->assertTrue($this->storage->regenerate(true)); + $this->session->addFlash('a', FlashBag::WARNING); + $this->assertEquals(array( + FlashBag::NOTICE => array('a'), + FlashBag::ERROR => array('a'), + FlashBag::WARNING => array('a'), + ), $this->session->getAllFlashes() + ); - $this->assertNull($this->storage->read('foo')); + $this->session->addFlash('a', FlashBag::INFO); + $this->assertEquals(array( + FlashBag::NOTICE => array('a'), + FlashBag::ERROR => array('a'), + FlashBag::WARNING => array('a'), + FlashBag::INFO => array('a'), + ), $this->session->getAllFlashes() + ); + + $this->assertEquals(array( + FlashBag::NOTICE => array('a'), + FlashBag::ERROR => array('a'), + FlashBag::WARNING => array('a'), + FlashBag::INFO => array('a'), + ), $this->session->getAllFlashes() + ); } - public function testStorageRemove() + /** + * @dataProvider provideFlashes + */ + public function testClearFlashes($type, $flashes) { - $this->storage->write('foo', 'bar'); - - $this->assertEquals('bar', $this->storage->read('foo')); - - $this->storage->remove('foo'); - - $this->assertNull($this->storage->read('foo')); + $this->session->setFlashes($type, $flashes); + $this->session->clearFlashes($type); + $this->assertEquals(array(), $this->session->getFlashes($type)); } - protected function getSession() + public function testClearAllFlashes() { - return new Session($this->storage); + $this->loadFlashes(); + $this->assertNotEquals(array(), $this->session->getAllFlashes()); + $this->session->clearAllFlashes(); + $this->assertEquals(array(), $this->session->getAllFlashes()); } + + protected function loadFlashes() + { + $flashes = $this->provideFlashes(); + foreach ($flashes as $data) { + $this->session->setFlashes($data[0], $data[1]); + } + } + + public function provideFlashes() + { + return array( + array(FlashBag::NOTICE, array('a', 'b', 'c')), + array(FlashBag::ERROR, array('d', 'e', 'f')), + array(FlashBag::WARNING, array('g', 'h', 'i')), + array(FlashBag::INFO, array('j', 'k', 'l')), + ); + } + } From 57ef984e959ce22a82b51940d236762b13f93679 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 3 Dec 2011 15:27:39 +0545 Subject: [PATCH 04/31] [HttpFoundation] Added unit and functional testing session storage objects. --- .../SessionStorage/ArraySessionStorage.php | 100 +++++++++--- .../SessionStorage/MockFileSessionStorage.php | 153 ++++++++++++++++++ .../NativeFileSessionStorage.php | 64 ++++++++ .../ArraySessionStorageTest.php | 80 +++++++++ .../MockFileSessionStorageTest.php | 103 ++++++++++++ 5 files changed, 474 insertions(+), 26 deletions(-) create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php index 5a6558a2b3..3d2f9741b0 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php @@ -11,50 +11,56 @@ namespace Symfony\Component\HttpFoundation\SessionStorage; +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + /** * ArraySessionStorage mocks the session for unit tests. * - * When doing functional testing, you should use FilesystemSessionStorage instead. + * No PHP session is actually started since a session can be initialized + * and shutdown only once per PHP execution cycle. + * + * When doing functional testing, you should use FileMockSessionStorage instead. * * @author Fabien Potencier * @author Bulat Shakirzyanov + * @author Drak */ - -class ArraySessionStorage implements SessionStorageInterface +class ArraySessionStorage extends AbstractSessionStorage { /** - * Storage data. - * + * @var string + */ + protected $sessionId; + + /** * @var array */ - private $data = array(); + private $attributes = array(); /** - * {@inheritdoc} + * @var array */ - public function read($key, $default = null) + private $flashes = array(); + + /** + * Injects array of attributes to simulate retrieval of existing session. + * + * @param array $array + */ + public function setAttributes(array $array) { - return array_key_exists($key, $this->data) ? $this->data[$key] : $default; + $this->attributes = $array; } /** - * {@inheritdoc} + * Injects array of flashes to simulate retrieval of existing session. + * + * @param array $array */ - public function regenerate($destroy = false) + public function setFlashes(array $array) { - if ($destroy) { - $this->data = array(); - } - - return true; - } - - /** - * {@inheritdoc} - */ - public function remove($key) - { - unset($this->data[$key]); + $this->flashes = $array; } /** @@ -62,6 +68,32 @@ class ArraySessionStorage implements SessionStorageInterface */ public function start() { + if ($this->started && !$this->closed) { + return true; + } + + $this->started = true; + $this->attributeBag->initialize($this->attributes); + $this->flashBag->initialize($this->flashes); + $this->sessionId = $this->generateSessionId(); + session_id($this->sessionId); + + return true; + } + + /** + * {@inheritdoc} + */ + public function regenerate($destroy = false) + { + if (!$this->started) { + $this->start(); + } + + $this->sessionId = $this->generateSessionId(); + session_id($this->sessionId); + + return true; } /** @@ -69,13 +101,29 @@ class ArraySessionStorage implements SessionStorageInterface */ public function getId() { + if (!$this->started) { + return ''; + } + + return $this->sessionId; } /** * {@inheritdoc} */ - public function write($key, $data) + public function save() { - $this->data[$key] = $data; + // nothing to do since we don't persist the session data + $this->closed = false; + } + + /** + * Generates a session ID. + * + * @return string + */ + protected function generateSessionId() + { + return sha1(uniqid(mt_rand(), true)); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php new file mode 100644 index 0000000000..f5f0999e83 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php @@ -0,0 +1,153 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +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. + * + * No PHP session is actually started since a session can be initialized + * and shutdown only once per PHP execution cycle. + * + * @author Drak + */ +class MockFileSessionStorage extends ArraySessionStorage +{ + /** + * @var array + */ + private $sessionData = array(); + + /** + * @var string + */ + private $savePath; + + /** + * Constructor. + * + * @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) + { + if (is_null($savePath)) { + $savePath = sys_get_temp_dir(); + } + + if (!is_dir($savePath)) { + mkdir($savePath, 0777, true); + } + + $this->savePath = $savePath; + + parent::__construct($attributes, $flashes, $options); + } + + /** + * {@inheritdoc} + */ + public function start() + { + if ($this->started) { + return true; + } + + if (!session_id()) { + session_id($this->generateSessionId()); + } + + $this->sessionId = session_id(); + + $this->read(); + + $this->started = true; + + return true; + } + + /** + * {@inheritdoc} + */ + public function regenerate($destroy = false) + { + if ($destroy) { + $this->destroy(); + } + + session_id($this->generateSessionId()); + $this->sessionId = session_id(); + + $this->save(); + + return true; + } + + /** + * {@inheritdoc} + */ + public function getId() + { + if (!$this->started) { + return ''; + } + + return $this->sessionId; + } + + /** + * {@inheritdoc} + */ + public function save() + { + file_put_contents($this->getFilePath(), serialize($this->sessionData)); + } + + private function destroy() + { + if (is_file($this->getFilePath())) { + unlink($this->getFilePath()); + } + } + + /** + * Calculate path to file. + * + * @return string File path + */ + public function getFilePath() + { + return $this->savePath . '/' . $this->sessionId . '.sess'; + } + + private function read() + { + $filePath = $this->getFilePath(); + $this->sessionData = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array(); + + $key = $this->attributeBag->getStorageKey(); + $this->sessionData[$key] = isset($this->sessionData[$key]) ? $this->sessionData[$key] : array(); + $this->attributeBag->initialize($this->sessionData[$key]); + + $key = $this->flashBag->getStorageKey(); + $this->sessionData[$key] = isset($this->sessionData[$key]) ? $this->sessionData[$key] : array(); + $this->flashBag->initialize($this->sessionData[$key]); + } + +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php new file mode 100644 index 0000000000..9297e4b718 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * NativeFileSessionStorage. + * + * Native session handler using PHP's built in file storage. + * + * @author Drak + */ +class NativeFileSessionStorage extends AbstractSessionStorage +{ + /** + * @var string + */ + private $savePath; + + /** + * Constructor. + * + * @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) + { + if (is_null($savePath)) { + $savePath = sys_get_temp_dir(); + } + + if (!is_dir($savePath)) { + mkdir($savePath, 0777, true); + } + + $this->savePath = $savePath; + + parent::__construct($attributes, $flashes, $options); + } + + /** + * {@inheritdoc} + */ + protected function registerSaveHandlers() + { + ini_set('session.save_handlers', 'files'); + ini_set('session.save_path', $this->savePath); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php new file mode 100644 index 0000000000..924fe72594 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php @@ -0,0 +1,80 @@ + + */ +class ArraySessionStorageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ArraySessionStorage + */ + private $storage; + + /** + * @var array + */ + private $attributes; + + /** + * @var array + */ + private $flashes; + + protected function setUp() + { + $this->attributes = array('foo' => 'bar'); + $this->flashes = array('notice' => 'hello'); + $this->storage = new ArraySessionStorage(new AttributeBag(), new FlashBag()); + $this->storage->setFlashes($this->flashes); + $this->storage->setAttributes($this->attributes); + } + + protected function tearDown() + { + $this->flashes = null; + $this->attributes = null; + $this->storage = null; + } + + public function testStart() + { + $this->assertEquals('', $this->storage->getId()); + $this->storage->start(); + $id = $this->storage->getId(); + $this->assertNotEquals('', $id); + $this->storage->start(); + $this->assertEquals($id, $this->storage->getId()); + } + + public function testRegenerate() + { + $this->storage->start(); + $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()); + + $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()); + } + + public function testGetId() + { + $this->assertEquals('', $this->storage->getId()); + $this->storage->start(); + $this->assertNotEquals('', $this->storage->getId()); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php new file mode 100644 index 0000000000..58f44ccf2e --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php @@ -0,0 +1,103 @@ + + */ +class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var string + */ + private $sessionDir; + + /** + * @var FileMockSessionStorage + */ + protected $storage; + + protected function setUp() + { + $this->sessionDir = sys_get_temp_dir().'/sf2test'; + $this->storage = $this->getStorage(); + } + + protected function tearDown() + { + $this->sessionDir = null; + $this->storage = null; + array_map('unlink', glob($this->sessionDir.'/*.session')); + if (is_dir($this->sessionDir)) { + rmdir($this->sessionDir); + } + } + + public function testStart() + { + $this->assertEquals('', $this->storage->getId()); + $this->assertTrue($this->storage->start()); + $id = $this->storage->getId(); + $this->assertNotEquals('', $this->storage->getId()); + $this->assertTrue($this->storage->start()); + $this->assertEquals($id, $this->storage->getId()); + } + + public function testRegenerate() + { + $this->storage->start(); + $this->storage->getAttributes()->set('regenerate', 1234); + $this->storage->regenerate(); + $this->assertEquals(1234, $this->storage->getAttributes()->get('regenerate')); + $this->storage->regenerate(true); + $this->assertEquals(1234, $this->storage->getAttributes()->get('regenerate')); + } + + public function testGetId() + { + $this->assertEquals('', $this->storage->getId()); + $this->storage->start(); + $this->assertNotEquals('', $this->storage->getId()); + } + + 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()->add('test', 'newkey'); + $this->storage->save(); + + $storage = $this->getStorage(); + $storage->start(); + $this->assertEquals('108', $storage->getAttributes()->get('new')); + $this->assertTrue($storage->getFlashes()->has('newkey')); + $this->assertEquals(array('test'), $storage->getFlashes()->get('newkey')); + } + + public function testMultipleInstances() + { + $storage1 = $this->getStorage(); + $storage1->start(); + $storage1->getAttributes()->set('foo', 'bar'); + $storage1->save(); + + $storage2 = $this->getStorage(); + $storage2->start(); + $this->assertEquals('bar', $storage2->getAttributes()->get('foo'), 'values persist between instances'); + } + + private function getStorage(array $options = array()) + { + return new MockFileSessionStorage($this->sessionDir, $options, new AttributeBag(), new FlashBag()); + } +} From 85b5c43c7ad893b7198f6a29da40d610139ef5a6 Mon Sep 17 00:00:00 2001 From: Drak Date: Sun, 4 Dec 2011 17:34:10 +0545 Subject: [PATCH 05/31] [HttpFoundation] Added drivers for PHP native session save handlers, files, sqlite, memcache and memcached. --- .../NativeMemcacheSessionStorage.php | 82 +++++++++++++++++++ .../NativeMemcachedSessionStorage.php | 81 ++++++++++++++++++ .../NativeSqliteSessionStorage.php | 59 +++++++++++++ .../NativeFileSessionStorageTest.php | 37 +++++++++ .../NativeMemcacheSessionStorageTest.php | 45 ++++++++++ .../NativeMemcachedSessionStorageTest.php | 54 ++++++++++++ .../NativeSqliteSessionStorageTest.php | 46 +++++++++++ 7 files changed, 404 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php new file mode 100644 index 0000000000..2467fd6187 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * NativeMemcacheSessionStorage. + * + * Session based on native PHP memcache database handler. + * + * @author Drak + */ +class NativeMemcacheSessionStorage extends AbstractSessionStorage +{ + /** + * @var string + */ + private $savePath; + + /** + * Constructor. + * + * @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) + { + if (!session_module_name('memcache')) { + throw new \RuntimeException('PHP does not have "memcache" session module registered'); + } + + $this->savePath = $savePath; + parent::__construct($attributes, $flashes, $options); + } + + /** + * {@inheritdoc} + */ + protected function registerSaveHandlers() + { + ini_set('session.save_handlers', 'memcache'); + ini_set('session.save_path', $this->savePath); + } + + /** + * {@inheritdoc} + * + * Sets any values memcached ini values. + * + * @see http://www.php.net/manual/en/memcache.ini.php + */ + protected function setOptions(array $options) + { + foreach ($options as $key => $value) { + if (in_array($key, array( + 'memcache.allow_failover', 'memcache.max_failover_attempts', + 'memcache.chunk_size', 'memcache.default_port', 'memcache.hash_strategy', + 'memcache.hash_function', 'memcache.protocol', 'memcache.redundancy', + 'memcache.session_redundancy', 'memcache.compress_threshold', + 'memcache.lock_timeout'))) { + ini_set($key, $value); + } + } + + parent::setOptions($options); + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php new file mode 100644 index 0000000000..71af999cef --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * NativeMemcachedSessionStorage. + * + * Session based on native PHP memcached database handler. + * + * @author Drak + */ +class NativeMemcachedSessionStorage extends AbstractSessionStorage +{ + /** + * @var string + */ + private $savePath; + + /** + * Constructor. + * + * @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) + { + if (!session_module_name('memcached')) { + throw new \RuntimeException('PHP does not have "memcached" session module registered'); + } + + $this->savePath = $savePath; + parent::__construct($attributes, $flashes, $options); + } + + /** + * {@inheritdoc} + */ + protected function registerSaveHandlers() + { + ini_set('session.save_handlers', 'memcached'); + ini_set('session.save_path', $this->savePath); + } + + /** + * {@inheritdoc} + * + * Sets any values memcached ini values. + * + * @see https://github.com/php-memcached-dev/php-memcached/blob/master/memcached.ini + */ + protected function setOptions(array $options) + { + foreach ($options as $key => $value) { + if (in_array($key, array( + 'memcached.sess_locking', 'memcached.sess_lock_wait', + 'memcached.sess_prefix', 'memcached.compression_type', + 'memcached.compression_factor', 'memcached.compression_threshold', + 'memcached.serializer'))) { + ini_set($key, $value); + } + } + + parent::setOptions($options); + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php new file mode 100644 index 0000000000..244c1624b4 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * NativeSqliteSessionStorage. + * + * Session based on native PHP sqlite database handler. + * + * @author Drak + */ +class NativeSqliteSessionStorage extends AbstractSessionStorage +{ + /** + * @var string + */ + private $dbPath; + + /** + * Constructor. + * + * @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) + { + if (!session_module_name('sqlite')) { + throw new \RuntimeException('PHP does not have "sqlite" session module registered'); + } + + $this->dbPath = $dbPath; + parent::__construct($attributes, $flashes, $options); + } + + /** + * {@inheritdoc} + */ + protected function registerSaveHandlers() + { + ini_set('session.save_handlers', 'sqlite'); + ini_set('session.save_path', $this->dbPath); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php new file mode 100644 index 0000000000..0bb4dd6cfc --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php @@ -0,0 +1,37 @@ + + * + * @runTestsInSeparateProcesses + */ +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); + $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()); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php new file mode 100644 index 0000000000..8f498f2e44 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php @@ -0,0 +1,45 @@ + + * + * @runTestsInSeparateProcesses + */ +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); + $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()); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php new file mode 100644 index 0000000000..1559a117ab --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php @@ -0,0 +1,54 @@ + + * + * @runTestsInSeparateProcesses + */ +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); + + $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()); + } +} + diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php new file mode 100644 index 0000000000..51d1b64ebe --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php @@ -0,0 +1,46 @@ + + * + * @runTestsInSeparateProcesses + */ +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); + $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()); + } +} + From e185c8d63b5b84bad89136c33db1d5bf413aa184 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 8 Dec 2011 11:35:01 +0545 Subject: [PATCH 06/31] [HttpFoundation] Refactored component for session workflow. --- .../Component/HttpFoundation/Request.php | 10 +- .../FilesystemSessionStorage.php | 192 ------------------ .../SessionStorage/NativeSessionStorage.php | 180 ---------------- .../SessionStorage/PdoSessionStorage.php | 102 +++------- .../Component/HttpFoundation/RequestTest.php | 8 +- .../FilesystemSessionStorageTest.php | 104 ---------- 6 files changed, 41 insertions(+), 555 deletions(-) delete mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php delete mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/NativeSessionStorage.php delete mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index fc33610906..bd6bf43629 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -11,6 +11,8 @@ namespace Symfony\Component\HttpFoundation; +use Symfony\Component\HttpFoundation\SessionInterface; + /** * Request represents an HTTP request. * @@ -122,7 +124,7 @@ class Request protected $format; /** - * @var \Symfony\Component\HttpFoundation\Session + * @var \Symfony\Component\HttpFoundation\SessionInterface */ protected $session; @@ -466,7 +468,7 @@ class Request /** * Gets the Session. * - * @return Session|null The session + * @return SessionInterface|null The session * * @api */ @@ -504,11 +506,11 @@ class Request /** * Sets the Session. * - * @param Session $session The Session + * @param SessionInterface $session The Session * * @api */ - public function setSession(Session $session) + public function setSession(SessionInterface $session) { $this->session = $session; } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php deleted file mode 100644 index ceb5913fe9..0000000000 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php +++ /dev/null @@ -1,192 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\SessionStorage; - -/** - * FilesystemSessionStorage simulates sessions for functional tests. - * - * This storage does not start the session (session_start()) - * as it is not "available" when running tests on the command line. - * - * @author Fabien Potencier - * - * @api - */ -class FilesystemSessionStorage extends NativeSessionStorage -{ - /** - * File path. - * - * @var string - */ - private $path; - - /** - * Data. - * - * @var array - */ - private $data; - - /** - * Session started flag. - * - * @var boolean - */ - private $started; - - /** - * Constructor. - */ - public function __construct($path, array $options = array()) - { - $this->path = $path; - $this->started = false; - - parent::__construct($options); - } - - /** - * Starts the session. - * - * @api - */ - public function start() - { - if ($this->started) { - return; - } - - session_set_cookie_params( - $this->options['lifetime'], - $this->options['path'], - $this->options['domain'], - $this->options['secure'], - $this->options['httponly'] - ); - - if (!ini_get('session.use_cookies') && isset($this->options['id']) && $this->options['id'] && $this->options['id'] != session_id()) { - session_id($this->options['id']); - } - - if (!session_id()) { - session_id(hash('md5', uniqid(mt_rand(), true))); - } - - $file = $this->path.'/'.session_id().'.session'; - - $this->data = is_file($file) ? unserialize(file_get_contents($file)) : array(); - $this->started = true; - } - - /** - * Returns the session ID - * - * @return mixed The session ID - * - * @throws \RuntimeException If the session was not started yet - * - * @api - */ - public function getId() - { - if (!$this->started) { - throw new \RuntimeException('The session must be started before reading its ID'); - } - - return session_id(); - } - - /** - * Reads data from this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * @param string $default The default value - * - * @return mixed Data associated with the key - * - * @throws \RuntimeException If an error occurs while reading data from this storage - * - * @api - */ - public function read($key, $default = null) - { - return array_key_exists($key, $this->data) ? $this->data[$key] : $default; - } - - /** - * Removes data from this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * - * @return mixed Data associated with the key - * - * @throws \RuntimeException If an error occurs while removing data from this storage - * - * @api - */ - public function remove($key) - { - $retval = $this->data[$key]; - - unset($this->data[$key]); - - return $retval; - } - - /** - * Writes data to this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * @param mixed $data Data associated with your key - * - * @throws \RuntimeException If an error occurs while writing to this storage - * - * @api - */ - public function write($key, $data) - { - $this->data[$key] = $data; - - if (!is_dir($this->path)) { - mkdir($this->path, 0777, true); - } - - file_put_contents($this->path.'/'.session_id().'.session', serialize($this->data)); - } - - /** - * Regenerates id that represents this storage. - * - * @param Boolean $destroy Destroy session when regenerating? - * - * @return Boolean True if session regenerated, false if error - * - * @throws \RuntimeException If an error occurs while regenerating this storage - * - * @api - */ - public function regenerate($destroy = false) - { - if ($destroy) { - $this->data = array(); - } - - return true; - } -} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSessionStorage.php deleted file mode 100644 index b759f7411a..0000000000 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSessionStorage.php +++ /dev/null @@ -1,180 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\SessionStorage; - -/** - * NativeSessionStorage. - * - * @author Fabien Potencier - * - * @api - */ -class NativeSessionStorage implements SessionStorageInterface -{ - static protected $sessionIdRegenerated = false; - static protected $sessionStarted = false; - - protected $options; - - /** - * Available options: - * - * * name: The cookie name (null [omitted] by default) - * * id: The session id (null [omitted] by default) - * * lifetime: Cookie lifetime - * * path: Cookie path - * * domain: Cookie domain - * * secure: Cookie secure - * * httponly: Cookie http only - * - * The default values for most options are those returned by the session_get_cookie_params() function - * - * @param array $options An associative array of session options - */ - public function __construct(array $options = array()) - { - $cookieDefaults = session_get_cookie_params(); - - $this->options = array_merge(array( - 'lifetime' => $cookieDefaults['lifetime'], - 'path' => $cookieDefaults['path'], - 'domain' => $cookieDefaults['domain'], - 'secure' => $cookieDefaults['secure'], - 'httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false, - ), $options); - - // Skip setting new session name if user don't want it - if (isset($this->options['name'])) { - session_name($this->options['name']); - } - } - - /** - * Starts the session. - * - * @api - */ - public function start() - { - if (self::$sessionStarted) { - return; - } - - session_set_cookie_params( - $this->options['lifetime'], - $this->options['path'], - $this->options['domain'], - $this->options['secure'], - $this->options['httponly'] - ); - - // disable native cache limiter as this is managed by HeaderBag directly - session_cache_limiter(false); - - if (!ini_get('session.use_cookies') && isset($this->options['id']) && $this->options['id'] && $this->options['id'] != session_id()) { - session_id($this->options['id']); - } - - session_start(); - - self::$sessionStarted = true; - } - - /** - * {@inheritDoc} - * - * @api - */ - public function getId() - { - if (!self::$sessionStarted) { - throw new \RuntimeException('The session must be started before reading its ID'); - } - - return session_id(); - } - - /** - * Reads data from this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * @param string $default Default value - * - * @return mixed Data associated with the key - * - * @api - */ - public function read($key, $default = null) - { - return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default; - } - - /** - * Removes data from this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * - * @return mixed Data associated with the key - * - * @api - */ - public function remove($key) - { - $retval = null; - - if (isset($_SESSION[$key])) { - $retval = $_SESSION[$key]; - unset($_SESSION[$key]); - } - - return $retval; - } - - /** - * Writes data to this storage. - * - * The preferred format for a key is directory style so naming conflicts can be avoided. - * - * @param string $key A unique key identifying your data - * @param mixed $data Data associated with your key - * - * @api - */ - public function write($key, $data) - { - $_SESSION[$key] = $data; - } - - /** - * Regenerates id that represents this storage. - * - * @param Boolean $destroy Destroy session when regenerating? - * - * @return Boolean True if session regenerated, false if error - * - * @api - */ - public function regenerate($destroy = false) - { - if (self::$sessionIdRegenerated) { - return; - } - - session_regenerate_id($destroy); - - self::$sessionIdRegenerated = true; - } -} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php index 24898b7c20..1a264f1753 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php @@ -11,24 +11,28 @@ namespace Symfony\Component\HttpFoundation\SessionStorage; +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + /** * PdoSessionStorage. * * @author Fabien Potencier * @author Michael Williams */ -class PdoSessionStorage extends NativeSessionStorage +class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { /** * PDO instance. * * @var \PDO */ - private $db; + private $pdo; /** * Database options. * + * * @var array */ private $dbOptions; @@ -36,59 +40,35 @@ class PdoSessionStorage extends NativeSessionStorage /** * Constructor. * - * @param \PDO $db A PDO instance - * @param array $options An associative array of session options - * @param array $dbOptions An associative array of DB options + * + * @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 NativeSessionStorage::__construct() + * @see AbstractSessionStorage::__construct() */ - public function __construct(\PDO $db, array $options = array(), array $dbOptions = array()) + public function __construct(\PDO $pdo, array $dbOptions = array(), array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { if (!array_key_exists('db_table', $dbOptions)) { throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.'); } - $this->db = $db; + $this->pdo = $pdo; $this->dbOptions = array_merge(array( 'db_id_col' => 'sess_id', 'db_data_col' => 'sess_data', 'db_time_col' => 'sess_time', ), $dbOptions); - parent::__construct($options); + parent::__construct($attributes, $flashes, $options); } /** - * Starts the session. - */ - public function start() - { - if (self::$sessionStarted) { - return; - } - - // use this object as the session handler - session_set_save_handler( - array($this, 'sessionOpen'), - array($this, 'sessionClose'), - array($this, 'sessionRead'), - array($this, 'sessionWrite'), - array($this, 'sessionDestroy'), - array($this, 'sessionGC') - ); - - parent::start(); - } - - /** - * Opens a session. - * - * @param string $path (ignored) - * @param string $name (ignored) - * - * @return Boolean true, if the session was opened, otherwise an exception is thrown + * {@inheritdoc} */ public function sessionOpen($path = null, $name = null) { @@ -96,22 +76,15 @@ class PdoSessionStorage extends NativeSessionStorage } /** - * Closes a session. - * - * @return Boolean true, if the session was closed, otherwise false + * {@inheritdoc} */ public function sessionClose() { - // do nothing return true; } /** - * Destroys a session. - * - * @param string $id A session ID - * - * @return Boolean true, if the session was destroyed, otherwise an exception is thrown + * {@inheritdoc} * * @throws \RuntimeException If the session cannot be destroyed */ @@ -125,7 +98,7 @@ class PdoSessionStorage extends NativeSessionStorage $sql = "DELETE FROM $dbTable WHERE $dbIdCol = :id"; try { - $stmt = $this->db->prepare($sql); + $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':id', $id, \PDO::PARAM_STR); $stmt->execute(); } catch (\PDOException $e) { @@ -136,15 +109,11 @@ class PdoSessionStorage extends NativeSessionStorage } /** - * Cleans up old sessions. - * - * @param int $lifetime The lifetime of a session - * - * @return Boolean true, if old sessions have been cleaned, otherwise an exception is thrown + * {@inheritdoc} * * @throws \RuntimeException If any old sessions cannot be cleaned */ - public function sessionGC($lifetime) + public function sessionGc($lifetime) { // get table/column $dbTable = $this->dbOptions['db_table']; @@ -154,7 +123,7 @@ class PdoSessionStorage extends NativeSessionStorage $sql = "DELETE FROM $dbTable WHERE $dbTimeCol < (:time - $lifetime)"; try { - $stmt = $this->db->prepare($sql); + $stmt = $this->pdo->prepare($sql); $stmt->bindValue(':time', time(), \PDO::PARAM_INT); $stmt->execute(); } catch (\PDOException $e) { @@ -165,11 +134,7 @@ class PdoSessionStorage extends NativeSessionStorage } /** - * Reads a session. - * - * @param string $id A session ID - * - * @return string The session data if the session was read or created, otherwise an exception is thrown + * {@inheritdoc} * * @throws \RuntimeException If the session cannot be read */ @@ -183,7 +148,7 @@ class PdoSessionStorage extends NativeSessionStorage try { $sql = "SELECT $dbDataCol FROM $dbTable WHERE $dbIdCol = :id"; - $stmt = $this->db->prepare($sql); + $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':id', $id, \PDO::PARAM_STR, 255); $stmt->execute(); @@ -200,17 +165,12 @@ class PdoSessionStorage extends NativeSessionStorage return ''; } catch (\PDOException $e) { - throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e); + throw new \RuntimeException(sprintf('PDOException was thrown when trying to read the session data: %s', $e->getMessage()), 0, $e); } } /** - * Writes session data. - * - * @param string $id A session ID - * @param string $data A serialized chunk of session data - * - * @return Boolean true, if the session was written, otherwise an exception is thrown + * {@inheritdoc} * * @throws \RuntimeException If the session data cannot be written */ @@ -222,7 +182,7 @@ class PdoSessionStorage extends NativeSessionStorage $dbIdCol = $this->dbOptions['db_id_col']; $dbTimeCol = $this->dbOptions['db_time_col']; - $sql = ('mysql' === $this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) + $sql = ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) ? "INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) " ."ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = CASE WHEN $dbTimeCol = :time THEN (VALUES($dbTimeCol) + 1) ELSE VALUES($dbTimeCol) END" : "UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id"; @@ -230,7 +190,7 @@ class PdoSessionStorage extends NativeSessionStorage try { //session data can contain non binary safe characters so we need to encode it $encoded = base64_encode($data); - $stmt = $this->db->prepare($sql); + $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':id', $id, \PDO::PARAM_STR); $stmt->bindParam(':data', $encoded, \PDO::PARAM_STR); $stmt->bindValue(':time', time(), \PDO::PARAM_INT); @@ -242,7 +202,7 @@ class PdoSessionStorage extends NativeSessionStorage $this->createNewSession($id, $data); } } catch (\PDOException $e) { - throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e); + throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e); } return true; @@ -268,7 +228,7 @@ class PdoSessionStorage extends NativeSessionStorage //session data can contain non binary safe characters so we need to encode it $encoded = base64_encode($data); - $stmt = $this->db->prepare($sql); + $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':id', $id, \PDO::PARAM_STR); $stmt->bindParam(':data', $encoded, \PDO::PARAM_STR); $stmt->bindValue(':time', time(), \PDO::PARAM_INT); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index 4b208bb3ec..9f144724f8 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -13,10 +13,10 @@ namespace Symfony\Tests\Component\HttpFoundation; use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; - 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 +848,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase $request = new Request; $this->assertFalse($request->hasSession()); - $request->setSession(new Session(new ArraySessionStorage())); + $request->setSession(new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()))); $this->assertTrue($request->hasSession()); } @@ -859,7 +859,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 ArraySessionStorage())); + $request->setSession(new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()))); $this->assertTrue($request->hasPreviousSession()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php deleted file mode 100644 index 060cb0e913..0000000000 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php +++ /dev/null @@ -1,104 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Tests\Component\HttpFoundation\SessionStorage; - -use Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage; - -class FilesystemSessionStorageTest extends \PHPUnit_Framework_TestCase -{ - private $path; - - protected function setUp() - { - $this->path = sys_get_temp_dir().'/sf2/session_test'; - if (!file_exists($this->path)) { - mkdir($this->path, 0777, true); - } - } - - protected function tearDown() - { - array_map('unlink', glob($this->path.'/*.session')); - rmdir($this->path); - - $this->path = null; - } - - public function testMultipleInstances() - { - $storage1 = new FilesystemSessionStorage($this->path); - $storage1->start(); - $storage1->write('foo', 'bar'); - - $storage2 = new FilesystemSessionStorage($this->path); - $storage2->start(); - $this->assertEquals('bar', $storage2->read('foo'), 'values persist between instances'); - } - - public function testGetIdThrowsErrorBeforeStart() - { - $this->setExpectedException('RuntimeException'); - - $storage = new FilesystemSessionStorage($this->path); - $storage->getId(); - } - - public function testGetIdWorksAfterStart() - { - $storage = new FilesystemSessionStorage($this->path); - $storage->start(); - $storage->getId(); - } - - public function testGetIdSetByOptions() - { - $previous = ini_get('session.use_cookies'); - - ini_set('session.use_cookies', false); - - $storage = new FilesystemSessionStorage($this->path, array('id' => 'symfony2-sessionId')); - $storage->start(); - - $this->assertEquals('symfony2-sessionId', $storage->getId()); - - ini_set('session.use_cookies', $previous); - } - - public function testRemoveVariable() - { - $storage = new FilesystemSessionStorage($this->path); - $storage->start(); - - $storage->write('foo', 'bar'); - - $this->assertEquals('bar', $storage->read('foo')); - - $storage->remove('foo', 'bar'); - - $this->assertNull($storage->read('foo')); - } - - public function testRegenerate() - { - $storage = new FilesystemSessionStorage($this->path); - $storage->start(); - $storage->write('foo', 'bar'); - - $storage->regenerate(); - - $this->assertEquals('bar', $storage->read('foo')); - - $storage->regenerate(true); - - $this->assertNull($storage->read('foo')); - } -} From 669bc96c7f4bc507fa8283637ff3dfd68a42b646 Mon Sep 17 00:00:00 2001 From: Drak Date: Sun, 11 Dec 2011 22:47:04 +0545 Subject: [PATCH 07/31] [HttpFoundation] Added pure Memcache, Memcached and Null storage drivers. --- .../SessionStorage/MemcacheSessionStorage.php | 143 +++++++++++++++++ .../MemcachedSessionStorage.php | 144 ++++++++++++++++++ .../SessionStorage/NullSessionStorage.php | 77 ++++++++++ .../SessionStorage/NullSessionStorageTest.php | 52 +++++++ 4 files changed, 416 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php new file mode 100644 index 0000000000..e3d8d4c42b --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php @@ -0,0 +1,143 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * MemcacheSessionStorage. + * + * @author Drak + */ +class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +{ + /** + * Memcache driver. + * + * @var Memcache + */ + private $memcache; + + /** + * Configuration options. + * + * @var array + */ + private $memcacheOptions; + + /** + * Key prefix for shared environments. + * + * @var string + */ + private $prefix; + + /** + * Constructor. + * + * @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) + { + $this->memcache = $memcache; + + // defaults + if (!isset($memcacheOptions['serverpool'])) { + $memcacheOptions['serverpool'] = array( + 'host' => '127.0.0.1', + 'port' => 11211, + 'timeout' => 1, + 'persistent' => false, + 'weight' => 1); + } + + $memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400; + $this->prefix = isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s'; + + $this->memcacheOptions = $memcacheOptions; + + parent::__construct($attributes, $flashes, $options); + } + + protected function addServer(array $server) + { + if (array_key_exists('host', $server)) { + throw new \InvalidArgumentException('host key must be set'); + } + $server['port'] = isset($server['port']) ? (int)$server['port'] : 11211; + $server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1; + $server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false; + $server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1; + } + + /** + * {@inheritdoc} + */ + public function sessionOpen($savePath, $sessionName) + { + foreach ($this->memcacheOptions['serverpool'] as $server) { + $this->addServer($server); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function sessionClose() + { + return $this->memcache->close(); + } + + /** + * {@inheritdoc} + */ + public function sessionRead($sessionId) + { + $result = $this->memcache->get($this->prefix.$sessionId); + + return ($result) ? $result : ''; + } + + /** + * {@inheritdoc} + */ + public function sessionWrite($sessionId, $data) + { + return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']); + } + + /** + * {@inheritdoc} + */ + public function sessionDestroy($sessionId) + { + return $this->memcache->delete($this->prefix.$sessionId); + } + + /** + * {@inheritdoc} + */ + public function sessionGc($lifetime) + { + // not required here because memcache will auto expire the records anyhow. + return true; + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php new file mode 100644 index 0000000000..f91c7da6d5 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php @@ -0,0 +1,144 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * MemcachedSessionStorage. + * + * @author Drak + */ +class MemcachedSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +{ + /** + * Memcached driver. + * + * @var Memcached + */ + private $memcached; + + /** + * Configuration options. + * + * @var array + */ + private $memcachedOptions; + + /** + * Constructor. + * + * @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) + { + $this->memcached = $memcached; + + // defaults + if (!isset($memcachedOptions['serverpool'])) { + $memcachedOptions['serverpool'] = array( + 'host' => '127.0.0.1', + 'port' => 11211, + 'timeout' => 1, + 'persistent' => false, + 'weight' => 1); + } + + $memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int)$memcachedOptions['expiretime'] : 86400; + + $this->memcached->setOption(\Memcached::OPT_PREFIX_KEY, isset($memcachedOptions['prefix']) ? $memcachedOption['prefix'] : 'sf2s'); + + $this->memcacheOptions = $memcachedOptions; + + parent::__construct($attributes, $flashes, $options); + } + + /** + * {@inheritdoc} + */ + public function sessionOpen($savePath, $sessionName) + { + foreach ($this->memcachedOptions['serverpool'] as $server) { + $this->addServer($server); + } + + return true; + } + + /** + * Close session. + * + * @return boolean + */ + public function sessionClose() + { + return $this->memcached->close(); + } + + /** + * {@inheritdoc} + */ + public function sessionRead($sessionId) + { + $result = $this->memcached->get($this->prefix.$sessionId); + + return $result ? $result : ''; + } + + /** + * {@inheritdoc} + */ + public function sessionWrite($sessionId, $data) + { + return $this->memcached->set($this->prefix.$sessionId, $data, false, $this->memcachedOptions['expiretime']); + } + + /** + * {@inheritdoc} + */ + public function sessionDestroy($sessionId) + { + return $this->memcached->delete($this->prefix.$sessionId); + } + + /** + * {@inheritdoc} + */ + public function sessionGc($lifetime) + { + // not required here because memcached will auto expire the records anyhow. + return true; + } + + /** + * Adds a server to the memcached handler. + * + * @param array $server + */ + protected function addServer(array $server) + { + if (array_key_exists('host', $server)) { + throw new \InvalidArgumentException('host key must be set'); + } + $server['port'] = isset($server['port']) ? (int)$server['port'] : 11211; + $server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1; + $server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false; + $server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1; + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php new file mode 100644 index 0000000000..bb62c50bd4 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * NullSessionStorage. + * + * Can be used in unit testing or in a sitation where persisted sessions are not desired. + * + * @author Drak + * + * @api + */ +class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +{ + /** + * {@inheritdoc} + */ + public function sessionOpen($savePath, $sessionName) + { + return true; + } + + /** + * Close session. + * + * @return boolean + */ + public function sessionClose() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function sessionRead($sessionId) + { + return ''; + } + + /** + * {@inheritdoc} + */ + public function sessionWrite($sessionId, $data) + { + return true; + } + + /** + * {@inheritdoc} + */ + public function sessionDestroy($sessionId) + { + return true; + } + + /** + * {@inheritdoc} + */ + public function sessionGc($lifetime) + { + return true; + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php new file mode 100644 index 0000000000..a8fcf98c40 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php @@ -0,0 +1,52 @@ + + * + * @runTestsInSeparateProcesses + */ +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(); + $this->assertEquals('user', ini_get('session.save_handler')); + } + + public function testSession() + { + session_id('nullsessionstorage'); + $storage = new NullSessionStorage(); + $session = new Session($storage); + $this->assertNull($session->get('something')); + $session->set('something', 'unique'); + $this->assertEquals('unique', $session->get('something')); + } + + public function testNothingIsPersisted() + { + session_id('nullsessionstorage'); + $storage = new NullSessionStorage(); + $session = new Session($storage); + $session->start(); + $this->assertEquals('nullsessionstorage', $session->getId()); + $this->assertNull($session->get('something')); + } +} + From 7aaf024b2ae09fc11e0f02e7c7f8fa817d1ca6e8 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 15 Dec 2011 14:31:43 +0545 Subject: [PATCH 08/31] [FrameworkBundle] Refactored code for changes to HttpFoundation component. Native PHP sessions stored to file are done with session.storage.native_file Functional testing is done with session.storage.mock_file Default flash message implementation done with FlashBag (session.flash_bag) Default attribute storage implementation with AttributeBag (session.attribute_bag) Services added: session.storage.native_file, session.storage.native_memcache, session.storage.native_memcache, session.storage.native_sqlite, session.storage.memcache, session.storage.memcached, session.storage.null, session.storage.mock_file, session.flash_bag, session.attribute_bag Services removed: session.storage.native, session.storage.filesystem --- .../DependencyInjection/Configuration.php | 2 +- .../EventListener/TestSessionListener.php | 1 - .../Resources/config/session.xml | 76 +++++++++++++++++-- .../Templating/Helper/SessionHelper.php | 13 ++-- .../DependencyInjection/Fixtures/php/full.php | 2 +- .../DependencyInjection/Fixtures/xml/full.xml | 2 +- .../DependencyInjection/Fixtures/yml/full.yml | 2 +- .../FrameworkExtensionTest.php | 2 +- .../Controller/SessionController.php | 6 +- .../Tests/Functional/app/config/framework.yml | 2 +- .../Templating/Helper/SessionHelperTest.php | 15 ++-- .../Tests/Templating/PhpEngineTest.php | 4 +- 12 files changed, 96 insertions(+), 31 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 5195ff1c2f..6738eb3e4a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -167,7 +167,7 @@ class Configuration implements ConfigurationInterface ->canBeUnset() ->children() ->booleanNode('auto_start')->defaultFalse()->end() - ->scalarNode('storage_id')->defaultValue('session.storage.native')->end() + ->scalarNode('storage_id')->defaultValue('session.storage.native_file')->end() ->scalarNode('name')->end() ->scalarNode('lifetime')->end() ->scalarNode('path')->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php index 7d6ac49a06..d335758b70 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php @@ -69,7 +69,6 @@ class TestSessionListener implements EventSubscriberInterface if ($session = $event->getRequest()->getSession()) { $session->save(); - $session->close(); $params = session_get_cookie_params(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index c3c2eba0c5..a1a746b781 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -6,8 +6,19 @@ Symfony\Component\HttpFoundation\Session - Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage + Symfony\Component\HttpFoundation\FlashBag + Symfony\Component\HttpFoundation\AttributeBag + Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\NativeMemcachedSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\NativeSqliteSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\MemcacheSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\MemcachedSessionStorage + Symfony\Component\HttpFoundation\SessionStorage\MockFileSessionStorage + Memcache + Memcached + Symfony\Bundle\FrameworkBundle\EventListener\SessionListener @@ -16,13 +27,66 @@ - - %session.storage.options% - + + + + - + %kernel.cache_dir%/sessions %session.storage.options% + + + + + + %kernel.cache_dir%/sessions + %session.storage.options% + + + + + + tcp://127.0.0.1:11211?persistent=0 + %session.storage.options% + + + + + + 127.0.0.1:11211 + %session.storage.options% + + + + + + + tcp://127.0.0.1:11211?persistent=0 + %session.storage.options% + + + + + + + tcp://127.0.0.1:11211?persistent=0 + %session.storage.options% + + + + + + %kernel.cache_dir%/sf2_sqlite_sess.db + %session.storage.options% + + + + + + %session.storage.options% + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index 041d9ddff9..a60b14997f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\FlashBagInterface; /** * SessionHelper provides read-only access to the session attributes. @@ -46,19 +47,19 @@ class SessionHelper extends Helper return $this->session->get($name, $default); } - public function getFlash($name, $default = null) + public function getFlashes($type) { - return $this->session->getFlash($name, $default); + return $this->session->getFlashes($type); } - public function getFlashes() + public function getAllFlashes() { - return $this->session->getFlashes(); + return $this->session->getAllFlashes(); } - public function hasFlash($name) + public function hasFlashes($type) { - return $this->session->hasFlash($name); + return $this->session->hasFlashes($type); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php index 01361f2280..3ab6488493 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php @@ -20,7 +20,7 @@ $container->loadFromExtension('framework', array( ), 'session' => array( 'auto_start' => true, - 'storage_id' => 'session.storage.native', + 'storage_id' => 'session.storage.native_file', 'name' => '_SYMFONY', 'lifetime' => 86400, 'path' => '/', diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index 2d6a06047e..e46a476a96 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -12,7 +12,7 @@ - + loader.foo loader.bar diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml index 3c7db0ee49..126baf2d4e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml @@ -14,7 +14,7 @@ framework: type: xml session: auto_start: true - storage_id: session.storage.native + storage_id: session.storage.native_file name: _SYMFONY lifetime: 86400 path: / diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 3da5483944..6aa952e3a6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -78,7 +78,7 @@ abstract class FrameworkExtensionTest extends TestCase $this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml'); $this->assertEquals('fr', $container->getParameter('kernel.default_locale')); $this->assertTrue($container->getDefinition('session_listener')->getArgument(1)); - $this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage')); + $this->assertEquals('session.storage.native_file', (string) $container->getAlias('session.storage')); $options = $container->getParameter('session.storage.options'); $this->assertEquals('_SYMFONY', $options['name']); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php index e77f1f1266..f1e30fd001 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php @@ -51,7 +51,7 @@ class SessionController extends ContainerAware { $request = $this->container->get('request'); $session = $request->getSession(); - $session->setFlash('notice', $message); + $session->addFlash($message, 'notice'); return new RedirectResponse($this->container->get('router')->generate('session_showflash')); } @@ -61,8 +61,8 @@ class SessionController extends ContainerAware $request = $this->container->get('request'); $session = $request->getSession(); - if ($session->hasFlash('notice')) { - $output = $session->getFlash('notice'); + if ($session->hasFlashes('notice')) { + list($output) = $session->popFlashes('notice'); } else { $output = 'No flash was set.'; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml index 18ed6f4d87..0cc6b74d30 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml @@ -10,7 +10,7 @@ framework: default_locale: en session: auto_start: true - storage_id: session.storage.filesystem + storage_id: session.storage.mock_file services: logger: { class: Symfony\Component\HttpKernel\Log\NullLogger } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index ee400cd7dc..e7e7b4a764 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -15,6 +15,8 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper; +use Symfony\Component\HttpFoundation\FlashBag; +use Symfony\Component\HttpFoundation\AttributeBag; class SessionHelperTest extends \PHPUnit_Framework_TestCase { @@ -24,9 +26,9 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $this->request = new Request(); - $session = new Session(new ArraySessionStorage()); + $session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag())); $session->set('foobar', 'bar'); - $session->setFlash('foo', 'bar'); + $session->addFlash('bar', FlashBag::NOTICE); $this->request->setSession($session); } @@ -40,14 +42,11 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $helper = new SessionHelper($this->request); - $this->assertTrue($helper->hasFlash('foo')); + $this->assertTrue($helper->hasFlashes(FlashBag::NOTICE)); - $this->assertEquals('bar', $helper->getFlash('foo')); - $this->assertEquals('foo', $helper->getFlash('bar', 'foo')); + $this->assertEquals(array('bar'), $helper->getFlashes(FlashBag::NOTICE)); - $this->assertNull($helper->getFlash('foobar')); - - $this->assertEquals(array('foo' => 'bar'), $helper->getFlashes()); + $this->assertEquals(array(FlashBag::NOTICE => array('bar')), $helper->getAllFlashes()); } public function testGet() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index cefc38b0ef..4da48ea6d8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -19,6 +19,8 @@ use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; +use Symfony\Component\HttpFoundation\FlashBag; +use Symfony\Component\HttpFoundation\AttributeBag; class PhpEngineTest extends TestCase { @@ -64,7 +66,7 @@ class PhpEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new ArraySessionStorage()); + $session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag())); $request->setSession($session); $container->set('request', $request); From 1ed6ee325c8dcba6b9da72a7ab0d2730d7e91ebe Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 24 Dec 2011 16:20:59 +0545 Subject: [PATCH 09/31] [DoctribeBridge][SecurityBundle][WebProfiler] Refactor code for HttpFoundation changes. --- .../HttpFoundation/DbalSessionStorage.php | 58 +++++++++---------- .../Tests/Functional/app/config/framework.yml | 2 +- .../EventListener/WebDebugToolbarListener.php | 10 +++- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 6fa324f85e..12697fe86f 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -3,7 +3,10 @@ namespace Symfony\Bridge\Doctrine\HttpFoundation; use Doctrine\DBAL\Platforms\MySqlPlatform; -use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage; +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; /** @@ -12,39 +15,32 @@ use Doctrine\DBAL\Driver\Connection; * @author Fabien Potencier * @author Johannes M. Schmitt */ -class DbalSessionStorage extends NativeSessionStorage +class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { + /** + * @var Connection + */ private $con; - private $tableName; - - public function __construct(Connection $con, $tableName = 'sessions', array $options = array()) - { - parent::__construct($options); - - $this->con = $con; - $this->tableName = $tableName; - } /** - * Starts the session. - */ - public function start() + * @var string + */ + private $tableName; + + /** + * + * @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) { - if (self::$sessionStarted) { - return; - } + $this->con = $con; + $this->tableName = $tableName; - // use this object as the session handler - session_set_save_handler( - array($this, 'sessionOpen'), - array($this, 'sessionClose'), - array($this, 'sessionRead'), - array($this, 'sessionWrite'), - array($this, 'sessionDestroy'), - array($this, 'sessionGC') - ); - - parent::start(); + parent::__construct($attributes, $flashes, $options); } /** @@ -102,7 +98,7 @@ class DbalSessionStorage extends NativeSessionStorage * * @throws \RuntimeException If any old sessions cannot be cleaned */ - public function sessionGC($lifetime) + public function sessionGc($lifetime) { try { $this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array( @@ -140,7 +136,7 @@ class DbalSessionStorage extends NativeSessionStorage return ''; } catch (\PDOException $e) { - throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e); + throw new \RuntimeException(sprintf('PDOException was thrown when trying to read the session data: %s', $e->getMessage()), 0, $e); } } @@ -181,7 +177,7 @@ class DbalSessionStorage extends NativeSessionStorage $this->createNewSession($id, $data); } } catch (\PDOException $e) { - throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e); + throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e); } return true; diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml index 18ed6f4d87..0cc6b74d30 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml @@ -10,7 +10,7 @@ framework: default_locale: en session: auto_start: true - storage_id: session.storage.filesystem + storage_id: session.storage.mock_file services: logger: { class: Symfony\Component\HttpKernel\Log\NullLogger } diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index a1b6d31a7c..9d6c6e766b 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -15,6 +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; /** * WebDebugToolbarListener injects the Web Debug Toolbar. @@ -70,9 +71,12 @@ class WebDebugToolbarListener } if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) { - if (null !== $session = $request->getSession()) { - // keep current flashes for one more request - $session->setFlashes($session->getFlashes()); + $session = $request->getSession(); + if ($session instanceof AutoExpireFlashBag) { + // keep current flashes for one more request if using AutoExpireFlashBag + foreach ($session->getFlashKeys() as $type) { + $session->setFlashes($session->getFlashes($type)); + } } $response->setContent($this->templating->render('WebProfilerBundle:Profiler:toolbar_redirect.html.twig', array('location' => $response->headers->get('Location')))); From 9dd4dbed6d4c9b804e9cb7db374b8612ad028230 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 24 Dec 2011 16:21:18 +0545 Subject: [PATCH 10/31] Documentation, changelogs and coding standards. --- CHANGELOG-2.1.md | 31 +++++ UPGRADE-2.1.md | 110 +++++++++++++++--- .../SessionStorage/AbstractSessionStorage.php | 6 +- .../SessionStorage/MemcacheSessionStorage.php | 2 +- .../MemcachedSessionStorage.php | 4 +- .../SessionStorage/MockFileSessionStorage.php | 4 +- .../NativeFileSessionStorage.php | 2 +- 7 files changed, 134 insertions(+), 25 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 35cbb4f96e..ae87cee725 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -40,6 +40,11 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * changed the default profiler storage to use the filesystem instead of SQLite * added support for placeholders in route defaults and requirements (replaced by the value set in the service container) * added Filesystem component as a dependency + * [BC BREAK] changed `session.xml` service name `session.storage.native` to `session.storage.native_file` + * added new session storage drivers to session.xml: `session.storage.native_memcache`, `session.storage.native_memcached`, + `session.storage.native_sqlite`, `session.storage.null`, `session.storage.memcache`, + and `session.storage.memcached`. Added `session.storage.mock_file` service for functional session testing. + * removed `session.storage.filesystem` service. ### MonologBundle @@ -224,6 +229,32 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * removed the ContentTypeMimeTypeGuesser class as it is deprecated and never used on PHP 5.3 * added ResponseHeaderBag::makeDisposition() (implements RFC 6266) * made mimetype to extension conversion configurable + * 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()`. + 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 `popFlashes()` but will expire regardless of + being retrieved or not, which retains th old behaviour. + * [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `hasFlash()`, + and `removeFlash()` and added new methods. Use `addFlashes()` to add new flash messages. + `getFlashes()` now returns and array of flash messages. + * `Session->clear()` now only clears session attributes as before it cleared flash messages and + attributes. `Session->clearAllFlashes()` clears flashes now. + * Added `AbstractSessionStorage` base class for session storage drivers. + * Added `SessionSaveHandler` interface which storage drivers should implement after inheriting from + `AbstractSessionStorage` when writing custom session save handlers. + * [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and `remove()`. Added + `getAttributes()`, `getFlashes()`. + * Moved attribute storage to `AttributeBagInterface`. + * Added `AttributeBag` to replicate attributes storage behaviour from 2.0.x (default). + * Added `NamespacedAttributeBag` for namespace session attributes. + * Session now implements `SessionInterface` making implementation customizable and portable. + * [BC BREAK] Removed `NativeSessionStorage` and replaced with `NativeFileSessionStorage`. + * Added session storage drivers for PHP native Memcache, Memcached and SQLite session save handlers. + * Added session storage drivers for custom Memcache, Memcached and Null session save handlers. + * Removed `FilesystemSessionStorage`, use `MockFileSessionStorage` for functional testing instead. ### HttpKernel diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index b94b511d79..0e7e631efe 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1,6 +1,8 @@ UPGRADE FROM 2.0 to 2.1 ======================= +### General + * assets_base_urls and base_urls merging strategy has changed Unlike most configuration blocks, successive values for @@ -11,6 +13,8 @@ UPGRADE FROM 2.0 to 2.1 and/or share a common base configuration (i.e. ``config.yml``), merging could yield a set of base URL's for multiple environments. +### [HttpFoundation] + * moved management of the locale from the Session class to the Request class Configuring the default locale: @@ -73,8 +77,8 @@ UPGRADE FROM 2.0 to 2.1 explicitely need to set the `Valid` constraint in your model if you want to validate associated objects. - If you don't want to set the `Valid` constraint, or if there is no reference - from the data of the parent form to the data of the child form, you can + If you don't want to set the `Valid` constraint, or if there is no reference + from the data of the parent form to the data of the child form, you can enable BC behaviour by setting the option "cascade_validation" to `true` on the parent form. @@ -229,22 +233,98 @@ UPGRADE FROM 2.0 to 2.1 return false; } } + the parent form. -* The options passed to `getParent` of the form types don't contain default - options anymore + Before: $session->getLocale() + After: $request->getLocale() - You should check with `isset` if options exist before checking their value. + * Flash Messages now returns and array based on type - Before: + Before (PHP): - public function getParent(array $options) - { - return 'single_text' === $options['widget'] ? 'text' : 'choice'; - } + hasFlash('notice')): ?> +
+ getFlash('notice') ?> +
+ - After: + After (PHP): + + popFlashes('notice') as $notice): ?> +
+ +
+ + + If You wanted to process all flash types you could also make use of the `popAllFlashes()` API: + + popAllFlashes() as $type => $flashes): ?> + +
+ +
+ + + +.. 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 `` + at the beginning of the PHP template. + + Before (Twig): + + {% if app.session.hasFlash('notice') %} +
+ {{ app.session.flash('notice') }} +
+ {% endif %} + + After (Twig): + + {% for flashMessage in app.session.popFlashes('notice') %} +
+ {{ flashMessage }} +
+ {% endforeach %} + + Again you can process all flash messages in one go with + + {% for type, flashMessages in app.session.popAllFlashes() %} + {% for flashMessage in flashMessages) %} +
+ {{ flashMessage }} +
+ {% endforeach %} + {% endforeach %} + +.. note:: + + You can access optionally use constants in Twig templates using `constant()` e.g. + `constant('Symfony\Component\HttpFoundation\FlashBag::NOTICE')`. + +* Session object + + The methods, `setFlash()`, `hasFlash()`, and `removeFlash()` have been removed from the `Session` + object. You may use `addFlash()` to add flashes. `getFlashes()`, now returns an array. Use + `popFlashes()` to get flashes for display, or `popAllFlashes()` to process all flashes in on go. + +* Session storage drivers + + Session storage drivers should inherit from + `Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage` + and no longer should implement `read()`, `write()`, `remove()` which were removed from the + `SessionStorageInterface`. + + Any session storage drive that wants to use custom save handlers should + implement `Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface` + +### [FrameworkBundle] + + The service `session.storage.native` is now called `session.storage.native_file` + + The service `session.storage.filesystem` is now called `session.storage.mock_file` + and is used for functional unit testing. You will need to update any references + in functional tests. - public function getParent(array $options) - { - return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice'; - } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php index db0ecca327..cf1df726c7 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php @@ -92,8 +92,8 @@ abstract class AbstractSessionStorage implements SessionStorageInterface */ public function __construct(AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, array $options = array()) { - $this->attributeBag = $attributes ? $attributes : new AttributeBag(); - $this->flashBag = $flashes ? $flashes : new FlashBag(); + $this->attributeBag = $attributes ?: new AttributeBag(); + $this->flashBag = $flashes ?: new FlashBag(); $this->setOptions($options); $this->registerSaveHandlers(); $this->registerShutdownFunction(); @@ -224,7 +224,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface // Unless session.cache_limiter has been set explicitly, disable it // because this is managed by HeaderBag directly (if used). - if (!array_key_exists('cache_limiter', $this->options)) { + if (!isset($this->options['cache_limiter'])) { $this->options['cache_limiter'] = 0; } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php index e3d8d4c42b..6176cb8560 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php @@ -111,7 +111,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa */ public function sessionRead($sessionId) { - $result = $this->memcache->get($this->prefix.$sessionId); + return $this->memcache->get($this->prefix.$sessionId) ?: ''; return ($result) ? $result : ''; } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php index f91c7da6d5..85bb358e9e 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php @@ -96,9 +96,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS */ public function sessionRead($sessionId) { - $result = $this->memcached->get($this->prefix.$sessionId); - - return $result ? $result : ''; + return $this->memcached->get($this->prefix.$sessionId) ?: ''; } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php index f5f0999e83..a785ce9f6f 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php @@ -47,7 +47,7 @@ class MockFileSessionStorage extends ArraySessionStorage */ public function __construct($savePath = null, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { - if (is_null($savePath)) { + if (null === $savePath) { $savePath = sys_get_temp_dir(); } @@ -133,7 +133,7 @@ class MockFileSessionStorage extends ArraySessionStorage */ public function getFilePath() { - return $this->savePath . '/' . $this->sessionId . '.sess'; + return $this->savePath.'/'.$this->sessionId.'.sess'; } private function read() diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php index 9297e4b718..8797e4ab27 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php @@ -40,7 +40,7 @@ class NativeFileSessionStorage extends AbstractSessionStorage */ public function __construct($savePath = null, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { - if (is_null($savePath)) { + if (null === $savePath) { $savePath = sys_get_temp_dir(); } From f98f9ae8ffed937f5f767947f77d7fefe035ac53 Mon Sep 17 00:00:00 2001 From: Drak Date: Mon, 2 Jan 2012 21:17:41 +0545 Subject: [PATCH 11/31] [HttpFoundation] Refactor for DRY code. Rename ArraySessionStorage to make it clear the session is a mock for testing purposes only. Has BC class for ArraySessionStorage Added sanity check when starting the session. Fixed typos and incorrect php extension test method session_module_name() also sets session.save_handler, so must use extension_loaded() to check if module exist or not. Respect autostart settings. --- UPGRADE-2.1.md | 4 +- .../Templating/Helper/SessionHelperTest.php | 4 +- .../Tests/Templating/PhpEngineTest.php | 4 +- .../TwigBundle/Tests/TwigEngineTest.php | 4 +- .../HttpFoundation/AttributeBagInterface.php | 15 +- .../HttpFoundation/FlashBagInterface.php | 18 +-- .../HttpFoundation/SessionBagInterface.php | 34 +++++ .../SessionStorage/AbstractSessionStorage.php | 39 ++++-- .../SessionStorage/ArraySessionStorage.php | 109 +-------------- .../MockArraySessionStorage.php | 129 ++++++++++++++++++ .../SessionStorage/MockFileSessionStorage.php | 11 +- .../NativeFileSessionStorage.php | 2 +- .../NativeMemcacheSessionStorage.php | 4 +- .../NativeMemcachedSessionStorage.php | 4 +- .../NativeSqliteSessionStorage.php | 4 +- .../Component/HttpFoundation/RequestTest.php | 6 +- ...st.php => MockArraySessionStorageTest.php} | 10 +- .../Component/HttpFoundation/SessionTest.php | 4 +- .../Http/Firewall/ContextListenerTest.php | 4 +- 19 files changed, 232 insertions(+), 177 deletions(-) create mode 100644 src/Symfony/Component/HttpFoundation/SessionBagInterface.php create mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php rename tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/{ArraySessionStorageTest.php => MockArraySessionStorageTest.php} (86%) diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 0e7e631efe..f1557e54be 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -301,7 +301,7 @@ UPGRADE FROM 2.0 to 2.1 .. note:: - You can access optionally use constants in Twig templates using `constant()` e.g. + You can optionally use constants in Twig templates using `constant()` e.g. `constant('Symfony\Component\HttpFoundation\FlashBag::NOTICE')`. * Session object @@ -317,7 +317,7 @@ UPGRADE FROM 2.0 to 2.1 and no longer should implement `read()`, `write()`, `remove()` which were removed from the `SessionStorageInterface`. - Any session storage drive that wants to use custom save handlers should + Any session storage driver that wants to use custom save handlers should implement `Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface` ### [FrameworkBundle] diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index e7e7b4a764..b20dc33cad 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -13,7 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper; use Symfony\Component\HttpFoundation\FlashBag; use Symfony\Component\HttpFoundation\AttributeBag; @@ -26,7 +26,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $this->request = new Request(); - $session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag())); + $session = new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag())); $session->set('foobar', 'bar'); $session->addFlash('bar', FlashBag::NOTICE); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index 4da48ea6d8..7950df497d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -15,7 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; @@ -66,7 +66,7 @@ class PhpEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag())); + $session = new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag())); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index 77b79dd545..4bde094136 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -15,7 +15,7 @@ use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; @@ -71,7 +71,7 @@ class TwigEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new ArraySessionStorage()); + $session = new Session(new MockArraySessionStorage()); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php b/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php index 5938a55573..e835533a37 100644 --- a/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php @@ -19,19 +19,6 @@ use Symfony\Component\HttpFoundation\SessionStorage\AttributeInterface; * * @author Drak */ -interface AttributeBagInterface extends AttributeInterface +interface AttributeBagInterface extends SessionBagInterface, AttributeInterface { - /** - * Initializes the AttributeBag - * - * @param array $attributes - */ - function initialize(array &$attributes); - - /** - * Gets the storage key for this bag. - * - * @return string - */ - function getStorageKey(); } diff --git a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php index 89ea8e197b..7d3eff4595 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php @@ -16,20 +16,13 @@ namespace Symfony\Component\HttpFoundation; * * @author Drak */ -interface FlashBagInterface +interface FlashBagInterface extends SessionBagInterface { const INFO = 'info'; const NOTICE = 'notice'; const WARNING = 'warning'; const ERROR = 'error'; - /** - * Initializes the FlashBag. - * - * @param array &$flashes - */ - function initialize(array &$flashes); - /** * Adds a flash to the stack for a given type. * @@ -57,7 +50,7 @@ interface FlashBagInterface function pop($type); /** - * Pops all flashes from the stacl and clears flashes. + * Pops all flashes from the stack and clears flashes. * * @param string $type * @@ -111,11 +104,4 @@ interface FlashBagInterface * @return array Empty array or indexed arrays or array if none. */ function clearAll(); - - /** - * Gets the storage key for this bag. - * - * @return string - */ - function getStorageKey(); } diff --git a/src/Symfony/Component/HttpFoundation/SessionBagInterface.php b/src/Symfony/Component/HttpFoundation/SessionBagInterface.php new file mode 100644 index 0000000000..2fd53621dc --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionBagInterface.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * Session Bag store. + * + * @author Drak + */ +interface SessionBagInterface +{ + /** + * Initializes the Bag + * + * @param array $array + */ + function initialize(array &$array); + + /** + * Gets the storage key for this bag. + * + * @return string + */ + function getStorageKey(); +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php index cf1df726c7..bef9a28ca6 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\FlashBag; use Symfony\Component\HttpFoundation\FlashBagInterface; use Symfony\Component\HttpFoundation\AttributeBag; use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\SessionBagInterface; /** * This provides a base class for session attribute storage. @@ -36,7 +37,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface /** * @var array */ - protected $options; + protected $options = array(); /** * @var boolean @@ -104,7 +105,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface */ public function getFlashes() { - if (!$this->started) { + if ($this->options['auto_start'] && !$this->started) { $this->start(); } @@ -116,7 +117,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface */ public function getAttributes() { - if (!$this->started) { + if ($this->options['auto_start'] && !$this->started) { $this->start(); } @@ -132,6 +133,10 @@ abstract class AbstractSessionStorage implements SessionStorageInterface return true; } + if ($this->options['use_cookies'] && headers_sent()) { + throw new \RuntimeException('Failed to start the session because header have already been sent.'); + } + // start the session if (!session_start()) { throw new \RuntimeException('Failed to start the session'); @@ -228,6 +233,14 @@ abstract class AbstractSessionStorage implements SessionStorageInterface $this->options['cache_limiter'] = 0; } + if (!isset($this->options['auto_start'])) { + $this->options['auto_start'] = 0; + } + + if (!isset($this->options['use_cookies'])) { + $this->options['use_cookies'] = 1; + } + foreach ($this->options as $key => $value) { if (in_array($key, array( 'auto_start', 'cookie_domain', 'cookie_httponly', @@ -315,12 +328,20 @@ abstract class AbstractSessionStorage implements SessionStorageInterface */ protected function loadSession() { - $key = $this->attributeBag->getStorageKey(); - $_SESSION[$key] = isset($_SESSION[$key]) ? $_SESSION[$key] : array(); - $this->attributeBag->initialize($_SESSION[$key]); + $this->link($this->attributeBag, $_SESSION); + $this->link($this->flashBag, $_SESSION); + } - $key = $this->flashBag->getStorageKey(); - $_SESSION[$key] = isset($_SESSION[$key]) ? $_SESSION[$key] : array(); - $this->flashBag->initialize($_SESSION[$key]); + /** + * 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]); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php index 3d2f9741b0..affea24142 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php @@ -11,12 +11,14 @@ namespace Symfony\Component\HttpFoundation\SessionStorage; -use Symfony\Component\HttpFoundation\AttributeBagInterface; -use Symfony\Component\HttpFoundation\FlashBagInterface; - /** * ArraySessionStorage mocks the session for unit tests. * + * BC Class for MockArraySessionStorage + * + * @deprecated since 2.1.0 + * @see MockArraySessionStorage + * * No PHP session is actually started since a session can be initialized * and shutdown only once per PHP execution cycle. * @@ -24,106 +26,7 @@ use Symfony\Component\HttpFoundation\FlashBagInterface; * * @author Fabien Potencier * @author Bulat Shakirzyanov - * @author Drak */ -class ArraySessionStorage extends AbstractSessionStorage +class ArraySessionStorage extends MockArraySessionStorage { - /** - * @var string - */ - protected $sessionId; - - /** - * @var array - */ - private $attributes = array(); - - /** - * @var array - */ - private $flashes = array(); - - /** - * Injects array of attributes to simulate retrieval of existing session. - * - * @param array $array - */ - public function setAttributes(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; - } - - /** - * {@inheritdoc} - */ - public function start() - { - if ($this->started && !$this->closed) { - return true; - } - - $this->started = true; - $this->attributeBag->initialize($this->attributes); - $this->flashBag->initialize($this->flashes); - $this->sessionId = $this->generateSessionId(); - session_id($this->sessionId); - - return true; - } - - /** - * {@inheritdoc} - */ - public function regenerate($destroy = false) - { - if (!$this->started) { - $this->start(); - } - - $this->sessionId = $this->generateSessionId(); - session_id($this->sessionId); - - return true; - } - - /** - * {@inheritdoc} - */ - public function getId() - { - if (!$this->started) { - return ''; - } - - return $this->sessionId; - } - - /** - * {@inheritdoc} - */ - public function save() - { - // nothing to do since we don't persist the session data - $this->closed = false; - } - - /** - * Generates a session ID. - * - * @return string - */ - protected function generateSessionId() - { - return sha1(uniqid(mt_rand(), true)); - } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php new file mode 100644 index 0000000000..45734dec31 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php @@ -0,0 +1,129 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * MockArraySessionStorage mocks the session for unit tests. + * + * No PHP session is actually started since a session can be initialized + * and shutdown only once per PHP execution cycle. + * + * When doing functional testing, you should use MockFileSessionStorage instead. + * + * @author Fabien Potencier + * @author Bulat Shakirzyanov + * @author Drak + */ +class MockArraySessionStorage extends AbstractSessionStorage +{ + /** + * @var string + */ + protected $sessionId; + + /** + * @var array + */ + private $attributes = array(); + + /** + * @var array + */ + private $flashes = array(); + + /** + * Injects array of attributes to simulate retrieval of existing session. + * + * @param array $array + */ + public function setAttributes(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; + } + + /** + * {@inheritdoc} + */ + public function start() + { + if ($this->started && !$this->closed) { + return true; + } + + $this->started = true; + $this->attributeBag->initialize($this->attributes); + $this->flashBag->initialize($this->flashes); + $this->sessionId = $this->generateSessionId(); + session_id($this->sessionId); + + return true; + } + + /** + * {@inheritdoc} + */ + public function regenerate($destroy = false) + { + if ($this->options['auto_start'] && !$this->started) { + $this->start(); + } + + $this->sessionId = $this->generateSessionId(); + session_id($this->sessionId); + + return true; + } + + /** + * {@inheritdoc} + */ + public function getId() + { + if (!$this->started) { + return ''; + } + + return $this->sessionId; + } + + /** + * {@inheritdoc} + */ + public function save() + { + // nothing to do since we don't persist the session data + $this->closed = false; + } + + /** + * Generates a session ID. + * + * @return string + */ + protected function generateSessionId() + { + return sha1(uniqid(mt_rand(), true)); + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php index a785ce9f6f..7bc7b141d0 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php @@ -23,7 +23,7 @@ use Symfony\Component\HttpFoundation\FlashBagInterface; * * @author Drak */ -class MockFileSessionStorage extends ArraySessionStorage +class MockFileSessionStorage extends MockArraySessionStorage { /** * @var array @@ -141,13 +141,8 @@ class MockFileSessionStorage extends ArraySessionStorage $filePath = $this->getFilePath(); $this->sessionData = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array(); - $key = $this->attributeBag->getStorageKey(); - $this->sessionData[$key] = isset($this->sessionData[$key]) ? $this->sessionData[$key] : array(); - $this->attributeBag->initialize($this->sessionData[$key]); - - $key = $this->flashBag->getStorageKey(); - $this->sessionData[$key] = isset($this->sessionData[$key]) ? $this->sessionData[$key] : array(); - $this->flashBag->initialize($this->sessionData[$key]); + $this->link($this->attributeBag, $this->sessionData); + $this->link($this->flashBag, $this->sessionData); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php index 8797e4ab27..f77763143f 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php @@ -58,7 +58,7 @@ class NativeFileSessionStorage extends AbstractSessionStorage */ protected function registerSaveHandlers() { - ini_set('session.save_handlers', 'files'); + ini_set('session.save_handler', 'files'); ini_set('session.save_path', $this->savePath); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php index 2467fd6187..a869eeaacc 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php @@ -40,7 +40,7 @@ class NativeMemcacheSessionStorage extends AbstractSessionStorage */ public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { - if (!session_module_name('memcache')) { + if (!extension_loaded('memcache')) { throw new \RuntimeException('PHP does not have "memcache" session module registered'); } @@ -53,7 +53,7 @@ class NativeMemcacheSessionStorage extends AbstractSessionStorage */ protected function registerSaveHandlers() { - ini_set('session.save_handlers', 'memcache'); + ini_set('session.save_handler', 'memcache'); ini_set('session.save_path', $this->savePath); } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php index 71af999cef..28635d6caf 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php @@ -40,7 +40,7 @@ class NativeMemcachedSessionStorage extends AbstractSessionStorage */ public function __construct($savePath = '127.0.0.1:11211', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { - if (!session_module_name('memcached')) { + if (!extension_loaded('memcached')) { throw new \RuntimeException('PHP does not have "memcached" session module registered'); } @@ -53,7 +53,7 @@ class NativeMemcachedSessionStorage extends AbstractSessionStorage */ protected function registerSaveHandlers() { - ini_set('session.save_handlers', 'memcached'); + ini_set('session.save_handler', 'memcached'); ini_set('session.save_path', $this->savePath); } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php index 244c1624b4..bdcaeeb6b4 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php @@ -40,7 +40,7 @@ class NativeSqliteSessionStorage extends AbstractSessionStorage */ public function __construct($dbPath, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { - if (!session_module_name('sqlite')) { + if (!extension_loaded('sqlite')) { throw new \RuntimeException('PHP does not have "sqlite" session module registered'); } @@ -53,7 +53,7 @@ class NativeSqliteSessionStorage extends AbstractSessionStorage */ protected function registerSaveHandlers() { - ini_set('session.save_handlers', 'sqlite'); + ini_set('session.save_handler', 'sqlite'); ini_set('session.save_path', $this->dbPath); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index 9f144724f8..f392b7e666 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -12,7 +12,7 @@ namespace Symfony\Tests\Component\HttpFoundation; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\FlashBag; @@ -848,7 +848,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase $request = new Request; $this->assertFalse($request->hasSession()); - $request->setSession(new Session(new ArraySessionStorage(new AttributeBag(), new FlashBag()))); + $request->setSession(new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag()))); $this->assertTrue($request->hasSession()); } @@ -859,7 +859,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 ArraySessionStorage(new AttributeBag(), new FlashBag()))); + $request->setSession(new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag()))); $this->assertTrue($request->hasPreviousSession()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php similarity index 86% rename from tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php index 924fe72594..441d7b91c3 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/ArraySessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php @@ -2,20 +2,20 @@ namespace Symfony\Tests\Component\HttpFoundation\SessionStorage; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\AttributeBag; use Symfony\Component\HttpFoundation\FlashBag; /** - * Test class for ArraySessionStorage. + * Test class for MockArraySessionStorage. * * @author Drak */ -class ArraySessionStorageTest extends \PHPUnit_Framework_TestCase +class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase { /** - * @var ArraySessionStorage + * @var MockArraySessionStorage */ private $storage; @@ -33,7 +33,7 @@ class ArraySessionStorageTest extends \PHPUnit_Framework_TestCase { $this->attributes = array('foo' => 'bar'); $this->flashes = array('notice' => 'hello'); - $this->storage = new ArraySessionStorage(new AttributeBag(), new FlashBag()); + $this->storage = new MockArraySessionStorage(new AttributeBag(), new FlashBag()); $this->storage->setFlashes($this->flashes); $this->storage->setAttributes($this->attributes); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index 892672e9e0..f4e3ee00d3 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\FlashBag; use Symfony\Component\HttpFoundation\FlashBagInterface; use Symfony\Component\HttpFoundation\AttributeBag; use Symfony\Component\HttpFoundation\AttributeBagInterface; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; /** * SessionTest @@ -39,7 +39,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->storage = new ArraySessionStorage(new AttributeBag(), new FlashBag()); + $this->storage = new MockArraySessionStorage(new AttributeBag(), new FlashBag()); $this->session = new Session($this->storage); } diff --git a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php index dfbcd79e7f..e839e8fd16 100644 --- a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php +++ b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php @@ -5,7 +5,7 @@ namespace Symfony\Test\Component\Security\Http\Firewall; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; @@ -63,7 +63,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase protected function runSessionOnKernelResponse($newToken, $original = null) { - $session = new Session(new ArraySessionStorage()); + $session = new Session(new MockArraySessionStorage()); if ($original !== null) { $session->set('_security_session', $original); From 398acc9e9fea915e2a481a3a8f4b209f8ff9bc42 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 2 Feb 2012 16:52:02 +0545 Subject: [PATCH 12/31] [HttpFoundation] Reworked flashes to maintain same behaviour as in Symfony 2.0 --- CHANGELOG-2.1.md | 10 +- UPGRADE-2.1.md | 49 +++-- .../Templating/Helper/SessionHelper.php | 12 +- .../Controller/SessionController.php | 6 +- .../Templating/Helper/SessionHelperTest.php | 10 +- .../Tests/Templating/PhpEngineTest.php | 4 +- .../HttpFoundation/AutoExpireFlashBag.php | 86 ++++----- .../Component/HttpFoundation/FlashBag.php | 75 +++----- .../HttpFoundation/FlashBagInterface.php | 59 ++---- .../Component/HttpFoundation/Session.php | 106 +---------- .../HttpFoundation/SessionInterface.php | 79 +------- .../SessionStorage/AbstractSessionStorage.php | 14 +- .../SessionStorage/MemcacheSessionStorage.php | 14 +- .../MemcachedSessionStorage.php | 12 +- .../SessionStorage/NullSessionStorage.php | 12 +- .../SessionStorage/PdoSessionStorage.php | 12 +- .../SessionSaveHandlerInterface.php | 12 +- .../HttpFoundation/AutoExpireFlashBagTest.php | 91 ++++----- .../Component/HttpFoundation/FlashBagTest.php | 94 +++++----- .../AbstractSessionStorageTest.php | 23 ++- .../MockFileSessionStorageTest.php | 10 +- .../SessionStorage/NullSessionStorageTest.php | 2 - .../Component/HttpFoundation/SessionTest.php | 175 +----------------- 23 files changed, 269 insertions(+), 698 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index ae87cee725..2a1d994881 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -235,13 +235,11 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * Added `FlashBag` (default). Flashes expire when retrieved by `popFlashes()`. 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 `popFlashes()` but will expire regardless of - being retrieved or not, which retains th old behaviour. - * [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `hasFlash()`, - and `removeFlash()` and added new methods. Use `addFlashes()` to add new flash messages. - `getFlashes()` now returns and array of flash messages. + after one page page load. Messages must be retrived by `pop()` or `popAll()`. + * [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()` + `getFlash()`, `hasFlash()`, andd `removeFlash()`. `getFlashes() returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and - attributes. `Session->clearAllFlashes()` clears flashes now. + attributes. `Session->getFlashes()->popAll()` clears flashes now. * Added `AbstractSessionStorage` base class for session storage drivers. * Added `SessionSaveHandler` interface which storage drivers should implement after inheriting from `AbstractSessionStorage` when writing custom session save handlers. diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index f1557e54be..220846aa6b 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -32,17 +32,17 @@ UPGRADE FROM 2.0 to 2.1 Retrieving the locale from a Twig template: - Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}` + Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}` After: `{{ app.request.locale }}` Retrieving the locale from a PHP template: - Before: `$view['session']->getLocale()` + Before: `$view['session']->getLocale()` After: `$view['request']->getLocale()` Retrieving the locale from PHP code: - Before: `$session->getLocale()` + Before: `$session->getLocale()` After: `$request->getLocale()` * Method `equals` of `Symfony\Component\Security\Core\User\UserInterface` has @@ -250,20 +250,18 @@ UPGRADE FROM 2.0 to 2.1 After (PHP): - popFlashes('notice') as $notice): ?> -
- -
- + getFlashes()->has('notice')): ?> +
+ getFlashes()->pop('notice') ?> +
+ If You wanted to process all flash types you could also make use of the `popAllFlashes()` API: - popAllFlashes() as $type => $flashes): ?> - -
- -
- + getFlashes()->all() as $type => $flash): ?> +
+ +
.. note:: @@ -283,20 +281,18 @@ UPGRADE FROM 2.0 to 2.1 After (Twig): - {% for flashMessage in app.session.popFlashes('notice') %} + {% if app.session.getFlashes.has('notice') %}
- {{ flashMessage }} + {{ app.session.getFlashes.pop('notice') }}
- {% endforeach %} + {% endif %} Again you can process all flash messages in one go with - {% for type, flashMessages in app.session.popAllFlashes() %} - {% for flashMessage in flashMessages) %} -
- {{ flashMessage }} -
- {% endforeach %} + {% for type, flashMessage in app.session.getFlashes.popAll() %} +
+ {{ flashMessage }} +
{% endforeach %} .. note:: @@ -306,9 +302,10 @@ UPGRADE FROM 2.0 to 2.1 * Session object - The methods, `setFlash()`, `hasFlash()`, and `removeFlash()` have been removed from the `Session` - object. You may use `addFlash()` to add flashes. `getFlashes()`, now returns an array. Use - `popFlashes()` to get flashes for display, or `popAllFlashes()` to process all flashes in on go. + The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()` + have been removed from the `Session` object. `getFlashes()` now returns a `FlashBagInterface`. + Flashes should be popped off the stack using `getFlashes()->pop()` or `getFlashes()->popAll()` + to get all flashes in one go. * Session storage drivers diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index a60b14997f..c861e23e01 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -47,19 +47,19 @@ class SessionHelper extends Helper return $this->session->get($name, $default); } - public function getFlashes($type) + public function getFlash($type) { - return $this->session->getFlashes($type); + return $this->session->getFlashes()->get($type); } - public function getAllFlashes() + public function getFlashes() { - return $this->session->getAllFlashes(); + return $this->session->getFlashes()->all(); } - public function hasFlashes($type) + public function hasFlash($type) { - return $this->session->hasFlashes($type); + return $this->session->getFlashes()->has($type); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php index f1e30fd001..0a1aa0c66c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php @@ -51,7 +51,7 @@ class SessionController extends ContainerAware { $request = $this->container->get('request'); $session = $request->getSession(); - $session->addFlash($message, 'notice'); + $session->getFlashes()->set('notice', $message); return new RedirectResponse($this->container->get('router')->generate('session_showflash')); } @@ -61,8 +61,8 @@ class SessionController extends ContainerAware $request = $this->container->get('request'); $session = $request->getSession(); - if ($session->hasFlashes('notice')) { - list($output) = $session->popFlashes('notice'); + if ($session->getFlashes()->has('notice')) { + $output = $session->getFlashes()->pop('notice'); } else { $output = 'No flash was set.'; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index b20dc33cad..b18526f836 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -26,9 +26,9 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $this->request = new Request(); - $session = new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag())); + $session = new Session(new MockArraySessionStorage()); $session->set('foobar', 'bar'); - $session->addFlash('bar', FlashBag::NOTICE); + $session->getFlashes()->set(FlashBag::NOTICE, 'bar'); $this->request->setSession($session); } @@ -42,11 +42,11 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $helper = new SessionHelper($this->request); - $this->assertTrue($helper->hasFlashes(FlashBag::NOTICE)); + $this->assertTrue($helper->hasFlash(FlashBag::NOTICE)); - $this->assertEquals(array('bar'), $helper->getFlashes(FlashBag::NOTICE)); + $this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE)); - $this->assertEquals(array(FlashBag::NOTICE => array('bar')), $helper->getAllFlashes()); + $this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes()); } public function testGet() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index 7950df497d..463df19ffc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -19,8 +19,6 @@ use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; -use Symfony\Component\HttpFoundation\FlashBag; -use Symfony\Component\HttpFoundation\AttributeBag; class PhpEngineTest extends TestCase { @@ -66,7 +64,7 @@ class PhpEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new MockArraySessionStorage(new AttributeBag(), new FlashBag())); + $session = new Session(new MockArraySessionStorage()); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php index 5c435a2821..2767e4817a 100644 --- a/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php +++ b/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php @@ -57,36 +57,46 @@ class AutoExpireFlashBag implements FlashBagInterface $this->flashes['new'] = array(); } - /** - * {@inheritdoc} - */ - public function add($message, $type = self::NOTICE) - { - $this->flashes['new'][$type][] = $message; - } - /** * {@inheritdoc} */ public function get($type) { if (!$this->has($type)) { - return array(); + throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); } return $this->flashes['display'][$type]; } + /** + * {@inheritdoc} + */ + public function all() + { + return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array(); + } + /** * {@inheritdoc} */ public function pop($type) { if (!$this->has($type)) { - return array(); + throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); } - return $this->clear($type); + $return = null; + if (isset($this->flashes['new'][$type])) { + unset($this->flashes['new'][$type]); + } + + if (isset($this->flashes['display'][$type])) { + $return = $this->flashes['display'][$type]; + unset($this->flashes['display'][$type]); + } + + return $return; } /** @@ -94,15 +104,26 @@ class AutoExpireFlashBag implements FlashBagInterface */ public function popAll() { - return $this->clearAll(); + $return = $this->flashes['display']; + $this->flashes = array('new' => array(), 'display' => array()); + + return $return; } /** * {@inheritdoc} */ - public function set($type, array $array) + public function setAll(array $messages) { - $this->flashes['new'][$type] = $array; + $this->flashes['new'] = $messages; + } + + /** + * {@inheritdoc} + */ + public function set($type, $message) + { + $this->flashes['new'][$type] = $message; } /** @@ -121,43 +142,6 @@ class AutoExpireFlashBag implements FlashBagInterface return array_keys($this->flashes['display']); } - /** - * {@inheritdoc} - */ - public function all() - { - return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array(); - } - - /** - * {@inheritdoc} - */ - public function clear($type) - { - $return = array(); - if (isset($this->flashes['new'][$type])) { - unset($this->flashes['new'][$type]); - } - - if (isset($this->flashes['display'][$type])) { - $return = $this->flashes['display'][$type]; - unset($this->flashes['display'][$type]); - } - - return $return; - } - - /** - * {@inheritdoc} - */ - public function clearAll() - { - $return = $this->flashes['display']; - $this->flashes = array('new' => array(), 'display' => array()); - - return $return; - } - /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/HttpFoundation/FlashBag.php b/src/Symfony/Component/HttpFoundation/FlashBag.php index 89d7dfffc2..31ccce4447 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/FlashBag.php @@ -50,36 +50,47 @@ class FlashBag implements FlashBagInterface $this->flashes = &$flashes; } - /** - * {@inheritdoc} - */ - public function add($message, $type = self::NOTICE) - { - $this->flashes[$type][] = $message; - } - /** * {@inheritdoc} */ public function get($type) { if (!$this->has($type)) { - return array(); + throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); } return $this->flashes[$type]; } + /** + * {@inheritdoc} + */ + public function set($type, $message) + { + $this->flashes[$type] = $message; + } + + /** + * {@inheritdoc} + */ + public function all() + { + return $this->flashes; + } + /** * {@inheritdoc} */ public function pop($type) { if (!$this->has($type)) { - return array(); + throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); } - return $this->clear($type); + $return = $this->get($type); + unset($this->flashes[$type]); + + return $return; } /** @@ -87,15 +98,18 @@ class FlashBag implements FlashBagInterface */ public function popAll() { - return $this->clearAll(); + $return = $this->all(); + $this->flashes = array(); + + return $return; } /** * {@inheritdoc} */ - public function set($type, array $array) + public function setAll(array $messages) { - $this->flashes[$type] = $array; + $this->flashes = $messages; } /** @@ -114,39 +128,6 @@ class FlashBag implements FlashBagInterface return array_keys($this->flashes); } - /** - * {@inheritdoc} - */ - public function all() - { - return $this->flashes; - } - - /** - * {@inheritdoc} - */ - public function clear($type) - { - $return = array(); - if (isset($this->flashes[$type])) { - $return = $this->flashes[$type]; - unset($this->flashes[$type]); - } - - return $return; - } - - /** - * {@inheritdoc} - */ - public function clearAll() - { - $return = $this->flashes; - $this->flashes = array(); - - return $return; - } - /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php index 7d3eff4595..c8dd96f14f 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php @@ -24,47 +24,49 @@ interface FlashBagInterface extends SessionBagInterface const ERROR = 'error'; /** - * Adds a flash to the stack for a given type. + * Registers a message for a given type. * - * @param string $message * @param string $type + * @param string $message */ - function add($message, $type = self::NOTICE); + function set($type, $message); /** - * Gets flash messages for a given type. + * Gets flash message for a given type. * - * @param string $type Message category type. + * @param string $type Message category type. * - * @return array + * @return string */ function get($type); /** - * Pops and clears flashes from the stack. + * Gets all flash messages. + * + * @return array + */ + function all(); + + /** + * Pops and clears flash from the stack. * * @param string $type * - * @return array + * @return string */ function pop($type); /** - * Pops all flashes from the stack and clears flashes. + * Pops and clears flashes from the stack. * - * @param string $type - * - * @return array Empty array, or indexed array of arrays. + * @return array */ function popAll(); /** - * Sets an array of flash messages for a given type. - * - * @param string $type - * @param array $array + * Sets all flash messages. */ - function set($type, array $array); + function setAll(array $messages); /** * Has flash messages for a given type? @@ -81,27 +83,4 @@ interface FlashBagInterface extends SessionBagInterface * @return array */ function keys(); - - /** - * Gets all flash messages. - * - * @return array - */ - function all(); - - /** - * Clears flash messages for a given type. - * - * @param string $type - * - * @return array Returns an array of what was just cleared. - */ - function clear($type); - - /** - * Clears all flash messages. - * - * @return array Empty array or indexed arrays or array if none. - */ - function clearAll(); } diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index e0961c411f..b6d2f142c9 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -218,113 +218,13 @@ class Session implements SessionInterface $this->storage = $storage; } - /** - * Adds a flash to the stack for a given type. - * - * @param string $message - * @param string $type - */ - public function addFlash($message, $type = FlashBagInterface::NOTICE) - { - $this->storage->getFlashes()->add($message, $type); - } - - /** - * Gets flash messages for a given type. - * - * @param string $type Message category type. - * - * @return array - */ - public function getFlashes($type = FlashBagInterface::NOTICE) - { - return $this->storage->getFlashes()->get($type); - } - - /** - * Pops flash messages off th stack for a given type. - * - * @param string $type Message category type. - * - * @return array - */ - public function popFlashes($type = FlashBagInterface::NOTICE) - { - return $this->storage->getFlashes()->pop($type); - } - - /** - * Pop all flash messages from the stack. - * - * @return array Empty array or indexed array of arrays. - */ - public function popAllFlashes() - { - return $this->storage->getFlashes()->popAll(); - } - - /** - * Sets an array of flash messages for a given type. - * - * @param string $type - * @param array $array - */ - public function setFlashes($type, array $array) - { - $this->storage->getFlashes()->set($type, $array); - } - - /** - * Has flash messages for a given type? - * - * @param string $type - * - * @return boolean - */ - public function hasFlashes($type) - { - return $this->storage->getFlashes()->has($type); - } - - /** - * Returns a list of all defined types. - * - * @return array - */ - public function getFlashKeys() - { - return $this->storage->getFlashes()->keys(); - } - /** * Gets all flash messages. * - * @return array + * @return FlashBagInterface */ - public function getAllFlashes() + public function getFlashes() { - return $this->storage->getFlashes()->all(); - } - - /** - * Clears flash messages for a given type. - * - * @param string $type - * - * @return array Returns an array of what was just cleared. - */ - public function clearFlashes($type) - { - return $this->storage->getFlashes()->clear($type); - } - - /** - * Clears all flash messages. - * - * @return array Empty array or indexed arrays or array if none. - */ - public function clearAllFlashes() - { - return $this->storage->getFlashes()->clearAll(); + return $this->storage->getFlashes(); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionInterface.php b/src/Symfony/Component/HttpFoundation/SessionInterface.php index acc53635fc..9cbadbe2c0 100644 --- a/src/Symfony/Component/HttpFoundation/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionInterface.php @@ -57,82 +57,9 @@ interface SessionInterface extends AttributeInterface, \Serializable function save(); /** - * Adds a flash to the stack for a given type. + * Gets the flashbag interface. * - * @param string $message - * @param string $type + * @return FlashBagInterface */ - function addFlash($message, $type = FlashBagInterface::NOTICE); - - /** - * Gets flash messages for a given type. - * - * @param string $type Message category type. - * - * @return array - */ - function getFlashes($type = FlashBagInterface::NOTICE); - - /** - * Pops flash messages off th stack for a given type. - * - * @param string $type Message category type. - * - * @return array - */ - function popFlashes($type = FlashBagInterface::NOTICE); - - /** - * Pop all flash messages from the stack. - * - * @return array Empty array or indexed array of arrays. - */ - function popAllFlashes(); - - /** - * Sets an array of flash messages for a given type. - * - * @param string $type - * @param array $array - */ - function setFlashes($type, array $array); - - /** - * Has flash messages for a given type? - * - * @param string $type - * - * @return boolean - */ - function hasFlashes($type); - - /** - * Returns a list of all defined types. - * - * @return array - */ - function getFlashKeys(); - - /** - * Gets all flash messages. - * - * @return array - */ - function getAllFlashes(); - - /** - * Clears flash messages for a given type. - * - * @param string $type - * - * @return array Returns an array of what was just cleared. - */ - function clearFlashes($type); - - /** - * Clears all flash messages. - * - * @return array Array of arrays or array if none. - */ - function clearAllFlashes(); + function getFlashes(); } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php index bef9a28ca6..7301f42110 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php @@ -195,7 +195,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface { // clear out the bags $this->attributeBag->clear(); - $this->flashBag->clearAll(); + $this->flashBag->popAll(); // clear out the session $_SESSION = array(); @@ -297,12 +297,12 @@ abstract class AbstractSessionStorage implements SessionStorageInterface // so long as ini_set() is called before the session is started. if ($this instanceof SessionSaveHandlerInterface) { session_set_save_handler( - array($this, 'sessionOpen'), - array($this, 'sessionClose'), - array($this, 'sessionRead'), - array($this, 'sessionWrite'), - array($this, 'sessionDestroy'), - array($this, 'sessionGc') + array($this, 'openSession'), + array($this, 'closeSession'), + array($this, 'readSession'), + array($this, 'writeSession'), + array($this, 'destroySession'), + array($this, 'gcSession') ); } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php index 6176cb8560..91424a68cf 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php @@ -89,7 +89,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa /** * {@inheritdoc} */ - public function sessionOpen($savePath, $sessionName) + public function openSession($savePath, $sessionName) { foreach ($this->memcacheOptions['serverpool'] as $server) { $this->addServer($server); @@ -101,7 +101,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa /** * {@inheritdoc} */ - public function sessionClose() + public function closeSession() { return $this->memcache->close(); } @@ -109,17 +109,15 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa /** * {@inheritdoc} */ - public function sessionRead($sessionId) + public function readSession($sessionId) { return $this->memcache->get($this->prefix.$sessionId) ?: ''; - - return ($result) ? $result : ''; } /** * {@inheritdoc} */ - public function sessionWrite($sessionId, $data) + public function writeSession($sessionId, $data) { return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']); } @@ -127,7 +125,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa /** * {@inheritdoc} */ - public function sessionDestroy($sessionId) + public function destroySession($sessionId) { return $this->memcache->delete($this->prefix.$sessionId); } @@ -135,7 +133,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa /** * {@inheritdoc} */ - public function sessionGc($lifetime) + public function gcSession($lifetime) { // not required here because memcache will auto expire the records anyhow. return true; diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php index 85bb358e9e..4d72f062f1 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php @@ -72,7 +72,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS /** * {@inheritdoc} */ - public function sessionOpen($savePath, $sessionName) + public function openSession($savePath, $sessionName) { foreach ($this->memcachedOptions['serverpool'] as $server) { $this->addServer($server); @@ -86,7 +86,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS * * @return boolean */ - public function sessionClose() + public function closeSession() { return $this->memcached->close(); } @@ -94,7 +94,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS /** * {@inheritdoc} */ - public function sessionRead($sessionId) + public function readSession($sessionId) { return $this->memcached->get($this->prefix.$sessionId) ?: ''; } @@ -102,7 +102,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS /** * {@inheritdoc} */ - public function sessionWrite($sessionId, $data) + public function writeSession($sessionId, $data) { return $this->memcached->set($this->prefix.$sessionId, $data, false, $this->memcachedOptions['expiretime']); } @@ -110,7 +110,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS /** * {@inheritdoc} */ - public function sessionDestroy($sessionId) + public function destroySession($sessionId) { return $this->memcached->delete($this->prefix.$sessionId); } @@ -118,7 +118,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS /** * {@inheritdoc} */ - public function sessionGc($lifetime) + public function gcSession($lifetime) { // not required here because memcached will auto expire the records anyhow. return true; diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php index bb62c50bd4..1ba5d5b898 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php @@ -28,7 +28,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa /** * {@inheritdoc} */ - public function sessionOpen($savePath, $sessionName) + public function openSession($savePath, $sessionName) { return true; } @@ -38,7 +38,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @return boolean */ - public function sessionClose() + public function closeSession() { return true; } @@ -46,7 +46,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa /** * {@inheritdoc} */ - public function sessionRead($sessionId) + public function readSession($sessionId) { return ''; } @@ -54,7 +54,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa /** * {@inheritdoc} */ - public function sessionWrite($sessionId, $data) + public function writeSession($sessionId, $data) { return true; } @@ -62,7 +62,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa /** * {@inheritdoc} */ - public function sessionDestroy($sessionId) + public function destroySession($sessionId) { return true; } @@ -70,7 +70,7 @@ class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHa /** * {@inheritdoc} */ - public function sessionGc($lifetime) + public function gcSession($lifetime) { return true; } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php index 1a264f1753..4f1c037153 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php @@ -70,7 +70,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan /** * {@inheritdoc} */ - public function sessionOpen($path = null, $name = null) + public function openSession($path = null, $name = null) { return true; } @@ -78,7 +78,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan /** * {@inheritdoc} */ - public function sessionClose() + public function closeSession() { return true; } @@ -88,7 +88,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan * * @throws \RuntimeException If the session cannot be destroyed */ - public function sessionDestroy($id) + public function destroySession($id) { // get table/column $dbTable = $this->dbOptions['db_table']; @@ -113,7 +113,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan * * @throws \RuntimeException If any old sessions cannot be cleaned */ - public function sessionGc($lifetime) + public function gcSession($lifetime) { // get table/column $dbTable = $this->dbOptions['db_table']; @@ -138,7 +138,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan * * @throws \RuntimeException If the session cannot be read */ - public function sessionRead($id) + public function readSession($id) { // get table/columns $dbTable = $this->dbOptions['db_table']; @@ -174,7 +174,7 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan * * @throws \RuntimeException If the session data cannot be written */ - public function sessionWrite($id, $data) + public function writeSession($id, $data) { // get table/column $dbTable = $this->dbOptions['db_table']; diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php index c45b9d22dc..774bfa946a 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php @@ -66,7 +66,7 @@ interface SessionSaveHandlerInterface * * @return boolean */ - function sessionOpen($savePath, $sessionName); + function openSession($savePath, $sessionName); /** * Close session. @@ -75,7 +75,7 @@ interface SessionSaveHandlerInterface * * @return boolean */ - function sessionClose(); + function closeSession(); /** * Read session. @@ -98,7 +98,7 @@ interface SessionSaveHandlerInterface * * @return string String as stored in persistent storage or empty string in all other cases. */ - function sessionRead($sessionId); + function readSession($sessionId); /** * Commit session to storage. @@ -120,7 +120,7 @@ interface SessionSaveHandlerInterface * * @return boolean */ - function sessionWrite($sessionId, $data); + function writeSession($sessionId, $data); /** * Destroys this session. @@ -137,7 +137,7 @@ interface SessionSaveHandlerInterface * * @return boolean */ - function sessionDestroy($sessionId); + function destroySession($sessionId); /** * Garbage collection for storage. @@ -153,5 +153,5 @@ interface SessionSaveHandlerInterface * * @return boolean */ - function sessionGc($lifetime); + function gcSession($lifetime); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php index d3c21c6c06..f7cd4d5dc6 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/AutoExpireFlashBagTest.php @@ -35,7 +35,7 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->bag = new FlashBag(); - $this->array = array('new' => array(FlashBag::NOTICE => array('A previous flash message'))); + $this->array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); $this->bag->initialize($this->array); } @@ -48,36 +48,36 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase public function testInitialize() { $bag = new FlashBag(); - $array = array('new' => array(FlashBag::NOTICE => array('A previous flash message'))); + $array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); $bag->initialize($array); - $this->assertEquals(array('A previous flash message'), $bag->get(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $bag->get(FlashBag::NOTICE)); $array = array('new' => array( - FlashBag::NOTICE => array('Something else'), - FlashBag::ERROR => array('a', 'b'), + FlashBag::NOTICE => 'Something else', + FlashBag::ERROR => 'a', )); $bag->initialize($array); - $this->assertEquals(array('Something else'), $bag->get(FlashBag::NOTICE)); - $this->assertEquals(array('a', 'b'), $bag->get(FlashBag::ERROR)); - } - - public function testAdd() - { - $this->bag->add('Something new', FlashBag::NOTICE); - $this->bag->add('Smile, it might work next time', FlashBag::ERROR); - $this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->get(FlashBag::ERROR)); + $this->assertEquals('Something else', $bag->get(FlashBag::NOTICE)); + $this->assertEquals('a', $bag->get(FlashBag::ERROR)); } public function testGet() { - $this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->get('non_existing_type')); + $this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE)); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testGetException() + { + $this->bag->get('non_existing_type'); } public function testSet() { - $this->bag->set(FlashBag::NOTICE, array('Foo', 'Bar')); - $this->assertNotEquals(array('Foo', 'Bar'), $this->bag->get(FlashBag::NOTICE)); + $this->bag->set(FlashBag::NOTICE, 'Foo'); + $this->assertNotEquals('Foo', $this->bag->get(FlashBag::NOTICE)); } public function testHas() @@ -95,58 +95,45 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase { $array = array( 'new' => array( - FlashBag::NOTICE => array('Foo'), - FlashBag::ERROR => array('Bar'), + FlashBag::NOTICE => 'Foo', + FlashBag::ERROR => 'Bar', ), ); $this->bag->initialize($array); $this->assertEquals(array( - FlashBag::NOTICE => array('Foo'), - FlashBag::ERROR => array('Bar'), + FlashBag::NOTICE => 'Foo', + FlashBag::ERROR => 'Bar', ), $this->bag->all() ); } + /** + * @expectedException \InvalidArgumentException + */ public function testPop() { - $this->assertEquals(array('A previous flash message'), $this->bag->pop(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->pop(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->pop('non_existing_type')); + $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); + $this->bag->pop(FlashBag::NOTICE); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testPopException() + { + $this->bag->pop('non_existing_type'); } public function testPopAll() { - $this->bag->set(FlashBag::NOTICE, array('Foo')); - $this->bag->set(FlashBag::ERROR, array('Bar')); + $this->bag->set(FlashBag::NOTICE, 'Foo'); + $this->bag->set(FlashBag::ERROR, 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => array('A previous flash message'), + FlashBag::NOTICE => 'A previous flash message', ), $this->bag->popAll() ); $this->assertEquals(array(), $this->bag->popAll()); } - - public function testClear() - { - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); - $this->assertEquals(array('A previous flash message'), $this->bag->clear(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->clear(FlashBag::NOTICE)); - $this->assertFalse($this->bag->has(FlashBag::NOTICE)); - } - - public function testClearAll() - { - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); - $this->bag->add('Smile, it might work next time', FlashBag::ERROR); - $this->assertFalse($this->bag->has(FlashBag::ERROR)); - $this->assertEquals(array( - FlashBag::NOTICE => array('A previous flash message'), - ), $this->bag->clearAll() - ); - $this->assertEquals(array(), $this->bag->clearAll()); - $this->assertFalse($this->bag->has(FlashBag::NOTICE)); - $this->assertFalse($this->bag->has(FlashBag::ERROR)); - } - } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php index 0d5a8a1ac5..356fd42516 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php @@ -35,7 +35,7 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->bag = new FlashBag(); - $this->array = array(FlashBag::NOTICE => array('A previous flash message')); + $this->array = array(FlashBag::NOTICE => 'A previous flash message'); $this->bag->initialize($this->array); } @@ -55,43 +55,58 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals($array, $bag->all()); } - public function testAdd() - { - $this->bag->add('Something new', FlashBag::NOTICE); - $this->bag->add('Smile, it might work next time', FlashBag::ERROR); - $this->assertEquals(array('A previous flash message', 'Something new'), $this->bag->get(FlashBag::NOTICE)); - $this->assertEquals(array('Smile, it might work next time'), $this->bag->get(FlashBag::ERROR)); - } - public function testGet() { - $this->assertEquals(array('A previous flash message'), $this->bag->get(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->get('non_existing_type')); + $this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE)); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testGetException() + { + $this->bag->get('non_existing_type'); } public function testPop() { - $this->assertEquals(array('A previous flash message'), $this->bag->pop(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->pop(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->pop('non_existing_type')); + $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testPopException() + { + $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); + $this->bag->pop(FlashBag::NOTICE); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testPopExceptionNotExisting() + { + $this->bag->pop('non_existing_type'); } public function testPopAll() { - $this->bag->set(FlashBag::NOTICE, array('Foo')); - $this->bag->set(FlashBag::ERROR, array('Bar')); + $this->bag->set(FlashBag::NOTICE, 'Foo'); + $this->bag->set(FlashBag::ERROR, 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => array('Foo'), - FlashBag::ERROR => array('Bar')), $this->bag->popAll() + FlashBag::NOTICE => 'Foo', + FlashBag::ERROR => 'Bar'), $this->bag->popAll() ); $this->assertEquals(array(), $this->bag->popAll()); } - public function testSet() + public function testset() { - $this->bag->set(FlashBag::NOTICE, array('Foo', 'Bar')); - $this->assertEquals(array('Foo', 'Bar'), $this->bag->get(FlashBag::NOTICE)); + $this->bag->set(FlashBag::NOTICE, 'Foo'); + $this->bag->set(FlashBag::NOTICE, 'Bar'); + $this->assertEquals('Bar', $this->bag->get(FlashBag::NOTICE)); } public function testHas() @@ -107,42 +122,19 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase public function testAll() { - $this->bag->set(FlashBag::NOTICE, array('Foo')); - $this->bag->set(FlashBag::ERROR, array('Bar')); + $this->bag->set(FlashBag::NOTICE, 'Foo'); + $this->bag->set(FlashBag::ERROR, 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => array('Foo'), - FlashBag::ERROR => array('Bar')), $this->bag->all() + FlashBag::NOTICE => 'Foo', + FlashBag::ERROR => 'Bar', + ), $this->bag->all() ); $this->assertTrue($this->bag->has(FlashBag::NOTICE)); $this->assertTrue($this->bag->has(FlashBag::ERROR)); $this->assertEquals(array( - FlashBag::NOTICE => array('Foo'), - FlashBag::ERROR => array('Bar'), + FlashBag::NOTICE => 'Foo', + FlashBag::ERROR => 'Bar', ), $this->bag->all() ); } - - public function testClear() - { - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); - $this->assertEquals(array('A previous flash message'), $this->bag->clear(FlashBag::NOTICE)); - $this->assertEquals(array(), $this->bag->clear(FlashBag::NOTICE)); - $this->assertFalse($this->bag->has(FlashBag::NOTICE)); - } - - public function testClearAll() - { - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); - $this->bag->add('Smile, it might work next time', FlashBag::ERROR); - $this->assertTrue($this->bag->has(FlashBag::ERROR)); - $this->assertEquals(array( - FlashBag::NOTICE => array('A previous flash message'), - FlashBag::ERROR => array('Smile, it might work next time'), - ), $this->bag->clearAll() - ); - $this->assertEquals(array(), $this->bag->clearAll()); - $this->assertFalse($this->bag->has(FlashBag::NOTICE)); - $this->assertFalse($this->bag->has(FlashBag::ERROR)); - } - } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php index c98d03cd87..1f00b059a5 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/AbstractSessionStorageTest.php @@ -2,14 +2,13 @@ namespace Symfony\Tests\Component\HttpFoundation\SessionStorage; -use Symfony\Component\HttpFoundation\AttributeBag; -use Symfony\Component\HttpFoundation\FlashBag; use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage; use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface; /** * Turn AbstractSessionStorage into something concrete because - * certain mocking features are broken in PHPUnit 3.6.4 + * certain mocking features are broken in PHPUnit-Mock-Objects < 1.1.2 + * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73 */ class ConcreteSessionStorage extends AbstractSessionStorage { @@ -17,27 +16,27 @@ class ConcreteSessionStorage extends AbstractSessionStorage class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { - public function sessionOpen($path, $id) + public function openSession($path, $id) { } - public function sessionClose() + public function closeSession() { } - public function sessionRead($id) + public function readSession($id) { } - public function sessionWrite($id, $data) + public function writeSession($id, $data) { } - public function sessionDestroy($id) + public function destroySession($id) { } - public function sessionGc($lifetime) + public function gcSession($lifetime) { } } @@ -58,7 +57,7 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase */ protected function getStorage() { - return new CustomHandlerSessionStorage(new AttributeBag(), new FlashBag()); + return new CustomHandlerSessionStorage(); } public function testGetFlashBag() @@ -106,13 +105,13 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase public function testCustomSaveHandlers() { - $storage = new CustomHandlerSessionStorage(new AttributeBag(), new FlashBag()); + $storage = new CustomHandlerSessionStorage(); $this->assertEquals('user', ini_get('session.save_handler')); } public function testNativeSaveHandlers() { - $storage = new ConcreteSessionStorage(new AttributeBag(), new FlashBag()); + $storage = new ConcreteSessionStorage(); $this->assertNotEquals('user', ini_get('session.save_handler')); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php index 58f44ccf2e..f0ba864b31 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php @@ -3,10 +3,6 @@ namespace Symfony\Test\Component\HttpFoundation\SessionStorage; use Symfony\Component\HttpFoundation\SessionStorage\MockFileSessionStorage; -use Symfony\Component\HttpFoundation\FlashBag; -use Symfony\Component\HttpFoundation\FlashBagInterface; -use Symfony\Component\HttpFoundation\AttributeBag; -use Symfony\Component\HttpFoundation\AttributeBagInterface; /** * Test class for MockFileSessionStorage. @@ -74,14 +70,14 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase $this->assertNotEquals('108', $this->storage->getAttributes()->get('new')); $this->assertFalse($this->storage->getFlashes()->has('newkey')); $this->storage->getAttributes()->set('new', '108'); - $this->storage->getFlashes()->add('test', 'newkey'); + $this->storage->getFlashes()->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(array('test'), $storage->getFlashes()->get('newkey')); + $this->assertEquals('test', $storage->getFlashes()->get('newkey')); } public function testMultipleInstances() @@ -98,6 +94,6 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase private function getStorage(array $options = array()) { - return new MockFileSessionStorage($this->sessionDir, $options, new AttributeBag(), new FlashBag()); + return new MockFileSessionStorage($this->sessionDir, $options); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php index a8fcf98c40..6bc94def52 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php @@ -2,8 +2,6 @@ namespace Symfony\Tests\Component\HttpFoundation\SessionStorage; use Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage; -use Symfony\Component\HttpFoundation\AttributeBag; -use Symfony\Component\HttpFoundation\FlashBag; use Symfony\Component\HttpFoundation\Session; /** diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index f4e3ee00d3..3066319463 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -123,28 +123,28 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function testInvalidate() { $this->session->set('invalidate', 123); - $this->session->addFlash('OK'); + $this->session->getFlashes()->set(FlashBag::NOTICE, 'OK'); $this->session->invalidate(); $this->assertEquals(array(), $this->session->all()); - $this->assertEquals(array(), $this->session->getAllFlashes()); + $this->assertEquals(array(), $this->session->getFlashes()->all()); } public function testMigrate() { $this->session->set('migrate', 321); - $this->session->addFlash('HI'); + $this->session->getFlashes()->set(FlashBag::NOTICE, 'HI'); $this->session->migrate(); $this->assertEquals(321, $this->session->get('migrate')); - $this->assertEquals(array('HI'), $this->session->getFlashes(FlashBag::NOTICE)); + $this->assertEquals('HI', $this->session->getFlashes()->get(FlashBag::NOTICE)); } public function testMigrateDestroy() { $this->session->set('migrate', 333); - $this->session->addFlash('Bye'); + $this->session->getFlashes()->set(FlashBag::NOTICE, 'Bye'); $this->session->migrate(true); $this->assertEquals(333, $this->session->get('migrate')); - $this->assertEquals(array('Bye'), $this->session->getFlashes(FlashBag::NOTICE)); + $this->assertEquals('Bye', $this->session->getFlashes()->get(FlashBag::NOTICE)); } public function testSerialize() @@ -176,167 +176,4 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->session->start(); $this->assertNotEquals('', $this->session->getId()); } - - /** - * @dataProvider provideFlashes - */ - public function testAddFlash($type, $flashes) - { - foreach ($flashes as $message) { - $this->session->addFlash($message, $type); - } - - $this->assertEquals($flashes, $this->session->getFlashes($type)); - } - - /** - * @dataProvider provideFlashes - */ - public function testGetFlashes($type, $flashes) - { - $this->session->setFlashes($type, $flashes); - $this->assertEquals($flashes, $this->session->getFlashes($type)); - } - - /** - * @dataProvider provideFlashes - */ - public function testPopFlashes($type, $flashes) - { - $this->session->setFlashes($type, $flashes); - $this->assertEquals($flashes, $this->session->popFlashes($type)); - $this->assertEquals(array(), $this->session->popFlashes($type)); - } - - /** - * @dataProvider provideFlashes - */ - public function testPopAllFlashes($type, $flashes) - { - $this->session->setFlashes(FlashBag::NOTICE, array('First', 'Second')); - $this->session->setFlashes(FlashBag::ERROR, array('Third')); - - $expected = array( - FlashBag::NOTICE => array('First', 'Second'), - FlashBag::ERROR => array('Third'), - ); - - $this->assertEquals($expected, $this->session->popAllFlashes()); - $this->assertEquals(array(), $this->session->popAllFlashes()); - } - - public function testSetFlashes() - { - $this->session->setFlashes(FlashBag::NOTICE, array('First', 'Second')); - $this->session->setFlashes(FlashBag::ERROR, array('Third')); - $this->assertEquals(array('First', 'Second'), $this->session->getFlashes(FlashBag::NOTICE, false)); - $this->assertEquals(array('Third'), $this->session->getFlashes(FlashBag::ERROR, false)); - } - - /** - * @dataProvider provideFlashes - */ - public function testHasFlashes($type, $flashes) - { - $this->assertFalse($this->session->hasFlashes($type)); - $this->session->setFlashes($type, $flashes); - $this->assertTrue($this->session->hasFlashes($type)); - } - - /** - * @dataProvider provideFlashes - */ - public function testGetFlashKeys($type, $flashes) - { - $this->assertEquals(array(), $this->session->getFlashKeys()); - $this->session->setFlashes($type, $flashes); - $this->assertEquals(array($type), $this->session->getFlashKeys()); - } - - public function testGetFlashKeysBulk() - { - $this->loadFlashes(); - $this->assertEquals(array( - FlashBag::NOTICE, FlashBag::ERROR, FlashBag::WARNING, FlashBag::INFO), $this->session->getFlashKeys() - ); - } - - public function testGetAllFlashes() - { - $this->assertEquals(array(), $this->session->getAllFlashes()); - - $this->session->addFlash('a', FlashBag::NOTICE); - $this->assertEquals(array( - FlashBag::NOTICE => array('a') - ), $this->session->getAllFlashes() - ); - - $this->session->addFlash('a', FlashBag::ERROR); - $this->assertEquals(array( - FlashBag::NOTICE => array('a'), - FlashBag::ERROR => array('a'), - ), $this->session->getAllFlashes()); - - $this->session->addFlash('a', FlashBag::WARNING); - $this->assertEquals(array( - FlashBag::NOTICE => array('a'), - FlashBag::ERROR => array('a'), - FlashBag::WARNING => array('a'), - ), $this->session->getAllFlashes() - ); - - $this->session->addFlash('a', FlashBag::INFO); - $this->assertEquals(array( - FlashBag::NOTICE => array('a'), - FlashBag::ERROR => array('a'), - FlashBag::WARNING => array('a'), - FlashBag::INFO => array('a'), - ), $this->session->getAllFlashes() - ); - - $this->assertEquals(array( - FlashBag::NOTICE => array('a'), - FlashBag::ERROR => array('a'), - FlashBag::WARNING => array('a'), - FlashBag::INFO => array('a'), - ), $this->session->getAllFlashes() - ); - } - - /** - * @dataProvider provideFlashes - */ - public function testClearFlashes($type, $flashes) - { - $this->session->setFlashes($type, $flashes); - $this->session->clearFlashes($type); - $this->assertEquals(array(), $this->session->getFlashes($type)); - } - - public function testClearAllFlashes() - { - $this->loadFlashes(); - $this->assertNotEquals(array(), $this->session->getAllFlashes()); - $this->session->clearAllFlashes(); - $this->assertEquals(array(), $this->session->getAllFlashes()); - } - - protected function loadFlashes() - { - $flashes = $this->provideFlashes(); - foreach ($flashes as $data) { - $this->session->setFlashes($data[0], $data[1]); - } - } - - public function provideFlashes() - { - return array( - array(FlashBag::NOTICE, array('a', 'b', 'c')), - array(FlashBag::ERROR, array('d', 'e', 'f')), - array(FlashBag::WARNING, array('g', 'h', 'i')), - array(FlashBag::INFO, array('j', 'k', 'l')), - ); - } - } From f9951a39eb5f2429ffb5881ccd1a94cc2d0c73fd Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 2 Feb 2012 17:21:28 +0545 Subject: [PATCH 13/31] Fixed formatting. --- UPGRADE-2.1.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 220846aa6b..fdfa5c2b96 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -33,16 +33,19 @@ UPGRADE FROM 2.0 to 2.1 Retrieving the locale from a Twig template: Before: `{{ app.request.session.locale }}` or `{{ app.session.locale }}` + After: `{{ app.request.locale }}` Retrieving the locale from a PHP template: Before: `$view['session']->getLocale()` + After: `$view['request']->getLocale()` Retrieving the locale from PHP code: Before: `$session->getLocale()` + After: `$request->getLocale()` * Method `equals` of `Symfony\Component\Security\Core\User\UserInterface` has @@ -138,7 +141,7 @@ UPGRADE FROM 2.0 to 2.1 * The strategy for generating the HTML attributes "id" and "name" of choices in a choice field has changed - + Instead of appending the choice value, a generated integer is now appended by default. Take care if your Javascript relies on that. If you can guarantee that your choice values only contain ASCII letters, digits, @@ -148,7 +151,7 @@ UPGRADE FROM 2.0 to 2.1 * The strategy for generating the HTML attributes "value" of choices in a choice field has changed - + Instead of using the choice value, a generated integer is now stored. Again, take care if your Javascript reads this value. If your choice field is a non-expanded single-choice field, or if the choices are guaranteed not From d64939aeee5cd6db9a8865cb26d53b5bab7e16d4 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 3 Feb 2012 03:33:28 +0545 Subject: [PATCH 14/31] [DoctrineBridge] Refactored driver for changed interface. --- .../Doctrine/HttpFoundation/DbalSessionStorage.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 12697fe86f..3b1e7ef516 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -51,7 +51,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @return Boolean true, if the session was opened, otherwise an exception is thrown */ - public function sessionOpen($path = null, $name = null) + public function openSession($path = null, $name = null) { return true; } @@ -61,7 +61,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @return Boolean true, if the session was closed, otherwise false */ - public function sessionClose() + public function closeSession() { // do nothing return true; @@ -76,7 +76,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @throws \RuntimeException If the session cannot be destroyed */ - public function sessionDestroy($id) + public function destroySession($id) { try { $this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_id = :id", array( @@ -98,7 +98,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @throws \RuntimeException If any old sessions cannot be cleaned */ - public function sessionGc($lifetime) + public function gcSession($lifetime) { try { $this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array( @@ -120,7 +120,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @throws \RuntimeException If the session cannot be read */ - public function sessionRead($id) + public function readSession($id) { try { $data = $this->con->executeQuery("SELECT sess_data FROM {$this->tableName} WHERE sess_id = :id", array( @@ -150,7 +150,7 @@ class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHa * * @throws \RuntimeException If the session data cannot be written */ - public function sessionWrite($id, $data) + public function writeSession($id, $data) { $platform = $this->con->getDatabasePlatform(); From 468391525a31731af90f2a4ccadba425e3dbdabd Mon Sep 17 00:00:00 2001 From: Drak Date: Wed, 8 Feb 2012 15:31:38 +0545 Subject: [PATCH 15/31] [HttpFoundation] Free bags from session storage and move classes to their own namespaces. --- CHANGELOG-2.1.md | 2 +- UPGRADE-2.1.md | 8 +- .../HttpFoundation/DbalSessionStorage.php | 8 +- .../Resources/config/session.xml | 22 +--- .../Templating/Helper/SessionHelper.php | 2 +- .../Templating/Helper/SessionHelperTest.php | 4 +- .../EventListener/WebDebugToolbarListener.php | 2 +- .../HttpFoundation/AttributeBagInterface.php | 24 ---- .../Component/HttpFoundation/Session.php | 40 +++++-- .../{ => SessionAttribute}/AttributeBag.php | 20 +++- .../AttributeBagInterface.php} | 13 +-- .../NamespacedAttributeBag.php | 5 +- .../HttpFoundation/SessionBagInterface.php | 14 +++ .../{ => SessionFlash}/AutoExpireFlashBag.php | 28 ++++- .../{ => SessionFlash}/FlashBag.php | 25 +++- .../{ => SessionFlash}/FlashBagInterface.php | 4 +- .../HttpFoundation/SessionInterface.php | 57 ++++++++- .../SessionStorage/AbstractSessionStorage.php | 110 +++++++++--------- .../SessionStorage/MemcacheSessionStorage.php | 9 +- .../MemcachedSessionStorage.php | 9 +- .../MockArraySessionStorage.php | 51 ++++---- .../SessionStorage/MockFileSessionStorage.php | 18 +-- .../NativeFileSessionStorage.php | 9 +- .../NativeMemcacheSessionStorage.php | 9 +- .../NativeMemcachedSessionStorage.php | 9 +- .../NativeSqliteSessionStorage.php | 9 +- .../SessionStorage/NullSessionStorage.php | 3 - .../SessionStorage/PdoSessionStorage.php | 9 +- .../SessionSaveHandlerInterface.php | 8 +- .../SessionStorageInterface.php | 15 +-- .../Component/HttpFoundation/RequestTest.php | 6 +- .../AttributeBagTest.php | 2 +- .../NamespacedAttributeBagTest.php | 2 +- .../AutoExpireFlashBagTest.php | 6 +- .../{ => SessionFlash}/FlashBagTest.php | 6 +- .../AbstractSessionStorageTest.php | 28 +++-- .../MockArraySessionStorageTest.php | 32 +++-- .../MockFileSessionStorageTest.php | 32 ++--- .../NativeFileSessionStorageTest.php | 18 +-- .../NativeMemcacheSessionStorageTest.php | 22 +--- .../NativeMemcachedSessionStorageTest.php | 26 +---- .../NativeSqliteSessionStorageTest.php | 22 +--- .../SessionStorage/NullSessionStorageTest.php | 8 -- .../Component/HttpFoundation/SessionTest.php | 12 +- 44 files changed, 385 insertions(+), 383 deletions(-) delete mode 100644 src/Symfony/Component/HttpFoundation/AttributeBagInterface.php rename src/Symfony/Component/HttpFoundation/{ => SessionAttribute}/AttributeBag.php (86%) rename src/Symfony/Component/HttpFoundation/{SessionStorage/AttributeInterface.php => SessionAttribute/AttributeBagInterface.php} (85%) rename src/Symfony/Component/HttpFoundation/{ => SessionAttribute}/NamespacedAttributeBag.php (97%) rename src/Symfony/Component/HttpFoundation/{ => SessionFlash}/AutoExpireFlashBag.php (87%) rename src/Symfony/Component/HttpFoundation/{ => SessionFlash}/FlashBag.php (86%) rename src/Symfony/Component/HttpFoundation/{ => SessionFlash}/FlashBagInterface.php (92%) rename tests/Symfony/Tests/Component/HttpFoundation/{ => SessionAttribute}/AttributeBagTest.php (98%) rename tests/Symfony/Tests/Component/HttpFoundation/{ => SessionAttribute}/NamespacedAttributeBagTest.php (98%) rename tests/Symfony/Tests/Component/HttpFoundation/{ => SessionFlash}/AutoExpireFlashBagTest.php (93%) rename tests/Symfony/Tests/Component/HttpFoundation/{ => SessionFlash}/FlashBagTest.php (94%) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 2a1d994881..6ae768c51f 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -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()`. diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index fdfa5c2b96..fea123a526 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -259,7 +259,7 @@ UPGRADE FROM 2.0 to 2.1 - 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: getFlashes()->all() as $type => $flash): ?>
@@ -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 `` + `Symfony\Component\HttpFoundation\SessionFlash\FlashBag::NOTICE`, which can also be abbreviated to + `FlashBag::NOTICE` providing you declare `` 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 diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 3b1e7ef516..8552a09f30 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index a1a746b781..dd1a2b6d3b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -6,8 +6,8 @@ Symfony\Component\HttpFoundation\Session - Symfony\Component\HttpFoundation\FlashBag - Symfony\Component\HttpFoundation\AttributeBag + Symfony\Component\HttpFoundation\SessionFlash\FlashBag + Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage @@ -25,6 +25,8 @@ + + @@ -35,58 +37,42 @@ %kernel.cache_dir%/sessions %session.storage.options% - - %kernel.cache_dir%/sessions %session.storage.options% - - tcp://127.0.0.1:11211?persistent=0 %session.storage.options% - - 127.0.0.1:11211 %session.storage.options% - - tcp://127.0.0.1:11211?persistent=0 %session.storage.options% - - tcp://127.0.0.1:11211?persistent=0 %session.storage.options% - - %kernel.cache_dir%/sf2_sqlite_sess.db %session.storage.options% - - %session.storage.options% - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index c861e23e01..983c3cfece 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -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. diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index b18526f836..bd7c3a962b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -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 { diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index 9d6c6e766b..d12cee0cb3 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -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. diff --git a/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php b/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php deleted file mode 100644 index e835533a37..0000000000 --- a/src/Symfony/Component/HttpFoundation/AttributeBagInterface.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * 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 - */ -interface AttributeBagInterface extends SessionBagInterface, AttributeInterface -{ -} diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index b6d2f142c9..6d40b880a7 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -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'); } } diff --git a/src/Symfony/Component/HttpFoundation/AttributeBag.php b/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBag.php similarity index 86% rename from src/Symfony/Component/HttpFoundation/AttributeBag.php rename to src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBag.php index 3db1650426..301f2e6628 100644 --- a/src/Symfony/Component/HttpFoundation/AttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBag.php @@ -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; } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php b/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBagInterface.php similarity index 85% rename from src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php rename to src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBagInterface.php index 176cb8bc8b..e24603ac7e 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/AttributeInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBagInterface.php @@ -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 */ -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(); } diff --git a/src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php b/src/Symfony/Component/HttpFoundation/SessionAttribute/NamespacedAttributeBag.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php rename to src/Symfony/Component/HttpFoundation/SessionAttribute/NamespacedAttributeBag.php index b175b5f233..74e432fe47 100644 --- a/src/Symfony/Component/HttpFoundation/NamespacedAttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/SessionAttribute/NamespacedAttributeBag.php @@ -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; } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionBagInterface.php b/src/Symfony/Component/HttpFoundation/SessionBagInterface.php index 2fd53621dc..a26596369b 100644 --- a/src/Symfony/Component/HttpFoundation/SessionBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionBagInterface.php @@ -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(); } diff --git a/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/SessionFlash/AutoExpireFlashBag.php similarity index 87% rename from src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php rename to src/Symfony/Component/HttpFoundation/SessionFlash/AutoExpireFlashBag.php index 2767e4817a..906ac302ae 100644 --- a/src/Symfony/Component/HttpFoundation/AutoExpireFlashBag.php +++ b/src/Symfony/Component/HttpFoundation/SessionFlash/AutoExpireFlashBag.php @@ -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; + } } diff --git a/src/Symfony/Component/HttpFoundation/FlashBag.php b/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBag.php similarity index 86% rename from src/Symfony/Component/HttpFoundation/FlashBag.php rename to src/Symfony/Component/HttpFoundation/SessionFlash/FlashBag.php index 31ccce4447..9dcb731a02 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBag.php @@ -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(); + } } diff --git a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBagInterface.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/FlashBagInterface.php rename to src/Symfony/Component/HttpFoundation/SessionFlash/FlashBagInterface.php index c8dd96f14f..41e8dde577 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBagInterface.php @@ -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. diff --git a/src/Symfony/Component/HttpFoundation/SessionInterface.php b/src/Symfony/Component/HttpFoundation/SessionInterface.php index 9cbadbe2c0..dc15518341 100644 --- a/src/Symfony/Component/HttpFoundation/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionInterface.php @@ -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 */ -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. * diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php index 7301f42110..8382d3a3ff 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php @@ -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]); + } } } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php index 91424a68cf..6836247ad3 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php @@ -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) diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php index 4d72f062f1..7a9c6ecb9a 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php index 45734dec31..96206f5847 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php @@ -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. * diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php index 7bc7b141d0..366002fee7 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php @@ -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); } - } diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php index f77763143f..25c36ad7fc 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php index a869eeaacc..338f4c2011 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php index 28635d6caf..446e5c197a 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php index bdcaeeb6b4..3acf2e7056 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php index 1ba5d5b898..4539cb72ad 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php @@ -11,9 +11,6 @@ namespace Symfony\Component\HttpFoundation\SessionStorage; -use Symfony\Component\HttpFoundation\AttributeBagInterface; -use Symfony\Component\HttpFoundation\FlashBagInterface; - /** * NullSessionStorage. * diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php index 4f1c037153..3b36a1fcef 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php @@ -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); } /** diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php index 774bfa946a..6c6929fb37 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php @@ -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 diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php index 8624e52ec6..bb76d15f74 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php @@ -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); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index f392b7e666..0023817aef 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -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()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionAttribute/AttributeBagTest.php similarity index 98% rename from tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/SessionAttribute/AttributeBagTest.php index 60e13794e0..4d35f35602 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/AttributeBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionAttribute/AttributeBagTest.php @@ -1,7 +1,7 @@ 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() diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php index 441d7b91c3..c3c1959735 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockArraySessionStorageTest.php @@ -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() diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php index f0ba864b31..469410100b 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/MockFileSessionStorageTest.php @@ -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; } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php index 0bb4dd6cfc..8b32840e25 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php @@ -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()); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php index 8f498f2e44..38019e2508 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php @@ -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()); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php index 1559a117ab..fe3307a1fd 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php @@ -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()); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php index 51d1b64ebe..455f1392d7 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php @@ -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()); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php index 6bc94def52..26782337c8 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NullSessionStorageTest.php @@ -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(); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index 3066319463..7f86b89b0b 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -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() From 27530cbb1e0121e1dd250971fd7aea8b2f2672f9 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 9 Feb 2012 05:00:47 +0545 Subject: [PATCH 16/31] [HttpFoundation] Moved session related classes to own sub-namespace. --- CHANGELOG-2.1.md | 2 ++ UPGRADE-2.1.md | 10 +++--- .../HttpFoundation/DbalSessionStorage.php | 4 +-- .../FrameworkExtension.php | 2 +- .../Resources/config/session.xml | 22 ++++++------- .../Templating/GlobalVariables.php | 2 +- .../Templating/Helper/SessionHelper.php | 2 +- .../EventListener/TestSessionListenerTest.php | 2 +- .../Templating/Helper/SessionHelperTest.php | 8 ++--- .../Tests/Templating/PhpEngineTest.php | 4 +-- .../TwigBundle/Tests/TwigEngineTest.php | 4 +-- .../EventListener/WebDebugToolbarListener.php | 2 +- .../WebDebugToolbarListenerTest.php | 2 +- .../Csrf/CsrfProvider/SessionCsrfProvider.php | 2 +- .../Component/HttpFoundation/Request.php | 4 +-- .../Attribute}/AttributeBag.php | 2 +- .../Attribute}/AttributeBagInterface.php | 4 +-- .../Attribute}/NamespacedAttributeBag.php | 2 +- .../Flash}/AutoExpireFlashBag.php | 2 +- .../Flash}/FlashBag.php | 2 +- .../Flash}/FlashBagInterface.php | 4 +-- .../HttpFoundation/{ => Session}/Session.php | 14 ++++---- .../{ => Session}/SessionBagInterface.php | 2 +- .../{ => Session}/SessionInterface.php | 6 ++-- .../Storage}/AbstractSessionStorage.php | 12 +++---- .../Storage}/MemcacheSessionStorage.php | 2 +- .../Storage}/MemcachedSessionStorage.php | 2 +- .../Storage}/MockArraySessionStorage.php | 2 +- .../Storage}/MockFileSessionStorage.php | 2 +- .../Storage}/NativeFileSessionStorage.php | 2 +- .../Storage}/NativeMemcacheSessionStorage.php | 2 +- .../NativeMemcachedSessionStorage.php | 2 +- .../Storage}/NativeSqliteSessionStorage.php | 2 +- .../Storage}/NullSessionStorage.php | 2 +- .../Storage}/PdoSessionStorage.php | 2 +- .../Storage}/SessionSaveHandlerInterface.php | 2 +- .../Storage}/SessionStorageInterface.php | 6 ++-- .../SessionStorage/ArraySessionStorage.php | 32 ------------------- .../CsrfProvider/SessionCsrfProviderTest.php | 2 +- .../Component/HttpFoundation/RequestTest.php | 4 +-- .../Attribute}/AttributeBagTest.php | 4 +-- .../Attribute}/NamespacedAttributeBagTest.php | 2 +- .../Flash}/AutoExpireFlashBagTest.php | 8 ++--- .../Flash}/FlashBagTest.php | 6 ++-- .../{ => Session}/SessionTest.php | 18 +++++------ .../Storage}/AbstractSessionStorageTest.php | 10 +++--- .../Storage}/MockArraySessionStorageTest.php | 8 ++--- .../Storage}/MockFileSessionStorageTest.php | 8 ++--- .../Storage}/NativeFileSessionStorageTest.php | 8 ++--- .../NativeMemcacheSessionStorageTest.php | 8 ++--- .../NativeMemcachedSessionStorageTest.php | 8 ++--- .../NativeSqliteSessionStorageTest.php | 8 ++--- .../Storage}/NullSessionStorageTest.php | 6 ++-- .../EventListener/LocaleListenerTest.php | 4 +-- .../Http/Firewall/ContextListenerTest.php | 4 +-- .../Http/Logout/SessionLogoutHandlerTest.php | 2 +- 56 files changed, 135 insertions(+), 165 deletions(-) rename src/Symfony/Component/HttpFoundation/{SessionAttribute => Session/Attribute}/AttributeBag.php (97%) rename src/Symfony/Component/HttpFoundation/{SessionAttribute => Session/Attribute}/AttributeBagInterface.php (91%) rename src/Symfony/Component/HttpFoundation/{SessionAttribute => Session/Attribute}/NamespacedAttributeBag.php (98%) rename src/Symfony/Component/HttpFoundation/{SessionFlash => Session/Flash}/AutoExpireFlashBag.php (98%) rename src/Symfony/Component/HttpFoundation/{SessionFlash => Session/Flash}/FlashBag.php (97%) rename src/Symfony/Component/HttpFoundation/{SessionFlash => Session/Flash}/FlashBagInterface.php (92%) rename src/Symfony/Component/HttpFoundation/{ => Session}/Session.php (91%) rename src/Symfony/Component/HttpFoundation/{ => Session}/SessionBagInterface.php (93%) rename src/Symfony/Component/HttpFoundation/{ => Session}/SessionInterface.php (92%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/AbstractSessionStorage.php (96%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/MemcacheSessionStorage.php (98%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/MemcachedSessionStorage.php (98%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/MockArraySessionStorage.php (97%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/MockFileSessionStorage.php (97%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeFileSessionStorage.php (95%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeMemcacheSessionStorage.php (97%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeMemcachedSessionStorage.php (97%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeSqliteSessionStorage.php (95%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/NullSessionStorage.php (95%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/PdoSessionStorage.php (99%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/SessionSaveHandlerInterface.php (98%) rename src/Symfony/Component/HttpFoundation/{SessionStorage => Session/Storage}/SessionStorageInterface.php (93%) delete mode 100644 src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php rename tests/Symfony/Tests/Component/HttpFoundation/{SessionAttribute => Session/Attribute}/AttributeBagTest.php (96%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionAttribute => Session/Attribute}/NamespacedAttributeBagTest.php (98%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionFlash => Session/Flash}/AutoExpireFlashBagTest.php (92%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionFlash => Session/Flash}/FlashBagTest.php (94%) rename tests/Symfony/Tests/Component/HttpFoundation/{ => Session}/SessionTest.php (88%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/AbstractSessionStorageTest.php (89%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/MockArraySessionStorageTest.php (89%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/MockFileSessionStorageTest.php (91%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeFileSessionStorageTest.php (67%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeMemcacheSessionStorageTest.php (73%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeMemcachedSessionStorageTest.php (75%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/NativeSqliteSessionStorageTest.php (72%) rename tests/Symfony/Tests/Component/HttpFoundation/{SessionStorage => Session/Storage}/NullSessionStorageTest.php (84%) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 6ae768c51f..53fa9ac088 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -229,6 +229,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * removed the ContentTypeMimeTypeGuesser class as it is deprecated and never used on PHP 5.3 * added ResponseHeaderBag::makeDisposition() (implements RFC 6266) * made mimetype to extension conversion configurable + * [BC BREAK] Moved all session related classes and interfaces into own namespace, as + `Symfony\Component\HttpFoudation\Session`. * 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`. diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index fea123a526..78169aacdf 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -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\SessionFlash\FlashBag::NOTICE`, which can also be abbreviated to - `FlashBag::NOTICE` providing you declare `` + `Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE`, which can also be abbreviated to + `FlashBag::NOTICE` providing you declare `` 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\SessionFlash\FlashBag::NOTICE')`. + `constant('Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE')`. * Session object @@ -313,12 +313,12 @@ UPGRADE FROM 2.0 to 2.1 * Session storage drivers Session storage drivers should inherit from - `Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage` + `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` and no longer should implement `read()`, `write()`, `remove()` which were removed from the `SessionStorageInterface`. Any session storage driver that wants to use custom save handlers should - implement `Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface` + implement `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` ### [FrameworkBundle] diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 8552a09f30..cda9dd2a0a 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -3,8 +3,8 @@ namespace Symfony\Bridge\Doctrine\HttpFoundation; use Doctrine\DBAL\Platforms\MySqlPlatform; -use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage; -use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface; use Doctrine\DBAL\Driver\Connection; /** diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 2e80aea918..426e78cf5e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -301,7 +301,7 @@ class FrameworkExtension extends Extension $this->addClassesToCompile(array( 'Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener', - 'Symfony\\Component\\HttpFoundation\\SessionStorage\\SessionStorageInterface', + 'Symfony\\Component\\HttpFoundation\\Session\Storage\\SessionStorageInterface', $container->getDefinition('session')->getClass(), )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index dd1a2b6d3b..7e651594ec 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -5,17 +5,17 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - Symfony\Component\HttpFoundation\Session - Symfony\Component\HttpFoundation\SessionFlash\FlashBag - Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag - Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\NullSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\NativeMemcachedSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\NativeSqliteSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\MemcacheSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\MemcachedSessionStorage - Symfony\Component\HttpFoundation\SessionStorage\MockFileSessionStorage + Symfony\Component\HttpFoundation\Session\Session + Symfony\Component\HttpFoundation\Session\Flash\FlashBag + Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag + Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NullSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\MemcacheSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\MemcachedSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage Memcache Memcached diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php b/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php index 08d32c86cf..f77ca12436 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php @@ -79,7 +79,7 @@ class GlobalVariables /** * Returns the current session. * - * @return Symfony\Component\HttpFoundation\Session|void The session + * @return Symfony\Component\HttpFoundation\Session\Session|void The session */ public function getSession() { diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index 983c3cfece..684e5d92f5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -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\SessionFlash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** * SessionHelper provides read-only access to the session attributes. diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php index 7c656ba7e0..fe551ba360 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php @@ -94,7 +94,7 @@ class TestSessionListenerTest extends \PHPUnit_Framework_TestCase private function getSession() { - return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session') + return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session') ->disableOriginalConstructor() ->getMock(); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index bd7c3a962b..b9f6e41670 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -12,11 +12,11 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper; -use Symfony\Component\HttpFoundation\SessionFlash\FlashBag; -use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; class SessionHelperTest extends \PHPUnit_Framework_TestCase { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index 463df19ffc..476b398e61 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -14,8 +14,8 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating; use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index 4bde094136..f15ef3f945 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -14,8 +14,8 @@ namespace Symfony\Bundle\TwigBundle\Tests; use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index d12cee0cb3..5d172a1cad 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -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\SessionFlash\AutoExpireFlashBag; +use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag; /** * WebDebugToolbarListener injects the Web Debug Toolbar. diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php index b6986176e8..ce75b593dc 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php @@ -177,7 +177,7 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html') { - $session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false); + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false); $request = $this->getMock( 'Symfony\Component\HttpFoundation\Request', array('getSession', 'isXmlHttpRequest', 'getRequestFormat'), diff --git a/src/Symfony/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProvider.php b/src/Symfony/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProvider.php index d52fcac4b3..5174412a97 100644 --- a/src/Symfony/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProvider.php +++ b/src/Symfony/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProvider.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Form\Extension\Csrf\CsrfProvider; -use Symfony\Component\HttpFoundation\Session; +use Symfony\Component\HttpFoundation\Session\Session; /** * This provider uses a Symfony2 Session object to retrieve the user's diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index bd6bf43629..beab652a42 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -11,7 +11,7 @@ namespace Symfony\Component\HttpFoundation; -use Symfony\Component\HttpFoundation\SessionInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Request represents an HTTP request. @@ -124,7 +124,7 @@ class Request protected $format; /** - * @var \Symfony\Component\HttpFoundation\SessionInterface + * @var \Symfony\Component\HttpFoundation\Session\SessionInterface */ protected $session; diff --git a/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBag.php b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBag.php rename to src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php index 301f2e6628..2915b03297 100644 --- a/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionAttribute; +namespace Symfony\Component\HttpFoundation\Session\Attribute; /** * This class relates to session attribute storage diff --git a/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBagInterface.php similarity index 91% rename from src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBagInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBagInterface.php index e24603ac7e..af16d642ef 100644 --- a/src/Symfony/Component/HttpFoundation/SessionAttribute/AttributeBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBagInterface.php @@ -9,9 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionAttribute; +namespace Symfony\Component\HttpFoundation\Session\Attribute; -use Symfony\Component\HttpFoundation\SessionBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * Attributes store. diff --git a/src/Symfony/Component/HttpFoundation/SessionAttribute/NamespacedAttributeBag.php b/src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/SessionAttribute/NamespacedAttributeBag.php rename to src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php index 74e432fe47..7a32405096 100644 --- a/src/Symfony/Component/HttpFoundation/SessionAttribute/NamespacedAttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionAttribute; +namespace Symfony\Component\HttpFoundation\Session\Attribute; /** * This class provides structured storage of session attributes using diff --git a/src/Symfony/Component/HttpFoundation/SessionFlash/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/SessionFlash/AutoExpireFlashBag.php rename to src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php index 906ac302ae..e69992568d 100644 --- a/src/Symfony/Component/HttpFoundation/SessionFlash/AutoExpireFlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionFlash; +namespace Symfony\Component\HttpFoundation\Session\Flash; /** * AutoExpireFlashBag flash message container. diff --git a/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/SessionFlash/FlashBag.php rename to src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php index 9dcb731a02..99508f760f 100644 --- a/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionFlash; +namespace Symfony\Component\HttpFoundation\Session\Flash; /** * FlashBag flash message container. diff --git a/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/SessionFlash/FlashBagInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php index 41e8dde577..262f624e80 100644 --- a/src/Symfony/Component/HttpFoundation/SessionFlash/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php @@ -9,9 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionFlash; +namespace Symfony\Component\HttpFoundation\Session\Flash; -use Symfony\Component\HttpFoundation\SessionBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * FlashBagInterface. diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php similarity index 91% rename from src/Symfony/Component/HttpFoundation/Session.php rename to src/Symfony/Component/HttpFoundation/Session/Session.php index 6d40b880a7..51cd368931 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation; +namespace Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface; -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; +use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * Session. diff --git a/src/Symfony/Component/HttpFoundation/SessionBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionBagInterface.php similarity index 93% rename from src/Symfony/Component/HttpFoundation/SessionBagInterface.php rename to src/Symfony/Component/HttpFoundation/Session/SessionBagInterface.php index a26596369b..50c2d4bd0c 100644 --- a/src/Symfony/Component/HttpFoundation/SessionBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionBagInterface.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation; +namespace Symfony\Component\HttpFoundation\Session; /** * Session Bag store. diff --git a/src/Symfony/Component/HttpFoundation/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/SessionInterface.php rename to src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index dc15518341..8bb013514b 100644 --- a/src/Symfony/Component/HttpFoundation/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -9,10 +9,10 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation; +namespace Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\AttributeInterface; -use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\Storage\AttributeInterface; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** * Interface for the session. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php similarity index 96% rename from src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php index 8382d3a3ff..d50462eb87 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/AbstractSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php @@ -9,13 +9,13 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; -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; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * This provides a base class for session attribute storage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php index 6836247ad3..f5d5910d86 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * MemcacheSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php index 7a9c6ecb9a..f7041811a3 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * MemcachedSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 96206f5847..2fd1a94c26 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * MockArraySessionStorage mocks the session for unit tests. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index 366002fee7..094c4d6005 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * MockFileSessionStorage is used to mock sessions for diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php index 25c36ad7fc..09350e8d21 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * NativeFileSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php index 338f4c2011..5572c47812 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * NativeMemcacheSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php index 446e5c197a..50d8d060fd 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * NativeMemcachedSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php index 3acf2e7056..1dac36ac2f 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * NativeSqliteSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php index 4539cb72ad..b81fa8ae3a 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/NullSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * NullSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php similarity index 99% rename from src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php index 3b36a1fcef..2015396b2f 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * PdoSessionStorage. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php index 6c6929fb37..ec4530a150 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionSaveHandlerInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; /** * Session Savehandler Interface. diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php similarity index 93% rename from src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index bb76d15f74..a5afda9672 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -9,12 +9,12 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\SessionStorage; +namespace Symfony\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\SessionBagInterface; +use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** - * SessionStorageInterface. + * StorageInterface. * * @author Fabien Potencier * @author Drak diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php deleted file mode 100644 index affea24142..0000000000 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/ArraySessionStorage.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\SessionStorage; - -/** - * ArraySessionStorage mocks the session for unit tests. - * - * BC Class for MockArraySessionStorage - * - * @deprecated since 2.1.0 - * @see MockArraySessionStorage - * - * No PHP session is actually started since a session can be initialized - * and shutdown only once per PHP execution cycle. - * - * When doing functional testing, you should use FileMockSessionStorage instead. - * - * @author Fabien Potencier - * @author Bulat Shakirzyanov - */ -class ArraySessionStorage extends MockArraySessionStorage -{ -} diff --git a/tests/Symfony/Tests/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProviderTest.php b/tests/Symfony/Tests/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProviderTest.php index 958a328295..3358ffcb21 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProviderTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Csrf/CsrfProvider/SessionCsrfProviderTest.php @@ -21,7 +21,7 @@ class SessionCsrfProviderTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->session = $this->getMock( - 'Symfony\Component\HttpFoundation\Session', + 'Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index 0023817aef..c147070fdf 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -12,8 +12,8 @@ namespace Symfony\Tests\Component\HttpFoundation; -use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; -use Symfony\Component\HttpFoundation\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Request; class RequestTest extends \PHPUnit_Framework_TestCase diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionAttribute/AttributeBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php similarity index 96% rename from tests/Symfony/Tests/Component/HttpFoundation/SessionAttribute/AttributeBagTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php index 4d35f35602..a5c921254c 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionAttribute/AttributeBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php @@ -1,7 +1,7 @@ cookies->set('foo', 'value'); - $session = $this->getMock('Symfony\Component\HttpFoundation\Session', array('get'), array(), '', false); + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array('get'), array(), '', false); $session->expects($this->once())->method('get')->will($this->returnValue('es')); $request->setSession($session); @@ -55,7 +55,7 @@ class LocaleListenerTest extends \PHPUnit_Framework_TestCase $event = $this->getEvent($request); // also updates the session _locale value - $session = $this->getMock('Symfony\Component\HttpFoundation\Session', array('set', 'get'), array(), '', false); + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array('set', 'get'), array(), '', false); $session->expects($this->once())->method('set')->with('_locale', 'es'); $session->expects($this->once())->method('get')->with('_locale')->will($this->returnValue('es')); $request->setSession($session); diff --git a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php index e839e8fd16..5c810062ef 100644 --- a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php +++ b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php @@ -4,8 +4,8 @@ namespace Symfony\Test\Component\Security\Http\Firewall; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\SessionStorage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; diff --git a/tests/Symfony/Tests/Component/Security/Http/Logout/SessionLogoutHandlerTest.php b/tests/Symfony/Tests/Component/Security/Http/Logout/SessionLogoutHandlerTest.php index 05df6cf8f5..9f38e4c9c1 100644 --- a/tests/Symfony/Tests/Component/Security/Http/Logout/SessionLogoutHandlerTest.php +++ b/tests/Symfony/Tests/Component/Security/Http/Logout/SessionLogoutHandlerTest.php @@ -22,7 +22,7 @@ class SessionLogoutHandlerTest extends \PHPUnit_Framework_TestCase $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); $response = new Response(); - $session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false); + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false); $request ->expects($this->once()) From 5b7ef116507fa05e1042065f61a50733c19116cb Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 9 Feb 2012 07:05:40 +0545 Subject: [PATCH 17/31] [HttpFoundation] Simplify session storage class names now we have a separate namespace for sessions. --- CHANGELOG-2.1.md | 28 +++++++++++-------- UPGRADE-2.1.md | 4 +-- .../HttpFoundation/DbalSessionStorage.php | 4 +-- .../DbalSessionStorageSchema.php | 2 +- .../FrameworkExtension.php | 2 +- .../Resources/config/session.xml | 16 +++++------ .../Templating/Helper/SessionHelperTest.php | 4 +-- .../Tests/Templating/PhpEngineTest.php | 4 +-- .../TwigBundle/Tests/TwigEngineTest.php | 4 +-- .../DependencyInjection/Container.php | 4 +-- .../HttpFoundation/Session/Session.php | 16 +++++------ ...SessionStorage.php => AbstractStorage.php} | 4 +-- ...SessionStorage.php => MemcacheStorage.php} | 6 ++-- ...essionStorage.php => MemcachedStorage.php} | 6 ++-- ...essionStorage.php => MockArrayStorage.php} | 6 ++-- ...SessionStorage.php => MockFileStorage.php} | 6 ++-- ...ssionStorage.php => NativeFileStorage.php} | 6 ++-- ...nStorage.php => NativeMemcacheStorage.php} | 6 ++-- ...Storage.php => NativeMemcachedStorage.php} | 6 ++-- ...ionStorage.php => NativeSqliteStorage.php} | 6 ++-- ...NullSessionStorage.php => NullStorage.php} | 4 +-- .../{PdoSessionStorage.php => PdoStorage.php} | 8 +++--- ...Interface.php => SaveHandlerInterface.php} | 2 +- ...rageInterface.php => StorageInterface.php} | 2 +- .../Component/HttpFoundation/RequestTest.php | 6 ++-- .../HttpFoundation/Session/SessionTest.php | 6 ++-- ...torageTest.php => AbstractStorageTest.php} | 22 +++++++-------- ...orageTest.php => MockArrayStorageTest.php} | 10 +++---- ...torageTest.php => MockFileStorageTest.php} | 10 +++---- ...rageTest.php => NativeFileStorageTest.php} | 8 +++--- ...Test.php => NativeMemcacheStorageTest.php} | 8 +++--- ...est.php => NativeMemcachedStorageTest.php} | 8 +++--- ...geTest.php => NativeSqliteStorageTest.php} | 8 +++--- ...ionStorageTest.php => NullStorageTest.php} | 12 ++++---- .../Http/Firewall/ContextListenerTest.php | 4 +-- 35 files changed, 131 insertions(+), 127 deletions(-) rename src/Symfony/Component/HttpFoundation/Session/Storage/{AbstractSessionStorage.php => AbstractStorage.php} (98%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MemcacheSessionStorage.php => MemcacheStorage.php} (95%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MemcachedSessionStorage.php => MemcachedStorage.php} (95%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MockArraySessionStorage.php => MockArrayStorage.php} (92%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MockFileSessionStorage.php => MockFileStorage.php} (94%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeFileSessionStorage.php => NativeFileStorage.php} (89%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeMemcacheSessionStorage.php => NativeMemcacheStorage.php} (93%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeMemcachedSessionStorage.php => NativeMemcachedStorage.php} (92%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeSqliteSessionStorage.php => NativeSqliteStorage.php} (88%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NullSessionStorage.php => NullStorage.php} (90%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{PdoSessionStorage.php => PdoStorage.php} (97%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{SessionSaveHandlerInterface.php => SaveHandlerInterface.php} (99%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{SessionStorageInterface.php => StorageInterface.php} (98%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{AbstractSessionStorageTest.php => AbstractStorageTest.php} (79%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{MockArraySessionStorageTest.php => MockArrayStorageTest.php} (89%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{MockFileSessionStorageTest.php => MockFileStorageTest.php} (91%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeFileSessionStorageTest.php => NativeFileStorageTest.php} (73%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeMemcacheSessionStorageTest.php => NativeMemcacheStorageTest.php} (74%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeMemcachedSessionStorageTest.php => NativeMemcachedStorageTest.php} (78%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeSqliteSessionStorageTest.php => NativeSqliteStorageTest.php} (75%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NullSessionStorageTest.php => NullStorageTest.php} (74%) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 53fa9ac088..bcf7a4a4f1 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -230,7 +230,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * added ResponseHeaderBag::makeDisposition() (implements RFC 6266) * made mimetype to extension conversion configurable * [BC BREAK] Moved all session related classes and interfaces into own namespace, as - `Symfony\Component\HttpFoudation\Session`. + `Symfony\Component\HttpFoudation\Session` and renamed classes accordingly. * 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`. @@ -242,19 +242,23 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c `getFlash()`, `hasFlash()`, andd `removeFlash()`. `getFlashes() returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and attributes. `Session->getFlashes()->popAll()` clears flashes now. - * Added `AbstractSessionStorage` base class for session storage drivers. - * Added `SessionSaveHandler` interface which storage drivers should implement after inheriting from - `AbstractSessionStorage` when writing custom session save handlers. - * [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and `remove()`. Added - `getAttributes()`, `getFlashes()`. - * Moved attribute storage to `AttributeBagInterface`. - * Added `AttributeBag` to replicate attributes storage behaviour from 2.0.x (default). - * Added `NamespacedAttributeBag` for namespace session attributes. - * Session now implements `SessionInterface` making implementation customizable and portable. - * [BC BREAK] Removed `NativeSessionStorage` and replaced with `NativeFileSessionStorage`. + * Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` base class for + session storage drivers. + * Added `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` interface + which storage drivers should implement after inheriting from + `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` when writing custom session save handlers. + * [BC BREAK] `StorageInterface` methods removed: `write()`, `read()` and `remove()`. Added + `getBag()`, `registerBag()`. + * Moved attribute storage to `Symfony\Component\HttpFoundation\Attribute\AttributeBagInterface`. + * Added `Symfony\Component\HttpFoundation\Attribute\AttributeBag` to replicate attributes storage + behaviour from 2.0.x (default). + * Added `Symfony\Component\HttpFoundation\Attribute\NamespacedAttributeBag` for namespace session attributes. + * Session now implements `Symfony\Component\HttpFoundation\Session\SessionInterface` making + implementation customizable and portable. + * [BC BREAK] Removed `NativeStorage` and replaced with `NativeFileStorage`. * Added session storage drivers for PHP native Memcache, Memcached and SQLite session save handlers. * Added session storage drivers for custom Memcache, Memcached and Null session save handlers. - * Removed `FilesystemSessionStorage`, use `MockFileSessionStorage` for functional testing instead. + * Removed `FilesystemStorage`, use `MockFileStorage` for functional testing instead. ### HttpKernel diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 78169aacdf..8cccf48f00 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -313,9 +313,9 @@ UPGRADE FROM 2.0 to 2.1 * Session storage drivers Session storage drivers should inherit from - `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` + `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` and no longer should implement `read()`, `write()`, `remove()` which were removed from the - `SessionStorageInterface`. + `StorageInterface`. Any session storage driver that wants to use custom save handlers should implement `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index cda9dd2a0a..8595138dfc 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -3,7 +3,7 @@ namespace Symfony\Bridge\Doctrine\HttpFoundation; use Doctrine\DBAL\Platforms\MySqlPlatform; -use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage; use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface; use Doctrine\DBAL\Driver\Connection; @@ -13,7 +13,7 @@ use Doctrine\DBAL\Driver\Connection; * @author Fabien Potencier * @author Johannes M. Schmitt */ -class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +class DbalStorage extends AbstractStorage implements SessionSaveHandlerInterface { /** * @var Connection diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php index b33862588c..016f4e7a99 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php @@ -9,7 +9,7 @@ use Doctrine\DBAL\Schema\Schema; * * @author Johannes M. Schmitt */ -final class DbalSessionStorageSchema extends Schema +final class DbalStorageSchema extends Schema { private $tableName; diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 426e78cf5e..fd67ff74aa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -301,7 +301,7 @@ class FrameworkExtension extends Extension $this->addClassesToCompile(array( 'Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener', - 'Symfony\\Component\\HttpFoundation\\Session\Storage\\SessionStorageInterface', + 'Symfony\\Component\\HttpFoundation\\Session\Storage\\StorageInterface', $container->getDefinition('session')->getClass(), )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index 7e651594ec..b4808f9a72 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -8,14 +8,14 @@ Symfony\Component\HttpFoundation\Session\Session Symfony\Component\HttpFoundation\Session\Flash\FlashBag Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag - Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NullSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\MemcacheSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\MemcachedSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeFileStorage + Symfony\Component\HttpFoundation\Session\Storage\NullStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteStorage + Symfony\Component\HttpFoundation\Session\Storage\MemcacheStorage + Symfony\Component\HttpFoundation\Session\Storage\MemcachedStorage + Symfony\Component\HttpFoundation\Session\Storage\MockFileStorage Memcache Memcached diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index b9f6e41670..823d949091 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -13,7 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; @@ -26,7 +26,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $this->request = new Request(); - $session = new Session(new MockArraySessionStorage()); + $session = new Session(new MockArrayStorage()); $session->set('foobar', 'bar'); $session->getFlashes()->set(FlashBag::NOTICE, 'bar'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index 476b398e61..1917f50a2c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -15,7 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; @@ -64,7 +64,7 @@ class PhpEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new MockArraySessionStorage()); + $session = new Session(new MockArrayStorage()); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index f15ef3f945..71e2ce518e 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -15,7 +15,7 @@ use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; @@ -71,7 +71,7 @@ class TwigEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new MockArraySessionStorage()); + $session = new Session(new MockArrayStorage()); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 759bd45029..f4444b4db9 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -43,8 +43,8 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; * *
    *
  • request -> getRequestService()
  • - *
  • mysql_session_storage -> getMysqlSessionStorageService()
  • - *
  • symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()
  • + *
  • mysql_session_storage -> getMysqlStorageService()
  • + *
  • symfony.mysql_session_storage -> getSymfony_MysqlStorageService()
  • *
* * The container can have three possible behaviors when a service does not exist: diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 51cd368931..23526bd544 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -11,7 +11,7 @@ namespace Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; +use Symfony\Component\HttpFoundation\Session\Storage\StorageInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; @@ -31,18 +31,18 @@ class Session implements SessionInterface /** * Storage driver. * - * @var SessionStorageInterface + * @var StorageInterface */ protected $storage; /** * Constructor. * - * @param SessionStorageInterface $storage A SessionStorageInterface instance. + * @param StorageInterface $storage A StorageInterface 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, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) + public function __construct(StorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { $this->storage = $storage; $this->registerBag($attributes ?: new AttributeBag()); @@ -204,7 +204,7 @@ class Session implements SessionInterface /** * Implements the \Serialize interface. * - * @return SessionStorageInterface + * @return StorageInterface */ public function serialize() { @@ -214,13 +214,13 @@ class Session implements SessionInterface /** * Implements the \Serialize interface. * - * @throws \InvalidArgumentException If the passed string does not unserialize to an instance of SessionStorageInterface + * @throws \InvalidArgumentException If the passed string does not unserialize to an instance of StorageInterface */ public function unserialize($serialized) { $storage = unserialize($serialized); - if (!$storage instanceof SessionStorageInterface) { - throw new \InvalidArgumentException('Serialized data did not return a valid instance of SessionStorageInterface'); + if (!$storage instanceof StorageInterface) { + throw new \InvalidArgumentException('Serialized data did not return a valid instance of StorageInterface'); } $this->storage = $storage; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php index d50462eb87..40b066c70e 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php @@ -22,7 +22,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; * * @author Drak */ -abstract class AbstractSessionStorage implements SessionStorageInterface +abstract class AbstractStorage implements StorageInterface { /** * Array of SessionBagInterface @@ -297,7 +297,7 @@ abstract class AbstractSessionStorage implements SessionStorageInterface { // 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 SaveHandlerInterface) { session_set_save_handler( array($this, 'openSession'), array($this, 'closeSession'), diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheStorage.php index f5d5910d86..39c15f57df 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheStorage.php @@ -12,11 +12,11 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MemcacheSessionStorage. + * MemcacheStorage. * * @author Drak */ -class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +class MemcacheStorage extends AbstractStorage implements SaveHandlerInterface { /** * Memcache driver. @@ -46,7 +46,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSa * @param array $memcacheOptions An associative array of Memcachge options * @param array $options Session configuration options. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedStorage.php index f7041811a3..114a41e33d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedStorage.php @@ -12,11 +12,11 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MemcachedSessionStorage. + * MemcachedStorage. * * @author Drak */ -class MemcachedSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +class MemcachedStorage extends AbstractStorage implements SaveHandlerInterface { /** * Memcached driver. @@ -39,7 +39,7 @@ class MemcachedSessionStorage extends AbstractSessionStorage implements SessionS * @param array $memcachedOptions An associative array of Memcached options * @param array $options Session configuration options. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct(\Memcached $memcache, array $memcachedOptions = array(), array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArrayStorage.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MockArrayStorage.php index 2fd1a94c26..c762c8c396 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArrayStorage.php @@ -12,18 +12,18 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MockArraySessionStorage mocks the session for unit tests. + * MockArrayStorage mocks the session for unit tests. * * No PHP session is actually started since a session can be initialized * and shutdown only once per PHP execution cycle. * - * When doing functional testing, you should use MockFileSessionStorage instead. + * When doing functional testing, you should use MockFileStorage instead. * * @author Fabien Potencier * @author Bulat Shakirzyanov * @author Drak */ -class MockArraySessionStorage extends AbstractSessionStorage +class MockArrayStorage extends AbstractStorage { /** * @var string diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileStorage.php similarity index 94% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MockFileStorage.php index 094c4d6005..314bf8e5b2 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileStorage.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MockFileSessionStorage is used to mock sessions for + * MockFileStorage is used to mock sessions for * functional testing when done in a single PHP process. * * No PHP session is actually started since a session can be initialized @@ -20,7 +20,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; * * @author Drak */ -class MockFileSessionStorage extends MockArraySessionStorage +class MockFileStorage extends MockArrayStorage { /** * @var string @@ -33,7 +33,7 @@ class MockFileSessionStorage extends MockArraySessionStorage * @param string $savePath Path of directory to save session files. * @param array $options Session options. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct($savePath = null, array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileStorage.php similarity index 89% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileStorage.php index 09350e8d21..da0a24086e 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeFileSessionStorage. + * NativeFileStorage. * * Native session handler using PHP's built in file storage. * * @author Drak */ -class NativeFileSessionStorage extends AbstractSessionStorage +class NativeFileStorage extends AbstractStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeFileSessionStorage extends AbstractSessionStorage * @param string $savePath Path of directory to save session files. * @param array $options Session configuration options. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct($savePath = null, array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheStorage.php similarity index 93% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheStorage.php index 5572c47812..91871ea848 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeMemcacheSessionStorage. + * NativeMemcacheStorage. * * Session based on native PHP memcache database handler. * * @author Drak */ -class NativeMemcacheSessionStorage extends AbstractSessionStorage +class NativeMemcacheStorage extends AbstractStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeMemcacheSessionStorage extends AbstractSessionStorage * @param string $savePath Path of memcache server. * @param array $options Session configuration options. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedStorage.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedStorage.php index 50d8d060fd..f074e93ce1 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeMemcachedSessionStorage. + * NativeMemcachedStorage. * * Session based on native PHP memcached database handler. * * @author Drak */ -class NativeMemcachedSessionStorage extends AbstractSessionStorage +class NativeMemcachedStorage extends AbstractStorage { /** * @var string @@ -31,7 +31,7 @@ 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. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct($savePath = '127.0.0.1:11211', array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteStorage.php similarity index 88% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteStorage.php index 1dac36ac2f..125d5f22e4 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeSqliteSessionStorage. + * NativeSqliteStorage. * * Session based on native PHP sqlite database handler. * * @author Drak */ -class NativeSqliteSessionStorage extends AbstractSessionStorage +class NativeSqliteStorage extends AbstractStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeSqliteSessionStorage extends AbstractSessionStorage * @param string $dbPath Path to SQLite database file. * @param array $options Session configuration options. * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ public function __construct($dbPath, array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NullStorage.php similarity index 90% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NullStorage.php index b81fa8ae3a..bebedc53b6 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NullStorage.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NullSessionStorage. + * NullStorage. * * Can be used in unit testing or in a sitation where persisted sessions are not desired. * @@ -20,7 +20,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; * * @api */ -class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +class NullStorage extends AbstractStorage implements SaveHandlerInterface { /** * {@inheritdoc} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/PdoStorage.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/PdoStorage.php index 2015396b2f..a70152febf 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/PdoStorage.php @@ -12,12 +12,12 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * PdoSessionStorage. + * PdoStorage. * * @author Fabien Potencier * @author Michael Williams */ -class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +class PdoStorage extends AbstractStorage implements SaveHandlerInterface { /** * PDO instance. @@ -44,12 +44,12 @@ class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHan * * @throws \InvalidArgumentException When "db_table" option is not provided * - * @see AbstractSessionStorage::__construct() + * @see AbstractStorage::__construct() */ 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.'); + throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoStorage.'); } $this->pdo = $pdo; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SaveHandlerInterface.php similarity index 99% rename from src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/SaveHandlerInterface.php index ec4530a150..0bc74ed8bb 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SaveHandlerInterface.php @@ -52,7 +52,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; * * @author Drak */ -interface SessionSaveHandlerInterface +interface SaveHandlerInterface { /** * Open session. diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/StorageInterface.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/StorageInterface.php index a5afda9672..171dadc0bf 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/StorageInterface.php @@ -21,7 +21,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; * * @api */ -interface SessionStorageInterface +interface StorageInterface { /** * Starts the session. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index c147070fdf..89de12e988 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -12,7 +12,7 @@ namespace Symfony\Tests\Component\HttpFoundation; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Request; @@ -846,7 +846,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase $request = new Request; $this->assertFalse($request->hasSession()); - $request->setSession(new Session(new MockArraySessionStorage())); + $request->setSession(new Session(new MockArrayStorage())); $this->assertTrue($request->hasSession()); } @@ -857,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())); + $request->setSession(new Session(new MockArrayStorage())); $this->assertTrue($request->hasPreviousSession()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php index 71f2ec56cb..1b1cbd622c 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php @@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; /** * SessionTest @@ -28,7 +28,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; class SessionTest extends \PHPUnit_Framework_TestCase { /** - * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface + * @var \Symfony\Component\HttpFoundation\Session\Storage\StorageInterface */ protected $storage; @@ -39,7 +39,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->storage = new MockArraySessionStorage(); + $this->storage = new MockArrayStorage(); $this->session = new Session($this->storage, new AttributeBag(), new FlashBag()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractStorageTest.php similarity index 79% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractStorageTest.php index 05bc5fefb3..e31503fa3d 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractStorageTest.php @@ -2,21 +2,21 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage; 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\SaveHandlerInterface; /** - * Turn AbstractSessionStorage into something concrete because + * Turn AbstractStorage into something concrete because * certain mocking features are broken in PHPUnit-Mock-Objects < 1.1.2 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73 */ -class ConcreteSessionStorage extends AbstractSessionStorage +class ConcreteStorage extends AbstractStorage { } -class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface +class CustomHandlerStorage extends AbstractStorage implements SaveHandlerInterface { public function openSession($path, $id) { @@ -44,7 +44,7 @@ class CustomHandlerSessionStorage extends AbstractSessionStorage implements Sess } /** - * Test class for AbstractSessionStorage. + * Test class for AbstractStorage. * * @author Drak * @@ -52,14 +52,14 @@ class CustomHandlerSessionStorage extends AbstractSessionStorage implements Sess * * @runTestsInSeparateProcesses */ -class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase +class AbstractStorageTest extends \PHPUnit_Framework_TestCase { /** - * @return AbstractSessionStorage + * @return AbstractStorage */ protected function getStorage() { - $storage = new CustomHandlerSessionStorage(); + $storage = new CustomHandlerStorage(); $storage->registerBag(new AttributeBag); return $storage; @@ -115,13 +115,13 @@ class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase public function testCustomSaveHandlers() { - $storage = new CustomHandlerSessionStorage(); + $storage = new CustomHandlerStorage(); $this->assertEquals('user', ini_get('session.save_handler')); } public function testNativeSaveHandlers() { - $storage = new ConcreteSessionStorage(); + $storage = new ConcreteStorage(); $this->assertNotEquals('user', ini_get('session.save_handler')); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArraySessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php similarity index 89% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArraySessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php index e2290f9459..33b43a4da2 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArraySessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php @@ -2,20 +2,20 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** - * Test class for MockArraySessionStorage. + * Test class for MockArrayStorage. * * @author Drak */ -class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase +class MockArrayStorageTest extends \PHPUnit_Framework_TestCase { /** - * @var MockArraySessionStorage + * @var MockArrayStorage */ private $storage; @@ -41,7 +41,7 @@ class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase $this->flashes->getStorageKey() => array('notice' => 'hello'), ); - $this->storage = new MockArraySessionStorage(); + $this->storage = new MockArrayStorage(); $this->storage->registerBag($this->flashes); $this->storage->registerBag($this->attributes); $this->storage->setSessionData($this->data); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php similarity index 91% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php index 33a0d54844..3ccc139605 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php @@ -2,16 +2,16 @@ namespace Symfony\Test\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockFileStorage; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; /** - * Test class for MockFileSessionStorage. + * Test class for MockFileStorage. * * @author Drak */ -class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase +class MockFileStorageTest extends \PHPUnit_Framework_TestCase { /** * @var string @@ -19,7 +19,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase private $sessionDir; /** - * @var FileMockSessionStorage + * @var FileMockStorage */ protected $storage; @@ -96,7 +96,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase private function getStorage(array $options = array()) { - $storage = new MockFileSessionStorage($this->sessionDir, $options); + $storage = new MockFileStorage($this->sessionDir, $options); $storage->registerBag(new FlashBag); $storage->registerBag(new AttributeBag); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php similarity index 73% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php index 2231c37fdd..f6f0ca6e91 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php @@ -2,22 +2,22 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeFileStorage; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** - * Test class for NativeFileSessionStorage. + * Test class for NativeFileStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeFileSessionStorageTest extends \PHPUnit_Framework_TestCase +class NativeFileStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { - $storage = new NativeFileSessionStorage(sys_get_temp_dir(), array('name' => 'TESTING')); + $storage = new NativeFileStorage(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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php similarity index 74% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php index 23145e2d14..568955af77 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php @@ -2,18 +2,18 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheStorage; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** - * Test class for NativeMemcacheSessionStorage. + * Test class for NativeMemcacheStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeMemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase +class NativeMemcacheStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { @@ -21,7 +21,7 @@ class NativeMemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Skipped tests SQLite extension is not present'); } - $storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0', array('name' => 'TESTING')); + $storage = new NativeMemcacheStorage('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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php similarity index 78% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php index 7cb81ea811..9e9092eb9d 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php @@ -2,18 +2,18 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedStorage; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** - * Test class for NativeMemcachedSessionStorage. + * Test class for NativeMemcachedStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeMemcachedSessionStorageTest extends \PHPUnit_Framework_TestCase +class NativeMemcachedStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { @@ -24,7 +24,7 @@ class NativeMemcachedSessionStorageTest extends \PHPUnit_Framework_TestCase // 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')); + $storage = new NativeMemcachedStorage('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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php similarity index 75% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php index 12a9b284a0..54dc6574ad 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php @@ -2,18 +2,18 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteStorage; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** - * Test class for NativeSqliteSessionStorage. + * Test class for NativeSqliteStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeSqliteSessionStorageTest extends \PHPUnit_Framework_TestCase +class NativeSqliteStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { @@ -21,7 +21,7 @@ class NativeSqliteSessionStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Skipped tests SQLite extension is not present'); } - $storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db', array('name' => 'TESTING')); + $storage = new NativeSqliteStorage(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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullStorageTest.php similarity index 74% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullSessionStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullStorageTest.php index 66599f68b3..9abd333d3c 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullStorageTest.php @@ -1,28 +1,28 @@ * * @runTestsInSeparateProcesses */ -class NullSessionStorageTest extends \PHPUnit_Framework_TestCase +class NullStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { - $storage = new NullSessionStorage(); + $storage = new NullStorage(); $this->assertEquals('user', ini_get('session.save_handler')); } public function testSession() { session_id('nullsessionstorage'); - $storage = new NullSessionStorage(); + $storage = new NullStorage(); $session = new Session($storage); $this->assertNull($session->get('something')); $session->set('something', 'unique'); @@ -32,7 +32,7 @@ class NullSessionStorageTest extends \PHPUnit_Framework_TestCase public function testNothingIsPersisted() { session_id('nullsessionstorage'); - $storage = new NullSessionStorage(); + $storage = new NullStorage(); $session = new Session($storage); $session->start(); $this->assertEquals('nullsessionstorage', $session->getId()); diff --git a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php index 5c810062ef..bac4fa52e6 100644 --- a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php +++ b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php @@ -5,7 +5,7 @@ namespace Symfony\Test\Component\Security\Http\Firewall; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; @@ -63,7 +63,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase protected function runSessionOnKernelResponse($newToken, $original = null) { - $session = new Session(new MockArraySessionStorage()); + $session = new Session(new MockArrayStorage()); if ($original !== null) { $session->set('_security_session', $original); From dad60efccc23d269202658c44bbe933217116971 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 10 Feb 2012 15:30:15 +0545 Subject: [PATCH 18/31] [HttpFoundation] Add back get defaults and small clean-up. Changed read-only method names from get*() to peek*() Typo --- .../HttpFoundation/DbalSessionStorage.php | 4 +- .../Templating/Helper/SessionHelper.php | 6 +-- .../Templating/Helper/SessionHelperTest.php | 4 ++ .../Session/Flash/AutoExpireFlashBag.php | 14 ++---- .../HttpFoundation/Session/Flash/FlashBag.php | 34 ++++++------- .../Session/Flash/FlashBagInterface.php | 10 ++-- .../Session/SessionInterface.php | 7 --- .../Session/Flash/AutoExpireFlashBagTest.php | 49 ++++++++----------- .../Session/Flash/FlashBagTest.php | 49 ++++++------------- .../HttpFoundation/Session/SessionTest.php | 6 --- .../Session/Storage/MockArrayStorageTest.php | 4 +- .../Session/Storage/MockFileStorageTest.php | 2 +- 12 files changed, 73 insertions(+), 116 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 8595138dfc..5d2c5d4d1a 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -4,7 +4,7 @@ namespace Symfony\Bridge\Doctrine\HttpFoundation; use Doctrine\DBAL\Platforms\MySqlPlatform; use Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage; -use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface; use Doctrine\DBAL\Driver\Connection; /** @@ -13,7 +13,7 @@ use Doctrine\DBAL\Driver\Connection; * @author Fabien Potencier * @author Johannes M. Schmitt */ -class DbalStorage extends AbstractStorage implements SessionSaveHandlerInterface +class DbalStorage extends AbstractStorage implements SaveHandlerInterface { /** * @var Connection diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index 684e5d92f5..15e25c460d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -47,14 +47,14 @@ class SessionHelper extends Helper return $this->session->get($name, $default); } - public function getFlash($type) + public function getFlash($type, $default = null) { - return $this->session->getFlashes()->get($type); + return $this->session->getFlashes()->pop($type); } public function getFlashes() { - return $this->session->getFlashes()->all(); + return $this->session->getFlashes()->popAll(); } public function hasFlash($type) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index 823d949091..e492829897 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -45,7 +45,11 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase $this->assertTrue($helper->hasFlash(FlashBag::NOTICE)); $this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE)); + } + public function testGetFlashes() + { + $helper = new SessionHelper($this->request); $this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes()); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php index e69992568d..39c068edcb 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php @@ -75,19 +75,15 @@ class AutoExpireFlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function get($type) + public function peek($type, $default = null) { - if (!$this->has($type)) { - throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); - } - - return $this->flashes['display'][$type]; + return $this->has($type) ? $this->flashes['display'][$type] : $default; } /** * {@inheritdoc} */ - public function all() + public function peekAll() { return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array(); } @@ -95,10 +91,10 @@ class AutoExpireFlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function pop($type) + public function pop($type, $default = null) { if (!$this->has($type)) { - throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); + return $default; } $return = null; diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php index 99508f760f..1ec7175847 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php @@ -68,27 +68,15 @@ class FlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function get($type) + public function peek($type, $default = null) { - if (!$this->has($type)) { - throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); - } - - return $this->flashes[$type]; + return $this->has($type) ? $this->flashes[$type] : $default; } /** * {@inheritdoc} */ - public function set($type, $message) - { - $this->flashes[$type] = $message; - } - - /** - * {@inheritdoc} - */ - public function all() + public function peekAll() { return $this->flashes; } @@ -96,13 +84,13 @@ class FlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function pop($type) + public function pop($type, $default = null) { if (!$this->has($type)) { - throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type)); + return $default; } - $return = $this->get($type); + $return = $this->peek($type); unset($this->flashes[$type]); return $return; @@ -113,12 +101,20 @@ class FlashBag implements FlashBagInterface */ public function popAll() { - $return = $this->all(); + $return = $this->peekAll(); $this->flashes = array(); return $return; } + /** + * {@inheritdoc} + */ + public function set($type, $message) + { + $this->flashes[$type] = $message; + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php index 262f624e80..51b4927a6b 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php @@ -36,27 +36,29 @@ interface FlashBagInterface extends SessionBagInterface /** * Gets flash message for a given type. * - * @param string $type Message category type. + * @param string $type Message category type. + * @param string $default Default value if $type doee not exist. * * @return string */ - function get($type); + function peek($type, $default = null); /** * Gets all flash messages. * * @return array */ - function all(); + function peekAll(); /** * Pops and clears flash from the stack. * * @param string $type + * @param string $default Default value if $type doee not exist. * * @return string */ - function pop($type); + function pop($type, $default = null); /** * Pops and clears flashes from the stack. diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 8bb013514b..b2c4b8e00b 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -108,11 +108,4 @@ interface SessionInterface extends \Serializable * Clears all attributes. */ function clear(); - - /** - * Gets the flashbag interface. - * - * @return FlashBagInterface - */ - function getFlashes(); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php index be47a83003..b7850ef165 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php @@ -50,34 +50,28 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase $bag = new FlashBag(); $array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); $bag->initialize($array); - $this->assertEquals('A previous flash message', $bag->get(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $bag->peek(FlashBag::NOTICE)); $array = array('new' => array( FlashBag::NOTICE => 'Something else', FlashBag::ERROR => 'a', )); $bag->initialize($array); - $this->assertEquals('Something else', $bag->get(FlashBag::NOTICE)); - $this->assertEquals('a', $bag->get(FlashBag::ERROR)); + $this->assertEquals('Something else', $bag->peek(FlashBag::NOTICE)); + $this->assertEquals('a', $bag->peek(FlashBag::ERROR)); } - public function testGet() + public function testPeek() { - $this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE)); - $this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE)); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testGetException() - { - $this->bag->get('non_existing_type'); + $this->assertNull($this->bag->peek('non_existing')); + $this->assertEquals('default', $this->bag->peek('non_existing', 'default')); + $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); } public function testSet() { $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->assertNotEquals('Foo', $this->bag->get(FlashBag::NOTICE)); + $this->assertNotEquals('Foo', $this->bag->peek(FlashBag::NOTICE)); } public function testHas() @@ -91,7 +85,7 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); } - public function testAll() + public function testPeekAll() { $array = array( 'new' => array( @@ -104,25 +98,22 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array( FlashBag::NOTICE => 'Foo', FlashBag::ERROR => 'Bar', - ), $this->bag->all() + ), $this->bag->peekAll() + ); + + $this->assertEquals(array( + FlashBag::NOTICE => 'Foo', + FlashBag::ERROR => 'Bar', + ), $this->bag->peekAll() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testPop() { + $this->assertNull($this->bag->pop('non_existing')); + $this->assertEquals('default', $this->bag->pop('non_existing', 'default')); $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - $this->bag->pop(FlashBag::NOTICE); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testPopException() - { - $this->bag->pop('non_existing_type'); + $this->assertNull($this->bag->pop(FlashBag::NOTICE)); } public function testPopAll() diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php index 5bd8f4b01d..8f173a7827 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php @@ -49,45 +49,26 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase { $bag = new FlashBag(); $bag->initialize($this->array); - $this->assertEquals($this->array, $bag->all()); + $this->assertEquals($this->array, $bag->peekAll()); $array = array('should' => array('change')); $bag->initialize($array); - $this->assertEquals($array, $bag->all()); + $this->assertEquals($array, $bag->peekAll()); } - public function testGet() + public function testPeek() { - $this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE)); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testGetException() - { - $this->bag->get('non_existing_type'); + $this->assertNull($this->bag->peek('non_existing')); + $this->assertEquals('default', $this->bag->peek('not_existing', 'default')); + $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); } public function testPop() { + $this->assertNull($this->bag->pop('non_existing')); + $this->assertEquals('default', $this->bag->pop('not_existing', 'default')); $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testPopException() - { - $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - $this->bag->pop(FlashBag::NOTICE); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testPopExceptionNotExisting() - { - $this->bag->pop('non_existing_type'); + $this->assertNull($this->bag->pop(FlashBag::NOTICE)); } public function testPopAll() @@ -102,11 +83,11 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(), $this->bag->popAll()); } - public function testset() + public function testSet() { $this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set(FlashBag::NOTICE, 'Bar'); - $this->assertEquals('Bar', $this->bag->get(FlashBag::NOTICE)); + $this->assertEquals('Bar', $this->bag->peek(FlashBag::NOTICE)); } public function testHas() @@ -120,21 +101,21 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); } - public function testAll() + public function testPeekAll() { $this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set(FlashBag::ERROR, 'Bar'); $this->assertEquals(array( FlashBag::NOTICE => 'Foo', FlashBag::ERROR => 'Bar', - ), $this->bag->all() + ), $this->bag->peekAll() ); $this->assertTrue($this->bag->has(FlashBag::NOTICE)); $this->assertTrue($this->bag->has(FlashBag::ERROR)); $this->assertEquals(array( FlashBag::NOTICE => 'Foo', FlashBag::ERROR => 'Bar', - ), $this->bag->all() + ), $this->bag->peekAll() ); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php index 1b1cbd622c..41c714ea59 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php @@ -123,28 +123,22 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function testInvalidate() { $this->session->set('invalidate', 123); - $this->session->getFlashes()->set(FlashBag::NOTICE, 'OK'); $this->session->invalidate(); $this->assertEquals(array(), $this->session->all()); - $this->assertEquals(array(), $this->session->getFlashes()->all()); } public function testMigrate() { $this->session->set('migrate', 321); - $this->session->getFlashes()->set(FlashBag::NOTICE, 'HI'); $this->session->migrate(); $this->assertEquals(321, $this->session->get('migrate')); - $this->assertEquals('HI', $this->session->getFlashes()->get(FlashBag::NOTICE)); } public function testMigrateDestroy() { $this->session->set('migrate', 333); - $this->session->getFlashes()->set(FlashBag::NOTICE, 'Bye'); $this->session->migrate(true); $this->assertEquals(333, $this->session->get('migrate')); - $this->assertEquals('Bye', $this->session->getFlashes()->get(FlashBag::NOTICE)); } public function testSerialize() diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php index 33b43a4da2..29d9078532 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php @@ -72,13 +72,13 @@ class MockArrayStorageTest extends \PHPUnit_Framework_TestCase $this->storage->regenerate(); $this->assertNotEquals($id, $this->storage->getId()); $this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all()); - $this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->all()); + $this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->peekAll()); $id = $this->storage->getId(); $this->storage->regenerate(true); $this->assertNotEquals($id, $this->storage->getId()); $this->assertEquals(array('foo' => 'bar'), $this->storage->getBag('attributes')->all()); - $this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->all()); + $this->assertEquals(array('notice' => 'hello'), $this->storage->getBag('flashes')->peekAll()); } public function testGetId() diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php index 3ccc139605..337faf4ca0 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php @@ -79,7 +79,7 @@ class MockFileStorageTest extends \PHPUnit_Framework_TestCase $storage->start(); $this->assertEquals('108', $storage->getBag('attributes')->get('new')); $this->assertTrue($storage->getBag('flashes')->has('newkey')); - $this->assertEquals('test', $storage->getBag('flashes')->get('newkey')); + $this->assertEquals('test', $storage->getBag('flashes')->peek('newkey')); } public function testMultipleInstances() From 0d2745f750db638d0e63252b1c56ac55ff82abb6 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 10 Feb 2012 15:38:21 +0545 Subject: [PATCH 19/31] [HttpFoundation] Remove constants from FlashBagInterface As requested by fabpot. Corrected a few mistakes in the documentation. --- CHANGELOG-2.1.md | 3 -- UPGRADE-2.1.md | 24 +++------- .../Templating/Helper/SessionHelperTest.php | 8 ++-- .../Session/Flash/FlashBagInterface.php | 5 -- .../Session/Flash/AutoExpireFlashBagTest.php | 48 +++++++++---------- .../Session/Flash/FlashBagTest.php | 44 ++++++++--------- 6 files changed, 56 insertions(+), 76 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index bcf7a4a4f1..69e632d86f 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -231,9 +231,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * made mimetype to extension conversion configurable * [BC BREAK] Moved all session related classes and interfaces into own namespace, as `Symfony\Component\HttpFoudation\Session` and renamed classes accordingly. - * 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 `pop()` or `popAll()`. This makes the implementation ESI compatible. * Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 8cccf48f00..76204f8bfd 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -259,21 +259,14 @@ UPGRADE FROM 2.0 to 2.1
- If you wanted to process all flash types you could also make use of the `getFlashes()->all()` API: + If you wanted to process all flash types you could also make use of the `getFlashes()->popAll()` API: - getFlashes()->all() as $type => $flash): ?> + getFlashes()->popAll() as $type => $flash): ?>
-.. note:: - - The Flash Message API provides constants which you can optionally use. For example - `Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE`, which can also be abbreviated to - `FlashBag::NOTICE` providing you declare `` - at the beginning of the PHP template. - Before (Twig): {% if app.session.hasFlash('notice') %} @@ -284,25 +277,20 @@ UPGRADE FROM 2.0 to 2.1 After (Twig): - {% if app.session.getFlashes.has('notice') %} + {% if app.session.getFlashes().has('notice') %}
- {{ app.session.getFlashes.pop('notice') }} + {{ app.session.getFlashes().pop('notice') }}
{% endif %} Again you can process all flash messages in one go with - {% for type, flashMessage in app.session.getFlashes.popAll() %} + {% for type, flashMessage in app.session.getFlashes().popAll() %}
{{ flashMessage }}
{% endforeach %} -.. note:: - - You can optionally use constants in Twig templates using `constant()` e.g. - `constant('Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE')`. - * Session object The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()` @@ -318,7 +306,7 @@ UPGRADE FROM 2.0 to 2.1 `StorageInterface`. Any session storage driver that wants to use custom save handlers should - implement `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` + implement `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` ### [FrameworkBundle] diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index e492829897..b95161c2ec 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -28,7 +28,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase $session = new Session(new MockArrayStorage()); $session->set('foobar', 'bar'); - $session->getFlashes()->set(FlashBag::NOTICE, 'bar'); + $session->getFlashes()->set('notice', 'bar'); $this->request->setSession($session); } @@ -42,15 +42,15 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $helper = new SessionHelper($this->request); - $this->assertTrue($helper->hasFlash(FlashBag::NOTICE)); + $this->assertTrue($helper->hasFlash('notice')); - $this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE)); + $this->assertEquals('bar', $helper->getFlash('notice')); } public function testGetFlashes() { $helper = new SessionHelper($this->request); - $this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes()); + $this->assertEquals(array('notice' => 'bar'), $helper->getFlashes()); } public function testGet() diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php index 51b4927a6b..5cca797f7d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php @@ -20,11 +20,6 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; */ interface FlashBagInterface extends SessionBagInterface { - const INFO = 'info'; - const NOTICE = 'notice'; - const WARNING = 'warning'; - const ERROR = 'error'; - /** * Registers a message for a given type. * diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php index b7850ef165..a81017c677 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php @@ -35,7 +35,7 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->bag = new FlashBag(); - $this->array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); + $this->array = array('new' => array('notice' => 'A previous flash message')); $this->bag->initialize($this->array); } @@ -48,62 +48,62 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase public function testInitialize() { $bag = new FlashBag(); - $array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); + $array = array('new' => array('notice' => 'A previous flash message')); $bag->initialize($array); - $this->assertEquals('A previous flash message', $bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $bag->peek('notice')); $array = array('new' => array( - FlashBag::NOTICE => 'Something else', - FlashBag::ERROR => 'a', + 'notice' => 'Something else', + 'error' => 'a', )); $bag->initialize($array); - $this->assertEquals('Something else', $bag->peek(FlashBag::NOTICE)); - $this->assertEquals('a', $bag->peek(FlashBag::ERROR)); + $this->assertEquals('Something else', $bag->peek('notice')); + $this->assertEquals('a', $bag->peek('error')); } public function testPeek() { $this->assertNull($this->bag->peek('non_existing')); $this->assertEquals('default', $this->bag->peek('non_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); } public function testSet() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->assertNotEquals('Foo', $this->bag->peek(FlashBag::NOTICE)); + $this->bag->set('notice', 'Foo'); + $this->assertNotEquals('Foo', $this->bag->peek('notice')); } public function testHas() { $this->assertFalse($this->bag->has('nothing')); - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertTrue($this->bag->has('notice')); } public function testKeys() { - $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); + $this->assertEquals(array('notice'), $this->bag->keys()); } public function testPeekAll() { $array = array( 'new' => array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), ); $this->bag->initialize($array); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); } @@ -112,16 +112,16 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase { $this->assertNull($this->bag->pop('non_existing')); $this->assertEquals('default', $this->bag->pop('non_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - $this->assertNull($this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->pop('notice')); + $this->assertNull($this->bag->pop('notice')); } public function testPopAll() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::ERROR, 'Bar'); + $this->bag->set('notice', 'Foo'); + $this->bag->set('error', 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => 'A previous flash message', + 'notice' => 'A previous flash message', ), $this->bag->popAll() ); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php index 8f173a7827..524106f509 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php @@ -35,7 +35,7 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->bag = new FlashBag(); - $this->array = array(FlashBag::NOTICE => 'A previous flash message'); + $this->array = array('notice' => 'A previous flash message'); $this->bag->initialize($this->array); } @@ -59,25 +59,25 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase { $this->assertNull($this->bag->peek('non_existing')); $this->assertEquals('default', $this->bag->peek('not_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); } public function testPop() { $this->assertNull($this->bag->pop('non_existing')); $this->assertEquals('default', $this->bag->pop('not_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - $this->assertNull($this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->pop('notice')); + $this->assertNull($this->bag->pop('notice')); } public function testPopAll() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::ERROR, 'Bar'); + $this->bag->set('notice', 'Foo'); + $this->bag->set('error', 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar'), $this->bag->popAll() + 'notice' => 'Foo', + 'error' => 'Bar'), $this->bag->popAll() ); $this->assertEquals(array(), $this->bag->popAll()); @@ -85,36 +85,36 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase public function testSet() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::NOTICE, 'Bar'); - $this->assertEquals('Bar', $this->bag->peek(FlashBag::NOTICE)); + $this->bag->set('notice', 'Foo'); + $this->bag->set('notice', 'Bar'); + $this->assertEquals('Bar', $this->bag->peek('notice')); } public function testHas() { $this->assertFalse($this->bag->has('nothing')); - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertTrue($this->bag->has('notice')); } public function testKeys() { - $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); + $this->assertEquals(array('notice'), $this->bag->keys()); } public function testPeekAll() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::ERROR, 'Bar'); + $this->bag->set('notice', 'Foo'); + $this->bag->set('error', 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); - $this->assertTrue($this->bag->has(FlashBag::ERROR)); + $this->assertTrue($this->bag->has('notice')); + $this->assertTrue($this->bag->has('error')); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); } From 7878a0a11aef9e0865ff5e68959a7cfa66a5d28e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 11:45:20 +0100 Subject: [PATCH 20/31] [HttpFoundation] renamed pop() to all() and getAll() to all() --- CHANGELOG-2.1.md | 6 +++--- UPGRADE-2.1.md | 14 +++++++------- .../Templating/Helper/SessionHelper.php | 4 ++-- .../TestBundle/Controller/SessionController.php | 2 +- .../Session/Flash/AutoExpireFlashBag.php | 6 +++--- .../HttpFoundation/Session/Flash/FlashBag.php | 9 +++++---- .../Session/Flash/FlashBagInterface.php | 8 ++++---- .../Session/Flash/AutoExpireFlashBagTest.php | 16 ++++++++-------- .../Session/Flash/FlashBagTest.php | 16 ++++++++-------- 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 69e632d86f..b08ac98781 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -231,14 +231,14 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * made mimetype to extension conversion configurable * [BC BREAK] Moved all session related classes and interfaces into own namespace, as `Symfony\Component\HttpFoudation\Session` and renamed classes accordingly. - * Added `FlashBag` (default). Flashes expire when retrieved by `pop()` or `popAll()`. + * Added `FlashBag` (default). Flashes expire when retrieved by `get()` or `all()`. 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()`. + after one page page load. Messages must be retrived by `get()` or `all()`. * [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()` `getFlash()`, `hasFlash()`, andd `removeFlash()`. `getFlashes() returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and - attributes. `Session->getFlashes()->popAll()` clears flashes now. + attributes. `Session->getFlashes()->all()` clears flashes now. * Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` base class for session storage drivers. * Added `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` interface diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 76204f8bfd..641c967c96 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -255,13 +255,13 @@ UPGRADE FROM 2.0 to 2.1 getFlashes()->has('notice')): ?>
- getFlashes()->pop('notice') ?> + getFlashes()->get('notice') ?>
- If you wanted to process all flash types you could also make use of the `getFlashes()->popAll()` API: + If you wanted to process all flash types you could also make use of the `getFlashes()->all()` API: - getFlashes()->popAll() as $type => $flash): ?> + getFlashes()->all() as $type => $flash): ?>
@@ -277,15 +277,15 @@ UPGRADE FROM 2.0 to 2.1 After (Twig): - {% if app.session.getFlashes().has('notice') %} + {% if app.session.flashes.has('notice') %}
- {{ app.session.getFlashes().pop('notice') }} + {{ app.session.flashes.get('notice') }}
{% endif %} Again you can process all flash messages in one go with - {% for type, flashMessage in app.session.getFlashes().popAll() %} + {% for type, flashMessage in app.session.flashes.all() %}
{{ flashMessage }}
@@ -295,7 +295,7 @@ UPGRADE FROM 2.0 to 2.1 The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()` have been removed from the `Session` object. `getFlashes()` now returns a `FlashBagInterface`. - Flashes should be popped off the stack using `getFlashes()->pop()` or `getFlashes()->popAll()` + Flashes should be popped off the stack using `getFlashes()->get()` or `getFlashes()->all()` to get all flashes in one go. * Session storage drivers diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index 15e25c460d..4a7842efd9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -49,12 +49,12 @@ class SessionHelper extends Helper public function getFlash($type, $default = null) { - return $this->session->getFlashes()->pop($type); + return $this->session->getFlashes()->get($type); } public function getFlashes() { - return $this->session->getFlashes()->popAll(); + return $this->session->getFlashes()->all(); } public function hasFlash($type) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php index 0a1aa0c66c..626c2e2cbd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php @@ -62,7 +62,7 @@ class SessionController extends ContainerAware $session = $request->getSession(); if ($session->getFlashes()->has('notice')) { - $output = $session->getFlashes()->pop('notice'); + $output = $session->getFlashes()->get('notice'); } else { $output = 'No flash was set.'; } diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php index 39c068edcb..1c7b9e9d07 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php @@ -91,7 +91,7 @@ class AutoExpireFlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function pop($type, $default = null) + public function get($type, $default = null) { if (!$this->has($type)) { return $default; @@ -113,7 +113,7 @@ class AutoExpireFlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function popAll() + public function all() { $return = $this->flashes['display']; $this->flashes = array('new' => array(), 'display' => array()); @@ -166,7 +166,7 @@ class AutoExpireFlashBag implements FlashBagInterface */ public function clear() { - $return = $this->popAll(); + $return = $this->all(); $this->flashes = array('display' => array(), 'new' => array()); return $return; diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php index 1ec7175847..cbb77af03d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php @@ -84,13 +84,14 @@ class FlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function pop($type, $default = null) + public function get($type, $default = null) { if (!$this->has($type)) { return $default; } - $return = $this->peek($type); + $return = $this->flashes[$type]; + unset($this->flashes[$type]); return $return; @@ -99,7 +100,7 @@ class FlashBag implements FlashBagInterface /** * {@inheritdoc} */ - public function popAll() + public function all() { $return = $this->peekAll(); $this->flashes = array(); @@ -152,6 +153,6 @@ class FlashBag implements FlashBagInterface */ public function clear() { - return $this->popAll(); + return $this->all(); } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php index 5cca797f7d..0c45d74bb7 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php @@ -46,21 +46,21 @@ interface FlashBagInterface extends SessionBagInterface function peekAll(); /** - * Pops and clears flash from the stack. + * Gets and clears flash from the stack. * * @param string $type * @param string $default Default value if $type doee not exist. * * @return string */ - function pop($type, $default = null); + function get($type, $default = null); /** - * Pops and clears flashes from the stack. + * Gets and clears flashes from the stack. * * @return array */ - function popAll(); + function all(); /** * Sets all flash messages. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php index a81017c677..3b12c0eaea 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php @@ -108,23 +108,23 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase ); } - public function testPop() + public function testGet() { - $this->assertNull($this->bag->pop('non_existing')); - $this->assertEquals('default', $this->bag->pop('non_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->pop('notice')); - $this->assertNull($this->bag->pop('notice')); + $this->assertNull($this->bag->get('non_existing')); + $this->assertEquals('default', $this->bag->get('non_existing', 'default')); + $this->assertEquals('A previous flash message', $this->bag->get('notice')); + $this->assertNull($this->bag->get('notice')); } - public function testPopAll() + public function testAll() { $this->bag->set('notice', 'Foo'); $this->bag->set('error', 'Bar'); $this->assertEquals(array( 'notice' => 'A previous flash message', - ), $this->bag->popAll() + ), $this->bag->all() ); - $this->assertEquals(array(), $this->bag->popAll()); + $this->assertEquals(array(), $this->bag->all()); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php index 524106f509..3616ec9156 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php @@ -63,24 +63,24 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals('A previous flash message', $this->bag->peek('notice')); } - public function testPop() + public function testGet() { - $this->assertNull($this->bag->pop('non_existing')); - $this->assertEquals('default', $this->bag->pop('not_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->pop('notice')); - $this->assertNull($this->bag->pop('notice')); + $this->assertNull($this->bag->get('non_existing')); + $this->assertEquals('default', $this->bag->get('not_existing', 'default')); + $this->assertEquals('A previous flash message', $this->bag->get('notice')); + $this->assertNull($this->bag->get('notice')); } - public function testPopAll() + public function testAll() { $this->bag->set('notice', 'Foo'); $this->bag->set('error', 'Bar'); $this->assertEquals(array( 'notice' => 'Foo', - 'error' => 'Bar'), $this->bag->popAll() + 'error' => 'Bar'), $this->bag->all() ); - $this->assertEquals(array(), $this->bag->popAll()); + $this->assertEquals(array(), $this->bag->all()); } public function testSet() From 04942502a5729da81698e9bfd2e069fe2e91e1f4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 11:47:51 +0100 Subject: [PATCH 21/31] removed unused use statements --- .../FrameworkBundle/Templating/Helper/SessionHelper.php | 1 - .../Tests/Templating/Helper/SessionHelperTest.php | 2 -- .../Component/HttpFoundation/Session/SessionInterface.php | 2 -- .../HttpFoundation/Session/Storage/AbstractStorage.php | 4 ---- .../Tests/Component/HttpFoundation/Session/SessionTest.php | 2 -- .../HttpFoundation/Session/Storage/NativeFileStorageTest.php | 2 -- .../Session/Storage/NativeMemcacheStorageTest.php | 2 -- .../Session/Storage/NativeMemcachedStorageTest.php | 2 -- .../Session/Storage/NativeSqliteStorageTest.php | 2 -- 9 files changed, 19 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index 4a7842efd9..f38403c181 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -13,7 +13,6 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** * SessionHelper provides read-only access to the session attributes. diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index b95161c2ec..9ba8496d92 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -15,8 +15,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; class SessionHelperTest extends \PHPUnit_Framework_TestCase { diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index b2c4b8e00b..1b4b7bfdbc 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -11,8 +11,6 @@ namespace Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\Session\Storage\AttributeInterface; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** * Interface for the session. diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php index 40b066c70e..1b17939c59 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php @@ -11,10 +11,6 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php index 41c714ea59..4499fce137 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php @@ -13,9 +13,7 @@ namespace Symfony\Tests\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; /** diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php index f6f0ca6e91..e44ae44170 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php @@ -3,8 +3,6 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\Storage\NativeFileStorage; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** * Test class for NativeFileStorage. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php index 568955af77..18a0276001 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php @@ -3,8 +3,6 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheStorage; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** * Test class for NativeMemcacheStorage. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php index 9e9092eb9d..4cc691df72 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php @@ -3,8 +3,6 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedStorage; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** * Test class for NativeMemcachedStorage. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php index 54dc6574ad..c8a7cacbe3 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php @@ -3,8 +3,6 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteStorage; -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** * Test class for NativeSqliteStorage. From 91f4f8aa3753b289f4ec5d453e721103393279df Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 11:53:48 +0100 Subject: [PATCH 22/31] [HttpFoundation] changed default flash bag to auto-expires to keep BC --- src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index b4808f9a72..e0c5a7d816 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -6,7 +6,7 @@ Symfony\Component\HttpFoundation\Session\Session - Symfony\Component\HttpFoundation\Session\Flash\FlashBag + Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag Symfony\Component\HttpFoundation\Session\Storage\NativeFileStorage Symfony\Component\HttpFoundation\Session\Storage\NullStorage From 74ccf7062a8ca0e6ea3ec664a833c67ab4bed6ea Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 12:04:05 +0100 Subject: [PATCH 23/31] reverted 5b7ef116507fa05e1042065f61a50733c19116cb (Simplify session storage class names now we have a separate namespace for sessions) --- CHANGELOG-2.1.md | 8 +++---- UPGRADE-2.1.md | 4 ++-- .../HttpFoundation/DbalSessionStorage.php | 6 ++--- .../DbalSessionStorageSchema.php | 2 +- .../FrameworkExtension.php | 2 +- .../Resources/config/session.xml | 16 +++++++------- .../Templating/Helper/SessionHelperTest.php | 4 ++-- .../Tests/Templating/PhpEngineTest.php | 4 ++-- .../TwigBundle/Tests/TwigEngineTest.php | 4 ++-- .../DependencyInjection/Container.php | 4 ++-- .../HttpFoundation/Session/Session.php | 16 +++++++------- ...Storage.php => AbstractSessionStorage.php} | 4 ++-- ...Storage.php => MemcacheSessionStorage.php} | 6 ++--- ...torage.php => MemcachedSessionStorage.php} | 6 ++--- ...torage.php => MockArraySessionStorage.php} | 6 ++--- ...Storage.php => MockFileSessionStorage.php} | 6 ++--- ...orage.php => NativeFileSessionStorage.php} | 6 ++--- ...e.php => NativeMemcacheSessionStorage.php} | 6 ++--- ....php => NativeMemcachedSessionStorage.php} | 6 ++--- ...age.php => NativeSqliteSessionStorage.php} | 6 ++--- ...NullStorage.php => NullSessionStorage.php} | 4 ++-- .../{PdoStorage.php => PdoSessionStorage.php} | 8 +++---- ...ce.php => SessionSaveHandlerInterface.php} | 2 +- ...erface.php => SessionStorageInterface.php} | 2 +- .../Component/HttpFoundation/RequestTest.php | 6 ++--- .../HttpFoundation/Session/SessionTest.php | 6 ++--- ...est.php => AbstractSessionStorageTest.php} | 22 +++++++++---------- ...st.php => MockArraySessionStorageTest.php} | 10 ++++----- ...est.php => MockFileSessionStorageTest.php} | 10 ++++----- ...t.php => NativeFileSessionStorageTest.php} | 8 +++---- ...p => NativeMemcacheSessionStorageTest.php} | 8 +++---- ... => NativeMemcachedSessionStorageTest.php} | 8 +++---- ...php => NativeSqliteSessionStorageTest.php} | 8 +++---- ...ageTest.php => NullSessionStorageTest.php} | 12 +++++----- .../Http/Firewall/ContextListenerTest.php | 4 ++-- 35 files changed, 120 insertions(+), 120 deletions(-) rename src/Symfony/Component/HttpFoundation/Session/Storage/{AbstractStorage.php => AbstractSessionStorage.php} (98%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MemcacheStorage.php => MemcacheSessionStorage.php} (95%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MemcachedStorage.php => MemcachedSessionStorage.php} (95%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MockArrayStorage.php => MockArraySessionStorage.php} (92%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MockFileStorage.php => MockFileSessionStorage.php} (94%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeFileStorage.php => NativeFileSessionStorage.php} (89%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeMemcacheStorage.php => NativeMemcacheSessionStorage.php} (93%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeMemcachedStorage.php => NativeMemcachedSessionStorage.php} (92%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NativeSqliteStorage.php => NativeSqliteSessionStorage.php} (88%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{NullStorage.php => NullSessionStorage.php} (90%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{PdoStorage.php => PdoSessionStorage.php} (97%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{SaveHandlerInterface.php => SessionSaveHandlerInterface.php} (99%) rename src/Symfony/Component/HttpFoundation/Session/Storage/{StorageInterface.php => SessionStorageInterface.php} (98%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{AbstractStorageTest.php => AbstractSessionStorageTest.php} (79%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{MockArrayStorageTest.php => MockArraySessionStorageTest.php} (89%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{MockFileStorageTest.php => MockFileSessionStorageTest.php} (91%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeFileStorageTest.php => NativeFileSessionStorageTest.php} (68%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeMemcacheStorageTest.php => NativeMemcacheSessionStorageTest.php} (71%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeMemcachedStorageTest.php => NativeMemcachedSessionStorageTest.php} (75%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NativeSqliteStorageTest.php => NativeSqliteSessionStorageTest.php} (72%) rename tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/{NullStorageTest.php => NullSessionStorageTest.php} (74%) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index b08ac98781..a78edf4a0d 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -239,11 +239,11 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c `getFlash()`, `hasFlash()`, andd `removeFlash()`. `getFlashes() returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and attributes. `Session->getFlashes()->all()` clears flashes now. - * Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` base class for + * Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` base class for session storage drivers. * Added `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` interface which storage drivers should implement after inheriting from - `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` when writing custom session save handlers. + `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` when writing custom session save handlers. * [BC BREAK] `StorageInterface` methods removed: `write()`, `read()` and `remove()`. Added `getBag()`, `registerBag()`. * Moved attribute storage to `Symfony\Component\HttpFoundation\Attribute\AttributeBagInterface`. @@ -252,10 +252,10 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * Added `Symfony\Component\HttpFoundation\Attribute\NamespacedAttributeBag` for namespace session attributes. * Session now implements `Symfony\Component\HttpFoundation\Session\SessionInterface` making implementation customizable and portable. - * [BC BREAK] Removed `NativeStorage` and replaced with `NativeFileStorage`. + * [BC BREAK] Removed `NativeSessionStorage` and replaced with `NativeFileSessionStorage`. * Added session storage drivers for PHP native Memcache, Memcached and SQLite session save handlers. * Added session storage drivers for custom Memcache, Memcached and Null session save handlers. - * Removed `FilesystemStorage`, use `MockFileStorage` for functional testing instead. + * Removed `FilesystemSessionStorage`, use `MockFileSessionStorage` for functional testing instead. ### HttpKernel diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 641c967c96..2df9529737 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -301,9 +301,9 @@ UPGRADE FROM 2.0 to 2.1 * Session storage drivers Session storage drivers should inherit from - `Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage` + `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` and no longer should implement `read()`, `write()`, `remove()` which were removed from the - `StorageInterface`. + `SessionStorageInterface`. Any session storage driver that wants to use custom save handlers should implement `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 5d2c5d4d1a..cda9dd2a0a 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -3,8 +3,8 @@ namespace Symfony\Bridge\Doctrine\HttpFoundation; use Doctrine\DBAL\Platforms\MySqlPlatform; -use Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage; -use Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface; use Doctrine\DBAL\Driver\Connection; /** @@ -13,7 +13,7 @@ use Doctrine\DBAL\Driver\Connection; * @author Fabien Potencier * @author Johannes M. Schmitt */ -class DbalStorage extends AbstractStorage implements SaveHandlerInterface +class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { /** * @var Connection diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php index 016f4e7a99..b33862588c 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorageSchema.php @@ -9,7 +9,7 @@ use Doctrine\DBAL\Schema\Schema; * * @author Johannes M. Schmitt */ -final class DbalStorageSchema extends Schema +final class DbalSessionStorageSchema extends Schema { private $tableName; diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index fd67ff74aa..426e78cf5e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -301,7 +301,7 @@ class FrameworkExtension extends Extension $this->addClassesToCompile(array( 'Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener', - 'Symfony\\Component\\HttpFoundation\\Session\Storage\\StorageInterface', + 'Symfony\\Component\\HttpFoundation\\Session\Storage\\SessionStorageInterface', $container->getDefinition('session')->getClass(), )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index e0c5a7d816..845bd2b501 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -8,14 +8,14 @@ Symfony\Component\HttpFoundation\Session\Session Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag - Symfony\Component\HttpFoundation\Session\Storage\NativeFileStorage - Symfony\Component\HttpFoundation\Session\Storage\NullStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteStorage - Symfony\Component\HttpFoundation\Session\Storage\MemcacheStorage - Symfony\Component\HttpFoundation\Session\Storage\MemcachedStorage - Symfony\Component\HttpFoundation\Session\Storage\MockFileStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NullSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\MemcacheSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\MemcachedSessionStorage + Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage Memcache Memcached diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index 9ba8496d92..8f87603a6c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -13,7 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper; class SessionHelperTest extends \PHPUnit_Framework_TestCase @@ -24,7 +24,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $this->request = new Request(); - $session = new Session(new MockArrayStorage()); + $session = new Session(new MockArraySessionStorage()); $session->set('foobar', 'bar'); $session->getFlashes()->set('notice', 'bar'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index 1917f50a2c..476b398e61 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -15,7 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; @@ -64,7 +64,7 @@ class PhpEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new MockArrayStorage()); + $session = new Session(new MockArraySessionStorage()); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index 71e2ce518e..f15ef3f945 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -15,7 +15,7 @@ use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; @@ -71,7 +71,7 @@ class TwigEngineTest extends TestCase { $container = new Container(); $request = new Request(); - $session = new Session(new MockArrayStorage()); + $session = new Session(new MockArraySessionStorage()); $request->setSession($session); $container->set('request', $request); diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index f4444b4db9..759bd45029 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -43,8 +43,8 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; * *
    *
  • request -> getRequestService()
  • - *
  • mysql_session_storage -> getMysqlStorageService()
  • - *
  • symfony.mysql_session_storage -> getSymfony_MysqlStorageService()
  • + *
  • mysql_session_storage -> getMysqlSessionStorageService()
  • + *
  • symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()
  • *
* * The container can have three possible behaviors when a service does not exist: diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 23526bd544..51cd368931 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -11,7 +11,7 @@ namespace Symfony\Component\HttpFoundation\Session; -use Symfony\Component\HttpFoundation\Session\Storage\StorageInterface; +use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; @@ -31,18 +31,18 @@ class Session implements SessionInterface /** * Storage driver. * - * @var StorageInterface + * @var SessionStorageInterface */ protected $storage; /** * Constructor. * - * @param StorageInterface $storage A StorageInterface instance. + * @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(StorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) + public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { $this->storage = $storage; $this->registerBag($attributes ?: new AttributeBag()); @@ -204,7 +204,7 @@ class Session implements SessionInterface /** * Implements the \Serialize interface. * - * @return StorageInterface + * @return SessionStorageInterface */ public function serialize() { @@ -214,13 +214,13 @@ class Session implements SessionInterface /** * Implements the \Serialize interface. * - * @throws \InvalidArgumentException If the passed string does not unserialize to an instance of StorageInterface + * @throws \InvalidArgumentException If the passed string does not unserialize to an instance of SessionStorageInterface */ public function unserialize($serialized) { $storage = unserialize($serialized); - if (!$storage instanceof StorageInterface) { - throw new \InvalidArgumentException('Serialized data did not return a valid instance of StorageInterface'); + if (!$storage instanceof SessionStorageInterface) { + throw new \InvalidArgumentException('Serialized data did not return a valid instance of SessionStorageInterface'); } $this->storage = $storage; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php index 1b17939c59..c703d09a00 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php @@ -18,7 +18,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; * * @author Drak */ -abstract class AbstractStorage implements StorageInterface +abstract class AbstractSessionStorage implements SessionStorageInterface { /** * Array of SessionBagInterface @@ -293,7 +293,7 @@ abstract class AbstractStorage implements StorageInterface { // 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 SaveHandlerInterface) { + if ($this instanceof SessionSaveHandlerInterface) { session_set_save_handler( array($this, 'openSession'), array($this, 'closeSession'), diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php index 39c15f57df..f5d5910d86 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php @@ -12,11 +12,11 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MemcacheStorage. + * MemcacheSessionStorage. * * @author Drak */ -class MemcacheStorage extends AbstractStorage implements SaveHandlerInterface +class MemcacheSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { /** * Memcache driver. @@ -46,7 +46,7 @@ class MemcacheStorage extends AbstractStorage implements SaveHandlerInterface * @param array $memcacheOptions An associative array of Memcachge options * @param array $options Session configuration options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php similarity index 95% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php index 114a41e33d..f7041811a3 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcachedSessionStorage.php @@ -12,11 +12,11 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MemcachedStorage. + * MemcachedSessionStorage. * * @author Drak */ -class MemcachedStorage extends AbstractStorage implements SaveHandlerInterface +class MemcachedSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { /** * Memcached driver. @@ -39,7 +39,7 @@ class MemcachedStorage extends AbstractStorage implements SaveHandlerInterface * @param array $memcachedOptions An associative array of Memcached options * @param array $options Session configuration options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct(\Memcached $memcache, array $memcachedOptions = array(), array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArrayStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MockArrayStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index c762c8c396..2fd1a94c26 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArrayStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -12,18 +12,18 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MockArrayStorage mocks the session for unit tests. + * MockArraySessionStorage mocks the session for unit tests. * * No PHP session is actually started since a session can be initialized * and shutdown only once per PHP execution cycle. * - * When doing functional testing, you should use MockFileStorage instead. + * When doing functional testing, you should use MockFileSessionStorage instead. * * @author Fabien Potencier * @author Bulat Shakirzyanov * @author Drak */ -class MockArrayStorage extends AbstractStorage +class MockArraySessionStorage extends AbstractSessionStorage { /** * @var string diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php similarity index 94% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MockFileStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index 314bf8e5b2..094c4d6005 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * MockFileStorage is used to mock sessions for + * MockFileSessionStorage is used to mock sessions for * functional testing when done in a single PHP process. * * No PHP session is actually started since a session can be initialized @@ -20,7 +20,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; * * @author Drak */ -class MockFileStorage extends MockArrayStorage +class MockFileSessionStorage extends MockArraySessionStorage { /** * @var string @@ -33,7 +33,7 @@ class MockFileStorage extends MockArrayStorage * @param string $savePath Path of directory to save session files. * @param array $options Session options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct($savePath = null, array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php similarity index 89% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php index da0a24086e..09350e8d21 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeFileSessionStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeFileStorage. + * NativeFileSessionStorage. * * Native session handler using PHP's built in file storage. * * @author Drak */ -class NativeFileStorage extends AbstractStorage +class NativeFileSessionStorage extends AbstractSessionStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeFileStorage extends AbstractStorage * @param string $savePath Path of directory to save session files. * @param array $options Session configuration options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct($savePath = null, array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php similarity index 93% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php index 91871ea848..5572c47812 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeMemcacheStorage. + * NativeMemcacheSessionStorage. * * Session based on native PHP memcache database handler. * * @author Drak */ -class NativeMemcacheStorage extends AbstractStorage +class NativeMemcacheSessionStorage extends AbstractSessionStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeMemcacheStorage extends AbstractStorage * @param string $savePath Path of memcache server. * @param array $options Session configuration options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php similarity index 92% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php index f074e93ce1..50d8d060fd 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeMemcachedStorage. + * NativeMemcachedSessionStorage. * * Session based on native PHP memcached database handler. * * @author Drak */ -class NativeMemcachedStorage extends AbstractStorage +class NativeMemcachedSessionStorage extends AbstractSessionStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeMemcachedStorage extends AbstractStorage * @param string $savePath Comma separated list of servers: e.g. memcache1.example.com:11211,memcache2.example.com:11211 * @param array $options Session configuration options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct($savePath = '127.0.0.1:11211', array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php similarity index 88% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php index 125d5f22e4..1dac36ac2f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorage.php @@ -12,13 +12,13 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NativeSqliteStorage. + * NativeSqliteSessionStorage. * * Session based on native PHP sqlite database handler. * * @author Drak */ -class NativeSqliteStorage extends AbstractStorage +class NativeSqliteSessionStorage extends AbstractSessionStorage { /** * @var string @@ -31,7 +31,7 @@ class NativeSqliteStorage extends AbstractStorage * @param string $dbPath Path to SQLite database file. * @param array $options Session configuration options. * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ public function __construct($dbPath, array $options = array()) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NullStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php similarity index 90% rename from src/Symfony/Component/HttpFoundation/Session/Storage/NullStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php index bebedc53b6..b81fa8ae3a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NullStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NullSessionStorage.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * NullStorage. + * NullSessionStorage. * * Can be used in unit testing or in a sitation where persisted sessions are not desired. * @@ -20,7 +20,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; * * @api */ -class NullStorage extends AbstractStorage implements SaveHandlerInterface +class NullSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { /** * {@inheritdoc} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/PdoStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php similarity index 97% rename from src/Symfony/Component/HttpFoundation/Session/Storage/PdoStorage.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php index a70152febf..2015396b2f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/PdoStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php @@ -12,12 +12,12 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; /** - * PdoStorage. + * PdoSessionStorage. * * @author Fabien Potencier * @author Michael Williams */ -class PdoStorage extends AbstractStorage implements SaveHandlerInterface +class PdoSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { /** * PDO instance. @@ -44,12 +44,12 @@ class PdoStorage extends AbstractStorage implements SaveHandlerInterface * * @throws \InvalidArgumentException When "db_table" option is not provided * - * @see AbstractStorage::__construct() + * @see AbstractSessionStorage::__construct() */ 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 PdoStorage.'); + throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.'); } $this->pdo = $pdo; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SaveHandlerInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php similarity index 99% rename from src/Symfony/Component/HttpFoundation/Session/Storage/SaveHandlerInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php index 0bc74ed8bb..ec4530a150 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SaveHandlerInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionSaveHandlerInterface.php @@ -52,7 +52,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; * * @author Drak */ -interface SaveHandlerInterface +interface SessionSaveHandlerInterface { /** * Open session. diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/StorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php similarity index 98% rename from src/Symfony/Component/HttpFoundation/Session/Storage/StorageInterface.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 171dadc0bf..a5afda9672 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/StorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -21,7 +21,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; * * @api */ -interface StorageInterface +interface SessionStorageInterface { /** * Starts the session. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index 89de12e988..c147070fdf 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -12,7 +12,7 @@ namespace Symfony\Tests\Component\HttpFoundation; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Request; @@ -846,7 +846,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase $request = new Request; $this->assertFalse($request->hasSession()); - $request->setSession(new Session(new MockArrayStorage())); + $request->setSession(new Session(new MockArraySessionStorage())); $this->assertTrue($request->hasSession()); } @@ -857,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 MockArrayStorage())); + $request->setSession(new Session(new MockArraySessionStorage())); $this->assertTrue($request->hasPreviousSession()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php index 4499fce137..3f57670581 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php @@ -14,7 +14,7 @@ namespace Symfony\Tests\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * SessionTest @@ -26,7 +26,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; class SessionTest extends \PHPUnit_Framework_TestCase { /** - * @var \Symfony\Component\HttpFoundation\Session\Storage\StorageInterface + * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface */ protected $storage; @@ -37,7 +37,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->storage = new MockArrayStorage(); + $this->storage = new MockArraySessionStorage(); $this->session = new Session($this->storage, new AttributeBag(), new FlashBag()); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractSessionStorageTest.php similarity index 79% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractSessionStorageTest.php index e31503fa3d..05bc5fefb3 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/AbstractSessionStorageTest.php @@ -2,21 +2,21 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage; +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\SaveHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface; /** - * Turn AbstractStorage into something concrete because + * Turn AbstractSessionStorage into something concrete because * certain mocking features are broken in PHPUnit-Mock-Objects < 1.1.2 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/73 */ -class ConcreteStorage extends AbstractStorage +class ConcreteSessionStorage extends AbstractSessionStorage { } -class CustomHandlerStorage extends AbstractStorage implements SaveHandlerInterface +class CustomHandlerSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface { public function openSession($path, $id) { @@ -44,7 +44,7 @@ class CustomHandlerStorage extends AbstractStorage implements SaveHandlerInterfa } /** - * Test class for AbstractStorage. + * Test class for AbstractSessionStorage. * * @author Drak * @@ -52,14 +52,14 @@ class CustomHandlerStorage extends AbstractStorage implements SaveHandlerInterfa * * @runTestsInSeparateProcesses */ -class AbstractStorageTest extends \PHPUnit_Framework_TestCase +class AbstractSessionStorageTest extends \PHPUnit_Framework_TestCase { /** - * @return AbstractStorage + * @return AbstractSessionStorage */ protected function getStorage() { - $storage = new CustomHandlerStorage(); + $storage = new CustomHandlerSessionStorage(); $storage->registerBag(new AttributeBag); return $storage; @@ -115,13 +115,13 @@ class AbstractStorageTest extends \PHPUnit_Framework_TestCase public function testCustomSaveHandlers() { - $storage = new CustomHandlerStorage(); + $storage = new CustomHandlerSessionStorage(); $this->assertEquals('user', ini_get('session.save_handler')); } public function testNativeSaveHandlers() { - $storage = new ConcreteStorage(); + $storage = new ConcreteSessionStorage(); $this->assertNotEquals('user', ini_get('session.save_handler')); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArraySessionStorageTest.php similarity index 89% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArraySessionStorageTest.php index 29d9078532..4611457940 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArrayStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockArraySessionStorageTest.php @@ -2,20 +2,20 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; /** - * Test class for MockArrayStorage. + * Test class for MockArraySessionStorage. * * @author Drak */ -class MockArrayStorageTest extends \PHPUnit_Framework_TestCase +class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase { /** - * @var MockArrayStorage + * @var MockArraySessionStorage */ private $storage; @@ -41,7 +41,7 @@ class MockArrayStorageTest extends \PHPUnit_Framework_TestCase $this->flashes->getStorageKey() => array('notice' => 'hello'), ); - $this->storage = new MockArrayStorage(); + $this->storage = new MockArraySessionStorage(); $this->storage->registerBag($this->flashes); $this->storage->registerBag($this->attributes); $this->storage->setSessionData($this->data); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileSessionStorageTest.php similarity index 91% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileSessionStorageTest.php index 337faf4ca0..770bcda8f0 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MockFileSessionStorageTest.php @@ -2,16 +2,16 @@ namespace Symfony\Test\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\MockFileStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; /** - * Test class for MockFileStorage. + * Test class for MockFileSessionStorage. * * @author Drak */ -class MockFileStorageTest extends \PHPUnit_Framework_TestCase +class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase { /** * @var string @@ -19,7 +19,7 @@ class MockFileStorageTest extends \PHPUnit_Framework_TestCase private $sessionDir; /** - * @var FileMockStorage + * @var FileMockSessionStorage */ protected $storage; @@ -96,7 +96,7 @@ class MockFileStorageTest extends \PHPUnit_Framework_TestCase private function getStorage(array $options = array()) { - $storage = new MockFileStorage($this->sessionDir, $options); + $storage = new MockFileSessionStorage($this->sessionDir, $options); $storage->registerBag(new FlashBag); $storage->registerBag(new AttributeBag); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileSessionStorageTest.php similarity index 68% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileSessionStorageTest.php index e44ae44170..df3ef95cbf 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeFileSessionStorageTest.php @@ -2,20 +2,20 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeFileStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage; /** - * Test class for NativeFileStorage. + * Test class for NativeFileSessionStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeFileStorageTest extends \PHPUnit_Framework_TestCase +class NativeFileSessionStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { - $storage = new NativeFileStorage(sys_get_temp_dir(), array('name' => 'TESTING')); + $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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorageTest.php similarity index 71% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorageTest.php index 18a0276001..4274ed5bd1 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcacheSessionStorageTest.php @@ -2,16 +2,16 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage; /** - * Test class for NativeMemcacheStorage. + * Test class for NativeMemcacheSessionStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeMemcacheStorageTest extends \PHPUnit_Framework_TestCase +class NativeMemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { @@ -19,7 +19,7 @@ class NativeMemcacheStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Skipped tests SQLite extension is not present'); } - $storage = new NativeMemcacheStorage('tcp://127.0.0.1:11211?persistent=0', array('name' => 'TESTING')); + $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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorageTest.php similarity index 75% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorageTest.php index 4cc691df72..c0a12aa2b4 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeMemcachedSessionStorageTest.php @@ -2,16 +2,16 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage; /** - * Test class for NativeMemcachedStorage. + * Test class for NativeMemcachedSessionStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeMemcachedStorageTest extends \PHPUnit_Framework_TestCase +class NativeMemcachedSessionStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { @@ -22,7 +22,7 @@ class NativeMemcachedStorageTest extends \PHPUnit_Framework_TestCase // test takes too long if memcached server is not running ini_set('memcached.sess_locking', '0'); - $storage = new NativeMemcachedStorage('127.0.0.1:11211', array('name' => 'TESTING')); + $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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorageTest.php similarity index 72% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorageTest.php index c8a7cacbe3..b1700326b1 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeSqliteSessionStorageTest.php @@ -2,16 +2,16 @@ namespace Symfony\Tests\Component\HttpFoundation\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteStorage; +use Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage; /** - * Test class for NativeSqliteStorage. + * Test class for NativeSqliteSessionStorage. * * @author Drak * * @runTestsInSeparateProcesses */ -class NativeSqliteStorageTest extends \PHPUnit_Framework_TestCase +class NativeSqliteSessionStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { @@ -19,7 +19,7 @@ class NativeSqliteStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Skipped tests SQLite extension is not present'); } - $storage = new NativeSqliteStorage(sys_get_temp_dir().'/sqlite.db', array('name' => 'TESTING')); + $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')); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullSessionStorageTest.php similarity index 74% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullStorageTest.php rename to tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullSessionStorageTest.php index 9abd333d3c..66599f68b3 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NullSessionStorageTest.php @@ -1,28 +1,28 @@ * * @runTestsInSeparateProcesses */ -class NullStorageTest extends \PHPUnit_Framework_TestCase +class NullSessionStorageTest extends \PHPUnit_Framework_TestCase { public function testSaveHandlers() { - $storage = new NullStorage(); + $storage = new NullSessionStorage(); $this->assertEquals('user', ini_get('session.save_handler')); } public function testSession() { session_id('nullsessionstorage'); - $storage = new NullStorage(); + $storage = new NullSessionStorage(); $session = new Session($storage); $this->assertNull($session->get('something')); $session->set('something', 'unique'); @@ -32,7 +32,7 @@ class NullStorageTest extends \PHPUnit_Framework_TestCase public function testNothingIsPersisted() { session_id('nullsessionstorage'); - $storage = new NullStorage(); + $storage = new NullSessionStorage(); $session = new Session($storage); $session->start(); $this->assertEquals('nullsessionstorage', $session->getId()); diff --git a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php index bac4fa52e6..5c810062ef 100644 --- a/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php +++ b/tests/Symfony/Tests/Component/Security/Http/Firewall/ContextListenerTest.php @@ -5,7 +5,7 @@ namespace Symfony\Test\Component\Security\Http\Firewall; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArrayStorage; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; @@ -63,7 +63,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase protected function runSessionOnKernelResponse($newToken, $original = null) { - $session = new Session(new MockArrayStorage()); + $session = new Session(new MockArraySessionStorage()); if ($original !== null) { $session->set('_security_session', $original); From 93d81a171c464655d0c03387d498d4b610e24a86 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 12:21:08 +0100 Subject: [PATCH 24/31] [HttpFoundation] removed configuration for session storages in session.xml as we cannot provide a way to configure them (like before this PR anyway) --- .../FrameworkExtension.php | 2 +- .../Resources/config/session.xml | 43 +------------------ .../Session/SessionInterface.php | 1 - 3 files changed, 2 insertions(+), 44 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 426e78cf5e..d635a7f598 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -301,7 +301,7 @@ class FrameworkExtension extends Extension $this->addClassesToCompile(array( 'Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener', - 'Symfony\\Component\\HttpFoundation\\Session\Storage\\SessionStorageInterface', + 'Symfony\\Component\\HttpFoundation\\Session\\Storage\\SessionStorageInterface', $container->getDefinition('session')->getClass(), )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index 845bd2b501..41f9a1d6d1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -9,16 +9,7 @@ Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NullSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeMemcacheSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeMemcachedSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\NativeSqliteSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\MemcacheSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\MemcachedSessionStorage Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage - Memcache - Memcached - Symfony\Bundle\FrameworkBundle\EventListener\SessionListener
@@ -30,9 +21,8 @@
+ - - %kernel.cache_dir%/sessions @@ -44,37 +34,6 @@ %session.storage.options% - - tcp://127.0.0.1:11211?persistent=0 - %session.storage.options% - - - - 127.0.0.1:11211 - %session.storage.options% - - - - - tcp://127.0.0.1:11211?persistent=0 - %session.storage.options% - - - - - tcp://127.0.0.1:11211?persistent=0 - %session.storage.options% - - - - %kernel.cache_dir%/sf2_sqlite_sess.db - %session.storage.options% - - - - %session.storage.options% - - diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 1b4b7bfdbc..c31da6d165 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -11,7 +11,6 @@ namespace Symfony\Component\HttpFoundation\Session; - /** * Interface for the session. * From 146a502a0e3527f6ffbda14ce06477b9b2a01232 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 12:24:21 +0100 Subject: [PATCH 25/31] [FrameworkBundle] added some service aliases to avoid some BC breaks --- CHANGELOG-2.1.md | 1 - .../Bundle/FrameworkBundle/Resources/config/session.xml | 4 ++++ .../FrameworkBundle/Templating/Helper/SessionHelper.php | 8 ++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index a78edf4a0d..005ff15227 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -40,7 +40,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * changed the default profiler storage to use the filesystem instead of SQLite * added support for placeholders in route defaults and requirements (replaced by the value set in the service container) * added Filesystem component as a dependency - * [BC BREAK] changed `session.xml` service name `session.storage.native` to `session.storage.native_file` * added new session storage drivers to session.xml: `session.storage.native_memcache`, `session.storage.native_memcached`, `session.storage.native_sqlite`, `session.storage.null`, `session.storage.memcache`, and `session.storage.memcached`. Added `session.storage.mock_file` service for functional session testing. diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index 41f9a1d6d1..31d374aa6b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -38,5 +38,9 @@ + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index f38403c181..849002dd53 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -46,9 +46,9 @@ class SessionHelper extends Helper return $this->session->get($name, $default); } - public function getFlash($type, $default = null) + public function getFlash($name, $default = null) { - return $this->session->getFlashes()->get($type); + return $this->session->getFlashes()->get($name); } public function getFlashes() @@ -56,9 +56,9 @@ class SessionHelper extends Helper return $this->session->getFlashes()->all(); } - public function hasFlash($type) + public function hasFlash($name) { - return $this->session->getFlashes()->has($type); + return $this->session->getFlashes()->has($name); } /** From 0f6c50ac69c54df0b17f1fb3080ca2fa86009a7b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 12:43:38 +0100 Subject: [PATCH 26/31] [HttpFoundation] added some method for a better BC --- .../HttpFoundation/Session/Session.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 51cd368931..e750a0eb27 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -245,4 +245,62 @@ class Session implements SessionInterface { return $this->getBag('flashes'); } + + // the following methods are kept for compatibility with Symfony 2.0 (they will be removed for Symfony 2.3) + + /** + * @deprecated + */ + public function getFlashes() + { + return $this->getBag('flashes')->all(); + } + + /** + * @deprecated + */ + public function setFlashes($values) + { + $this->getBag('flashes')->setAll($values); + } + + /** + * @deprecated + */ + public function getFlash($name, $default = null) + { + return $this->getBag('flashes')->get($name, $default); + } + + /** + * @deprecated + */ + public function setFlash($name, $value) + { + $this->getBag('flashes')->set($name, $value); + } + + /** + * @deprecated + */ + public function hasFlash($name) + { + return $this->getBag('flashes')->has($name); + } + + /** + * @deprecated + */ + public function removeFlash($name) + { + $this->getBag('flashes')->get($name); + } + + /** + * @deprecated + */ + public function clearFlashes() + { + return $this->getBag('flashes')->clear(); + } } From 282d3ae1d8a7fb73e65553b0c016f4c1369e34bf Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 12:44:56 +0100 Subject: [PATCH 27/31] updated CHANGELOG for 2.1 --- CHANGELOG-2.1.md | 16 +++---- UPGRADE-2.1.md | 43 +++++++++---------- .../HttpFoundation/Session/Session.php | 4 ++ 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 005ff15227..4b69f6a794 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -40,10 +40,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * changed the default profiler storage to use the filesystem instead of SQLite * added support for placeholders in route defaults and requirements (replaced by the value set in the service container) * added Filesystem component as a dependency - * added new session storage drivers to session.xml: `session.storage.native_memcache`, `session.storage.native_memcached`, - `session.storage.native_sqlite`, `session.storage.null`, `session.storage.memcache`, - and `session.storage.memcached`. Added `session.storage.mock_file` service for functional session testing. - * removed `session.storage.filesystem` service. ### MonologBundle @@ -230,20 +226,20 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * made mimetype to extension conversion configurable * [BC BREAK] Moved all session related classes and interfaces into own namespace, as `Symfony\Component\HttpFoudation\Session` and renamed classes accordingly. - * Added `FlashBag` (default). Flashes expire when retrieved by `get()` or `all()`. + * Added `FlashBag`. Flashes expire when retrieved by `get()` or `all()`. This makes the implementation ESI compatible. - * Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring + * Added `AutoExpireFlashBag` (default) to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring after one page page load. Messages must be retrived by `get()` or `all()`. - * [BC BREAK] Removed the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()` - `getFlash()`, `hasFlash()`, andd `removeFlash()`. `getFlashes() returns a `FlashBagInterface`. + * Deprecated the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()` + `getFlash()`, `hasFlash()`, and `removeFlash()`. Use `getFlashes() instead which returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and attributes. `Session->getFlashes()->all()` clears flashes now. * Added `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` base class for session storage drivers. - * Added `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` interface + * Added `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` interface which storage drivers should implement after inheriting from `Symfony\Component\HttpFoundation\Session\Storage\AbstractSessionStorage` when writing custom session save handlers. - * [BC BREAK] `StorageInterface` methods removed: `write()`, `read()` and `remove()`. Added + * [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and `remove()`. Added `getBag()`, `registerBag()`. * Moved attribute storage to `Symfony\Component\HttpFoundation\Attribute\AttributeBagInterface`. * Added `Symfony\Component\HttpFoundation\Attribute\AttributeBag` to replicate attributes storage diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 2df9529737..316efdff81 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -80,8 +80,8 @@ UPGRADE FROM 2.0 to 2.1 explicitely need to set the `Valid` constraint in your model if you want to validate associated objects. - If you don't want to set the `Valid` constraint, or if there is no reference - from the data of the parent form to the data of the child form, you can + If you don't want to set the `Valid` constraint, or if there is no reference + from the data of the parent form to the data of the child form, you can enable BC behaviour by setting the option "cascade_validation" to `true` on the parent form. @@ -236,12 +236,27 @@ UPGRADE FROM 2.0 to 2.1 return false; } } - the parent form. - Before: $session->getLocale() - After: $request->getLocale() +* The options passed to `getParent` of the form types don't contain default + options anymore - * Flash Messages now returns and array based on type + You should check with `isset` if options exist before checking their value. + + Before: + + public function getParent(array $options) + { + return 'single_text' === $options['widget'] ? 'text' : 'choice'; + } + + After: + + public function getParent(array $options) + { + return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice'; + } + +* Flash Messages now returns and array based on type (the old method are still available but deprecated) Before (PHP): @@ -291,13 +306,6 @@ UPGRADE FROM 2.0 to 2.1 {% endforeach %} -* Session object - - The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()` - have been removed from the `Session` object. `getFlashes()` now returns a `FlashBagInterface`. - Flashes should be popped off the stack using `getFlashes()->get()` or `getFlashes()->all()` - to get all flashes in one go. - * Session storage drivers Session storage drivers should inherit from @@ -307,12 +315,3 @@ UPGRADE FROM 2.0 to 2.1 Any session storage driver that wants to use custom save handlers should implement `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` - -### [FrameworkBundle] - - The service `session.storage.native` is now called `session.storage.native_file` - - The service `session.storage.filesystem` is now called `session.storage.mock_file` - and is used for functional unit testing. You will need to update any references - in functional tests. - diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index e750a0eb27..8428266278 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -303,4 +303,8 @@ class Session implements SessionInterface { return $this->getBag('flashes')->clear(); } + + public function close() + { + } } From 8a01dd5cff02e2e77a0b138bf6ed2b5c4e73c16d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 13:09:41 +0100 Subject: [PATCH 28/31] renamed getFlashes() to getFlashBag() to avoid clashes --- CHANGELOG-2.1.md | 4 ++-- UPGRADE-2.1.md | 8 ++++---- .../FrameworkBundle/Templating/Helper/SessionHelper.php | 6 +++--- .../Bundle/TestBundle/Controller/SessionController.php | 6 +++--- .../Tests/Templating/Helper/SessionHelperTest.php | 2 +- .../WebProfilerBundle/Controller/ProfilerController.php | 7 ++++--- .../EventListener/WebDebugToolbarListener.php | 4 +--- src/Symfony/Component/HttpFoundation/Session/Session.php | 2 +- 8 files changed, 19 insertions(+), 20 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 4b69f6a794..b6e0e7c55a 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -231,9 +231,9 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * Added `AutoExpireFlashBag` (default) to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring after one page page load. Messages must be retrived by `get()` or `all()`. * Deprecated the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()` - `getFlash()`, `hasFlash()`, and `removeFlash()`. Use `getFlashes() instead which returns a `FlashBagInterface`. + `getFlash()`, `hasFlash()`, and `removeFlash()`. Use `getFlashBag() instead which returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and - attributes. `Session->getFlashes()->all()` clears flashes now. + 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 diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 316efdff81..862806138b 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -268,15 +268,15 @@ UPGRADE FROM 2.0 to 2.1 After (PHP): - getFlashes()->has('notice')): ?> + getFlashBag()->has('notice')): ?>
- getFlashes()->get('notice') ?> + getFlashBag()->get('notice') ?>
- If you wanted to process all flash types you could also make use of the `getFlashes()->all()` API: + If you wanted to process all flash types you could also make use of the `getFlashBag()->all()` API: - getFlashes()->all() as $type => $flash): ?> + getFlashBag()->all() as $type => $flash): ?>
diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index 849002dd53..c6628172f2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -48,17 +48,17 @@ class SessionHelper extends Helper public function getFlash($name, $default = null) { - return $this->session->getFlashes()->get($name); + return $this->session->getFlashBag()->get($name); } public function getFlashes() { - return $this->session->getFlashes()->all(); + return $this->session->getFlashBag()->all(); } public function hasFlash($name) { - return $this->session->getFlashes()->has($name); + return $this->session->getFlashBag()->has($name); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php index 626c2e2cbd..b1c4334cde 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php @@ -51,7 +51,7 @@ class SessionController extends ContainerAware { $request = $this->container->get('request'); $session = $request->getSession(); - $session->getFlashes()->set('notice', $message); + $session->getFlashBag()->set('notice', $message); return new RedirectResponse($this->container->get('router')->generate('session_showflash')); } @@ -61,8 +61,8 @@ class SessionController extends ContainerAware $request = $this->container->get('request'); $session = $request->getSession(); - if ($session->getFlashes()->has('notice')) { - $output = $session->getFlashes()->get('notice'); + if ($session->getFlashBag()->has('notice')) { + $output = $session->getFlashBag()->get('notice'); } else { $output = 'No flash was set.'; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index 8f87603a6c..ce8c91b173 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -26,7 +26,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase $session = new Session(new MockArraySessionStorage()); $session->set('foobar', 'bar'); - $session->getFlashes()->set('notice', 'bar'); + $session->getFlashBag()->set('notice', 'bar'); $this->request->setSession($session); } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php index d6235025d0..96347eaf3a 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php @@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag; /** * ProfilerController. @@ -146,9 +147,9 @@ class ProfilerController extends ContainerAware { $request = $this->container->get('request'); - if (null !== $session = $request->getSession()) { - // keep current flashes for one more request - $session->setFlashes($session->getFlashes()); + if (null !== $session = $request->getSession() && $session instanceof AutoExpireFlashBag) { + // keep current flashes for one more request if using AutoExpireFlashBag + $session->getFlashBag()->setAll($session->getFlashBag()->peekAll()); } if (null === $token) { diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index 5d172a1cad..9d615c9c5c 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -74,9 +74,7 @@ class WebDebugToolbarListener $session = $request->getSession(); if ($session instanceof AutoExpireFlashBag) { // keep current flashes for one more request if using AutoExpireFlashBag - foreach ($session->getFlashKeys() as $type) { - $session->setFlashes($session->getFlashes($type)); - } + $session->getFlashBag()->setAll($session->getFlashBag()->peekAll()); } $response->setContent($this->templating->render('WebProfilerBundle:Profiler:toolbar_redirect.html.twig', array('location' => $response->headers->get('Location')))); diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 8428266278..56ee1f0949 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -241,7 +241,7 @@ class Session implements SessionInterface * * @return FlashBagInterface */ - public function getFlashes() + public function getFlashBag() { return $this->getBag('flashes'); } From b8df1620acebc0f1deb34afc530bc88d30bb5295 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 11 Feb 2012 19:47:15 +0545 Subject: [PATCH 29/31] Correct instanceof condition. --- .../Bundle/WebProfilerBundle/Controller/ProfilerController.php | 2 +- .../WebProfilerBundle/EventListener/WebDebugToolbarListener.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php index 96347eaf3a..491ca0b6ea 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php @@ -147,7 +147,7 @@ class ProfilerController extends ContainerAware { $request = $this->container->get('request'); - if (null !== $session = $request->getSession() && $session instanceof AutoExpireFlashBag) { + if (null !== $session = $request->getSession() && $session->getFlashBag() instanceof AutoExpireFlashBag) { // keep current flashes for one more request if using AutoExpireFlashBag $session->getFlashBag()->setAll($session->getFlashBag()->peekAll()); } diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index 9d615c9c5c..b9418e6b2a 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -72,7 +72,7 @@ class WebDebugToolbarListener if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) { $session = $request->getSession(); - if ($session instanceof AutoExpireFlashBag) { + if ($session->getFlashBag() instanceof AutoExpireFlashBag) { // keep current flashes for one more request if using AutoExpireFlashBag $session->getFlashBag()->setAll($session->getFlashBag()->peekAll()); } From c59d880593de085289deb37f7f6e155b74525d5d Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 11 Feb 2012 20:15:36 +0545 Subject: [PATCH 30/31] Docblocks. --- .../HttpFoundation/Session/Session.php | 31 ++++++++++++++----- .../Storage/SessionStorageInterface.php | 2 ++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 56ee1f0949..d85768c96d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -226,11 +226,23 @@ class Session implements SessionInterface $this->storage = $storage; } + /** + * Registers a SessionBagInterface with the sessio. + * + * @param SessionBagInterface $bag + */ public function registerBag(SessionBagInterface $bag) { $this->storage->registerBag($bag); } + /** + * Get's a bag instance. + * + * @param string $name + * + * @return SessionBagInterface + */ public function getBag($name) { return $this->storage->getBag($name); @@ -249,7 +261,7 @@ class Session implements SessionInterface // the following methods are kept for compatibility with Symfony 2.0 (they will be removed for Symfony 2.3) /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function getFlashes() { @@ -257,7 +269,7 @@ class Session implements SessionInterface } /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function setFlashes($values) { @@ -265,7 +277,7 @@ class Session implements SessionInterface } /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function getFlash($name, $default = null) { @@ -273,7 +285,7 @@ class Session implements SessionInterface } /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function setFlash($name, $value) { @@ -281,7 +293,7 @@ class Session implements SessionInterface } /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function hasFlash($name) { @@ -289,7 +301,7 @@ class Session implements SessionInterface } /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function removeFlash($name) { @@ -297,13 +309,18 @@ class Session implements SessionInterface } /** - * @deprecated + * @deprecated since 2.1, will be removed from 2.3 */ public function clearFlashes() { return $this->getBag('flashes')->clear(); } + /** + * This method does not do anything. + * + * @deprecated since 2.1, will be removed from 2.3 + */ public function close() { } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index a5afda9672..7eb720b7c6 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -85,6 +85,8 @@ interface SessionStorageInterface /** * Registers a SessionBagInterface for use. + * + * @param SessionBagInterface $bag */ function registerBag(SessionBagInterface $bag); } From cb6fdb1f5e69ce2a0b352786e4408d99a0d10e3d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Feb 2012 15:53:54 +0100 Subject: [PATCH 31/31] [HttpFoundation] removed Session::close() --- CHANGELOG-2.1.md | 3 ++- src/Symfony/Component/HttpFoundation/Session/Session.php | 9 --------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index b6e0e7c55a..02ff0a5301 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -230,7 +230,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c This makes the implementation ESI compatible. * Added `AutoExpireFlashBag` (default) to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring after one page page load. Messages must be retrived by `get()` or `all()`. - * Deprecated the following methods from the Session class: `close()`, `setFlash()`, `setFlashes()` + * [BC BREAK] Removed the `close()` method from the Session class + * Deprecated the following methods from the Session class: `setFlash()`, `setFlashes()` `getFlash()`, `hasFlash()`, and `removeFlash()`. Use `getFlashBag() instead which returns a `FlashBagInterface`. * `Session->clear()` now only clears session attributes as before it cleared flash messages and attributes. `Session->getFlashBag()->all()` clears flashes now. diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index d85768c96d..468040e3aa 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -315,13 +315,4 @@ class Session implements SessionInterface { return $this->getBag('flashes')->clear(); } - - /** - * This method does not do anything. - * - * @deprecated since 2.1, will be removed from 2.3 - */ - public function close() - { - } }