PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Web/SessionNamespace.php

https://github.com/DocX/nette
PHP | 255 lines | 95 code | 57 blank | 103 comment | 12 complexity | 434adea657a35bddcf45833a07b45423 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Web
  17. */
  18. /*namespace Nette\Web;*/
  19. require_once dirname(__FILE__) . '/../Object.php';
  20. /**
  21. * Session namespace for Session.
  22. *
  23. * @author David Grudl
  24. * @copyright Copyright (c) 2004, 2009 David Grudl
  25. * @package Nette\Web
  26. */
  27. final class SessionNamespace extends /*Nette\*/Object implements /*\*/IteratorAggregate, /*\*/ArrayAccess
  28. {
  29. /** @var array session data storage */
  30. private $data;
  31. /** @var array session metadata storage */
  32. private $meta;
  33. /** @var bool */
  34. public $warnOnUndefined = FALSE;
  35. /**
  36. * Do not call directly. Use Session::getNamespace().
  37. */
  38. public function __construct(& $data, & $meta)
  39. {
  40. $this->data = & $data;
  41. $this->meta = & $meta;
  42. }
  43. /**
  44. * Returns an iterator over all namespace variables.
  45. * @return \ArrayIterator
  46. */
  47. public function getIterator()
  48. {
  49. if (isset($this->data)) {
  50. return new /*\*/ArrayIterator($this->data);
  51. } else {
  52. return new /*\*/ArrayIterator;
  53. }
  54. }
  55. /**
  56. * Sets a variable in this session namespace.
  57. *
  58. * @param string name
  59. * @param mixed value
  60. * @return void
  61. */
  62. public function __set($name, $value)
  63. {
  64. $this->data[$name] = $value;
  65. }
  66. /**
  67. * Gets a variable from this session namespace.
  68. *
  69. * @param string name
  70. * @return mixed
  71. */
  72. public function &__get($name)
  73. {
  74. if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
  75. trigger_error("The variable '$name' does not exist in session namespace", E_USER_NOTICE);
  76. }
  77. return $this->data[$name];
  78. }
  79. /**
  80. * Determines whether a variable in this session namespace is set.
  81. *
  82. * @param string name
  83. * @return bool
  84. */
  85. public function __isset($name)
  86. {
  87. return isset($this->data[$name]);
  88. }
  89. /**
  90. * Unsets a variable in this session namespace.
  91. *
  92. * @param string name
  93. * @return void
  94. */
  95. public function __unset($name)
  96. {
  97. unset($this->data[$name], $this->meta['EXP'][$name]);
  98. }
  99. /**
  100. * Sets a variable in this session namespace.
  101. *
  102. * @param string name
  103. * @param mixed value
  104. * @return void
  105. */
  106. public function offsetSet($name, $value)
  107. {
  108. $this->__set($name, $value);
  109. }
  110. /**
  111. * Gets a variable from this session namespace.
  112. *
  113. * @param string name
  114. * @return mixed
  115. */
  116. public function offsetGet($name)
  117. {
  118. return $this->__get($name);
  119. }
  120. /**
  121. * Determines whether a variable in this session namespace is set.
  122. *
  123. * @param string name
  124. * @return bool
  125. */
  126. public function offsetExists($name)
  127. {
  128. return $this->__isset($name);
  129. }
  130. /**
  131. * Unsets a variable in this session namespace.
  132. *
  133. * @param string name
  134. * @return void
  135. */
  136. public function offsetUnset($name)
  137. {
  138. $this->__unset($name);
  139. }
  140. /**
  141. * Sets the expiration of the namespace or specific variables.
  142. * @param mixed time in seconds, value 0 means "until the browser is closed"
  143. * @param mixed optional list of variables / single variable to expire
  144. * @return SessionNamespace provides a fluent interface
  145. */
  146. public function setExpiration($seconds, $variables = NULL)
  147. {
  148. if (is_string($seconds) && !is_numeric($seconds)) {
  149. $seconds = strtotime($seconds);
  150. }
  151. $whenBrowserIsClosed = $seconds == 0;
  152. if ($seconds <= 0) {
  153. $seconds = 0;
  154. } elseif ($seconds <= /*Nette\*/Tools::YEAR) {
  155. $seconds += time();
  156. }
  157. if ($variables === NULL) {
  158. // to entire namespace
  159. $this->meta['EXP'][''] = array($seconds, $whenBrowserIsClosed);
  160. } elseif (is_array($variables)) {
  161. // to variables
  162. foreach ($variables as $variable) {
  163. $this->meta['EXP'][$variable] = array($seconds, $whenBrowserIsClosed);
  164. }
  165. } else {
  166. $this->meta['EXP'][$variables] = array($seconds, $whenBrowserIsClosed);
  167. }
  168. return $this;
  169. }
  170. /**
  171. * Removes the expiration from the namespace or specific variables.
  172. * @param mixed optional list of variables / single variable to expire
  173. * @return void
  174. */
  175. public function removeExpiration($variables = NULL)
  176. {
  177. if ($variables === NULL) {
  178. // from entire namespace
  179. unset($this->meta['EXP']['']);
  180. } elseif (is_array($variables)) {
  181. // from variables
  182. foreach ($variables as $variable) {
  183. unset($this->meta['EXP'][$variable]);
  184. }
  185. } else {
  186. unset($this->meta['EXP'][$variables]);
  187. }
  188. }
  189. /**
  190. * Cancels the current session namespace.
  191. * @return void
  192. */
  193. public function remove()
  194. {
  195. $this->data = NULL;
  196. $this->meta = NULL;
  197. }
  198. }