[HttpFoundation] Renamed MetaBag to MetadataBag

This commit is contained in:
Drak 2012-03-31 09:00:05 +05:45
parent 2f03b31258
commit 4fc04fae18
8 changed files with 76 additions and 72 deletions

View File

@ -11,7 +11,7 @@
namespace Symfony\Component\HttpFoundation\Session; 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\Storage\SessionStorageInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
@ -212,9 +212,9 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
/** /**
* {@iheritdoc} * {@iheritdoc}
*/ */
public function getMetadata() public function getMetadataBag()
{ {
return $this->storage->getMetaBag(); return $this->storage->getMetadataBag();
} }
/** /**

View File

@ -193,7 +193,7 @@ interface SessionInterface
/** /**
* Gets session meta. * Gets session meta.
* *
* @return MetaBag * @return MetadataBag
*/ */
public function getMeta(); public function getMetadataBag();
} }

View File

@ -16,16 +16,20 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
/** /**
* Metadata container. * Metadata container.
* *
* Adds meta data to the session. * Adds metadata to the session.
* *
* @author Drak <drak@zikula.org> * @author Drak <drak@zikula.org>
*/ */
class MetaBag implements SessionBagInterface class MetadataBag implements SessionBagInterface
{ {
const CREATED = 'c';
const UPDATED = 'u';
const LIFETIME = 'l';
/** /**
* @var string * @var string
*/ */
private $name = '__meta'; private $name = '__metadata';
/** /**
* @var string * @var string
@ -52,19 +56,19 @@ class MetaBag implements SessionBagInterface
public function __construct($storageKey = '_sf2_meta') public function __construct($storageKey = '_sf2_meta')
{ {
$this->storageKey = $storageKey; $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} * {@inheritdoc}
*/ */
public function initialize(array &$meta) public function initialize(array &$array)
{ {
$this->meta = &$meta; $this->meta = &$array;
if (isset($meta['created'])) { if (isset($array[self::CREATED])) {
$this->lastUsed = $this->meta['lastused']; $this->lastUsed = $this->meta[self::UPDATED];
$this->meta['lastused'] = time(); $this->meta[self::UPDATED] = time();
} else { } else {
$this->stampCreated(); $this->stampCreated();
} }
@ -77,11 +81,11 @@ class MetaBag implements SessionBagInterface
*/ */
public function getLifetime() 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 * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value
* will leave the system settings unchanged, 0 sets the cookie * 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 * @return integer Unix timestamp
*/ */
public function getCreated() 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 * @return integer Unix timestamp
*/ */
@ -150,7 +154,7 @@ class MetaBag implements SessionBagInterface
private function stampCreated($lifetime = null) private function stampCreated($lifetime = null)
{ {
$timeStamp = time(); $timeStamp = time();
$this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = $timeStamp; $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
$this->meta['lifetime'] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime; $this->meta[self::LIFETIME] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime;
} }
} }

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\HttpFoundation\Session\Storage; namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; 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. * MockArraySessionStorage mocks the session for unit tests.
@ -54,20 +54,20 @@ class MockArraySessionStorage implements SessionStorageInterface
protected $data = array(); protected $data = array();
/** /**
* @var MetaBag * @var MetadataBag
*/ */
protected $metaBag; protected $metadataBag;
/** /**
* Constructor. * Constructor.
* *
* @param string $name Session name * @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->name = $name;
$this->setMetaBag($metaBag); $this->setMetadataBag($metaBag);
} }
/** /**
@ -107,7 +107,7 @@ class MockArraySessionStorage implements SessionStorageInterface
$this->start(); $this->start();
} }
$this->metaBag->stampNew($lifetime); $this->metadataBag->stampNew($lifetime);
$this->id = $this->generateId(); $this->id = $this->generateId();
return true; 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) { if (null === $bag) {
$metaBag = new MetaBag(); $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() protected function loadSession()
{ {
$bags = array_merge($this->bags, array($this->metaBag)); $bags = array_merge($this->bags, array($this->metadataBag));
foreach ($bags as $bag) { foreach ($bags as $bag) {
$key = $bag->getStorageKey(); $key = $bag->getStorageKey();

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\HttpFoundation\Session\Storage; namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; 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\NativeProxy;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
@ -47,9 +47,9 @@ class NativeSessionStorage implements SessionStorageInterface
protected $saveHandler; protected $saveHandler;
/** /**
* @var MetaBag * @var MetadataBag
*/ */
protected $metaBag; protected $metadataBag;
/** /**
* Constructor. * Constructor.
@ -91,9 +91,9 @@ class NativeSessionStorage implements SessionStorageInterface
* *
* @param array $options Session configuration options. * @param array $options Session configuration options.
* @param object $handler SessionHandlerInterface. * @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 // sensible defaults
ini_set('session.auto_start', 0); // by default we prefer to explicitly start the session using the class. 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'); register_shutdown_function('session_write_close');
} }
$this->setMetaBag($metaBag); $this->setMetadataBag($metaBag);
$this->setOptions($options); $this->setOptions($options);
$this->setSaveHandler($handler); $this->setSaveHandler($handler);
} }
@ -202,7 +202,7 @@ class NativeSessionStorage implements SessionStorageInterface
} }
if ($destroy) { if ($destroy) {
$this->metaBag->stampNew(); $this->metadataBag->stampNew();
} }
return session_regenerate_id($destroy); 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) { 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; $session = &$_SESSION;
} }
$bags = array_merge($this->bags, array($this->metaBag)); $bags = array_merge($this->bags, array($this->metadataBag));
foreach ($bags as $bag) { foreach ($bags as $bag) {
$key = $bag->getStorageKey(); $key = $bag->getStorageKey();

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\HttpFoundation\Session\Storage; namespace Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\MetaBag; use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
/** /**
* StorageInterface. * StorageInterface.
@ -130,7 +130,7 @@ interface SessionStorageInterface
function registerBag(SessionBagInterface $bag); function registerBag(SessionBagInterface $bag);
/** /**
* @return MetaBag * @return MetadataBag
*/ */
function getMetaBag(); function getMetadataBag();
} }

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\HttpFoundation\Tests\Session; namespace Symfony\Component\HttpFoundation\Tests\Session;
use Symfony\Component\HttpFoundation\Session\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\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
@ -257,6 +257,6 @@ class SessionTest extends \PHPUnit_Framework_TestCase
public function testGetMeta() 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());
} }
} }

