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 Middleware |
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
|
|
|
* SessionHandler |
28
|
|
|
* |
29
|
|
|
* @category Middleware |
30
|
|
|
* @package Vperyod\SessionHandler |
31
|
|
|
* @author Jake Johns <[email protected]> |
32
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
33
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
34
|
|
|
*/ |
35
|
|
|
class SessionHandler |
36
|
|
|
{ |
37
|
|
|
const SESSION_ATTRIBUTE = Session\Session::class; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Session factory |
41
|
|
|
* |
42
|
|
|
* @var Session\SessionFactory |
43
|
|
|
* |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected $sessionFactory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a session handler |
50
|
|
|
* |
51
|
|
|
* @param SessionFactory $sessionFactory Session factory |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Session\SessionFactory $sessionFactory = null) |
56
|
1 |
|
{ |
57
|
|
|
$this->sessionFactory = $sessionFactory ?: new Session\SessionFactory(); |
58
|
1 |
|
} |
59
|
1 |
|
|
60
|
|
|
/** |
61
|
|
|
* Create Session stores it on the specified request attribute |
62
|
|
|
* |
63
|
|
|
* @param Request $request PSR7 Request |
64
|
|
|
* @param Response $response PSR7 Response |
65
|
|
|
* @param callable $next Next callable middleware |
66
|
|
|
* |
67
|
|
|
* @return Response |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
*/ |
71
|
|
|
public function __invoke(Request $request, Response $response, callable $next) |
72
|
1 |
|
{ |
73
|
|
|
$session = $this->newSession($request); |
74
|
1 |
|
$request = $request->withAttribute(self::SESSION_ATTRIBUTE, $session); |
75
|
1 |
|
return $next($request, $response); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
/** |
79
|
|
|
* Create new session |
80
|
|
|
* |
81
|
|
|
* @param Request $request PSR7 Request |
82
|
|
|
* |
83
|
|
|
* @return Session\Session |
84
|
|
|
* |
85
|
|
|
* @access protected |
86
|
|
|
*/ |
87
|
|
|
protected function newSession(Request $request) |
88
|
|
|
{ |
89
|
|
|
$factory = $this->sessionFactory; |
90
|
|
|
$cookie = $request->getCookieParams(); |
91
|
|
|
return $factory->newInstance($cookie); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|