diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 721a6c7240b99..9c3d398e94c7e 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -37,8 +37,8 @@ class Session implements \Serializable public function __construct(SessionStorageInterface $storage) { $this->storage = $storage; - $this->flashes = array(); - $this->oldFlashes = array(); + $this->flashes = array('status' => array()); + $this->oldFlashes = array('status' => array()); $this->attributes = array(); $this->started = false; $this->closed = false; @@ -174,7 +174,7 @@ public function clear() } $this->attributes = array(); - $this->flashes = array(); + $this->flashes = array('status' => array()); } /** @@ -216,28 +216,54 @@ public function getId() } /** - * Gets the flash messages. + * Gets the flash messages of a given type. * + * @param string $type * @return array */ - public function getFlashes() + public function getFlashes($type = 'status') { + return $this->flashes[$type]; + } + + /** + * Gets the flash messages. + * + * @return array + */ + public function getAllFlashes() { return $this->flashes; } + /** + * Sets the flash messages of a specific type. + * + * @param array $values + * @param string $type + */ + public function setFlashes($values, $type = 'status') + { + if (false === $this->started) { + $this->start(); + } + + $this->flashes[$type] = $values; + $this->oldFlashes = array('status' => array()); + } + /** * Sets the flash messages. * * @param array $values */ - public function setFlashes($values) + public function setAllFlashes($values) { if (false === $this->started) { $this->start(); } $this->flashes = $values; - $this->oldFlashes = array(); + $this->oldFlashes = array('status' => array()); } /** @@ -248,9 +274,11 @@ public function setFlashes($values) * * @return string */ - public function getFlash($name, $default = null) + public function getFlash($name, $default = null, $type = 'status') { - return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default; + if (array_key_exists($type, $this->flashes)) { + return array_key_exists($name, $this->flashes[$type]) ? $this->flashes[$type][$name] : $default; + } } /** @@ -258,31 +286,33 @@ public function getFlash($name, $default = null) * * @param string $name * @param string $value + * @param string $type */ - public function setFlash($name, $value) + public function setFlash($name, $value, $type = 'status') { if (false === $this->started) { $this->start(); } - $this->flashes[$name] = $value; - unset($this->oldFlashes[$name]); + $this->flashes[$type][$name] = $value; + unset($this->oldFlashes[$type][$name]); } /** * Checks whether a flash message exists. * * @param string $name + * @param string $type * * @return Boolean */ - public function hasFlash($name) + public function hasFlash($name, $type = 'status') { if (false === $this->started) { $this->start(); } - return array_key_exists($name, $this->flashes); + return array_key_exists($name, $this->flashes[$type]); } /** @@ -290,13 +320,13 @@ public function hasFlash($name) * * @param string $name */ - public function removeFlash($name) + public function removeFlash($name, $type = 'status') { if (false === $this->started) { $this->start(); } - unset($this->flashes[$name]); + unset($this->flashes[$type][$name]); } /** @@ -308,8 +338,19 @@ public function clearFlashes() $this->start(); } - $this->flashes = array(); - $this->oldFlashes = array(); + $this->flashes = array('status' => array()); + $this->oldFlashes = array('status' => array()); + } + + /** + * Adds a generic flash message to the session. + * + * @param string $type + * + * @return array + */ + public function addFlash($value) { + $this->flashes['status'][] = $value; } public function save() @@ -318,7 +359,9 @@ public function save() $this->start(); } - $this->flashes = array_diff_key($this->flashes, $this->oldFlashes); + foreach ($this->flashes as $type => $flashes) { + $this->flashes[$type] = array_diff_key($flashes, $this->oldFlashes[$type]); + } $this->storage->write('_symfony2', array( 'attributes' => $this->attributes, diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index d5f615a6b1370..c3f2807373df2 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -59,6 +59,11 @@ public function testFlash() $this->session->setFlashes($flashes); $this->assertSame($flashes, $this->session->getFlashes()); + + $this->session->clearFlashes(); + $this->session->addFlash('foo'); + $compare = $this->session->getFlashes(); + $this->assertSame($compare, array(0 => 'foo')); } public function testFlashesAreFlushedWhenNeeded() @@ -149,7 +154,7 @@ public function testSave() $this->session->set('foo', 'bar'); $this->session->save(); - $compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array())); + $compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array('status' => array()))); $r = new \ReflectionObject($this->storage); $p = $r->getProperty('data'); @@ -179,7 +184,7 @@ public function testSavedOnDestruct() $expected = array( 'attributes'=>array('foo'=>'bar'), - 'flashes'=>array(), + 'flashes'=>array('status' => array()), ); $saved = $this->storage->read('_symfony2'); $this->assertSame($expected, $saved); @@ -195,7 +200,7 @@ public function testSavedOnDestructAfterManualSave() $expected = array( 'attributes'=>array('foo'=>'bar'), - 'flashes'=>array(), + 'flashes'=>array('status' => array()), ); $saved = $this->storage->read('_symfony2'); $this->assertSame($expected, $saved);