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@14
|
32 private $hasBeenStarted;
|
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@14
|
141 return count($this->getAttributeBag()->all());
|
Chris@14
|
142 }
|
Chris@14
|
143
|
Chris@14
|
144 /**
|
Chris@14
|
145 * @return bool
|
Chris@14
|
146 *
|
Chris@14
|
147 * @internal
|
Chris@14
|
148 */
|
Chris@14
|
149 public function hasBeenStarted()
|
Chris@14
|
150 {
|
Chris@14
|
151 return $this->hasBeenStarted;
|
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@14
|
161 foreach ($this->data as &$data) {
|
Chris@14
|
162 if (!empty($data)) {
|
Chris@14
|
163 return false;
|
Chris@14
|
164 }
|
Chris@14
|
165 }
|
Chris@14
|
166
|
Chris@14
|
167 return true;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * {@inheritdoc}
|
Chris@0
|
172 */
|
Chris@0
|
173 public function invalidate($lifetime = null)
|
Chris@0
|
174 {
|
Chris@0
|
175 $this->storage->clear();
|
Chris@0
|
176
|
Chris@0
|
177 return $this->migrate(true, $lifetime);
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * {@inheritdoc}
|
Chris@0
|
182 */
|
Chris@0
|
183 public function migrate($destroy = false, $lifetime = null)
|
Chris@0
|
184 {
|
Chris@0
|
185 return $this->storage->regenerate($destroy, $lifetime);
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * {@inheritdoc}
|
Chris@0
|
190 */
|
Chris@0
|
191 public function save()
|
Chris@0
|
192 {
|
Chris@0
|
193 $this->storage->save();
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * {@inheritdoc}
|
Chris@0
|
198 */
|
Chris@0
|
199 public function getId()
|
Chris@0
|
200 {
|
Chris@0
|
201 return $this->storage->getId();
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 /**
|
Chris@0
|
205 * {@inheritdoc}
|
Chris@0
|
206 */
|
Chris@0
|
207 public function setId($id)
|
Chris@0
|
208 {
|
Chris@0
|
209 $this->storage->setId($id);
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 public function getName()
|
Chris@0
|
216 {
|
Chris@0
|
217 return $this->storage->getName();
|
Chris@0
|
218 }
|
Chris@0
|
219
|
Chris@0
|
220 /**
|
Chris@0
|
221 * {@inheritdoc}
|
Chris@0
|
222 */
|
Chris@0
|
223 public function setName($name)
|
Chris@0
|
224 {
|
Chris@0
|
225 $this->storage->setName($name);
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * {@inheritdoc}
|
Chris@0
|
230 */
|
Chris@0
|
231 public function getMetadataBag()
|
Chris@0
|
232 {
|
Chris@0
|
233 return $this->storage->getMetadataBag();
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * {@inheritdoc}
|
Chris@0
|
238 */
|
Chris@0
|
239 public function registerBag(SessionBagInterface $bag)
|
Chris@0
|
240 {
|
Chris@14
|
241 $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->hasBeenStarted));
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * {@inheritdoc}
|
Chris@0
|
246 */
|
Chris@0
|
247 public function getBag($name)
|
Chris@0
|
248 {
|
Chris@14
|
249 return $this->storage->getBag($name)->getBag();
|
Chris@0
|
250 }
|
Chris@0
|
251
|
Chris@0
|
252 /**
|
Chris@0
|
253 * Gets the flashbag interface.
|
Chris@0
|
254 *
|
Chris@0
|
255 * @return FlashBagInterface
|
Chris@0
|
256 */
|
Chris@0
|
257 public function getFlashBag()
|
Chris@0
|
258 {
|
Chris@0
|
259 return $this->getBag($this->flashName);
|
Chris@0
|
260 }
|
Chris@14
|
261
|
Chris@14
|
262 /**
|
Chris@14
|
263 * Gets the attributebag interface.
|
Chris@14
|
264 *
|
Chris@14
|
265 * Note that this method was added to help with IDE autocompletion.
|
Chris@14
|
266 *
|
Chris@14
|
267 * @return AttributeBagInterface
|
Chris@14
|
268 */
|
Chris@14
|
269 private function getAttributeBag()
|
Chris@14
|
270 {
|
Chris@14
|
271 return $this->getBag($this->attributeName);
|
Chris@14
|
272 }
|
Chris@0
|
273 }
|