From 29bd787b7e4828ac9f28c5714a46ff455f587113 Mon Sep 17 00:00:00 2001 From: Drak Date: Wed, 28 Mar 2012 21:07:12 +0545 Subject: [PATCH 1/8] [HttpFoundation] Added some basic meta-data to Session This commit allows applications to know certain meta-data about the session Session storage is designed to only store some data against a session ID so this method is necessary to be compatible with any session handler, including native handlers. --- .../HttpFoundation/Session/MetaBag.php | 121 ++++++++++++++++++ .../HttpFoundation/Session/Session.php | 74 +++++++---- .../Session/SessionInterface.php | 23 ++++ .../Tests/Session/SessionTest.php | 9 ++ .../HttpFoundation/Session/MetaBagTest.php | 100 +++++++++++++++ 5 files changed, 300 insertions(+), 27 deletions(-) create mode 100644 src/Symfony/Component/HttpFoundation/Session/MetaBag.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php diff --git a/src/Symfony/Component/HttpFoundation/Session/MetaBag.php b/src/Symfony/Component/HttpFoundation/Session/MetaBag.php new file mode 100644 index 0000000000..53b9bf6a33 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/MetaBag.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\Session; + +use Symfony\Component\HttpFoundation\Session\SessionBagInterface; + +/** + * Metadata container. + * + * Adds standard meta data to the session. + * + * @author Drak + */ +class MetaBag implements SessionBagInterface +{ + /** + * @var string + */ + private $name = '__meta'; + + /** + * @var string + */ + private $storageKey; + + /** + * @var array + */ + protected $meta = array(); + + /** + * Unix timestamp. + * + * @var integer + */ + private $lastUsed; + + /** + * Constructor. + * + * @param string $storageKey The key used to store flashes in the session. + */ + public function __construct($storageKey = '_sf2_meta') + { + $this->storageKey = $storageKey; + $this->meta = array('created' => 0, 'lastused' => 0); + } + + /** + * {@inheritdoc} + */ + public function initialize(array &$meta) + { + $this->meta = &$meta; + + if (isset($meta['created'])) { + $this->lastUsed = $this->meta['lastused']; + $this->meta['lastused'] = time(); + } else { + $this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = time(); + } + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } + + /** + * {@inheritdoc} + */ + public function getStorageKey() + { + return $this->storageKey; + } + + /** + * Gets the created timestamp meta data. + * + * @return integer Unix timestamp + */ + public function getCreated() + { + return $this->meta['created']; + } + + /** + * Gets the last used meta data. + * + * @return integer Unix timestamp + */ + public function getLastUsed() + { + return $this->lastUsed; + } + + /** + * {@inheritdoc} + */ + public function clear() + { + // nothing to do + } +} diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 145f96440f..d67cc55220 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -46,24 +46,34 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable */ private $attributeName; + /** + * @var string + */ + private $metaName; + /** * 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) + * @param MetaBag $meta A MetaBag instance (defaults null for default MetaBag) */ - public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) + public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, MetaBag $meta = null) { $this->storage = $storage ?: new NativeSessionStorage(); - $attributeBag = $attributes ?: new AttributeBag(); - $this->attributeName = $attributeBag->getName(); - $this->registerBag($attributeBag); + $attributes = $attributes ?: new AttributeBag(); + $this->attributeName = $attributes->getName(); + $this->registerBag($attributes); - $flashBag = $flashes ?: new FlashBag(); - $this->flashName = $flashBag->getName(); - $this->registerBag($flashBag); + $flashes = $flashes ?: new FlashBag(); + $this->flashName = $flashes->getName(); + $this->registerBag($flashes); + + $metaBag = $meta ?: new MetaBag(); + $this->metaName = $metaBag->getName(); + $this->registerBag($metaBag); } /** @@ -130,6 +140,26 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable $this->storage->getBag($this->attributeName)->clear(); } + /** + * Returns an iterator for attributes. + * + * @return \ArrayIterator An \ArrayIterator instance + */ + public function getIterator() + { + return new \ArrayIterator($this->storage->getBag($this->attributeName)->all()); + } + + /** + * Returns the number of attributes. + * + * @return int The number of attributes + */ + public function count() + { + return count($this->storage->getBag($this->attributeName)->all()); + } + /** * {@inheritdoc} */ @@ -188,6 +218,16 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable $this->storage->setName($name); } + /** + * Gets session meta. + * + * @return MetaBag + */ + public function getMeta() + { + return $this->getBag($this->metaName); + } + /** * Registers a SessionBagInterface with the session. * @@ -310,24 +350,4 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable { return $this->getBag($this->flashName)->clear(); } - - /** - * Returns an iterator for attributes. - * - * @return \ArrayIterator An \ArrayIterator instance - */ - public function getIterator() - { - return new \ArrayIterator($this->storage->getBag('attributes')->all()); - } - - /** - * Returns the number of attributes. - * - * @return int The number of attributes - */ - public function count() - { - return count($this->storage->getBag('attributes')->all()); - } } diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 4e4962de4e..7cd6f95ea4 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -164,4 +164,27 @@ interface SessionInterface * @api */ function clear(); + + /** + * Gets session meta. + * + * @return MetaBag + */ + public function getMeta(); + + /** + * Registers a SessionBagInterface with the session. + * + * @param SessionBagInterface $bag + */ + public function registerBag(SessionBagInterface $bag); + + /** + * Get's a bag instance. + * + * @param string $name + * + * @return SessionBagInterface + */ + public function getBag($name); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index 79a4068a5d..9505a7450d 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpFoundation\Tests\Session; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\MetaBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; @@ -253,4 +254,12 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->assertEquals(2, count($this->session)); } + + public function testGetMeta() + { + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\MetaBag', $this->session->getMeta()); + $metaBag = new MetaBag(); + $session = new Session($this->storage, new AttributeBag(), new FlashBag(), $metaBag); + $this->assertSame($metaBag, $session->getBag($metaBag->getName())); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php new file mode 100644 index 0000000000..e6ca9911a9 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpFoundation\Session; + +use Symfony\Component\HttpFoundation\Session\MetaBag; + +/** + * Test class for MetaBag. + */ +class MetaBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var MetaBag + */ + protected $bag; + + /** + * @var array + */ + protected $array = array(); + + protected function setUp() + { + $this->bag = new MetaBag(); + $this->array = array('created' => 1234567, 'lastused' => 12345678); + $this->bag->initialize($this->array); + } + + protected function tearDown() + { + $this->array = array(); + $this->bag = null; + } + + public function testInitialize() + { + $p = new \ReflectionProperty('Symfony\Component\HttpFoundation\Session\MetaBag', 'meta'); + $p->setAccessible(true); + + $bag1 = new MetaBag(); + $array = array(); + $bag1->initialize($array); + $this->assertGreaterThanOrEqual(time(), $bag1->getCreated()); + $this->assertEquals($bag1->getCreated(), $bag1->getLastUsed()); + + sleep(1); + $bag2 = new MetaBag(); + $array2 = $p->getValue($bag1); + $bag2->initialize($array2); + $this->assertEquals($bag1->getCreated(), $bag2->getCreated()); + $this->assertEquals($bag1->getLastUsed(), $bag2->getLastUsed()); + $this->assertEquals($bag2->getCreated(), $bag2->getLastUsed()); + + sleep(1); + $bag3 = new MetaBag(); + $array3 = $p->getValue($bag2); + $bag3->initialize($array3); + $this->assertEquals($bag1->getCreated(), $bag3->getCreated()); + $this->assertGreaterThan($bag2->getLastUsed(), $bag3->getLastUsed()); + $this->assertNotEquals($bag3->getCreated(), $bag3->getLastUsed()); + } + + public function testGetSetName() + { + $this->assertEquals('__meta', $this->bag->getName()); + $this->bag->setName('foo'); + $this->assertEquals('foo', $this->bag->getName()); + + } + + public function testGetStorageKey() + { + $this->assertEquals('_sf2_meta', $this->bag->getStorageKey()); + } + + public function testGetCreated() + { + $this->assertEquals(1234567, $this->bag->getCreated()); + } + + public function testGetLastUsed() + { + $this->assertLessThanOrEqual(time(), $this->bag->getLastUsed()); + } + + public function testClear() + { + $this->bag->clear(); + } + +} From d9fd14f26133564746dfd324e438b620a0e4eaf4 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 29 Mar 2012 12:47:14 +0545 Subject: [PATCH 2/8] [HttpFoundation] Refactored for moved tests location. --- .../Component/HttpFoundation/Tests}/Session/MetaBagTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {tests/Symfony/Tests/Component/HttpFoundation => src/Symfony/Component/HttpFoundation/Tests}/Session/MetaBagTest.php (97%) diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/MetaBagTest.php similarity index 97% rename from tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php rename to src/Symfony/Component/HttpFoundation/Tests/Session/MetaBagTest.php index e6ca9911a9..f0bd48cf92 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/MetaBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/MetaBagTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Tests\Component\HttpFoundation\Session; +namespace Symfony\Component\HttpFoundation\Tests\Session; use Symfony\Component\HttpFoundation\Session\MetaBag; From 402254ca7e075b6effb323bb16a5e83670fb6979 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 29 Mar 2012 17:15:37 +0545 Subject: [PATCH 3/8] [HttpFoundation] Changed meta-data responsibility to SessionStorageInterface Added cookie_lifetime to the meta-data. This allows to know how old a cookie is and when the cookie will expire. --- .../HttpFoundation/Session/Session.php | 15 ++---- .../Session/{ => Storage}/MetaBag.php | 40 ++++++++++++---- .../Storage/MockArraySessionStorage.php | 44 ++++++++++++++++-- .../Storage/MockFileSessionStorage.php | 5 ++ .../Session/Storage/NativeSessionStorage.php | 46 +++++++++++++++++-- .../Tests/Session/SessionTest.php | 5 +- .../Session/{ => Storage}/MetaBagTest.php | 6 +-- 7 files changed, 126 insertions(+), 35 deletions(-) rename src/Symfony/Component/HttpFoundation/Session/{ => Storage}/MetaBag.php (71%) rename src/Symfony/Component/HttpFoundation/Tests/Session/{ => Storage}/MetaBagTest.php (93%) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index d67cc55220..a049622ea4 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpFoundation\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; @@ -46,20 +47,14 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable */ private $attributeName; - /** - * @var string - */ - private $metaName; - /** * 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) - * @param MetaBag $meta A MetaBag instance (defaults null for default MetaBag) */ - public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, MetaBag $meta = null) + public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null) { $this->storage = $storage ?: new NativeSessionStorage(); @@ -70,10 +65,6 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable $flashes = $flashes ?: new FlashBag(); $this->flashName = $flashes->getName(); $this->registerBag($flashes); - - $metaBag = $meta ?: new MetaBag(); - $this->metaName = $metaBag->getName(); - $this->registerBag($metaBag); } /** @@ -225,7 +216,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable */ public function getMeta() { - return $this->getBag($this->metaName); + return $this->storage->getMetaBag(); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/MetaBag.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php similarity index 71% rename from src/Symfony/Component/HttpFoundation/Session/MetaBag.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php index 53b9bf6a33..4506687316 100644 --- a/src/Symfony/Component/HttpFoundation/Session/MetaBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\Session; +namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; @@ -47,12 +47,12 @@ class MetaBag implements SessionBagInterface /** * Constructor. * - * @param string $storageKey The key used to store flashes in the session. + * @param string $storageKey The key used to store bag in the session. */ public function __construct($storageKey = '_sf2_meta') { $this->storageKey = $storageKey; - $this->meta = array('created' => 0, 'lastused' => 0); + $this->meta = array('created' => 0, 'lastused' => 0, 'lifetime' => 0); } /** @@ -66,21 +66,23 @@ class MetaBag implements SessionBagInterface $this->lastUsed = $this->meta['lastused']; $this->meta['lastused'] = time(); } else { - $this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = time(); + $this->stampCreated(); } } /** - * {@inheritdoc} + * Gets the lifetime that this cooke was set with. + * + * @return integer */ - public function getName() + public function getLifetime() { - return $this->name; + return $this->meta['lifetime']; } - public function setName($name) + public function stampNew() { - $this->name = $name; + $this->stampCreated(); } /** @@ -118,4 +120,24 @@ class MetaBag implements SessionBagInterface { // nothing to do } + + /** + * {@inheritdoc} + */ + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } + + private function stampCreated() + { + $timeStamp = time();//(null === $timeStamp) ? time() : $timeStamp; + $this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = $timeStamp; + $this->meta['lifetime'] = ini_get('session.cookie_lifetime'); + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 6f1e279f41..3dc8fa1a87 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; +use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; /** * MockArraySessionStorage mocks the session for unit tests. @@ -52,14 +53,21 @@ class MockArraySessionStorage implements SessionStorageInterface */ protected $data = array(); + /** + * @var MetaBag + */ + protected $metaBag; + /** * Constructor. * - * @param string $name Session name + * @param string $name Session name + * @param MetaBag $metaBag MetaBag instance. */ - public function __construct($name = 'MOCKSESSID') + public function __construct($name = 'MOCKSESSID', MetaBag $metaBag = null) { $this->name = $name; + $this->setMetaBag($metaBag); } /** @@ -100,6 +108,10 @@ class MockArraySessionStorage implements SessionStorageInterface $this->start(); } + if ($destroy) { + $this->metaBag->stampNew(); + } + $this->id = $this->generateId(); return true; @@ -191,6 +203,30 @@ class MockArraySessionStorage implements SessionStorageInterface return $this->bags[$name]; } + /** + * Sets the metabag. + * + * @param MetaBag $metaBag + */ + public function setMetaBag(MetaBag $metaBag = null) + { + if (null === $metaBag) { + $metaBag = new MetaBag(); + } + + $this->metaBag = $metaBag; + } + + /** + * Gets the MetaBag. + * + * @return MetaBag + */ + public function getMetaBag() + { + return $this->metaBag; + } + /** * Generates a session ID. * @@ -206,7 +242,9 @@ class MockArraySessionStorage implements SessionStorageInterface protected function loadSession() { - foreach ($this->bags as $bag) { + $bags = array_merge($this->bags, array($this->metaBag)); + + foreach ($bags as $bag) { $key = $bag->getStorageKey(); $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array(); $bag->initialize($this->data[$key]); diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index 24457319f9..f39d4e9c9f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -75,8 +75,13 @@ class MockFileSessionStorage extends MockArraySessionStorage */ public function regenerate($destroy = false) { + if (!$this->started) { + $this->start(); + } + if ($destroy) { $this->destroy(); + $this->metaBag->stampNew(); } $this->id = $this->generateId(); diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index ed591cad21..fd3fb8a99c 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; +use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; @@ -45,6 +46,11 @@ class NativeSessionStorage implements SessionStorageInterface */ protected $saveHandler; + /** + * @var MetaBag + */ + protected $metaBag; + /** * Constructor. * @@ -83,10 +89,11 @@ class NativeSessionStorage implements SessionStorageInterface * upload_progress.min-freq, "1" * url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset=" * - * @param array $options Session configuration options. - * @param object $handler SessionHandlerInterface. + * @param array $options Session configuration options. + * @param object $handler SessionHandlerInterface. + * @param MetaBag $handler MetaBag. */ - public function __construct(array $options = array(), $handler = null) + public function __construct(array $options = array(), $handler = null, MetaBag $metaBag = null) { // sensible defaults ini_set('session.auto_start', 0); // by default we prefer to explicitly start the session using the class. @@ -99,6 +106,7 @@ class NativeSessionStorage implements SessionStorageInterface register_shutdown_function('session_write_close'); } + $this->setMetaBag($metaBag); $this->setOptions($options); $this->setSaveHandler($handler); } @@ -189,6 +197,10 @@ class NativeSessionStorage implements SessionStorageInterface */ public function regenerate($destroy = false) { + if ($destroy) { + $this->metaBag->stampNew(); + } + return session_regenerate_id($destroy); } @@ -249,6 +261,30 @@ class NativeSessionStorage implements SessionStorageInterface return $this->bags[$name]; } + /** + * Sets the metabag. + * + * @param MetaBag $metaBag + */ + public function setMetaBag(MetaBag $metaBag = null) + { + if (null === $metaBag) { + $metaBag = new MetaBag(); + } + + $this->metaBag = $metaBag; + } + + /** + * Gets the MetaBag. + * + * @return MetaBag + */ + public function getMetaBag() + { + return $this->metaBag; + } + /** * Sets session.* ini variables. * @@ -338,7 +374,9 @@ class NativeSessionStorage implements SessionStorageInterface $session = &$_SESSION; } - foreach ($this->bags as $bag) { + $bags = array_merge($this->bags, array($this->metaBag)); + + foreach ($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/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index 9505a7450d..cb903369c6 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -257,9 +257,6 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function testGetMeta() { - $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\MetaBag', $this->session->getMeta()); - $metaBag = new MetaBag(); - $session = new Session($this->storage, new AttributeBag(), new FlashBag(), $metaBag); - $this->assertSame($metaBag, $session->getBag($metaBag->getName())); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetaBag', $this->session->getMeta()); } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/MetaBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php similarity index 93% rename from src/Symfony/Component/HttpFoundation/Tests/Session/MetaBagTest.php rename to src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php index f0bd48cf92..45730e8aa7 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/MetaBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php @@ -9,9 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\HttpFoundation\Tests\Session; +namespace Symfony\Component\HttpFoundation\Tests\Session\Storage; -use Symfony\Component\HttpFoundation\Session\MetaBag; +use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; /** * Test class for MetaBag. @@ -43,7 +43,7 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase public function testInitialize() { - $p = new \ReflectionProperty('Symfony\Component\HttpFoundation\Session\MetaBag', 'meta'); + $p = new \ReflectionProperty('Symfony\Component\HttpFoundation\Session\Storage\MetaBag', 'meta'); $p->setAccessible(true); $bag1 = new MetaBag(); From ec3f88f339484f869a02a51d18e4dd58d2be5a6f Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 29 Mar 2012 17:18:17 +0545 Subject: [PATCH 4/8] [HttpFoundation] Add methods to interface --- .../HttpFoundation/Session/SessionInterface.php | 14 +++++++------- .../Session/Storage/SessionStorageInterface.php | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 7cd6f95ea4..5fe264eb65 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -165,13 +165,6 @@ interface SessionInterface */ function clear(); - /** - * Gets session meta. - * - * @return MetaBag - */ - public function getMeta(); - /** * Registers a SessionBagInterface with the session. * @@ -187,4 +180,11 @@ interface SessionInterface * @return SessionBagInterface */ public function getBag($name); + + /** + * Gets session meta. + * + * @return MetaBag + */ + public function getMeta(); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 8bf2e5d32a..058d017380 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; +use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; /** * StorageInterface. @@ -123,4 +124,9 @@ interface SessionStorageInterface * @param SessionBagInterface $bag */ function registerBag(SessionBagInterface $bag); + + /** + * @return MetaBag + */ + function getMetaBag(); } From 39141e865b997a99f7f3b38bb2fd0d0a7a16031f Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 29 Mar 2012 18:02:06 +0545 Subject: [PATCH 5/8] [HttpFoundation] Add ability to force the lifetime (allows update of session cookie expiry-time) --- .../HttpFoundation/Session/Storage/MetaBag.php | 13 ++++++++----- .../Tests/Session/Storage/MetaBagTest.php | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php index 4506687316..b2abcd8d68 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php @@ -80,9 +80,12 @@ class MetaBag implements SessionBagInterface return $this->meta['lifetime']; } - public function stampNew() + /** + * Stamps a new session's meta. + */ + public function stampNew($lifetime = null) { - $this->stampCreated(); + $this->stampCreated($lifetime); } /** @@ -134,10 +137,10 @@ class MetaBag implements SessionBagInterface $this->name = $name; } - private function stampCreated() + private function stampCreated($lifetime = null) { - $timeStamp = time();//(null === $timeStamp) ? time() : $timeStamp; + $timeStamp = time(); $this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = $timeStamp; - $this->meta['lifetime'] = ini_get('session.cookie_lifetime'); + $this->meta['lifetime'] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime; } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php index 45730e8aa7..a3b578c4cb 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php @@ -96,5 +96,4 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase { $this->bag->clear(); } - } From 2f03b31258bacff81142a8a1b9ee373d2bcefa33 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 30 Mar 2012 22:47:35 +0545 Subject: [PATCH 6/8] [HttpFoundation] Added the ability to change the session cookie lifetime on migrate(). This is a very important option which allows the cookie lifetime to be changed on migrate. For example when a user converts from an anonymous session to a logged in session one might wish to change from a persistent cookie to browser session (e.g. a banking application). --- .../HttpFoundation/Session/Session.php | 24 +++++++------------ .../Session/SessionInterface.php | 17 +++++++++---- .../Session/Storage/MetaBag.php | 14 +++++++++-- .../Storage/MockArraySessionStorage.php | 8 ++----- .../Storage/MockFileSessionStorage.php | 9 +++---- .../Session/Storage/NativeSessionStorage.php | 6 ++++- .../Storage/SessionStorageInterface.php | 8 +++++-- 7 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index a049622ea4..c68bd30fce 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -154,19 +154,19 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable /** * {@inheritdoc} */ - public function invalidate() + public function invalidate($lifetime = null) { $this->storage->clear(); - return $this->storage->regenerate(true); + return $this->migrate(true, $lifetime); } /** * {@inheritdoc} */ - public function migrate($destroy = false) + public function migrate($destroy = false, $lifetime = null) { - return $this->storage->regenerate($destroy); + return $this->storage->regenerate($destroy, $lifetime); } /** @@ -210,19 +210,15 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable } /** - * Gets session meta. - * - * @return MetaBag + * {@iheritdoc} */ - public function getMeta() + public function getMetadata() { return $this->storage->getMetaBag(); } /** - * Registers a SessionBagInterface with the session. - * - * @param SessionBagInterface $bag + * {@iheritdoc} */ public function registerBag(SessionBagInterface $bag) { @@ -230,11 +226,7 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable } /** - * Get's a bag instance. - * - * @param string $name - * - * @return SessionBagInterface + * {@iheritdoc} */ public function getBag($name) { diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index 5fe264eb65..bfd7f6abb8 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -71,23 +71,32 @@ interface SessionInterface * Clears all session attributes and flashes and regenerates the * session and deletes the old session from persistence. * + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. + * * @return Boolean True if session invalidated, false if error. * * @api */ - function invalidate(); + function invalidate($lifetime = null); /** * 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. + * @param Boolean $destroy Whether to delete the old session or leave it to garbage collection. + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. * * @return Boolean True if session migrated, false if error. * * @api */ - function migrate($destroy = false); + function migrate($destroy = false, $lifetime = null); /** * Force the session to be saved and closed. @@ -173,7 +182,7 @@ interface SessionInterface public function registerBag(SessionBagInterface $bag); /** - * Get's a bag instance. + * Gets a bag instance by name. * * @param string $name * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php index b2abcd8d68..0bb160f843 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php @@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * Metadata container. * - * Adds standard meta data to the session. + * Adds meta data to the session. * * @author Drak */ @@ -71,7 +71,7 @@ class MetaBag implements SessionBagInterface } /** - * Gets the lifetime that this cooke was set with. + * Gets the lifetime that the session cookie was set with. * * @return integer */ @@ -82,6 +82,11 @@ class MetaBag implements SessionBagInterface /** * Stamps a new session's meta. + * + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. */ public function stampNew($lifetime = null) { @@ -132,6 +137,11 @@ class MetaBag implements SessionBagInterface return $this->name; } + /** + * Sets name. + * + * @param string $name + */ public function setName($name) { $this->name = $name; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 3dc8fa1a87..f77116a014 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -98,20 +98,16 @@ class MockArraySessionStorage implements SessionStorageInterface return true; } - /** * {@inheritdoc} */ - public function regenerate($destroy = false) + public function regenerate($destroy = false, $lifetime = null) { if (!$this->started) { $this->start(); } - if ($destroy) { - $this->metaBag->stampNew(); - } - + $this->metaBag->stampNew($lifetime); $this->id = $this->generateId(); return true; diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index f39d4e9c9f..56e6513800 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -73,20 +73,17 @@ class MockFileSessionStorage extends MockArraySessionStorage /** * {@inheritdoc} */ - public function regenerate($destroy = false) + public function regenerate($destroy = false, $lifetime = null) { if (!$this->started) { $this->start(); } - + if ($destroy) { $this->destroy(); - $this->metaBag->stampNew(); } - $this->id = $this->generateId(); - - return true; + return parent::regenerate($destroy, $lifetime); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index fd3fb8a99c..72b3f5c79a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -195,8 +195,12 @@ class NativeSessionStorage implements SessionStorageInterface /** * {@inheritdoc} */ - public function regenerate($destroy = false) + public function regenerate($destroy = false, $lifetime = null) { + if (null !== $lifetime) { + ini_set('session.cookie_lifetime', $lifetime); + } + if ($destroy) { $this->metaBag->stampNew(); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 058d017380..1c1014b89f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -82,7 +82,11 @@ interface SessionStorageInterface * Note regenerate+destroy should not clear the session data in memory * only delete the session data from persistent storage. * - * @param Boolean $destroy Destroy session when regenerating? + * @param Boolean $destroy Destroy session when regenerating? + * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value + * will leave the system settings unchanged, 0 sets the cookie + * to expire with browser session. Time is in seconds, and is + * not a Unix timestamp. * * @return Boolean True if session regenerated, false if error * @@ -90,7 +94,7 @@ interface SessionStorageInterface * * @api */ - function regenerate($destroy = false); + function regenerate($destroy = false, $lifetime = null); /** * Force the session to be saved and closed. From 4fc04fae18ac40e7d0401430e95f4d73ffa5fe2b Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 31 Mar 2012 09:00:05 +0545 Subject: [PATCH 7/8] [HttpFoundation] Renamed MetaBag to MetadataBag --- .../HttpFoundation/Session/Session.php | 6 ++-- .../Session/SessionInterface.php | 4 +-- .../Storage/{MetaBag.php => MetadataBag.php} | 36 ++++++++++--------- .../Storage/MockArraySessionStorage.php | 36 +++++++++---------- .../Session/Storage/NativeSessionStorage.php | 34 +++++++++--------- .../Storage/SessionStorageInterface.php | 6 ++-- .../Tests/Session/SessionTest.php | 4 +-- .../{MetaBagTest.php => MetadataBagTest.php} | 22 ++++++------ 8 files changed, 76 insertions(+), 72 deletions(-) rename src/Symfony/Component/HttpFoundation/Session/Storage/{MetaBag.php => MetadataBag.php} (72%) rename src/Symfony/Component/HttpFoundation/Tests/Session/Storage/{MetaBagTest.php => MetadataBagTest.php} (79%) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index c68bd30fce..cbadcd38a2 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\MetaBag; +use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; @@ -212,9 +212,9 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable /** * {@iheritdoc} */ - public function getMetadata() + public function getMetadataBag() { - return $this->storage->getMetaBag(); + return $this->storage->getMetadataBag(); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php index bfd7f6abb8..15b4c2924c 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionInterface.php @@ -193,7 +193,7 @@ interface SessionInterface /** * Gets session meta. * - * @return MetaBag + * @return MetadataBag */ - public function getMeta(); + public function getMetadataBag(); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MetadataBag.php similarity index 72% rename from src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php rename to src/Symfony/Component/HttpFoundation/Session/Storage/MetadataBag.php index 0bb160f843..892d004b5d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MetaBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MetadataBag.php @@ -16,16 +16,20 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; /** * Metadata container. * - * Adds meta data to the session. + * Adds metadata to the session. * * @author Drak */ -class MetaBag implements SessionBagInterface +class MetadataBag implements SessionBagInterface { + const CREATED = 'c'; + const UPDATED = 'u'; + const LIFETIME = 'l'; + /** * @var string */ - private $name = '__meta'; + private $name = '__metadata'; /** * @var string @@ -52,19 +56,19 @@ class MetaBag implements SessionBagInterface public function __construct($storageKey = '_sf2_meta') { $this->storageKey = $storageKey; - $this->meta = array('created' => 0, 'lastused' => 0, 'lifetime' => 0); + $this->meta = array(self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0); } /** * {@inheritdoc} */ - public function initialize(array &$meta) + public function initialize(array &$array) { - $this->meta = &$meta; + $this->meta = &$array; - if (isset($meta['created'])) { - $this->lastUsed = $this->meta['lastused']; - $this->meta['lastused'] = time(); + if (isset($array[self::CREATED])) { + $this->lastUsed = $this->meta[self::UPDATED]; + $this->meta[self::UPDATED] = time(); } else { $this->stampCreated(); } @@ -77,11 +81,11 @@ class MetaBag implements SessionBagInterface */ public function getLifetime() { - return $this->meta['lifetime']; + return $this->meta[self::LIFETIME]; } /** - * Stamps a new session's meta. + * Stamps a new session's metadata. * * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value * will leave the system settings unchanged, 0 sets the cookie @@ -102,17 +106,17 @@ class MetaBag implements SessionBagInterface } /** - * Gets the created timestamp meta data. + * Gets the created timestamp metadata. * * @return integer Unix timestamp */ public function getCreated() { - return $this->meta['created']; + return $this->meta[self::CREATED]; } /** - * Gets the last used meta data. + * Gets the last used metadata. * * @return integer Unix timestamp */ @@ -150,7 +154,7 @@ class MetaBag implements SessionBagInterface private function stampCreated($lifetime = null) { $timeStamp = time(); - $this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = $timeStamp; - $this->meta['lifetime'] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime; + $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp; + $this->meta[self::LIFETIME] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime; } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index f77116a014..5500a03419 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; -use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; +use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; /** * MockArraySessionStorage mocks the session for unit tests. @@ -54,20 +54,20 @@ class MockArraySessionStorage implements SessionStorageInterface protected $data = array(); /** - * @var MetaBag + * @var MetadataBag */ - protected $metaBag; + protected $metadataBag; /** * Constructor. * * @param string $name Session name - * @param MetaBag $metaBag MetaBag instance. + * @param MetadataBag $metaBag MetadataBag instance. */ - public function __construct($name = 'MOCKSESSID', MetaBag $metaBag = null) + public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null) { $this->name = $name; - $this->setMetaBag($metaBag); + $this->setMetadataBag($metaBag); } /** @@ -107,7 +107,7 @@ class MockArraySessionStorage implements SessionStorageInterface $this->start(); } - $this->metaBag->stampNew($lifetime); + $this->metadataBag->stampNew($lifetime); $this->id = $this->generateId(); return true; @@ -200,27 +200,27 @@ class MockArraySessionStorage implements SessionStorageInterface } /** - * Sets the metabag. + * Sets the MetadataBag. * - * @param MetaBag $metaBag + * @param MetadataBag $bag */ - public function setMetaBag(MetaBag $metaBag = null) + public function setMetadataBag(MetadataBag $bag = null) { - if (null === $metaBag) { - $metaBag = new MetaBag(); + if (null === $bag) { + $bag = new MetadataBag(); } - $this->metaBag = $metaBag; + $this->metadataBag = $bag; } /** - * Gets the MetaBag. + * Gets the MetadataBag. * - * @return MetaBag + * @return MetadataBag */ - public function getMetaBag() + public function getMetadataBag() { - return $this->metaBag; + return $this->metadataBag; } /** @@ -238,7 +238,7 @@ class MockArraySessionStorage implements SessionStorageInterface protected function loadSession() { - $bags = array_merge($this->bags, array($this->metaBag)); + $bags = array_merge($this->bags, array($this->metadataBag)); foreach ($bags as $bag) { $key = $bag->getStorageKey(); diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index 72b3f5c79a..215ed26ab0 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; -use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; +use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; @@ -47,9 +47,9 @@ class NativeSessionStorage implements SessionStorageInterface protected $saveHandler; /** - * @var MetaBag + * @var MetadataBag */ - protected $metaBag; + protected $metadataBag; /** * Constructor. @@ -91,9 +91,9 @@ class NativeSessionStorage implements SessionStorageInterface * * @param array $options Session configuration options. * @param object $handler SessionHandlerInterface. - * @param MetaBag $handler MetaBag. + * @param MetadataBag $handler MetadataBag. */ - public function __construct(array $options = array(), $handler = null, MetaBag $metaBag = null) + public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null) { // sensible defaults ini_set('session.auto_start', 0); // by default we prefer to explicitly start the session using the class. @@ -106,7 +106,7 @@ class NativeSessionStorage implements SessionStorageInterface register_shutdown_function('session_write_close'); } - $this->setMetaBag($metaBag); + $this->setMetadataBag($metaBag); $this->setOptions($options); $this->setSaveHandler($handler); } @@ -202,7 +202,7 @@ class NativeSessionStorage implements SessionStorageInterface } if ($destroy) { - $this->metaBag->stampNew(); + $this->metadataBag->stampNew(); } return session_regenerate_id($destroy); @@ -266,27 +266,27 @@ class NativeSessionStorage implements SessionStorageInterface } /** - * Sets the metabag. + * Sets the MetadataBag. * - * @param MetaBag $metaBag + * @param MetadataBag $metaBag */ - public function setMetaBag(MetaBag $metaBag = null) + public function setMetadataBag(MetadataBag $metaBag = null) { if (null === $metaBag) { - $metaBag = new MetaBag(); + $metaBag = new MetadataBag(); } - $this->metaBag = $metaBag; + $this->metadataBag = $metaBag; } /** - * Gets the MetaBag. + * Gets the MetadataBag. * - * @return MetaBag + * @return MetadataBag */ - public function getMetaBag() + public function getMetadataBag() { - return $this->metaBag; + return $this->metadataBag; } /** @@ -378,7 +378,7 @@ class NativeSessionStorage implements SessionStorageInterface $session = &$_SESSION; } - $bags = array_merge($this->bags, array($this->metaBag)); + $bags = array_merge($this->bags, array($this->metadataBag)); foreach ($bags as $bag) { $key = $bag->getStorageKey(); diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 1c1014b89f..20c533c2eb 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; -use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; +use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; /** * StorageInterface. @@ -130,7 +130,7 @@ interface SessionStorageInterface function registerBag(SessionBagInterface $bag); /** - * @return MetaBag + * @return MetadataBag */ - function getMetaBag(); + function getMetadataBag(); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index cb903369c6..bd45b2fbe6 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -12,7 +12,7 @@ namespace Symfony\Component\HttpFoundation\Tests\Session; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\MetaBag; +use Symfony\Component\HttpFoundation\Session\MetadataBag; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; @@ -257,6 +257,6 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function testGetMeta() { - $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetaBag', $this->session->getMeta()); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag()); } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetadataBagTest.php similarity index 79% rename from src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php rename to src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetadataBagTest.php index a3b578c4cb..39f268b4b5 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetaBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetadataBagTest.php @@ -11,15 +11,15 @@ namespace Symfony\Component\HttpFoundation\Tests\Session\Storage; -use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; +use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; /** - * Test class for MetaBag. + * Test class for MetadataBag. */ -class MetaBagTest extends \PHPUnit_Framework_TestCase +class MetadataBagTest extends \PHPUnit_Framework_TestCase { /** - * @var MetaBag + * @var MetadataBag */ protected $bag; @@ -30,8 +30,8 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->bag = new MetaBag(); - $this->array = array('created' => 1234567, 'lastused' => 12345678); + $this->bag = new MetadataBag(); + $this->array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 0); $this->bag->initialize($this->array); } @@ -43,17 +43,17 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase public function testInitialize() { - $p = new \ReflectionProperty('Symfony\Component\HttpFoundation\Session\Storage\MetaBag', 'meta'); + $p = new \ReflectionProperty('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', 'meta'); $p->setAccessible(true); - $bag1 = new MetaBag(); + $bag1 = new MetadataBag(); $array = array(); $bag1->initialize($array); $this->assertGreaterThanOrEqual(time(), $bag1->getCreated()); $this->assertEquals($bag1->getCreated(), $bag1->getLastUsed()); sleep(1); - $bag2 = new MetaBag(); + $bag2 = new MetadataBag(); $array2 = $p->getValue($bag1); $bag2->initialize($array2); $this->assertEquals($bag1->getCreated(), $bag2->getCreated()); @@ -61,7 +61,7 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals($bag2->getCreated(), $bag2->getLastUsed()); sleep(1); - $bag3 = new MetaBag(); + $bag3 = new MetadataBag(); $array3 = $p->getValue($bag2); $bag3->initialize($array3); $this->assertEquals($bag1->getCreated(), $bag3->getCreated()); @@ -71,7 +71,7 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase public function testGetSetName() { - $this->assertEquals('__meta', $this->bag->getName()); + $this->assertEquals('__metadata', $this->bag->getName()); $this->bag->setName('foo'); $this->assertEquals('foo', $this->bag->getName()); From 8a0e6d24bc96551931ef2abe613b4dde77627e29 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 31 Mar 2012 22:36:36 +0545 Subject: [PATCH 8/8] [HttpFoundation] Update changelog. --- CHANGELOG-2.1.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 65e06aeb08..d4a006d931 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -314,6 +314,9 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * Flash API can stores messages in an array so there may be multiple messages per flash type. The old `Session` class API remains without BC break as it will single messages as before. + * Added basic session meta-data to the session to record session create time, + last updated time, and the lifetime of the session cookie that was provided + to the client. ### HttpKernel