File indexing completed on 2024-06-16 05:29:52

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Application
0017  * @subpackage Resource
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Application_Resource_ResourceAbstract
0025  */
0026 // require_once 'Zend/Application/Resource/ResourceAbstract.php';
0027 
0028 
0029 /**
0030  * Resource for setting session options
0031  *
0032  * @uses       Zend_Application_Resource_ResourceAbstract
0033  * @category   Zend
0034  * @package    Zend_Application
0035  * @subpackage Resource
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Application_Resource_Session extends Zend_Application_Resource_ResourceAbstract
0040 {
0041     /**
0042      * Save handler to use
0043      *
0044      * @var Zend_Session_SaveHandler_Interface
0045      */
0046     protected $_saveHandler = null;
0047 
0048     /**
0049      * Set session save handler
0050      *
0051      * @param  array|string|Zend_Session_SaveHandler_Interface $saveHandler
0052      * @return Zend_Application_Resource_Session
0053      * @throws Zend_Application_Resource_Exception When $saveHandler is not a valid save handler
0054      */
0055     public function setSaveHandler($saveHandler)
0056     {
0057         $this->_saveHandler = $saveHandler;
0058         return $this;
0059     }
0060 
0061     /**
0062      * Get session save handler
0063      *
0064      * @return Zend_Session_SaveHandler_Interface
0065      * @throws Zend_Application_Resource_Exception
0066      */
0067     public function getSaveHandler()
0068     {
0069         if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
0070             if (is_array($this->_saveHandler)) {
0071                 if (!array_key_exists('class', $this->_saveHandler)) {
0072                     throw new Zend_Application_Resource_Exception('Session save handler class not provided in options');
0073                 }
0074                 $options = array();
0075                 if (array_key_exists('options', $this->_saveHandler)) {
0076                     $options = $this->_saveHandler['options'];
0077                 }
0078                 $this->_saveHandler = $this->_saveHandler['class'];
0079                 $this->_saveHandler = new $this->_saveHandler($options);
0080             } elseif (is_string($this->_saveHandler)) {
0081                 $this->_saveHandler = new $this->_saveHandler();
0082             }
0083 
0084             if (!$this->_saveHandler instanceof Zend_Session_SaveHandler_Interface) {
0085                 throw new Zend_Application_Resource_Exception('Invalid session save handler');
0086             }
0087         }
0088         return $this->_saveHandler;
0089     }
0090 
0091     /**
0092      * @return bool
0093      */
0094     protected function _hasSaveHandler()
0095     {
0096         return ($this->_saveHandler !== null);
0097     }
0098 
0099     /**
0100      * Defined by Zend_Application_Resource_Resource
0101      *
0102      * @return void
0103      */
0104     public function init()
0105     {
0106         $options = array_change_key_case($this->getOptions(), CASE_LOWER);
0107         if (isset($options['savehandler'])) {
0108             unset($options['savehandler']);
0109         }
0110 
0111         if (count($options) > 0) {
0112             Zend_Session::setOptions($options);
0113         }
0114 
0115         if ($this->_hasSaveHandler()) {
0116             Zend_Session::setSaveHandler($this->getSaveHandler());
0117         }
0118     }
0119 }