|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DrMVC\Session; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Session |
|
7
|
|
|
* @package DrMVC\Session |
|
8
|
|
|
*/ |
|
9
|
|
|
class Session implements SessionInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Session prefix name |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $_prefix; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Session constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string|null $prefix |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(string $prefix = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->setPrefix($prefix); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getPrefix() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->_prefix; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string|null $prefix |
|
37
|
|
|
* @return SessionInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setPrefix(string $prefix = null): SessionInterface |
|
40
|
|
|
{ |
|
41
|
|
|
$this->_prefix = $prefix; |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
public function isStarted(): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return (session_status() === PHP_SESSION_ACTIVE); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* if session has not started, start sessions |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function init(): string |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->isStarted()) { |
|
61
|
|
|
session_start(); |
|
62
|
|
|
} |
|
63
|
|
|
return $this->id(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add value to a session. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array|string $name name the data to save |
|
70
|
|
|
* @param string|bool $value the data to save |
|
71
|
|
|
* @return SessionInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
public function set($name, $value = false): SessionInterface |
|
74
|
|
|
{ |
|
75
|
|
|
if (\is_array($name) && false === $value) { |
|
76
|
|
|
foreach ($name as $key => $val) { |
|
77
|
|
|
$_SESSION[$this->getPrefix() . $key] = $val; |
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
$_SESSION[$this->getPrefix() . $name] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Extract item from session then delete from the session, |
|
88
|
|
|
* finally return the item. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name item to extract |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
|
|
public function pull(string $name): string |
|
94
|
|
|
{ |
|
95
|
|
|
$key = $this->getPrefix() . $name; |
|
96
|
|
|
|
|
97
|
|
|
if (isset($_SESSION[$key])) { |
|
98
|
|
|
$value = $_SESSION[$key]; |
|
99
|
|
|
unset($_SESSION[$key]); |
|
100
|
|
|
return $value; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get item from session |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $name item to look for in session |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function get(string $name) |
|
113
|
|
|
{ |
|
114
|
|
|
$key = $this->getPrefix() . $name; |
|
115
|
|
|
return $_SESSION[$key] ?? false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return session id |
|
120
|
|
|
* |
|
121
|
|
|
* @return string with the session id. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function id(): string |
|
124
|
|
|
{ |
|
125
|
|
|
return session_id(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Regenerate session_id and return new id |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function regenerate(): string |
|
134
|
|
|
{ |
|
135
|
|
|
session_regenerate_id(true); |
|
136
|
|
|
return $this->id(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Return the session array. |
|
141
|
|
|
* |
|
142
|
|
|
* @return array of session indexes |
|
143
|
|
|
*/ |
|
144
|
|
|
public function display(): array |
|
145
|
|
|
{ |
|
146
|
|
|
return $_SESSION; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Clean all keys stored in session array |
|
151
|
|
|
* |
|
152
|
|
|
* @param string|null $prefix |
|
153
|
|
|
* @return SessionInterface |
|
154
|
|
|
*/ |
|
155
|
|
|
private function cleanUp(string $prefix = null): SessionInterface |
|
156
|
|
|
{ |
|
157
|
|
|
if (null === $prefix) { |
|
158
|
|
|
session_unset(); |
|
159
|
|
|
session_destroy(); |
|
160
|
|
|
} else { |
|
161
|
|
|
foreach ($_SESSION as $key => $value) { |
|
162
|
|
|
if (strpos($key, $prefix) === 0) { |
|
163
|
|
|
unset($_SESSION[$key]); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Remove some single key, remove keys by prefix or destroy session |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $name session name to destroy |
|
175
|
|
|
* @param boolean $byPrefix if set to true clear all sessions for current prefix of session |
|
176
|
|
|
* @return SessionInterface |
|
177
|
|
|
*/ |
|
178
|
|
|
public function destroy($name = '', $byPrefix = false): SessionInterface |
|
179
|
|
|
{ |
|
180
|
|
|
$prefix = $this->getPrefix(); |
|
181
|
|
|
|
|
182
|
|
|
if ('' === $name) { |
|
183
|
|
|
// Clean up if key name is not provided |
|
184
|
|
|
$this->cleanUp($prefix); |
|
185
|
|
|
} else { |
|
186
|
|
|
// Or just remove single key |
|
187
|
|
|
unset($_SESSION[$prefix . $name]); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|