Skip to content

[HttpFoundation] Added structured namespacing to session attributes. #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 89 additions & 13 deletions src/Symfony/Component/HttpFoundation/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,52 @@
*/
class Session implements \Serializable
{
/**
* Storage driver.
*
* @var SessionStorageInterface
*/
protected $storage;

/**
* Flag if session has started.
*
* @var boolean
*/
protected $started;

/**
* Array of attributes.
*
* @var array
*/
protected $attributes;

/**
* Array of flash messages.
*
* @var array
*/
protected $flashes;

/**
* Flashes to be removed.
*
* @var array
*/
protected $oldFlashes;

/**
* Flag if session has terminated.
*
* @var boolean
*/
protected $closed;

/**
* Constructor.
*
* @param SessionStorageInterface $storage A SessionStorageInterface instance
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
*/
public function __construct(SessionStorageInterface $storage)
{
Expand Down Expand Up @@ -79,41 +114,80 @@ public function start()
*
* @api
*/
public function has($name)
public function has($name, $namespace = '/')
{
return array_key_exists($name, $this->attributes);
$attributes = $this->resolveAttributePath($namespace);

return array_key_exists($name, $attributes);
}

/**
* Returns an attribute.
*
* @param string $name The attribute name
* @param mixed $default The default value
* @param string $name The attribute name
* @param mixed $default The default value
* @param string $namespace Namespace
*
* @return mixed
*
* @api
*/
public function get($name, $default = null)
public function get($name, $default = null, $namespace = '/')
{
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
$attributes = $this->resolveAttributePath($namespace);

return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
}

/**
* Sets an attribute.
*
* @param string $name
* @param mixed $value
* @param string $namespace
*
* @api
*/
public function set($name, $value)
public function set($name, $value, $namespace = '/')
{
if (false === $this->started) {
$this->start();
}

$attributes = & $this->resolveAttributePath($namespace);
$attributes[$name] = $value;
}

/**
* Resolves a path in attributes property and returns it as a reference.
*
* This method allows structured namespacing of session attributes.
*
* @param string $namespace
*
* @return array
*/
private function &resolveAttributePath($namespace)
{
$array = & $this->attributes;
$namespace = (strpos($namespace, '/') === 0) ? substr($namespace, 1) : $namespace;

// Check if there is anything to do, else return
if (!$namespace) {
return $array;
}

$parts = explode('/', $namespace);

foreach ($parts as $part) {
if (!array_key_exists($part, $array)) {
$array[$part] = array();
}

$this->attributes[$name] = $value;
$array = & $array[$part];
}

return $array;
}

/**
Expand Down Expand Up @@ -148,17 +222,19 @@ public function replace(array $attributes)
* Removes an attribute.
*
* @param string $name
* @param string $namespace
*
* @api
*/
public function remove($name)
public function remove($name, $namespace = '/')
{
if (false === $this->started) {
$this->start();
}

if (array_key_exists($name, $this->attributes)) {
unset($this->attributes[$name]);

$attributes = & $this->resolveAttributePath($namespace);
if (array_key_exists($name, $attributes)) {
unset($attributes[$name]);
}
}

Expand Down
18 changes: 15 additions & 3 deletions tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,31 @@ public function testAll()
$this->assertFalse($this->session->has('foo'));
$this->assertNull($this->session->get('foo'));

$this->assertFalse($this->session->has('foo', '/example'));
$this->assertNull($this->session->get('foo', null, '/example'));

$this->session->set('foo', 'bar');

$this->assertTrue($this->session->has('foo'));
$this->assertSame('bar', $this->session->get('foo'));

// test namespacing
$this->session->set('foo', 'bar', '/example');
$this->assertTrue($this->session->has('foo', '/example'));
$this->assertSame('bar', $this->session->get('foo', '/example'));

$this->session = $this->getSession();

$this->session->remove('foo');
$this->session->set('foo', 'bar');

$this->session->remove('foo');

$this->assertFalse($this->session->has('foo'));
$this->assertTrue($this->session->has('foo', '/example'));

$this->session->remove('foo', '/example');
$this->session->set('foo', 'bar', '/example');
$this->session->remove('foo', '/example');
$this->assertFalse($this->session->has('foo'));
$this->assertFalse($this->session->has('foo', '/example'));

$attrs = array('foo' => 'bar', 'bar' => 'foo');

Expand Down