Skip to content

Commit

Permalink
[HttpFoundation] added automatic session start() when changing someth…
Browse files Browse the repository at this point in the history
…ing in the session, renamed accessor methods, added remove()/has() methods
  • Loading branch information
fabpot committed Aug 20, 2010
1 parent bf82cf4 commit 2746bcc
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions src/Symfony/Component/HttpFoundation/Session.php
Expand Up @@ -33,10 +33,11 @@ class Session
* @param SessionStorageInterface $session A SessionStorageInterface instance
* @param array $options An array of options
*/
public function __construct(SessionStorageInterface $storage, $options = array())
public function __construct(SessionStorageInterface $storage, array $options = array())
{
$this->storage = $storage;
$this->options = $options;
$this->attributes = array();
}

/**
Expand All @@ -62,14 +63,26 @@ public function start()
}

/**
* Returns an attribute
* Checks if an attribute is defined.
*
* @param string $name The attribute name
*
* @return Boolean true if the attribute is defined, false otherwise
*/
public function has($name)
{
return array_key_exists($name, $this->attributes);
}

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

This comment has been minimized.

Copy link
@trompette

trompette Aug 23, 2010

Contributor

There is some code duplication here. I would have written :
return $this->has($name) ? $this->attributes[$name] : $default;

}
Expand All @@ -80,8 +93,12 @@ public function getAttribute($name, $default = null)
* @param string $name
* @param mixed $value
*/
public function setAttribute($name, $value)
public function set($name, $value)
{
if (false === $this->started) {
$this->start();
}

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

Expand All @@ -96,15 +113,35 @@ public function getAttributes()
}

/**
* Sets attributes
* Sets attributes.
*
* @param array $attributes Attributes
*/
public function setAttributes($attributes)
{
if (false === $this->started) {
$this->start();
}

$this->attributes = $attributes;
}

/**
* Removes an attribute.
*
* @param string $name
*/
public function remove($name)
{
if (array_key_exists($this->attributes, $name)) {

This comment has been minimized.

Copy link
@trompette

trompette Aug 23, 2010

Contributor

same here.

if (false === $this->started) {
$this->start();
}

unset($this->attributes[$name]);
}
}

/**
* Returns the locale
*
Expand Down Expand Up @@ -134,6 +171,10 @@ public function getFlashMessages()

public function setFlashMessages($values)
{
if (false === $this->started) {
$this->start();
}

$this->attributes['_flash'] = $values;
}

Expand All @@ -144,6 +185,10 @@ public function getFlash($name, $default = null)

public function setFlash($name, $value)
{
if (false === $this->started) {
$this->start();
}

$this->attributes['_flash'][$name] = $value;
unset($this->oldFlashes[$name]);
}
Expand Down

0 comments on commit 2746bcc

Please sign in to comment.