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\Storage\NativeSessionStorage;
20:
21: 22: 23: 24: 25: 26:
27: class Session implements SessionInterface, \IteratorAggregate, \Countable
28: {
29: 30: 31: 32: 33:
34: protected $storage;
35:
36: 37: 38:
39: private $flashName;
40:
41: 42: 43:
44: private $attributeName;
45:
46: 47: 48: 49: 50: 51: 52:
53: public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
54: {
55: $this->storage = $storage ?: new NativeSessionStorage();
56:
57: $attributes = $attributes ?: new AttributeBag();
58: $this->attributeName = $attributes->getName();
59: $this->registerBag($attributes);
60:
61: $flashes = $flashes ?: new FlashBag();
62: $this->flashName = $flashes->getName();
63: $this->registerBag($flashes);
64: }
65:
66: 67: 68:
69: public function start()
70: {
71: return $this->storage->start();
72: }
73:
74: 75: 76:
77: public function has($name)
78: {
79: return $this->storage->getBag($this->attributeName)->has($name);
80: }
81:
82: 83: 84:
85: public function get($name, $default = null)
86: {
87: return $this->storage->getBag($this->attributeName)->get($name, $default);
88: }
89:
90: 91: 92:
93: public function set($name, $value)
94: {
95: $this->storage->getBag($this->attributeName)->set($name, $value);
96: }
97:
98: 99: 100:
101: public function all()
102: {
103: return $this->storage->getBag($this->attributeName)->all();
104: }
105:
106: 107: 108:
109: public function replace(array $attributes)
110: {
111: $this->storage->getBag($this->attributeName)->replace($attributes);
112: }
113:
114: 115: 116:
117: public function remove($name)
118: {
119: return $this->storage->getBag($this->attributeName)->remove($name);
120: }
121:
122: 123: 124:
125: public function clear()
126: {
127: $this->storage->getBag($this->attributeName)->clear();
128: }
129:
130: 131: 132:
133: public function isStarted()
134: {
135: return $this->storage->isStarted();
136: }
137:
138: 139: 140: 141: 142:
143: public function getIterator()
144: {
145: return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
146: }
147:
148: 149: 150: 151: 152:
153: public function count()
154: {
155: return count($this->storage->getBag($this->attributeName)->all());
156: }
157:
158: 159: 160:
161: public function invalidate($lifetime = null)
162: {
163: $this->storage->clear();
164:
165: return $this->migrate(true, $lifetime);
166: }
167:
168: 169: 170:
171: public function migrate($destroy = false, $lifetime = null)
172: {
173: return $this->storage->regenerate($destroy, $lifetime);
174: }
175:
176: 177: 178:
179: public function save()
180: {
181: $this->storage->save();
182: }
183:
184: 185: 186:
187: public function getId()
188: {
189: return $this->storage->getId();
190: }
191:
192: 193: 194:
195: public function setId($id)
196: {
197: $this->storage->setId($id);
198: }
199:
200: 201: 202:
203: public function getName()
204: {
205: return $this->storage->getName();
206: }
207:
208: 209: 210:
211: public function setName($name)
212: {
213: $this->storage->setName($name);
214: }
215:
216: 217: 218:
219: public function getMetadataBag()
220: {
221: return $this->storage->getMetadataBag();
222: }
223:
224: 225: 226:
227: public function registerBag(SessionBagInterface $bag)
228: {
229: $this->storage->registerBag($bag);
230: }
231:
232: 233: 234:
235: public function getBag($name)
236: {
237: return $this->storage->getBag($name);
238: }
239:
240: 241: 242: 243: 244:
245: public function getFlashBag()
246: {
247: return $this->getBag($this->flashName);
248: }
249: }
250: