Source for file SessionNamespace.php

Documentation is available at SessionNamespace.php

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