1 | <?php |
||
19 | class Session |
||
20 | { |
||
21 | /** |
||
22 | * |
||
23 | * Session key for the "next" flash values. |
||
24 | * |
||
25 | * @const string |
||
26 | * |
||
27 | */ |
||
28 | const FLASH_NEXT = 'Aura\Session\Flash\Next'; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * Session key for the "current" flash values. |
||
33 | * |
||
34 | * @const string |
||
35 | * |
||
36 | */ |
||
37 | const FLASH_NOW = 'Aura\Session\Flash\Now'; |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | * A session segment factory. |
||
42 | * |
||
43 | * @var SegmentFactory |
||
44 | * |
||
45 | */ |
||
46 | protected $segment_factory; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * The CSRF token for this session. |
||
51 | * |
||
52 | * @var CsrfToken |
||
53 | * |
||
54 | */ |
||
55 | protected $csrf_token; |
||
56 | |||
57 | /** |
||
58 | * |
||
59 | * A CSRF token factory, for lazy-creating the CSRF token. |
||
60 | * |
||
61 | * @var CsrfTokenFactory |
||
62 | * |
||
63 | */ |
||
64 | protected $csrf_token_factory; |
||
65 | |||
66 | /** |
||
67 | * |
||
68 | * Incoming cookies from the client, typically a copy of the $_COOKIE |
||
69 | * superglobal. |
||
70 | * |
||
71 | * @var array |
||
72 | * |
||
73 | */ |
||
74 | protected $cookies; |
||
75 | |||
76 | /** |
||
77 | * |
||
78 | * Session cookie parameters. |
||
79 | * |
||
80 | * @var array |
||
81 | * |
||
82 | */ |
||
83 | protected $cookie_params = array(); |
||
84 | |||
85 | /** |
||
86 | * |
||
87 | * An object to intercept PHP function calls; this makes testing easier. |
||
88 | * |
||
89 | * @var Phpfunc |
||
90 | * |
||
91 | */ |
||
92 | protected $phpfunc; |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | * A callable to invoke when deleting the session cookie. The callable |
||
97 | * should have the signature ... |
||
98 | * |
||
99 | * function ($cookie_name, $cookie_params) |
||
100 | * |
||
101 | * ... and return null. |
||
102 | * |
||
103 | * @var callable|null |
||
104 | * |
||
105 | * @see setDeleteCookie() |
||
106 | * |
||
107 | */ |
||
108 | protected $delete_cookie; |
||
109 | |||
110 | /** |
||
111 | * |
||
112 | * Have the flash values been moved forward? |
||
113 | * |
||
114 | * @var bool |
||
115 | * |
||
116 | */ |
||
117 | protected $flash_moved = false; |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * Constructor |
||
122 | * |
||
123 | * @param SegmentFactory $segment_factory A session segment factory. |
||
124 | * |
||
125 | * @param CsrfTokenFactory $csrf_token_factory A CSRF token factory. |
||
126 | * |
||
127 | * @param Phpfunc $phpfunc An object to intercept PHP function calls; |
||
128 | * this makes testing easier. |
||
129 | * |
||
130 | * @param array $cookies Optional: An array of cookies from the client, typically a |
||
131 | * copy of $_COOKIE. Empty array by default. |
||
132 | * |
||
133 | * @param callable|null $delete_cookie Optional: An alternative callable |
||
134 | * to invoke when deleting the session cookie. Defaults to `null`. |
||
135 | * |
||
136 | */ |
||
137 | 33 | public function __construct( |
|
153 | |||
154 | /** |
||
155 | * |
||
156 | * Sets the delete-cookie callable. |
||
157 | * |
||
158 | * If parameter is `null`, the session cookie will be deleted using the |
||
159 | * traditional way, i.e. using an expiration date in the past. |
||
160 | * |
||
161 | * @param callable|null $delete_cookie The callable to invoke when deleting the |
||
162 | * session cookie. |
||
163 | * |
||
164 | */ |
||
165 | 33 | public function setDeleteCookie($delete_cookie) |
|
184 | |||
185 | /** |
||
186 | * |
||
187 | * Gets a new session segment instance by name. Segments with the same |
||
188 | * name will be different objects but will reference the same $_SESSION |
||
189 | * values, so it is possible to have two or more objects that share state. |
||
190 | * For good or bad, this a function of how $_SESSION works. |
||
191 | * |
||
192 | * @param string $name The name of the session segment, typically a |
||
193 | * fully-qualified class name. |
||
194 | * |
||
195 | * @return Segment New Segment instance. |
||
196 | * |
||
197 | */ |
||
198 | 19 | public function getSegment($name) |
|
202 | |||
203 | /** |
||
204 | * |
||
205 | * Is a session available to be resumed? |
||
206 | * |
||
207 | * @return bool |
||
208 | * |
||
209 | */ |
||
210 | 18 | public function isResumable() |
|
215 | |||
216 | /** |
||
217 | * |
||
218 | * Is the session already started? |
||
219 | * |
||
220 | * @return bool |
||
221 | * |
||
222 | */ |
||
223 | 22 | public function isStarted() |
|
239 | |||
240 | /** |
||
241 | * |
||
242 | * Returns the session status. |
||
243 | * |
||
244 | * Nota bene: |
||
245 | * |
||
246 | * PHP 5.3 implementation of session_status() for only active/none. |
||
247 | * Relies on the fact that ini setting 'session.use_trans_sid' cannot be |
||
248 | * changed when a session is active. |
||
249 | * |
||
250 | * PHP ini_set() raises a warning when we attempt to change this setting |
||
251 | * and session is active. Note that the attempted change is to the |
||
252 | * pre-existing value, so nothing will actually change on success. |
||
253 | * |
||
254 | */ |
||
255 | 1 | protected function sessionStatus() |
|
264 | |||
265 | /** |
||
266 | * |
||
267 | * Starts a new or existing session. |
||
268 | * |
||
269 | * @return bool |
||
270 | * |
||
271 | */ |
||
272 | 17 | public function start() |
|
280 | |||
281 | /** |
||
282 | * |
||
283 | * Moves the "next" flash values to the "now" values, thereby clearing the |
||
284 | * "next" values. |
||
285 | * |
||
286 | * @return null |
||
287 | * |
||
288 | */ |
||
289 | 18 | protected function moveFlash() |
|
298 | |||
299 | /** |
||
300 | * |
||
301 | * Resumes a session, but does not start a new one if there is no |
||
302 | * existing one. |
||
303 | * |
||
304 | * @return bool |
||
305 | * |
||
306 | */ |
||
307 | 17 | public function resume() |
|
319 | |||
320 | /** |
||
321 | * |
||
322 | * Clears all session variables across all segments. |
||
323 | * |
||
324 | * @return null |
||
325 | * |
||
326 | */ |
||
327 | 3 | public function clear() |
|
331 | |||
332 | /** |
||
333 | * |
||
334 | * Writes session data from all segments and ends the session. |
||
335 | * |
||
336 | * @return null |
||
337 | * |
||
338 | */ |
||
339 | 2 | public function commit() |
|
343 | |||
344 | /** |
||
345 | * |
||
346 | * Destroys the session entirely. |
||
347 | * |
||
348 | * @return bool |
||
349 | * |
||
350 | * @see http://php.net/manual/en/function.session-destroy.php |
||
351 | * |
||
352 | */ |
||
353 | 2 | public function destroy() |
|
370 | |||
371 | /** |
||
372 | * |
||
373 | * Returns the CSRF token, creating it if needed (and thereby starting a |
||
374 | * session). |
||
375 | * |
||
376 | * @return CsrfToken |
||
377 | * |
||
378 | */ |
||
379 | 5 | public function getCsrfToken() |
|
387 | |||
388 | // ======================================================================= |
||
389 | // |
||
390 | // support and admin methods |
||
391 | // |
||
392 | |||
393 | /** |
||
394 | * |
||
395 | * Sets the session cache expire time. |
||
396 | * |
||
397 | * @param int $expire The expiration time in seconds. |
||
398 | * |
||
399 | * @return int |
||
400 | * |
||
401 | * @see session_cache_expire() |
||
402 | * |
||
403 | */ |
||
404 | 1 | public function setCacheExpire($expire) |
|
408 | |||
409 | /** |
||
410 | * |
||
411 | * Gets the session cache expire time. |
||
412 | * |
||
413 | * @return int The cache expiration time in seconds. |
||
414 | * |
||
415 | * @see session_cache_expire() |
||
416 | * |
||
417 | */ |
||
418 | 1 | public function getCacheExpire() |
|
422 | |||
423 | /** |
||
424 | * |
||
425 | * Sets the session cache limiter value. |
||
426 | * |
||
427 | * @param string $limiter The limiter value. |
||
428 | * |
||
429 | * @return string |
||
430 | * |
||
431 | * @see session_cache_limiter() |
||
432 | * |
||
433 | */ |
||
434 | 1 | public function setCacheLimiter($limiter) |
|
438 | |||
439 | /** |
||
440 | * |
||
441 | * Gets the session cache limiter value. |
||
442 | * |
||
443 | * @return string The limiter value. |
||
444 | * |
||
445 | * @see session_cache_limiter() |
||
446 | * |
||
447 | */ |
||
448 | 1 | public function getCacheLimiter() |
|
452 | |||
453 | /** |
||
454 | * |
||
455 | * Sets the session cookie params. Param array keys are: |
||
456 | * |
||
457 | * - `lifetime` : Lifetime of the session cookie, defined in seconds. |
||
458 | * |
||
459 | * - `path` : Path on the domain where the cookie will work. |
||
460 | * Use a single slash ('/') for all paths on the domain. |
||
461 | * |
||
462 | * - `domain` : Cookie domain, for example 'www.php.net'. |
||
463 | * To make cookies visible on all subdomains then the domain must be |
||
464 | * prefixed with a dot like '.php.net'. |
||
465 | * |
||
466 | * - `secure` : If TRUE cookie will only be sent over secure connections. |
||
467 | * |
||
468 | * - `httponly` : If set to TRUE then PHP will attempt to send the httponly |
||
469 | * flag when setting the session cookie. |
||
470 | * |
||
471 | * @param array $params The array of session cookie param keys and values. |
||
472 | * |
||
473 | * @return null |
||
474 | * |
||
475 | * @see session_set_cookie_params() |
||
476 | * |
||
477 | */ |
||
478 | 1 | public function setCookieParams(array $params) |
|
489 | |||
490 | /** |
||
491 | * |
||
492 | * Gets the session cookie params. |
||
493 | * |
||
494 | * @return array |
||
495 | * |
||
496 | */ |
||
497 | 3 | public function getCookieParams() |
|
501 | |||
502 | /** |
||
503 | * |
||
504 | * Gets the current session id. |
||
505 | * |
||
506 | * @return string |
||
507 | * |
||
508 | */ |
||
509 | 1 | public function getId() |
|
513 | |||
514 | /** |
||
515 | * |
||
516 | * Regenerates and replaces the current session id; also regenerates the |
||
517 | * CSRF token value if one exists. |
||
518 | * |
||
519 | * @return bool True if regeneration worked, false if not. |
||
520 | * |
||
521 | */ |
||
522 | 1 | public function regenerateId() |
|
530 | |||
531 | /** |
||
532 | * |
||
533 | * Sets the current session name. |
||
534 | * |
||
535 | * @param string $name The session name to use. |
||
536 | * |
||
537 | * @return string |
||
538 | * |
||
539 | * @see session_name() |
||
540 | * |
||
541 | */ |
||
542 | 1 | public function setName($name) |
|
546 | |||
547 | /** |
||
548 | * |
||
549 | * Returns the current session name. |
||
550 | * |
||
551 | * @return string |
||
552 | * |
||
553 | */ |
||
554 | 19 | public function getName() |
|
558 | |||
559 | /** |
||
560 | * |
||
561 | * Sets the session save path. |
||
562 | * |
||
563 | * @param string $path The new save path. |
||
564 | * |
||
565 | * @return string |
||
566 | * |
||
567 | * @see session_save_path() |
||
568 | * |
||
569 | */ |
||
570 | 1 | public function setSavePath($path) |
|
574 | |||
575 | /** |
||
576 | * |
||
577 | * Gets the session save path. |
||
578 | * |
||
579 | * @return string |
||
580 | * |
||
581 | * @see session_save_path() |
||
582 | * |
||
583 | */ |
||
584 | 1 | public function getSavePath() |
|
588 | } |
||
589 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: