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