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