web/lib/Zend/Application/Resource/Session.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Application
       
    17  * @subpackage Resource
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Session.php 20814 2010-02-01 20:13:08Z freak $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Application_Resource_ResourceAbstract
       
    25  */
       
    26 require_once 'Zend/Application/Resource/ResourceAbstract.php';
       
    27 
       
    28 
       
    29 /**
       
    30  * Resource for setting session options
       
    31  *
       
    32  * @uses       Zend_Application_Resource_ResourceAbstract
       
    33  * @category   Zend
       
    34  * @package    Zend_Application
       
    35  * @subpackage Resource
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Application_Resource_Session extends Zend_Application_Resource_ResourceAbstract
       
    40 {
       
    41     /**
       
    42      * Save handler to use
       
    43      *
       
    44      * @var Zend_Session_SaveHandler_Interface
       
    45      */
       
    46     protected $_saveHandler = null;
       
    47 
       
    48     /**
       
    49      * Set session save handler
       
    50      *
       
    51      * @param  array|string|Zend_Session_SaveHandler_Interface $saveHandler
       
    52      * @return Zend_Application_Resource_Session
       
    53      * @throws Zend_Application_Resource_Exception When $saveHandler is no valid save handler
       
    54      */
       
    55     public function setSaveHandler($saveHandler)
       
    56     {
       
    57         $this->_saveHandler = $saveHandler;
       
    58         return $this;
       
    59     }
       
    60 
       
    61     /**
       
    62      * Get session save handler
       
    63      *
       
    64      * @return Zend_Session_SaveHandler_Interface
       
    65      */
       
    66     public function getSaveHandler()
       
    67     {
       
    68         if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
       
    69             if (is_array($this->_saveHandler)) {
       
    70                 if (!array_key_exists('class', $this->_saveHandler)) {
       
    71                     throw new Zend_Application_Resource_Exception('Session save handler class not provided in options');
       
    72                 }
       
    73                 $options = array();
       
    74                 if (array_key_exists('options', $this->_saveHandler)) {
       
    75                     $options = $this->_saveHandler['options'];
       
    76                 }
       
    77                 $this->_saveHandler = $this->_saveHandler['class'];
       
    78                 $this->_saveHandler = new $this->_saveHandler($options);
       
    79             } elseif (is_string($this->_saveHandler)) {
       
    80                 $this->_saveHandler = new $this->_saveHandler();
       
    81             }
       
    82 
       
    83             if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
       
    84                 throw new Zend_Application_Resource_Exception('Invalid session save handler');
       
    85             }
       
    86         }
       
    87         return $this->_saveHandler;
       
    88     }
       
    89 
       
    90     /**
       
    91      * @return bool
       
    92      */
       
    93     protected function _hasSaveHandler()
       
    94     {
       
    95         return ($this->_saveHandler !== null);
       
    96     }
       
    97 
       
    98     /**
       
    99      * Defined by Zend_Application_Resource_Resource
       
   100      *
       
   101      * @return void
       
   102      */
       
   103     public function init()
       
   104     {
       
   105         $options = array_change_key_case($this->getOptions(), CASE_LOWER);
       
   106         if (isset($options['savehandler'])) {
       
   107             unset($options['savehandler']);
       
   108         }
       
   109 
       
   110         if (count($options) > 0) {
       
   111             Zend_Session::setOptions($options);
       
   112         }
       
   113 
       
   114         if ($this->_hasSaveHandler()) {
       
   115             Zend_Session::setSaveHandler($this->getSaveHandler());
       
   116         }
       
   117     }
       
   118 }