1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
final class Session |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $id; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $contents; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var SessionIdInterface |
19
|
|
|
*/ |
20
|
|
|
private $sessionId; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $oldIds = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $status = \PHP_SESSION_NONE; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $id |
34
|
|
|
* @param array $contents |
35
|
|
|
* @param SessionIdInterface $sessionId |
36
|
|
|
*/ |
37
|
28 |
|
public function __construct(string $id, array $contents, SessionIdInterface $sessionId) |
38
|
|
|
{ |
39
|
28 |
|
$this->id = $id; |
40
|
28 |
|
$this->contents = $contents; |
41
|
28 |
|
$this->sessionId = $sessionId; |
42
|
|
|
|
43
|
28 |
|
if ($this->id !== '') { |
44
|
9 |
|
$this->status = \PHP_SESSION_ACTIVE; |
45
|
|
|
} |
46
|
28 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
21 |
|
public function getId(): string |
52
|
|
|
{ |
53
|
21 |
|
return $this->id; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $contents |
58
|
|
|
*/ |
59
|
4 |
|
public function setContents(array $contents): void |
60
|
|
|
{ |
61
|
4 |
|
$this->contents = $contents; |
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
18 |
|
public function getContents(): array |
68
|
|
|
{ |
69
|
18 |
|
return $this->contents; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
19 |
|
public function getOldIds(): array |
76
|
|
|
{ |
77
|
19 |
|
return $this->oldIds; |
78
|
|
|
} |
79
|
|
|
|
80
|
14 |
|
public function begin(): void |
81
|
|
|
{ |
82
|
14 |
|
if ($this->status === \PHP_SESSION_ACTIVE) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
14 |
|
$this->status = \PHP_SESSION_ACTIVE; |
87
|
|
|
|
88
|
14 |
|
if ($this->id === '') { |
89
|
14 |
|
$this->id = $this->sessionId->generate(); |
90
|
|
|
} |
91
|
14 |
|
} |
92
|
|
|
|
93
|
2 |
|
public function end(): void |
94
|
|
|
{ |
95
|
2 |
|
if ($this->status === \PHP_SESSION_NONE) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$this->oldIds[] = $this->id; |
100
|
2 |
|
$this->status = \PHP_SESSION_NONE; |
101
|
2 |
|
$this->id = ''; |
102
|
2 |
|
$this->contents = []; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
20 |
|
public function isActive(): bool |
109
|
|
|
{ |
110
|
20 |
|
return $this->status === \PHP_SESSION_ACTIVE; |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
public function regenerate(): bool |
114
|
|
|
{ |
115
|
|
|
// Can only regenerate active sessions |
116
|
3 |
|
if ($this->status !== \PHP_SESSION_ACTIVE) { |
117
|
1 |
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
$this->oldIds[] = $this->id; |
121
|
3 |
|
$this->id = $this->sessionId->generate(); |
122
|
|
|
|
123
|
3 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
public function toArray(): array |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
2 |
|
'id' => $this->id, |
130
|
2 |
|
'contents' => $this->contents, |
131
|
2 |
|
'oldIds' => $this->oldIds, |
132
|
2 |
|
'status' => $this->status, |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $session |
138
|
|
|
* @param bool $clone |
139
|
|
|
* @throws \InvalidArgumentException |
140
|
|
|
* @return Session |
141
|
|
|
*/ |
142
|
6 |
|
public function fromArray(array $session, bool $clone = true): self |
143
|
|
|
{ |
144
|
6 |
|
if (!isset($session['id']) || !isset($session['contents']) || !isset($session['oldIds']) || !isset($session['status'])) { |
145
|
4 |
|
throw new \InvalidArgumentException('Session array most contain "id", "contents", "oldIds", and "status".'); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
$self = $this; |
149
|
2 |
|
if ($clone === true) { |
150
|
1 |
|
$self = clone $this; |
151
|
|
|
} |
152
|
2 |
|
$self->id = $session['id']; |
153
|
2 |
|
$self->contents = $session['contents']; |
154
|
2 |
|
$self->oldIds = $session['oldIds']; |
155
|
2 |
|
$self->status = $session['status']; |
156
|
|
|
|
157
|
2 |
|
return $self; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|