Skip to content

Commit 29bd787

Browse files
author
Drak
committed
[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.
1 parent 0c7b291 commit 29bd787

File tree

5 files changed

+300
-27
lines changed

5 files changed

+300
-27
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Session;
13+
14+
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15+
16+
/**
17+
* Metadata container.
18+
*
19+
* Adds standard meta data to the session.
20+
*
21+
* @author Drak <drak@zikula.org>
22+
*/
23+
class MetaBag implements SessionBagInterface
24+
{
25+
/**
26+
* @var string
27+
*/
28+
private $name = '__meta';
29+
30+
/**
31+
* @var string
32+
*/
33+
private $storageKey;
34+
35+
/**
36+
* @var array
37+
*/
38+
protected $meta = array();
39+
40+
/**
41+
* Unix timestamp.
42+
*
43+
* @var integer
44+
*/
45+
private $lastUsed;
46+
47+
/**
48+
* Constructor.
49+
*
50+
* @param string $storageKey The key used to store flashes in the session.
51+
*/
52+
public function __construct($storageKey = '_sf2_meta')
53+
{
54+
$this->storageKey = $storageKey;
55+
$this->meta = array('created' => 0, 'lastused' => 0);
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function initialize(array &$meta)
62+
{
63+
$this->meta = &$meta;
64+
65+
if (isset($meta['created'])) {
66+
$this->lastUsed = $this->meta['lastused'];
67+
$this->meta['lastused'] = time();
68+
} else {
69+
$this->meta['created'] = $this->meta['lastused'] = $this->lastUsed = time();
70+
}
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getName()
77+
{
78+
return $this->name;
79+
}
80+
81+
public function setName($name)
82+
{
83+
$this->name = $name;
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function getStorageKey()
90+
{
91+
return $this->storageKey;
92+
}
93+
94+
/**
95+
* Gets the created timestamp meta data.
96+
*
97+
* @return integer Unix timestamp
98+
*/
99+
public function getCreated()
100+
{
101+
return $this->meta['created'];
102+
}
103+
104+
/**
105+
* Gets the last used meta data.
106+
*
107+
* @return integer Unix timestamp
108+
*/
109+
public function getLastUsed()
110+
{
111+
return $this->lastUsed;
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function clear()
118+
{
119+
// nothing to do
120+
}
121+
}

src/Symfony/Component/HttpFoundation/Session/Session.php

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,34 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
4646
*/
4747
private $attributeName;
4848

49+
/**
50+
* @var string
51+
*/
52+
private $metaName;
53+
4954
/**
5055
* Constructor.
5156
*
5257
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
5358
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
5459
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
60+
* @param MetaBag $meta A MetaBag instance (defaults null for default MetaBag)
5561
*/
56-
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
62+
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, MetaBag $meta = null)
5763
{
5864
$this->storage = $storage ?: new NativeSessionStorage();
5965

60-
$attributeBag = $attributes ?: new AttributeBag();
61-
$this->attributeName = $attributeBag->getName();
62-
$this->registerBag($attributeBag);
66+
$attributes = $attributes ?: new AttributeBag();
67+
$this->attributeName = $attributes->getName();
68+
$this->registerBag($attributes);
69+
70+
$flashes = $flashes ?: new FlashBag();
71+
$this->flashName = $flashes->getName();
72+
$this->registerBag($flashes);
6373

64-
$flashBag = $flashes ?: new FlashBag();
65-
$this->flashName = $flashBag->getName();
66-
$this->registerBag($flashBag);
74+
$metaBag = $meta ?: new MetaBag();
75+
$this->metaName = $metaBag->getName();
76+
$this->registerBag($metaBag);
6777
}
6878

6979
/**
@@ -130,6 +140,26 @@ public function clear()
130140
$this->storage->getBag($this->attributeName)->clear();
131141
}
132142

143+
/**
144+
* Returns an iterator for attributes.
145+
*
146+
* @return \ArrayIterator An \ArrayIterator instance
147+
*/
148+
public function getIterator()
149+
{
150+
return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
151+
}
152+
153+
/**
154+
* Returns the number of attributes.
155+
*
156+
* @return int The number of attributes
157+
*/
158+
public function count()
159+
{
160+
return count($this->storage->getBag($this->attributeName)->all());
161+
}
162+
133163
/**
134164
* {@inheritdoc}
135165
*/
@@ -188,6 +218,16 @@ public function setName($name)
188218
$this->storage->setName($name);
189219
}
190220

221+
/**
222+
* Gets session meta.
223+
*
224+
* @return MetaBag
225+
*/
226+
public function getMeta()
227+
{
228+
return $this->getBag($this->metaName);
229+
}
230+
191231
/**
192232
* Registers a SessionBagInterface with the session.
193233
*
@@ -310,24 +350,4 @@ public function clearFlashes()
310350
{
311351
return $this->getBag($this->flashName)->clear();
312352
}
313-
314-
/**
315-
* Returns an iterator for attributes.
316-
*
317-
* @return \ArrayIterator An \ArrayIterator instance
318-
*/
319-
public function getIterator()
320-
{
321-
return new \ArrayIterator($this->storage->getBag('attributes')->all());
322-
}
323-
324-
/**
325-
* Returns the number of attributes.
326-
*
327-
* @return int The number of attributes
328-
*/
329-
public function count()
330-
{
331-
return count($this->storage->getBag('attributes')->all());
332-
}
333353
}

src/Symfony/Component/HttpFoundation/Session/SessionInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,27 @@ function remove($name);
164164
* @api
165165
*/
166166
function clear();
167+
168+
/**
169+
* Gets session meta.
170+
*
171+
* @return MetaBag
172+
*/
173+
public function getMeta();
174+
175+
/**
176+
* Registers a SessionBagInterface with the session.
177+
*
178+
* @param SessionBagInterface $bag
179+
*/
180+
public function registerBag(SessionBagInterface $bag);
181+
182+
/**
183+
* Get's a bag instance.
184+
*
185+
* @param string $name
186+
*
187+
* @return SessionBagInterface
188+
*/
189+
public function getBag($name);
167190
}

src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session;
1313

1414
use Symfony\Component\HttpFoundation\Session\Session;
15+
use Symfony\Component\HttpFoundation\Session\MetaBag;
1516
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
1617
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
1718
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
@@ -253,4 +254,12 @@ public function testGetCount()
253254

254255
$this->assertEquals(2, count($this->session));
255256
}
257+
258+
public function testGetMeta()
259+
{
260+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\MetaBag', $this->session->getMeta());
261+
$metaBag = new MetaBag();
262+
$session = new Session($this->storage, new AttributeBag(), new FlashBag(), $metaBag);
263+
$this->assertSame($metaBag, $session->getBag($metaBag->getName()));
264+
}
256265
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\HttpFoundation\Session;
13+
14+
use Symfony\Component\HttpFoundation\Session\MetaBag;
15+
16+
/**
17+
* Test class for MetaBag.
18+
*/
19+
class MetaBagTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var MetaBag
23+
*/
24+
protected $bag;
25+
26+
/**
27+
* @var array
28+
*/
29+
protected $array = array();
30+
31+
protected function setUp()
32+
{
33+
$this->bag = new MetaBag();
34+
$this->array = array('created' => 1234567, 'lastused' => 12345678);
35+
$this->bag->initialize($this->array);
36+
}
37+
38+
protected function tearDown()
39+
{
40+
$this->array = array();
41+
$this->bag = null;
42+
}
43+
44+
public function testInitialize()
45+
{
46+
$p = new \ReflectionProperty('Symfony\Component\HttpFoundation\Session\MetaBag', 'meta');
47+
$p->setAccessible(true);
48+
49+
$bag1 = new MetaBag();
50+
$array = array();
51+
$bag1->initialize($array);
52+
$this->assertGreaterThanOrEqual(time(), $bag1->getCreated());
53+
$this->assertEquals($bag1->getCreated(), $bag1->getLastUsed());
54+
55+
sleep(1);
56+
$bag2 = new MetaBag();
57+
$array2 = $p->getValue($bag1);
58+
$bag2->initialize($array2);
59+
$this->assertEquals($bag1->getCreated(), $bag2->getCreated());
60+
$this->assertEquals($bag1->getLastUsed(), $bag2->getLastUsed());
61+
$this->assertEquals($bag2->getCreated(), $bag2->getLastUsed());
62+
63+
sleep(1);
64+
$bag3 = new MetaBag();
65+
$array3 = $p->getValue($bag2);
66+
$bag3->initialize($array3);
67+
$this->assertEquals($bag1->getCreated(), $bag3->getCreated());
68+
$this->assertGreaterThan($bag2->getLastUsed(), $bag3->getLastUsed());
69+
$this->assertNotEquals($bag3->getCreated(), $bag3->getLastUsed());
70+
}
71+
72+
public function testGetSetName()
73+
{
74+
$this->assertEquals('__meta', $this->bag->getName());
75+
$this->bag->setName('foo');
76+
$this->assertEquals('foo', $this->bag->getName());
77+
78+
}
79+
80+
public function testGetStorageKey()
81+
{
82+
$this->assertEquals('_sf2_meta', $this->bag->getStorageKey());
83+
}
84+
85+
public function testGetCreated()
86+
{
87+
$this->assertEquals(1234567, $this->bag->getCreated());
88+
}
89+
90+
public function testGetLastUsed()
91+
{
92+
$this->assertLessThanOrEqual(time(), $this->bag->getLastUsed());
93+
}
94+
95+
public function testClear()
96+
{
97+
$this->bag->clear();
98+
}
99+
100+
}

0 commit comments

Comments
 (0)