Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpFoundation\Session;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
Chris@0
|
17 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
Chris@0
|
18 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
Chris@17
|
19 use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
23 * @author Drak <drak@zikula.org>
|
Chris@0
|
24 */
|
Chris@0
|
25 class Session implements SessionInterface, \IteratorAggregate, \Countable
|
Chris@0
|
26 {
|
Chris@0
|
27 protected $storage;
|
Chris@0
|
28
|
Chris@0
|
29 private $flashName;
|
Chris@14
|
30 private $attributeName;
|
Chris@17
|
31 private $data = [];
|
Chris@16
|
32 private $usageIndex = 0;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * @param SessionStorageInterface $storage A SessionStorageInterface instance
|
Chris@0
|
36 * @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
|
Chris@0
|
37 * @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
|
Chris@0
|
38 */
|
Chris@0
|
39 public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
|
Chris@0
|
40 {
|
Chris@0
|
41 $this->storage = $storage ?: new NativeSessionStorage();
|
Chris@0
|
42
|
Chris@0
|
43 $attributes = $attributes ?: new AttributeBag();
|
Chris@0
|
44 $this->attributeName = $attributes->getName();
|
Chris@0
|
45 $this->registerBag($attributes);
|
Chris@0
|
46
|
Chris@0
|
47 $flashes = $flashes ?: new FlashBag();
|
Chris@0
|
48 $this->flashName = $flashes->getName();
|
Chris@0
|
49 $this->registerBag($flashes);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 */
|
Chris@0
|
55 public function start()
|
Chris@0
|
56 {
|
Chris@0
|
57 return $this->storage->start();
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 public function has($name)
|
Chris@0
|
64 {
|
Chris@14
|
65 return $this->getAttributeBag()->has($name);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function get($name, $default = null)
|
Chris@0
|
72 {
|
Chris@14
|
73 return $this->getAttributeBag()->get($name, $default);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 */
|
Chris@0
|
79 public function set($name, $value)
|
Chris@0
|
80 {
|
Chris@14
|
81 $this->getAttributeBag()->set($name, $value);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public function all()
|
Chris@0
|
88 {
|
Chris@14
|
89 return $this->getAttributeBag()->all();
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * {@inheritdoc}
|
Chris@0
|
94 */
|
Chris@0
|
95 public function replace(array $attributes)
|
Chris@0
|
96 {
|
Chris@14
|
97 $this->getAttributeBag()->replace($attributes);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * {@inheritdoc}
|
Chris@0
|
102 */
|
Chris@0
|
103 public function remove($name)
|
Chris@0
|
104 {
|
Chris@14
|
105 return $this->getAttributeBag()->remove($name);
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * {@inheritdoc}
|
Chris@0
|
110 */
|
Chris@0
|
111 public function clear()
|
Chris@0
|
112 {
|
Chris@14
|
113 $this->getAttributeBag()->clear();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function isStarted()
|
Chris@0
|
120 {
|
Chris@0
|
121 return $this->storage->isStarted();
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * Returns an iterator for attributes.
|
Chris@0
|
126 *
|
Chris@0
|
127 * @return \ArrayIterator An \ArrayIterator instance
|
Chris@0
|
128 */
|
Chris@0
|
129 public function getIterator()
|
Chris@0
|
130 {
|
Chris@14
|
131 return new \ArrayIterator($this->getAttributeBag()->all());
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * Returns the number of attributes.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @return int The number of attributes
|
Chris@0
|
138 */
|
Chris@0
|
139 public function count()
|
Chris@0
|
140 {
|
Chris@17
|
141 return \count($this->getAttributeBag()->all());
|
Chris@14
|
142 }
|
Chris@14
|
143
|
Chris@14
|
144 /**
|
Chris@16
|
145 * @return int
|
Chris@14
|
146 *
|
Chris@14
|
147 * @internal
|
Chris@14
|
148 */
|
Chris@16
|
149 public function getUsageIndex()
|
Chris@14
|
150 {
|
Chris@16
|
151 return $this->usageIndex;
|
Chris@14
|
152 }
|
Chris@14
|
153
|
Chris@14
|
154 /**
|
Chris@14
|
155 * @return bool
|
Chris@14
|
156 *
|
Chris@14
|
157 * @internal
|
Chris@14
|
158 */
|
Chris@14
|
159 public function isEmpty()
|
Chris@14
|
160 {
|
Chris@17
|
161 if ($this->isStarted()) {
|
Chris@17
|
162 ++$this->usageIndex;
|
Chris@17
|
163 }
|
Chris@14
|
164 foreach ($this->data as &$data) {
|
Chris@14
|
165 if (!empty($data)) {
|
Chris@14
|
166 return false;
|
Chris@14
|
167 }
|
Chris@14
|
168 }
|
Chris@14
|
169
|
Chris@14
|
170 return true;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * {@inheritdoc}
|
Chris@0
|
175 */
|
Chris@0
|
176 public function invalidate($lifetime = null)
|
Chris@0
|
177 {
|
Chris@0
|
178 $this->storage->clear();
|
Chris@0
|
179
|
Chris@0
|
180 return $this->migrate(true, $lifetime);
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * {@inheritdoc}
|
Chris@0
|
185 */
|
Chris@0
|
186 public function migrate($destroy = false, $lifetime = null)
|
Chris@0
|
187 {
|
Chris@0
|
188 return $this->storage->regenerate($destroy, $lifetime);
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * {@inheritdoc}
|
Chris@0
|
193 */
|
Chris@0
|
194 public function save()
|
Chris@0
|
195 {
|
Chris@0
|
196 $this->storage->save();
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * {@inheritdoc}
|
Chris@0
|
201 */
|
Chris@0
|
202 public function getId()
|
Chris@0
|
203 {
|
Chris@0
|
204 return $this->storage->getId();
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 /**
|
Chris@0
|
208 * {@inheritdoc}
|
Chris@0
|
209 */
|
Chris@0
|
210 public function setId($id)
|
Chris@0
|
211 {
|
Chris@17
|
212 if ($this->storage->getId() !== $id) {
|
Chris@17
|
213 $this->storage->setId($id);
|
Chris@17
|
214 }
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * {@inheritdoc}
|
Chris@0
|
219 */
|
Chris@0
|
220 public function getName()
|
Chris@0
|
221 {
|
Chris@0
|
222 return $this->storage->getName();
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * {@inheritdoc}
|
Chris@0
|
227 */
|
Chris@0
|
228 public function setName($name)
|
Chris@0
|
229 {
|
Chris@0
|
230 $this->storage->setName($name);
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 /**
|
Chris@0
|
234 * {@inheritdoc}
|
Chris@0
|
235 */
|
Chris@0
|
236 public function getMetadataBag()
|
Chris@0
|
237 {
|
Chris@16
|
238 ++$this->usageIndex;
|
Chris@16
|
239
|
Chris@0
|
240 return $this->storage->getMetadataBag();
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * {@inheritdoc}
|
Chris@0
|
245 */
|
Chris@0
|
246 public function registerBag(SessionBagInterface $bag)
|
Chris@0
|
247 {
|
Chris@16
|
248 $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex));
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * {@inheritdoc}
|
Chris@0
|
253 */
|
Chris@0
|
254 public function getBag($name)
|
Chris@0
|
255 {
|
Chris@14
|
256 return $this->storage->getBag($name)->getBag();
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 /**
|
Chris@0
|
260 * Gets the flashbag interface.
|
Chris@0
|
261 *
|
Chris@0
|
262 * @return FlashBagInterface
|
Chris@0
|
263 */
|
Chris@0
|
264 public function getFlashBag()
|
Chris@0
|
265 {
|
Chris@0
|
266 return $this->getBag($this->flashName);
|
Chris@0
|
267 }
|
Chris@14
|
268
|
Chris@14
|
269 /**
|
Chris@14
|
270 * Gets the attributebag interface.
|
Chris@14
|
271 *
|
Chris@14
|
272 * Note that this method was added to help with IDE autocompletion.
|
Chris@14
|
273 *
|
Chris@14
|
274 * @return AttributeBagInterface
|
Chris@14
|
275 */
|
Chris@14
|
276 private function getAttributeBag()
|
Chris@14
|
277 {
|
Chris@14
|
278 return $this->getBag($this->attributeName);
|
Chris@14
|
279 }
|
Chris@0
|
280 }
|