Session.php 4.2 KB

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