1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use const PHP_SESSION_NONE; |
6
|
|
|
use const PHP_SESSION_ACTIVE; |
7
|
|
|
|
8
|
|
|
final class Session |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $id = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
10 |
|
* @var array |
17
|
|
|
*/ |
18
|
10 |
|
private $contents = []; |
19
|
10 |
|
|
20
|
|
|
/** |
21
|
|
|
* @var SessionIdInterface |
22
|
|
|
*/ |
23
|
|
|
private $sessionId = []; |
24
|
2 |
|
|
25
|
|
|
/** |
26
|
2 |
|
* @var PHP_SESSION_NONE|PHP_SESSION_ACTIVE |
27
|
2 |
|
*/ |
28
|
|
|
private $status = PHP_SESSION_NONE; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $id |
32
|
10 |
|
* @param array $contents |
33
|
|
|
* @param SessionIdInterface $sessionId |
34
|
10 |
|
*/ |
35
|
|
|
public function __construct(string $id, array $contents, SessionIdInterface $sessionId) |
36
|
|
|
{ |
37
|
|
|
$this->id = $id; |
38
|
|
|
$this->contents = $contents; |
39
|
|
|
$this->sessionId = $sessionId; |
40
|
|
|
|
41
|
|
|
if ($this->id !== '') { |
42
|
|
|
$this->status = PHP_SESSION_ACTIVE; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getId(): string |
50
|
|
|
{ |
51
|
|
|
return $this->id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $contents |
56
|
|
|
*/ |
57
|
|
|
public function setContents(array $contents) |
58
|
|
|
{ |
59
|
|
|
$this->contents = $contents; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getContents(): array |
66
|
|
|
{ |
67
|
|
|
return $this->contents; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function begin() |
71
|
|
|
{ |
72
|
|
|
if ($this->status === PHP_SESSION_ACTIVE) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->status = PHP_SESSION_ACTIVE; |
|
|
|
|
77
|
|
|
|
78
|
|
|
if ($this->id === '') { |
79
|
|
|
$this->id = $this->sessionId->generate(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function end() |
84
|
|
|
{ |
85
|
|
|
if ($this->status === PHP_SESSION_NONE) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->status = PHP_SESSION_NONE; |
|
|
|
|
90
|
|
|
$this->id = ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function isActive(): bool |
97
|
|
|
{ |
98
|
|
|
return $this->status === PHP_SESSION_ACTIVE; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function regenerate() |
102
|
|
|
{ |
103
|
|
|
// Can only regenerate active sessions |
104
|
|
|
if ($this->status !== PHP_SESSION_ACTIVE) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->id = $this->sessionId->generate(); |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..