From 2746bcc84cad53c125494ae141ca0a8147fff24f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 20 Aug 2010 23:20:30 +0200 Subject: [PATCH] [HttpFoundation] added automatic session start() when changing something in the session, renamed accessor methods, added remove()/has() methods --- .../Component/HttpFoundation/Session.php | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index afbe3934b572..90ecb95ca5b3 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -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(); } /** @@ -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; } @@ -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; } @@ -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)) { + if (false === $this->started) { + $this->start(); + } + + unset($this->attributes[$name]); + } + } + /** * Returns the locale * @@ -134,6 +171,10 @@ public function getFlashMessages() public function setFlashMessages($values) { + if (false === $this->started) { + $this->start(); + } + $this->attributes['_flash'] = $values; } @@ -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]); }