1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Utils; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Middleware to use php session. |
12
|
|
|
*/ |
13
|
|
|
class PhpSession |
14
|
|
|
{ |
15
|
|
|
use Utils\StorageTrait; |
16
|
|
|
|
17
|
|
|
const STORAGE_KEY = 'PHP_SESSION_STORAGE'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|null |
21
|
|
|
*/ |
22
|
|
|
private $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
private $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. Defines de session name. |
31
|
|
|
* |
32
|
|
|
* @param null|string $name |
33
|
|
|
*/ |
34
|
|
|
public function __construct($name = null) |
35
|
|
|
{ |
36
|
|
|
if ($name !== null) { |
37
|
|
|
$this->name($name); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Configure the session name. |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function name($name) |
49
|
|
|
{ |
50
|
|
|
$this->name = $name; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Configure the session id. |
57
|
|
|
* |
58
|
|
|
* @param string $id |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
|
|
public function id($id) |
63
|
|
|
{ |
64
|
|
|
$this->id = $id; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Execute the middleware. |
71
|
|
|
* |
72
|
|
|
* @param ServerRequestInterface $request |
73
|
|
|
* @param ResponseInterface $response |
74
|
|
|
* @param callable $next |
75
|
|
|
* |
76
|
|
|
* @return ResponseInterface |
77
|
|
|
*/ |
78
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if (session_status() === PHP_SESSION_DISABLED) { |
81
|
|
|
throw new RuntimeException('PHP sessions are disabled'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (session_status() === PHP_SESSION_ACTIVE) { |
85
|
|
|
throw new RuntimeException('Failed to start the session: already started by PHP.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//Session name |
89
|
|
|
$name = $this->name ?: session_name(); |
90
|
|
|
session_name($name); |
91
|
|
|
|
92
|
|
|
//Session id |
93
|
|
|
$id = $this->id; |
94
|
|
|
|
95
|
|
|
if (empty($id)) { |
96
|
|
|
$cookies = $request->getCookieParams(); |
97
|
|
|
|
98
|
|
|
if (!empty($cookies[$name])) { |
99
|
|
|
$id = $cookies[$name]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!empty($id)) { |
104
|
|
|
session_id($id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
session_start(); |
108
|
|
|
|
109
|
|
|
$request = self::startStorage($request, isset($_SESSION[self::STORAGE_KEY]) ? $_SESSION[self::STORAGE_KEY] : []); |
110
|
|
|
$response = $next($request, $response); |
111
|
|
|
|
112
|
|
|
if ((session_status() === PHP_SESSION_ACTIVE) && (session_name() === $name)) { |
113
|
|
|
$_SESSION[self::STORAGE_KEY] = self::stopStorage($request); |
114
|
|
|
session_write_close(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $response; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: