1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\Session; |
6
|
|
|
|
7
|
|
|
use Jasny\Session\Flash\FlashBag; |
8
|
|
|
use Jasny\Session\Flash\FlashTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Wrapper round $_SESSION. |
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
class GlobalSession implements SessionInterface |
14
|
|
|
{ |
15
|
|
|
use FlashTrait; |
16
|
|
|
|
17
|
|
|
protected array $options; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Session constructor. |
21
|
|
|
* |
22
|
|
|
* @param array<string,mixed> $options Passed to session_start() |
|
|
|
|
23
|
|
|
* @param FlashBag $flashes |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct(array $options = [], ?FlashBag $flashes = null) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->options = $options; |
28
|
|
|
$this->flashes = $flashes ?? new FlashBag(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Start the session. |
34
|
|
|
* @see session_start() |
|
|
|
|
35
|
|
|
*/ |
|
|
|
|
36
|
|
|
public function start(): void |
37
|
|
|
{ |
38
|
|
|
session_start($this->options); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Write session data and end session. |
43
|
|
|
* @see session_write_close() |
|
|
|
|
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
public function stop(): void |
46
|
|
|
{ |
47
|
|
|
session_write_close(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Discard session array changes and finish session. |
52
|
|
|
* @see session_abort() |
|
|
|
|
53
|
|
|
* |
54
|
|
|
* Only a shallow clone is done to save the original data. If the session data contains objects, make sure that |
55
|
|
|
* `__clone()` is overwritten, so it does a deep clone. |
56
|
|
|
*/ |
|
|
|
|
57
|
|
|
public function abort(): void |
58
|
|
|
{ |
59
|
|
|
session_abort(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the sessions status. |
65
|
|
|
* @see session_status() |
|
|
|
|
66
|
|
|
*/ |
|
|
|
|
67
|
|
|
public function status(): int |
68
|
|
|
{ |
69
|
|
|
return session_status(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Clear all data from the session. |
75
|
|
|
*/ |
|
|
|
|
76
|
|
|
public function clear(): void |
77
|
|
|
{ |
78
|
|
|
$this->assertStarted(); |
79
|
|
|
|
80
|
|
|
$_SESSION = []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* @param string $offset |
|
|
|
|
85
|
|
|
* @return mixed |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
public function offsetExists($offset) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->assertStarted(); |
90
|
|
|
|
91
|
|
|
return array_key_exists($offset, $_SESSION); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @param string $offset |
|
|
|
|
96
|
|
|
* @return mixed |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
public function offsetGet($offset) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$this->assertStarted(); |
101
|
|
|
|
102
|
|
|
return $_SESSION[$offset]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
|
|
|
|
106
|
|
|
* @param string $offset |
|
|
|
|
107
|
|
|
* @param mixed $value |
|
|
|
|
108
|
|
|
*/ |
|
|
|
|
109
|
|
|
public function offsetSet($offset, $value) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->assertStarted(); |
112
|
|
|
|
113
|
|
|
$_SESSION[$offset] = $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
|
|
|
|
117
|
|
|
* @param string $offset |
|
|
|
|
118
|
|
|
*/ |
|
|
|
|
119
|
|
|
public function offsetUnset($offset) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this->assertStarted(); |
122
|
|
|
|
123
|
|
|
unset($_SESSION[$offset]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Assert that there is an active session. |
128
|
|
|
* |
129
|
|
|
* @throws \RuntimeException |
130
|
|
|
*/ |
|
|
|
|
131
|
|
|
protected function assertStarted() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
if (session_status() !== \PHP_SESSION_ACTIVE) { |
134
|
|
|
throw new \RuntimeException("Session not started"); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|