|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Session Handler |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (C) 2016 Jake Johns |
|
8
|
|
|
* |
|
9
|
|
|
* This software may be modified and distributed under the terms |
|
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
11
|
|
|
* |
|
12
|
|
|
* @category Trait |
|
13
|
|
|
* @package Vperyod\SessionHandler |
|
14
|
|
|
* @author Jake Johns <[email protected]> |
|
15
|
|
|
* @copyright 2016 Jake Johns |
|
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
17
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Vperyod\SessionHandler; |
|
21
|
|
|
|
|
22
|
|
|
use Aura\Session; |
|
23
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Session aware trait |
|
28
|
|
|
* |
|
29
|
|
|
* Trait for objects which need to know where the session attribute is stored in |
|
30
|
|
|
* the request and some helpers to interact with it. |
|
31
|
|
|
* |
|
32
|
|
|
* @category Trait |
|
33
|
|
|
* @package Vperyod\SessionHandler |
|
34
|
|
|
* @author Jake Johns <[email protected]> |
|
35
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
36
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
|
37
|
|
|
*/ |
|
38
|
|
|
trait SessionAwareTrait |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Get session from request |
|
42
|
|
|
* |
|
43
|
|
|
* @param Request $request PSR7 Request |
|
44
|
|
|
* |
|
45
|
|
|
* @return Session\Session |
|
46
|
|
|
* @throws Exception if session attribute is invalid |
|
47
|
|
|
* |
|
48
|
|
|
* @access protected |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getSession(Request $request) : Session\Session |
|
51
|
|
|
{ |
|
52
|
|
|
$session = $request->getAttribute(SessionHandler::SESSION_ATTRIBUTE); |
|
53
|
|
|
|
|
54
|
|
|
if (! $session instanceof Session\Session) { |
|
55
|
|
|
throw new \Exception( |
|
56
|
|
|
'Session not available in request at: ' |
|
57
|
|
|
. SessionHandler::SESSION_ATTRIBUTE |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
return $session; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* GetSessionSegment |
|
65
|
|
|
* |
|
66
|
|
|
* @param Request $request incoming request |
|
67
|
|
|
* @param string $name segment name |
|
68
|
|
|
* |
|
69
|
|
|
* @return Session\SegmentInterface |
|
70
|
|
|
* |
|
71
|
|
|
* @access protected |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getSessionSegment( |
|
74
|
|
|
Request $request, |
|
75
|
|
|
string $name |
|
76
|
|
|
) : Session\SegmentInterface { |
|
77
|
|
|
$session = $this->getSession($request); |
|
78
|
|
|
return $session->getSegment($name); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|