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