Browse Source

Implemented Session::flash()

Jose Lorenzo Rodriguez 12 years ago
parent
commit
4c3f85bfc1
2 changed files with 94 additions and 0 deletions
  1. 52 0
      src/Network/Session.php
  2. 42 0
      tests/TestCase/Network/SessionTest.php

+ 52 - 0
src/Network/Session.php

@@ -170,6 +170,7 @@ class Session {
 		if (isset($defaults[$name])) {
 			return $defaults[$name];
 		}
+
 		return false;
 	}
 
@@ -508,6 +509,57 @@ class Session {
 	}
 
 /**
+ * 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.
  *

+ 42 - 0
tests/TestCase/Network/SessionTest.php

@@ -430,4 +430,46 @@ class SessionTest extends TestCase {
 		$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'));
+	}
+
 }