Skip to content

Commit

Permalink
Implemented Session::flash()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 18, 2014
1 parent 91f4284 commit 4c3f85b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Network/Session.php
Expand Up @@ -170,6 +170,7 @@ protected static function _defaultConfig($name) {
if (isset($defaults[$name])) {
return $defaults[$name];
}

return false;
}

Expand Down Expand Up @@ -507,6 +508,57 @@ public function renew() {
session_regenerate_id(true);
}

/**
* Stores a single string under the "Message.flash" session key. This is useful
* for persisting messages from one request to another that should be displayed
* to the user.
*
* The options array accepts `key`, this is is useful for assigning a domain
* to the flash message since you can only store one per domain.
*
* ### Example
*
* {{{
* $session->flash('Welcome, Mark', 'success');
* $session->flash('This is a message in a different domain', 'info', ['key' => 'another']);
* }}}
*
* @param string $message the message to display to persist
* @param string $type the type of message
* @param array $options A list of extra options to persist related to this message
* @return void
*/
public function flash($message, $type = 'info', $options = []) {
$options += ['key' => 'flash'];
$key = $options['key'];
unset($options['key']);
$this->write("Message.$key", [
'message' => $message,
'type' => $type,
'params' => $options
]);
}

/**
* Returns the flash message stored in the given key if exists.
*
* @param string $key the message domain
* @return array|null
*/
public function readFlash($key = 'flash') {
return $this->read("Message.$key");
}

/**
* Deletes the flash message stored in the given key
*
* @param string $key the message domain
* @return void
*/
public function deleteFlash($key = 'flash') {
$this->delete("Message.$key");
}

/**
* Returns true if the session is no longer valid because the last time it was
* accessed was after the configured timeout.
Expand Down
42 changes: 42 additions & 0 deletions tests/TestCase/Network/SessionTest.php
Expand Up @@ -430,4 +430,46 @@ public function testCookieTimeoutFallback() {
$this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
}

/**
* Tests setting, reading and deleting flash messages
*
* @return void
*/
public function testFlash() {
$session = new Session();
$session->flash('Hello there!');
$expected = [
'message' => 'Hello there!',
'type' => 'info',
'params' => []
];
$this->assertEquals($expected, $session->readFlash());

$this->assertEquals($expected, $session->readFlash());
$session->deleteFlash();
$this->assertNull($session->readFlash());
}

/**
* Tests using the key option in the flash method
*
* @return void
*/
public function testFlashKey() {
$session = new Session();
$session->flash('Hello there!', 'success', ['key' => 'foo']);
$expected = [
'message' => 'Hello there!',
'type' => 'success',
'params' => []
];

$this->assertNull($session->readFlash());
$this->assertEquals($expected, $session->readFlash('foo'));
$session->deleteFlash();
$this->assertEquals($expected, $session->readFlash('foo'));
$session->deleteFlash('foo');
$this->assertNull($session->readFlash('foo'));
}

}

0 comments on commit 4c3f85b

Please sign in to comment.