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