From a528e9c442d58f22b2d22fbca0ea1c4700b3f076 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Fri, 21 Oct 2011 16:58:45 -0700 Subject: [PATCH 1/3] Add a Type parameter for session flash messages. --- .../Component/HttpFoundation/Session.php | 70 ++++++++++++++----- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 721a6c7240b99..cb835f9a967cb 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,8 @@ public function clearFlashes() $this->start(); } - $this->flashes = array(); - $this->oldFlashes = array(); + $this->flashes = array('status' => array()); + $this->oldFlashes = array('status' => array()); } public function save() @@ -318,7 +348,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, From 57d432bcd0246c7d3320b3077f533594aa744b4a Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Fri, 28 Oct 2011 10:11:55 -0400 Subject: [PATCH 2/3] Update session tests to handle the default status array key. --- .../Symfony/Tests/Component/HttpFoundation/SessionTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index d5f615a6b1370..0ac557299eed7 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -149,7 +149,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 +179,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 +195,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); From bc1fe9834dfe38c03f4dae349b50866664afe986 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Fri, 28 Oct 2011 10:26:50 -0400 Subject: [PATCH 3/3] Add an addFlash method to easily add a single flash message. --- src/Symfony/Component/HttpFoundation/Session.php | 11 +++++++++++ .../Tests/Component/HttpFoundation/SessionTest.php | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index cb835f9a967cb..9c3d398e94c7e 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -342,6 +342,17 @@ public function clearFlashes() $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() { if (false === $this->started) { diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index 0ac557299eed7..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() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy