1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Session;
13:
14: use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
15: use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
16: use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
17: use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
18: use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
19: use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
20: use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
21:
22: 23: 24: 25: 26: 27: 28: 29:
30: class Session implements SessionInterface, \IteratorAggregate, \Countable
31: {
32: 33: 34: 35: 36:
37: protected $storage;
38:
39: 40: 41:
42: private $flashName;
43:
44: 45: 46:
47: private $attributeName;
48:
49: 50: 51: 52: 53: 54: 55:
56: public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
57: {
58: $this->storage = $storage ?: new NativeSessionStorage();
59:
60: $attributes = $attributes ?: new AttributeBag();
61: $this->attributeName = $attributes->getName();
62: $this->registerBag($attributes);
63:
64: $flashes = $flashes ?: new FlashBag();
65: $this->flashName = $flashes->getName();
66: $this->registerBag($flashes);
67: }
68:
69: 70: 71:
72: public function start()
73: {
74: return $this->storage->start();
75: }
76:
77: 78: 79:
80: public function has($name)
81: {
82: return $this->storage->getBag($this->attributeName)->has($name);
83: }
84:
85: 86: 87:
88: public function get($name, $default = null)
89: {
90: return $this->storage->getBag($this->attributeName)->get($name, $default);
91: }
92:
93: 94: 95:
96: public function set($name, $value)
97: {
98: $this->storage->getBag($this->attributeName)->set($name, $value);
99: }
100:
101: 102: 103:
104: public function all()
105: {
106: return $this->storage->getBag($this->attributeName)->all();
107: }
108:
109: 110: 111:
112: public function replace(array $attributes)
113: {
114: $this->storage->getBag($this->attributeName)->replace($attributes);
115: }
116:
117: 118: 119:
120: public function remove($name)
121: {
122: return $this->storage->getBag($this->attributeName)->remove($name);
123: }
124:
125: 126: 127:
128: public function clear()
129: {
130: $this->storage->getBag($this->attributeName)->clear();
131: }
132:
133: 134: 135:
136: public function isStarted()
137: {
138: return $this->storage->isStarted();
139: }
140:
141: 142: 143: 144: 145:
146: public function getIterator()
147: {
148: return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
149: }
150:
151: 152: 153: 154: 155:
156: public function count()
157: {
158: return count($this->storage->getBag($this->attributeName)->all());
159: }
160:
161: 162: 163:
164: public function invalidate($lifetime = null)
165: {
166: $this->storage->clear();
167:
168: return $this->migrate(true, $lifetime);
169: }
170:
171: 172: 173:
174: public function migrate($destroy = false, $lifetime = null)
175: {
176: return $this->storage->regenerate($destroy, $lifetime);
177: }
178:
179: 180: 181:
182: public function save()
183: {
184: $this->storage->save();
185: }
186:
187: 188: 189:
190: public function getId()
191: {
192: return $this->storage->getId();
193: }
194:
195: 196: 197:
198: public function setId($id)
199: {
200: $this->storage->setId($id);
201: }
202:
203: 204: 205:
206: public function getName()
207: {
208: return $this->storage->getName();
209: }
210:
211: 212: 213:
214: public function setName($name)
215: {
216: $this->storage->setName($name);
217: }
218:
219: 220: 221:
222: public function getMetadataBag()
223: {
224: return $this->storage->getMetadataBag();
225: }
226:
227: 228: 229:
230: public function registerBag(SessionBagInterface $bag)
231: {
232: $this->storage->registerBag($bag);
233: }
234:
235: 236: 237:
238: public function getBag($name)
239: {
240: return $this->storage->getBag($name);
241: }
242:
243: 244: 245: 246: 247:
248: public function getFlashBag()
249: {
250: return $this->getBag($this->flashName);
251: }
252:
253:
254:
255: 256: 257: 258: 259:
260: public function getFlashes()
261: {
262: trigger_error('getFlashes() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
263:
264: $all = $this->getBag($this->flashName)->all();
265:
266: $return = array();
267: if ($all) {
268: foreach ($all as $name => $array) {
269: if (is_numeric(key($array))) {
270: $return[$name] = reset($array);
271: } else {
272: $return[$name] = $array;
273: }
274: }
275: }
276:
277: return $return;
278: }
279:
280: 281: 282: 283: 284:
285: public function setFlashes($values)
286: {
287: trigger_error('setFlashes() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
288:
289: foreach ($values as $name => $value) {
290: $this->getBag($this->flashName)->set($name, $value);
291: }
292: }
293:
294: 295: 296: 297: 298: 299: 300: 301:
302: public function getFlash($name, $default = null)
303: {
304: trigger_error('getFlash() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
305:
306: $return = $this->getBag($this->flashName)->get($name);
307:
308: return empty($return) ? $default : reset($return);
309: }
310:
311: 312: 313: 314: 315: 316:
317: public function setFlash($name, $value)
318: {
319: trigger_error('setFlash() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
320:
321: $this->getBag($this->flashName)->set($name, $value);
322: }
323:
324: 325: 326: 327: 328: 329: 330:
331: public function hasFlash($name)
332: {
333: trigger_error('hasFlash() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
334:
335: return $this->getBag($this->flashName)->has($name);
336: }
337:
338: 339: 340: 341: 342:
343: public function removeFlash($name)
344: {
345: trigger_error('removeFlash() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
346:
347: $this->getBag($this->flashName)->get($name);
348: }
349:
350: 351: 352: 353: 354:
355: public function clearFlashes()
356: {
357: trigger_error('clearFlashes() is deprecated since version 2.1 and will be removed in 2.3. Use the FlashBag instead.', E_USER_DEPRECATED);
358:
359: return $this->getBag($this->flashName)->clear();
360: }
361: }
362: