web/lib/Zend/Http/UserAgent/Storage/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_Http
       
    17  * @subpackage UserAgent
       
    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  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Http_UserAgent_Storage
       
    24  */
       
    25 require_once 'Zend/Http/UserAgent/Storage.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Session_Namespace
       
    29  */
       
    30 require_once 'Zend/Session/Namespace.php';
       
    31 
       
    32 /**
       
    33  * @package    Zend_Http
       
    34  * @subpackage UserAgent
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Http_UserAgent_Storage_Session implements Zend_Http_UserAgent_Storage 
       
    39 {
       
    40     /**
       
    41      * Default session namespace
       
    42      */
       
    43     const NAMESPACE_DEFAULT = 'Zend_Http_UserAgent';
       
    44     
       
    45     /**
       
    46      * Default session object member name
       
    47      */
       
    48     const MEMBER_DEFAULT = 'storage';
       
    49     
       
    50     /**
       
    51      * Object to proxy $_SESSION storage
       
    52      *
       
    53      * @var Zend_Session_Namespace
       
    54      */
       
    55     protected $_session;
       
    56     
       
    57     /**
       
    58      * Session namespace
       
    59      *
       
    60      * @var mixed
       
    61      */
       
    62     protected $_namespace;
       
    63     
       
    64     /**
       
    65      * Session object member
       
    66      *
       
    67      * @var mixed
       
    68      */
       
    69     protected $_member;
       
    70     
       
    71     /**
       
    72      * Sets session storage options and initializes session namespace object
       
    73      *
       
    74      * Expects options to contain 0 or more of the following keys:
       
    75      * - browser_type -- maps to "namespace" internally
       
    76      * - member
       
    77      *
       
    78      * @param  null|array|object $options
       
    79      * @return void
       
    80      * @throws Zend_Http_UserAgent_Storage_Exception on invalid $options argument
       
    81      */
       
    82     public function __construct($options = null) 
       
    83     {
       
    84         if (is_object($options) && method_exists($options, 'toArray')) {
       
    85             $options = $options->toArray();
       
    86         } elseif (is_object($options)) {
       
    87             $options = (array) $options;
       
    88         }
       
    89         if (null !== $options && !is_array($options)) {
       
    90             require_once 'Zend/Http/UserAgent/Storage/Exception.php';
       
    91             throw new Zend_Http_UserAgent_Storage_Exception(sprintf(
       
    92                 'Expected array or object options; "%s" provided',
       
    93                 gettype($options)
       
    94             ));
       
    95         }
       
    96 
       
    97         // add '.' to prevent the message ''Session namespace must not start with a number'
       
    98         $this->_namespace = '.' 
       
    99                           . (isset($options['browser_type']) 
       
   100                              ? $options['browser_type'] 
       
   101                              : self::NAMESPACE_DEFAULT);
       
   102         $this->_member    = isset($options['member']) ? $options['member'] : self::MEMBER_DEFAULT;
       
   103         $this->_session   = new Zend_Session_Namespace($this->_namespace);
       
   104     }
       
   105     
       
   106     /**
       
   107      * Returns the session namespace name
       
   108      *
       
   109      * @return string
       
   110      */
       
   111     public function getNamespace() 
       
   112     {
       
   113         return $this->_namespace;
       
   114     }
       
   115     
       
   116     /**
       
   117      * Returns the name of the session object member
       
   118      *
       
   119      * @return string
       
   120      */
       
   121     public function getMember() 
       
   122     {
       
   123         return $this->_member;
       
   124     }
       
   125     
       
   126     /**
       
   127      * Defined by Zend_Http_UserAgent_Storage
       
   128      *
       
   129      * @return boolean
       
   130      */
       
   131     public function isEmpty() 
       
   132     {
       
   133         return empty($this->_session->{$this->_member});
       
   134     }
       
   135     
       
   136     /**
       
   137      * Defined by Zend_Http_UserAgent_Storage
       
   138      *
       
   139      * @return mixed
       
   140      */
       
   141     public function read() 
       
   142     {
       
   143         return $this->_session->{$this->_member};
       
   144     }
       
   145     
       
   146     /**
       
   147      * Defined by Zend_Http_UserAgent_Storage
       
   148      *
       
   149      * @param  mixed $contents
       
   150      * @return void
       
   151      */
       
   152     public function write($content) 
       
   153     {
       
   154         $this->_session->{$this->_member} = $content;
       
   155     }
       
   156     
       
   157     /**
       
   158      * Defined by Zend_Http_UserAgent_Storage
       
   159      *
       
   160      * @return void
       
   161      */
       
   162     public function clear() 
       
   163     {
       
   164         unset($this->_session->{$this->_member});
       
   165     }
       
   166 }