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