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