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