Flash.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <[email protected]>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim\Middleware;
  34. /**
  35. * Flash
  36. *
  37. * This is middleware for a Slim application that enables
  38. * Flash messaging between HTTP requests. This allows you
  39. * set Flash messages for the current request, for the next request,
  40. * or to retain messages from the previous request through to
  41. * the next request.
  42. *
  43. * @package Slim
  44. * @author Josh Lockhart
  45. * @since 1.6.0
  46. */
  47. class Flash extends \Slim\Middleware implements \ArrayAccess, \IteratorAggregate
  48. {
  49. /**
  50. * @var array
  51. */
  52. protected $settings;
  53. /**
  54. * @var array
  55. */
  56. protected $messages;
  57. /**
  58. * Constructor
  59. * @param \Slim $app
  60. * @param array $settings
  61. */
  62. public function __construct($settings = array())
  63. {
  64. $this->settings = array_merge(array('key' => 'slim.flash'), $settings);
  65. $this->messages = array(
  66. 'prev' => array(), //flash messages from prev request (loaded when middleware called)
  67. 'next' => array(), //flash messages for next request
  68. 'now' => array() //flash messages for current request
  69. );
  70. }
  71. /**
  72. * Call
  73. */
  74. public function call()
  75. {
  76. //Read flash messaging from previous request if available
  77. $this->loadMessages();
  78. //Prepare flash messaging for current request
  79. $env = $this->app->environment();
  80. $env['slim.flash'] = $this;
  81. $this->next->call();
  82. $this->save();
  83. }
  84. /**
  85. * Now
  86. *
  87. * Specify a flash message for a given key to be shown for the current request
  88. *
  89. * @param string $key
  90. * @param string $value
  91. */
  92. public function now($key, $value)
  93. {
  94. $this->messages['now'][(string) $key] = $value;
  95. }
  96. /**
  97. * Set
  98. *
  99. * Specify a flash message for a given key to be shown for the next request
  100. *
  101. * @param string $key
  102. * @param string $value
  103. */
  104. public function set($key, $value)
  105. {
  106. $this->messages['next'][(string) $key] = $value;
  107. }
  108. /**
  109. * Keep
  110. *
  111. * Retain flash messages from the previous request for the next request
  112. */
  113. public function keep()
  114. {
  115. foreach ($this->messages['prev'] as $key => $val) {
  116. $this->messages['next'][$key] = $val;
  117. }
  118. }
  119. /**
  120. * Save
  121. */
  122. public function save()
  123. {
  124. $_SESSION[$this->settings['key']] = $this->messages['next'];
  125. }
  126. /**
  127. * Load messages from previous request if available
  128. */
  129. public function loadMessages()
  130. {
  131. if (isset($_SESSION[$this->settings['key']])) {
  132. $this->messages['prev'] = $_SESSION[$this->settings['key']];
  133. }
  134. }
  135. /**
  136. * Return array of flash messages to be shown for the current request
  137. *
  138. * @return array
  139. */
  140. public function getMessages()
  141. {
  142. return array_merge($this->messages['prev'], $this->messages['now']);
  143. }
  144. /**
  145. * Array Access: Offset Exists
  146. */
  147. public function offsetExists($offset)
  148. {
  149. $messages = $this->getMessages();
  150. return isset($messages[$offset]);
  151. }
  152. /**
  153. * Array Access: Offset Get
  154. */
  155. public function offsetGet($offset)
  156. {
  157. $messages = $this->getMessages();
  158. return isset($messages[$offset]) ? $messages[$offset] : null;
  159. }
  160. /**
  161. * Array Access: Offset Set
  162. */
  163. public function offsetSet($offset, $value)
  164. {
  165. $this->now($offset, $value);
  166. }
  167. /**
  168. * Array Access: Offset Unset
  169. */
  170. public function offsetUnset($offset)
  171. {
  172. unset($this->messages['prev'][$offset], $this->messages['now'][$offset]);
  173. }
  174. /**
  175. * Iterator Aggregate: Get Iterator
  176. * @return \ArrayIterator
  177. */
  178. public function getIterator()
  179. {
  180. $messages = $this->getMessages();
  181. return new \ArrayIterator($messages);
  182. }
  183. }