|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* Session. |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
* |
|
|
21 |
* @api |
|
|
22 |
*/ |
|
|
23 |
class Session implements \Serializable |
|
|
24 |
{ |
|
|
25 |
protected $storage; |
|
|
26 |
protected $started; |
|
|
27 |
protected $attributes; |
|
|
28 |
protected $flashes; |
|
|
29 |
protected $oldFlashes; |
|
|
30 |
protected $locale; |
|
|
31 |
protected $defaultLocale; |
|
|
32 |
protected $saved; |
|
|
33 |
|
|
|
34 |
/** |
|
|
35 |
* Constructor. |
|
|
36 |
* |
|
|
37 |
* @param SessionStorageInterface $storage A SessionStorageInterface instance |
|
|
38 |
* @param string $defaultLocale The default locale |
|
|
39 |
*/ |
|
|
40 |
public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en') |
|
|
41 |
{ |
|
|
42 |
$this->storage = $storage; |
|
|
43 |
$this->defaultLocale = $defaultLocale; |
|
|
44 |
$this->locale = $defaultLocale; |
|
|
45 |
$this->flashes = array(); |
|
|
46 |
$this->oldFlashes = array(); |
|
|
47 |
$this->attributes = array(); |
|
|
48 |
$this->setPhpDefaultLocale($this->defaultLocale); |
|
|
49 |
$this->started = false; |
|
|
50 |
$this->saved = false; |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
/** |
|
|
54 |
* Starts the session storage. |
|
|
55 |
* |
|
|
56 |
* @api |
|
|
57 |
*/ |
|
|
58 |
public function start() |
|
|
59 |
{ |
|
|
60 |
if (true === $this->started) { |
|
|
61 |
return; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
$this->storage->start(); |
|
|
65 |
|
|
|
66 |
$attributes = $this->storage->read('_symfony2'); |
|
|
67 |
|
|
|
68 |
if (isset($attributes['attributes'])) { |
|
|
69 |
$this->attributes = $attributes['attributes']; |
|
|
70 |
$this->flashes = $attributes['flashes']; |
|
|
71 |
$this->locale = $attributes['locale']; |
|
|
72 |
$this->setPhpDefaultLocale($this->locale); |
|
|
73 |
|
|
|
74 |
// flag current flash messages to be removed at shutdown |
|
|
75 |
$this->oldFlashes = $this->flashes; |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
$this->started = true; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
/** |
|
|
82 |
* Checks if an attribute is defined. |
|
|
83 |
* |
|
|
84 |
* @param string $name The attribute name |
|
|
85 |
* |
|
|
86 |
* @return Boolean true if the attribute is defined, false otherwise |
|
|
87 |
* |
|
|
88 |
* @api |
|
|
89 |
*/ |
|
|
90 |
public function has($name) |
|
|
91 |
{ |
|
|
92 |
return array_key_exists($name, $this->attributes); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
/** |
|
|
96 |
* Returns an attribute. |
|
|
97 |
* |
|
|
98 |
* @param string $name The attribute name |
|
|
99 |
* @param mixed $default The default value |
|
|
100 |
* |
|
|
101 |
* @return mixed |
|
|
102 |
* |
|
|
103 |
* @api |
|
|
104 |
*/ |
|
|
105 |
public function get($name, $default = null) |
|
|
106 |
{ |
|
|
107 |
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default; |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
/** |
|
|
111 |
* Sets an attribute. |
|
|
112 |
* |
|
|
113 |
* @param string $name |
|
|
114 |
* @param mixed $value |
|
|
115 |
* |
|
|
116 |
* @api |
|
|
117 |
*/ |
|
|
118 |
public function set($name, $value) |
|
|
119 |
{ |
|
|
120 |
if (false === $this->started) { |
|
|
121 |
$this->start(); |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
$this->attributes[$name] = $value; |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
/** |
|
|
128 |
* Returns attributes. |
|
|
129 |
* |
|
|
130 |
* @return array Attributes |
|
|
131 |
* |
|
|
132 |
* @api |
|
|
133 |
*/ |
|
|
134 |
public function all() |
|
|
135 |
{ |
|
|
136 |
return $this->attributes; |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
/** |
|
|
140 |
* Sets attributes. |
|
|
141 |
* |
|
|
142 |
* @param array $attributes Attributes |
|
|
143 |
* |
|
|
144 |
* @api |
|
|
145 |
*/ |
|
|
146 |
public function replace(array $attributes) |
|
|
147 |
{ |
|
|
148 |
if (false === $this->started) { |
|
|
149 |
$this->start(); |
|
|
150 |
} |
|
|
151 |
|
|
|
152 |
$this->attributes = $attributes; |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
/** |
|
|
156 |
* Removes an attribute. |
|
|
157 |
* |
|
|
158 |
* @param string $name |
|
|
159 |
* |
|
|
160 |
* @api |
|
|
161 |
*/ |
|
|
162 |
public function remove($name) |
|
|
163 |
{ |
|
|
164 |
if (false === $this->started) { |
|
|
165 |
$this->start(); |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
if (array_key_exists($name, $this->attributes)) { |
|
|
169 |
unset($this->attributes[$name]); |
|
|
170 |
} |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
/** |
|
|
174 |
* Clears all attributes. |
|
|
175 |
* |
|
|
176 |
* @api |
|
|
177 |
*/ |
|
|
178 |
public function clear() |
|
|
179 |
{ |
|
|
180 |
if (false === $this->started) { |
|
|
181 |
$this->start(); |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
$this->attributes = array(); |
|
|
185 |
$this->flashes = array(); |
|
|
186 |
$this->setPhpDefaultLocale($this->locale = $this->defaultLocale); |
|
|
187 |
} |
|
|
188 |
|
|
|
189 |
/** |
|
|
190 |
* Invalidates the current session. |
|
|
191 |
* |
|
|
192 |
* @api |
|
|
193 |
*/ |
|
|
194 |
public function invalidate() |
|
|
195 |
{ |
|
|
196 |
$this->clear(); |
|
|
197 |
$this->storage->regenerate(); |
|
|
198 |
} |
|
|
199 |
|
|
|
200 |
/** |
|
|
201 |
* Migrates the current session to a new session id while maintaining all |
|
|
202 |
* session attributes. |
|
|
203 |
* |
|
|
204 |
* @api |
|
|
205 |
*/ |
|
|
206 |
public function migrate() |
|
|
207 |
{ |
|
|
208 |
$this->storage->regenerate(); |
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
/** |
|
|
212 |
* Returns the session ID |
|
|
213 |
* |
|
|
214 |
* @return mixed The session ID |
|
|
215 |
* |
|
|
216 |
* @api |
|
|
217 |
*/ |
|
|
218 |
public function getId() |
|
|
219 |
{ |
|
|
220 |
if (false === $this->started) { |
|
|
221 |
$this->start(); |
|
|
222 |
} |
|
|
223 |
|
|
|
224 |
return $this->storage->getId(); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
/** |
|
|
228 |
* Returns the locale |
|
|
229 |
* |
|
|
230 |
* @return string |
|
|
231 |
*/ |
|
|
232 |
public function getLocale() |
|
|
233 |
{ |
|
|
234 |
return $this->locale; |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
/** |
|
|
238 |
* Sets the locale. |
|
|
239 |
* |
|
|
240 |
* @param string $locale |
|
|
241 |
*/ |
|
|
242 |
public function setLocale($locale) |
|
|
243 |
{ |
|
|
244 |
if (false === $this->started) { |
|
|
245 |
$this->start(); |
|
|
246 |
} |
|
|
247 |
|
|
|
248 |
$this->setPhpDefaultLocale($this->locale = $locale); |
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
/** |
|
|
252 |
* Gets the flash messages. |
|
|
253 |
* |
|
|
254 |
* @return array |
|
|
255 |
*/ |
|
|
256 |
public function getFlashes() |
|
|
257 |
{ |
|
|
258 |
return $this->flashes; |
|
|
259 |
} |
|
|
260 |
|
|
|
261 |
/** |
|
|
262 |
* Sets the flash messages. |
|
|
263 |
* |
|
|
264 |
* @param array $values |
|
|
265 |
*/ |
|
|
266 |
public function setFlashes($values) |
|
|
267 |
{ |
|
|
268 |
if (false === $this->started) { |
|
|
269 |
$this->start(); |
|
|
270 |
} |
|
|
271 |
|
|
|
272 |
$this->flashes = $values; |
|
|
273 |
$this->oldFlashes = array(); |
|
|
274 |
} |
|
|
275 |
|
|
|
276 |
/** |
|
|
277 |
* Gets a flash message. |
|
|
278 |
* |
|
|
279 |
* @param string $name |
|
|
280 |
* @param string|null $default |
|
|
281 |
* |
|
|
282 |
* @return string |
|
|
283 |
*/ |
|
|
284 |
public function getFlash($name, $default = null) |
|
|
285 |
{ |
|
|
286 |
return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default; |
|
|
287 |
} |
|
|
288 |
|
|
|
289 |
/** |
|
|
290 |
* Sets a flash message. |
|
|
291 |
* |
|
|
292 |
* @param string $name |
|
|
293 |
* @param string $value |
|
|
294 |
*/ |
|
|
295 |
public function setFlash($name, $value) |
|
|
296 |
{ |
|
|
297 |
if (false === $this->started) { |
|
|
298 |
$this->start(); |
|
|
299 |
} |
|
|
300 |
|
|
|
301 |
$this->flashes[$name] = $value; |
|
|
302 |
unset($this->oldFlashes[$name]); |
|
|
303 |
} |
|
|
304 |
|
|
|
305 |
/** |
|
|
306 |
* Checks whether a flash message exists. |
|
|
307 |
* |
|
|
308 |
* @param string $name |
|
|
309 |
* |
|
|
310 |
* @return Boolean |
|
|
311 |
*/ |
|
|
312 |
public function hasFlash($name) |
|
|
313 |
{ |
|
|
314 |
if (false === $this->started) { |
|
|
315 |
$this->start(); |
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
return array_key_exists($name, $this->flashes); |
|
|
319 |
} |
|
|
320 |
|
|
|
321 |
/** |
|
|
322 |
* Removes a flash message. |
|
|
323 |
* |
|
|
324 |
* @param string $name |
|
|
325 |
*/ |
|
|
326 |
public function removeFlash($name) |
|
|
327 |
{ |
|
|
328 |
if (false === $this->started) { |
|
|
329 |
$this->start(); |
|
|
330 |
} |
|
|
331 |
|
|
|
332 |
unset($this->flashes[$name]); |
|
|
333 |
} |
|
|
334 |
|
|
|
335 |
/** |
|
|
336 |
* Removes the flash messages. |
|
|
337 |
*/ |
|
|
338 |
public function clearFlashes() |
|
|
339 |
{ |
|
|
340 |
if (false === $this->started) { |
|
|
341 |
$this->start(); |
|
|
342 |
} |
|
|
343 |
|
|
|
344 |
$this->flashes = array(); |
|
|
345 |
$this->oldFlashes = array(); |
|
|
346 |
} |
|
|
347 |
|
|
|
348 |
public function save() |
|
|
349 |
{ |
|
|
350 |
if (false === $this->started) { |
|
|
351 |
$this->start(); |
|
|
352 |
} |
|
|
353 |
|
|
|
354 |
$this->flashes = array_diff_key($this->flashes, $this->oldFlashes); |
|
|
355 |
|
|
|
356 |
$this->storage->write('_symfony2', array( |
|
|
357 |
'attributes' => $this->attributes, |
|
|
358 |
'flashes' => $this->flashes, |
|
|
359 |
'locale' => $this->locale, |
|
|
360 |
)); |
|
|
361 |
$this->saved = true; |
|
|
362 |
} |
|
|
363 |
|
|
|
364 |
public function __destruct() |
|
|
365 |
{ |
|
|
366 |
if (true === $this->started && !$this->saved) { |
|
|
367 |
$this->save(); |
|
|
368 |
} |
|
|
369 |
} |
|
|
370 |
|
|
|
371 |
public function serialize() |
|
|
372 |
{ |
|
|
373 |
return serialize(array($this->storage, $this->defaultLocale)); |
|
|
374 |
} |
|
|
375 |
|
|
|
376 |
public function unserialize($serialized) |
|
|
377 |
{ |
|
|
378 |
list($this->storage, $this->defaultLocale) = unserialize($serialized); |
|
|
379 |
$this->attributes = array(); |
|
|
380 |
$this->started = false; |
|
|
381 |
} |
|
|
382 |
|
|
|
383 |
private function setPhpDefaultLocale($locale) |
|
|
384 |
{ |
|
|
385 |
// if either the class Locale doesn't exist, or an exception is thrown when |
|
|
386 |
// setting the default locale, the intl module is not installed, and |
|
|
387 |
// the call can be ignored: |
|
|
388 |
try { |
|
|
389 |
if (class_exists('Locale', false)) { |
|
|
390 |
\Locale::setDefault($locale); |
|
|
391 |
} |
|
|
392 |
} catch (\Exception $e) { |
|
|
393 |
} |
|
|
394 |
} |
|
|
395 |
} |