1: <?php
2: /**
3: * Slim - a micro PHP 5 framework
4: *
5: * @author Josh Lockhart <info@slimframework.com>
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: /**
36: * Flash
37: *
38: * This is middleware for a Slim application that enables
39: * Flash messaging between HTTP requests. This allows you
40: * set Flash messages for the current request, for the next request,
41: * or to retain messages from the previous request through to
42: * the next request.
43: *
44: * @package Slim
45: * @author Josh Lockhart
46: * @since 1.6.0
47: */
48: class Flash extends \Slim\Middleware implements \ArrayAccess, \IteratorAggregate
49: {
50: /**
51: * @var array
52: */
53: protected $settings;
54:
55: /**
56: * @var array
57: */
58: protected $messages;
59:
60: /**
61: * Constructor
62: * @param \Slim $app
63: * @param array $settings
64: */
65: public function __construct($settings = array())
66: {
67: $this->settings = array_merge(array('key' => 'slim.flash'), $settings);
68: $this->messages = array(
69: 'prev' => array(), //flash messages from prev request (loaded when middleware called)
70: 'next' => array(), //flash messages for next request
71: 'now' => array() //flash messages for current request
72: );
73: }
74:
75: /**
76: * Call
77: */
78: public function call()
79: {
80: //Read flash messaging from previous request if available
81: $this->loadMessages();
82:
83: //Prepare flash messaging for current request
84: $env = $this->app->environment();
85: $env['slim.flash'] = $this;
86: $this->next->call();
87: $this->save();
88: }
89:
90: /**
91: * Now
92: *
93: * Specify a flash message for a given key to be shown for the current request
94: *
95: * @param string $key
96: * @param string $value
97: */
98: public function now($key, $value)
99: {
100: $this->messages['now'][(string) $key] = $value;
101: }
102:
103: /**
104: * Set
105: *
106: * Specify a flash message for a given key to be shown for the next request
107: *
108: * @param string $key
109: * @param string $value
110: */
111: public function set($key, $value)
112: {
113: $this->messages['next'][(string) $key] = $value;
114: }
115:
116: /**
117: * Keep
118: *
119: * Retain flash messages from the previous request for the next request
120: */
121: public function keep()
122: {
123: foreach ($this->messages['prev'] as $key => $val) {
124: $this->messages['next'][$key] = $val;
125: }
126: }
127:
128: /**
129: * Save
130: */
131: public function save()
132: {
133: $_SESSION[$this->settings['key']] = $this->messages['next'];
134: }
135:
136: /**
137: * Load messages from previous request if available
138: */
139: public function loadMessages()
140: {
141: if (isset($_SESSION[$this->settings['key']])) {
142: $this->messages['prev'] = $_SESSION[$this->settings['key']];
143: }
144: }
145:
146: /**
147: * Return array of flash messages to be shown for the current request
148: *
149: * @return array
150: */
151: public function getMessages()
152: {
153: return array_merge($this->messages['prev'], $this->messages['now']);
154: }
155:
156: /**
157: * Array Access: Offset Exists
158: */
159: public function offsetExists($offset)
160: {
161: $messages = $this->getMessages();
162:
163: return isset($messages[$offset]);
164: }
165:
166: /**
167: * Array Access: Offset Get
168: */
169: public function offsetGet($offset)
170: {
171: $messages = $this->getMessages();
172:
173: return isset($messages[$offset]) ? $messages[$offset] : null;
174: }
175:
176: /**
177: * Array Access: Offset Set
178: */
179: public function offsetSet($offset, $value)
180: {
181: $this->now($offset, $value);
182: }
183:
184: /**
185: * Array Access: Offset Unset
186: */
187: public function offsetUnset($offset)
188: {
189: unset($this->messages['prev'][$offset], $this->messages['now'][$offset]);
190: }
191:
192: /**
193: * Iterator Aggregate: Get Iterator
194: * @return \ArrayIterator
195: */
196: public function getIterator()
197: {
198: $messages = $this->getMessages();
199:
200: return new \ArrayIterator($messages);
201: }
202: }
203: