rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

Flash.php (5398B)


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