diff options
author | Dracony <draconyster@gmail.com> | 2013-02-26 11:41:27 +0200 |
---|---|---|
committer | Dracony <draconyster@gmail.com> | 2013-02-26 11:41:27 +0200 |
commit | b1a5ef8ddbca236874301a4cdd572e1f45558915 (patch) | |
tree | 69e78e9f62406b9abe8f505b31e8f1eb8698c467 | |
parent | 8bcac872d874c68db5a6c2c9bb45c6c3d9ae4d52 (diff) | |
download | PHPixie-b1a5ef8ddbca236874301a4cdd572e1f45558915.zip PHPixie-b1a5ef8ddbca236874301a4cdd572e1f45558915.tar.gz PHPixie-b1a5ef8ddbca236874301a4cdd572e1f45558915.tar.bz2 |
Flash messages support
-rw-r--r-- | system/classes/session.php | 28 | ||||
-rw-r--r-- | tests/system/sessionTest.php | 11 |
2 files changed, 39 insertions, 0 deletions
diff --git a/system/classes/session.php b/system/classes/session.php index 37e5be9..3749396 100644 --- a/system/classes/session.php +++ b/system/classes/session.php @@ -57,6 +57,10 @@ class Session{ */ public static function remove($key) { Session::check(); + + if (!isset($_SESSION[$key])) + return; + $var = $_SESSION[$key]; unset($_SESSION[$key], $var); } @@ -72,4 +76,28 @@ class Session{ Session::check(); $_SESSION=array(); } + + /** + * Gets ot sets flash messages. + * If the value parameter is passed the message is set, otherwise it is retrieved. + * After the message is retrieved for the first time it is removed. + * + * @param $key The name of the flash message + * @param $val Flash message content + * @return mixed + * @access public + * @static + */ + public static function flash($key,$val = null) { + Session::check(); + $key="flash_{$key}"; + if($val != null) { + Session::set($key,$val); + }else { + $val = Session::get($key); + Session::remove($key); + } + + return $val; + } }
\ No newline at end of file diff --git a/tests/system/sessionTest.php b/tests/system/sessionTest.php index bb0a8f5..2656a5a 100644 --- a/tests/system/sessionTest.php +++ b/tests/system/sessionTest.php @@ -77,4 +77,15 @@ class sessionTest extends PHPUnit_Framework_TestCase Session::reset(); $this->assertEquals(0, count($_SESSION)); } + + /** + * @covers Session::flash + */ + public function testFlash() + { + Session::flash('test', 'Trixie'); + Session::flash('test', 'Tinkerbell'); + $this->assertEquals('Tinkerbell', Session::flash('test')); + $this->assertEquals(null,Session::flash('test')); + } } |