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\Storage\SessionStorageInterface;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
|
Chris@0
|
17 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
Chris@0
|
18 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
Chris@0
|
19 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
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@14
|
31 private $data = array();
|
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@16
|
57 ++$this->usageIndex;
|
Chris@16
|
58
|
Chris@0
|
59 return $this->storage->start();
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * {@inheritdoc}
|
Chris@0
|
64 */
|
Chris@0
|
65 public function has($name)
|
Chris@0
|
66 {
|
Chris@14
|
67 return $this->getAttributeBag()->has($name);
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 public function get($name, $default = null)
|
Chris@0
|
74 {
|
Chris@14
|
75 return $this->getAttributeBag()->get($name, $default);
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * {@inheritdoc}
|
Chris@0
|
80 */
|
Chris@0
|
81 public function set($name, $value)
|
Chris@0
|
82 {
|
Chris@14
|
83 $this->getAttributeBag()->set($name, $value);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * {@inheritdoc}
|
Chris@0
|
88 */
|
Chris@0
|
89 public function all()
|
Chris@0
|
90 {
|
Chris@14
|
91 return $this->getAttributeBag()->all();
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function replace(array $attributes)
|
Chris@0
|
98 {
|
Chris@14
|
99 $this->getAttributeBag()->replace($attributes);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * {@inheritdoc}
|
Chris@0
|
104 */
|
Chris@0
|
105 public function remove($name)
|
Chris@0
|
106 {
|
Chris@14
|
107 return $this->getAttributeBag()->remove($name);
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * {@inheritdoc}
|
Chris@0
|
112 */
|
Chris@0
|
113 public function clear()
|
Chris@0
|
114 {
|
Chris@14
|
115 $this->getAttributeBag()->clear();
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function isStarted()
|
Chris@0
|
122 {
|
Chris@0
|
123 return $this->storage->isStarted();
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * Returns an iterator for attributes.
|
Chris@0
|
128 *
|
Chris@0
|
129 * @return \ArrayIterator An \ArrayIterator instance
|
Chris@0
|
130 */
|
Chris@0
|
131 public function getIterator()
|
Chris@0
|
132 {
|
Chris@14
|
133 return new \ArrayIterator($this->getAttributeBag()->all());
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Returns the number of attributes.
|
Chris@0
|
138 *
|
Chris@0
|
139 * @return int The number of attributes
|
Chris@0
|
140 */
|
Chris@0
|
141 public function count()
|
Chris@0
|
142 {
|
Chris@14
|
143 return count($this->getAttributeBag()->all());
|
Chris@14
|
144 }
|
Chris@14
|
145
|
Chris@14
|
146 /**
|
Chris@16
|
147 * @return int
|
Chris@14
|
148 *
|
Chris@14
|
149 * @internal
|
Chris@14
|
150 */
|
Chris@16
|
151 public function getUsageIndex()
|
Chris@14
|
152 {
|
Chris@16
|
153 return $this->usageIndex;
|
Chris@14
|
154 }
|
Chris@14
|
155
|
Chris@14
|
156 /**
|
Chris@14
|
157 * @return bool
|
Chris@14
|
158 *
|
Chris@14
|
159 * @internal
|
Chris@14
|
160 */
|
Chris@14
|
161 public function isEmpty()
|
Chris@14
|
162 {
|
Chris@16
|
163 ++$this->usageIndex;
|
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@16
|
188 ++$this->usageIndex;
|
Chris@16
|
189
|
Chris@0
|
190 return $this->storage->regenerate($destroy, $lifetime);
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /**
|
Chris@0
|
194 * {@inheritdoc}
|
Chris@0
|
195 */
|
Chris@0
|
196 public function save()
|
Chris@0
|
197 {
|
Chris@16
|
198 ++$this->usageIndex;
|
Chris@16
|
199
|
Chris@0
|
200 $this->storage->save();
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * {@inheritdoc}
|
Chris@0
|
205 */
|
Chris@0
|
206 public function getId()
|
Chris@0
|
207 {
|
Chris@0
|
208 return $this->storage->getId();
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * {@inheritdoc}
|
Chris@0
|
213 */
|
Chris@0
|
214 public function setId($id)
|
Chris@0
|
215 {
|
Chris@0
|
216 $this->storage->setId($id);
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * {@inheritdoc}
|
Chris@0
|
221 */
|
Chris@0
|
222 public function getName()
|
Chris@0
|
223 {
|
Chris@0
|
224 return $this->storage->getName();
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * {@inheritdoc}
|
Chris@0
|
229 */
|
Chris@0
|
230 public function setName($name)
|
Chris@0
|
231 {
|
Chris@0
|
232 $this->storage->setName($name);
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * {@inheritdoc}
|
Chris@0
|
237 */
|
Chris@0
|
238 public function getMetadataBag()
|
Chris@0
|
239 {
|
Chris@16
|
240 ++$this->usageIndex;
|
Chris@16
|
241
|
Chris@0
|
242 return $this->storage->getMetadataBag();
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * {@inheritdoc}
|
Chris@0
|
247 */
|
Chris@0
|
248 public function registerBag(SessionBagInterface $bag)
|
Chris@0
|
249 {
|
Chris@16
|
250 $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex));
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 /**
|
Chris@0
|
254 * {@inheritdoc}
|
Chris@0
|
255 */
|
Chris@0
|
256 public function getBag($name)
|
Chris@0
|
257 {
|
Chris@14
|
258 return $this->storage->getBag($name)->getBag();
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 /**
|
Chris@0
|
262 * Gets the flashbag interface.
|
Chris@0
|
263 *
|
Chris@0
|
264 * @return FlashBagInterface
|
Chris@0
|
265 */
|
Chris@0
|
266 public function getFlashBag()
|
Chris@0
|
267 {
|
Chris@0
|
268 return $this->getBag($this->flashName);
|
Chris@0
|
269 }
|
Chris@14
|
270
|
Chris@14
|
271 /**
|
Chris@14
|
272 * Gets the attributebag interface.
|
Chris@14
|
273 *
|
Chris@14
|
274 * Note that this method was added to help with IDE autocompletion.
|
Chris@14
|
275 *
|
Chris@14
|
276 * @return AttributeBagInterface
|
Chris@14
|
277 */
|
Chris@14
|
278 private function getAttributeBag()
|
Chris@14
|
279 {
|
Chris@14
|
280 return $this->getBag($this->attributeName);
|
Chris@14
|
281 }
|
Chris@0
|
282 }
|