1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei;
10:
11: 12: 13: 14: 15:
16: class Session extends Base implements \ArrayAccess, \Countable, \IteratorAggregate
17: {
18: 19: 20: 21: 22:
23: protected $namespace = false;
24:
25: 26: 27: 28: 29:
30: protected $data = array();
31:
32: 33: 34: 35: 36: 37:
38: protected $inis = array();
39:
40: 41: 42: 43: 44:
45: public function __construct(array $options = array())
46: {
47: parent::__construct($options);
48: $this->start();
49: }
50:
51: 52: 53: 54: 55:
56: public function start()
57: {
58: if (!session_id()) {
59: $file = $line = null;
60: if (headers_sent($file, $line)) {
61: throw new \RuntimeException(sprintf('Unable to start session, output started at %s:%s', $file, $line));
62: }
63: session_start();
64: }
65:
66: if ($this->namespace) {
67: if (!isset($_SESSION[$this->namespace])) {
68: $_SESSION[$this->namespace] = array();
69: }
70: $this->data = &$_SESSION[$this->namespace];
71: } else {
72: $this->data = &$_SESSION;
73: }
74: }
75:
76: 77: 78: 79: 80: 81: 82:
83: public function __invoke($key, $value = null)
84: {
85: if (1 == func_num_args()) {
86: return $this->get($key);
87: } else {
88: return $this->set($key, $value);
89: }
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: public function get($key, $default = null)
100: {
101: return isset($this->data[$key]) ? $this->data[$key] : $default;
102: }
103:
104: 105: 106: 107: 108: 109: 110:
111: public function set($name, $value = null)
112: {
113: if (!is_array($name)) {
114: $this->data[$name] = $value;
115: } else {
116: foreach ($name as $key => $value) {
117: $this->data[$key] = $value;
118: }
119: }
120: return $this;
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function remove($name)
130: {
131: unset($this->data[$name]);
132: return $this;
133: }
134:
135: 136: 137: 138: 139: 140:
141: public function exists($name)
142: {
143: return array_key_exists($name, $this->data);
144: }
145:
146: 147: 148: 149: 150:
151: public function clear()
152: {
153: $this->data = array();
154: return $this;
155: }
156:
157: 158: 159: 160: 161:
162: public function destroy()
163: {
164: if (session_id()) {
165: session_destroy();
166: }
167: return $this->clear();
168: }
169:
170: 171: 172: 173: 174:
175: public function toArray()
176: {
177: return $this->data;
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function setInis($inis)
187: {
188: foreach ($inis as $name => $value) {
189: ini_set('session.' . $name, $value);
190: }
191: $this->inis = $inis + $this->inis;
192: return $this;
193: }
194:
195: 196: 197: 198: 199: 200:
201: public function offsetExists($offset)
202: {
203: return array_key_exists($offset, $this->data);
204: }
205:
206: 207: 208: 209: 210: 211:
212: public function &offsetGet($offset)
213: {
214: return $this->data[$offset];
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function offsetSet($offset, $value)
224: {
225: $this->data[$offset] = $value;
226: }
227:
228: 229: 230: 231: 232:
233: public function offsetUnset($offset)
234: {
235: unset($this->data[$offset]);
236: }
237:
238: 239: 240: 241: 242:
243: public function count()
244: {
245: return count($this->data);
246: }
247:
248: 249: 250: 251: 252:
253: public function getIterator()
254: {
255: return new \ArrayIterator($this->data);
256: }
257: }
258: