PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Nette/Web/SessionNamespace.php

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