View File

@ -11,15 +11,15 @@
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage; 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; protected $bag;
@ -30,8 +30,8 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase
protected function setUp() protected function setUp()
{ {
$this->bag = new MetaBag(); $this->bag = new MetadataBag();
$this->array = array('created' => 1234567, 'lastused' => 12345678); $this->array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 0);
$this->bag->initialize($this->array); $this->bag->initialize($this->array);
} }
@ -43,17 +43,17 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase
public function testInitialize() 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); $p->setAccessible(true);
$bag1 = new MetaBag(); $bag1 = new MetadataBag();
$array = array(); $array = array();
$bag1->initialize($array); $bag1->initialize($array);
$this->assertGreaterThanOrEqual(time(), $bag1->getCreated()); $this->assertGreaterThanOrEqual(time(), $bag1->getCreated());
$this->assertEquals($bag1->getCreated(), $bag1->getLastUsed()); $this->assertEquals($bag1->getCreated(), $bag1->getLastUsed());
sleep(1); sleep(1);
$bag2 = new MetaBag(); $bag2 = new MetadataBag();
$array2 = $p->getValue($bag1); $array2 = $p->getValue($bag1);
$bag2->initialize($array2); $bag2->initialize($array2);
$this->assertEquals($bag1->getCreated(), $bag2->getCreated()); $this->assertEquals($bag1->getCreated(), $bag2->getCreated());
@ -61,7 +61,7 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($bag2->getCreated(), $bag2->getLastUsed()); $this->assertEquals($bag2->getCreated(), $bag2->getLastUsed());
sleep(1); sleep(1);
$bag3 = new MetaBag(); $bag3 = new MetadataBag();
$array3 = $p->getValue($bag2); $array3 = $p->getValue($bag2);
$bag3->initialize($array3); $bag3->initialize($array3);
$this->assertEquals($bag1->getCreated(), $bag3->getCreated()); $this->assertEquals($bag1->getCreated(), $bag3->getCreated());
@ -71,7 +71,7 @@ class MetaBagTest extends \PHPUnit_Framework_TestCase
public function testGetSetName() public function testGetSetName()
{ {
$this->assertEquals('__meta', $this->bag->getName()); $this->assertEquals('__metadata', $this->bag->getName());
$this->bag->setName('foo'); $this->bag->setName('foo');
$this->assertEquals('foo', $this->bag->getName()); $this->assertEquals('foo', $this->bag->getName());