1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Web;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: final class SessionNamespace extends Nette\Object implements \IteratorAggregate, \ArrayAccess
24: {
25:
26: private $data;
27:
28:
29: private $meta;
30:
31:
32: public $warnOnUndefined = FALSE;
33:
34:
35:
36: 37: 38:
39: public function __construct(& $data, & $meta)
40: {
41: $this->data = & $data;
42: $this->meta = & $meta;
43: }
44:
45:
46:
47: 48: 49: 50:
51: public function getIterator()
52: {
53: if (isset($this->data)) {
54: return new \ArrayIterator($this->data);
55: } else {
56: return new \ArrayIterator;
57: }
58: }
59:
60:
61:
62: 63: 64: 65: 66: 67:
68: public function __set($name, $value)
69: {
70: $this->data[$name] = $value;
71: if (is_object($value)) {
72: $this->meta[$name]['V'] = Nette\Reflection\ClassReflection::from($value)->getAnnotation('serializationVersion');
73: }
74: }
75:
76:
77:
78: 79: 80: 81: 82:
83: public function &__get($name)
84: {
85: if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
86: trigger_error("The variable '$name' does not exist in session namespace", E_USER_NOTICE);
87: }
88:
89: return $this->data[$name];
90: }
91:
92:
93:
94: 95: 96: 97: 98:
99: public function __isset($name)
100: {
101: return isset($this->data[$name]);
102: }
103:
104:
105:
106: 107: 108: 109: 110:
111: public function __unset($name)
112: {
113: unset($this->data[$name], $this->meta[$name]);
114: }
115:
116:
117:
118: 119: 120: 121: 122: 123:
124: public function offsetSet($name, $value)
125: {
126: $this->__set($name, $value);
127: }
128:
129:
130:
131: 132: 133: 134: 135:
136: public function offsetGet($name)
137: {
138: return $this->__get($name);
139: }
140:
141:
142:
143: 144: 145: 146: 147:
148: public function offsetExists($name)
149: {
150: return $this->__isset($name);
151: }
152:
153:
154:
155: 156: 157: 158: 159:
160: public function offsetUnset($name)
161: {
162: $this->__unset($name);
163: }
164:
165:
166:
167: 168: 169: 170: 171: 172:
173: public function setExpiration($time, $variables = NULL)
174: {
175: if (empty($time)) {
176: $time = NULL;
177: $whenBrowserIsClosed = TRUE;
178: } else {
179: $time = Nette\DateTime::from($time)->format('U');
180: $whenBrowserIsClosed = FALSE;
181: }
182:
183: if ($variables === NULL) {
184: $this->meta['']['T'] = $time;
185: $this->meta['']['B'] = $whenBrowserIsClosed;
186:
187: } elseif (is_array($variables)) {
188: foreach ($variables as $variable) {
189: $this->meta[$variable]['T'] = $time;
190: $this->meta[$variable]['B'] = $whenBrowserIsClosed;
191: }
192:
193: } else {
194: $this->meta[$variables]['T'] = $time;
195: $this->meta[$variables]['B'] = $whenBrowserIsClosed;
196: }
197: return $this;
198: }
199:
200:
201:
202: 203: 204: 205: 206:
207: public function removeExpiration($variables = NULL)
208: {
209: if ($variables === NULL) {
210:
211: unset($this->meta['']['T'], $this->meta['']['B']);
212:
213: } elseif (is_array($variables)) {
214:
215: foreach ($variables as $variable) {
216: unset($this->meta[$variable]['T'], $this->meta[$variable]['B']);
217: }
218: } else {
219: unset($this->meta[$variables]['T'], $this->meta[$variable]['B']);
220: }
221: }
222:
223:
224:
225: 226: 227: 228:
229: public function remove()
230: {
231: $this->data = NULL;
232: $this->meta = NULL;
233: }
234:
235: }
236: