ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
Flash.php
Go to the documentation of this file.
1 <?php
57 class Slim_Session_Flash implements ArrayAccess {
58 
62  protected $sessionKey = 'flash';
63 
67  protected $messages = array(
68  'prev' => array(), //flash messages from prev request
69  'next' => array(), //flash messages for next request
70  'now' => array() //flash messages for current request
71  );
72 
82  public function __construct( $sessionKey = null ) {
83  if ( !is_null($sessionKey) ) {
84  $this->setSessionKey($sessionKey);
85  }
86  $this->load();
87  }
88 
95  public function setSessionKey( $key ) {
96  if ( is_null($key) ) {
97  throw new RuntimeException('Session key cannot be null');
98  }
99  $this->sessionKey = (string)$key;
100  return $this;
101  }
102 
107  public function getSessionKey() {
108  return $this->sessionKey;
109  }
110 
117  public function now( $key, $value ) {
118  $this->messages['now'][(string)$key] = $value;
119  return $this->save();
120  }
121 
128  public function set( $key, $value ) {
129  $this->messages['next'][(string)$key] = $value;
130  return $this->save();
131  }
132 
137  public function getMessages() {
138  return array_merge($this->messages['prev'], $this->messages['now']);
139  }
140 
145  public function load() {
146  $this->messages['prev'] = isset($_SESSION[$this->sessionKey]) ? $_SESSION[$this->sessionKey] : array();
147  return $this;
148  }
149 
155  public function keep() {
156  foreach ( $this->messages['prev'] as $key => $val ) {
157  $this->messages['next'][$key] = $val;
158  }
159  return $this->save();
160  }
161 
166  public function save() {
167  $_SESSION[$this->sessionKey] = $this->messages['next'];
168  return $this;
169  }
170 
171  /***** ARRAY ACCESS INTERFACE *****/
172 
173  public function offsetExists( $offset ) {
174  $messages = $this->getMessages();
175  return isset($messages[$offset]);
176  }
177 
178  public function offsetGet( $offset ) {
179  $messages = $this->getMessages();
180  return isset($messages[$offset]) ? $messages[$offset] : null;
181  }
182 
183  public function offsetSet( $offset, $value ) {
184  $this->now($offset, $value);
185  }
186 
187  public function offsetUnset( $offset ) {
188  unset($this->messages['prev'][$offset]);
189  unset($this->messages['now'][$offset]);
190  }
191 
192 }