1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class SessionManager | SessionManager.php |
4
|
|
|
* |
5
|
|
|
* @package Faulancer\Session; |
6
|
|
|
* @author Florian Knapp <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace Faulancer\Session; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SessionManager |
12
|
|
|
*/ |
13
|
|
|
class SessionManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Start session handler |
17
|
|
|
* @return void |
18
|
|
|
* @codeCoverageIgnore |
19
|
|
|
*/ |
20
|
|
|
public function startSession() |
21
|
|
|
{ |
22
|
|
|
if (!$this->hasSession()) { |
23
|
|
|
session_start(); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Destroy session at all |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function destroySession() |
32
|
|
|
{ |
33
|
|
|
if ($this->hasSession()) { |
34
|
|
|
session_destroy(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check if session exists |
40
|
|
|
* @return boolean |
41
|
|
|
*/ |
42
|
|
|
public function hasSession() |
43
|
|
|
{ |
44
|
|
|
if (!empty(session_id())) { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set session key with value |
53
|
|
|
* @param string $key |
54
|
|
|
* @param string|array $value |
55
|
|
|
*/ |
56
|
|
|
public function set(string $key, $value) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$_SESSION[$key] = $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get session value by key |
63
|
|
|
* @param string $key |
64
|
|
|
* @return null|string|array |
65
|
|
|
*/ |
66
|
|
|
public function get(string $key) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if (!isset($_SESSION[$key])) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $_SESSION[$key]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Delete session value by key |
77
|
|
|
* @param string $key |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function delete(string $key) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if (!isset($_SESSION[$key])) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
unset($_SESSION[$key]); |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if flashbag key exists |
93
|
|
|
* @param string $key |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
|
|
public function hasFlashbagKey(string $key) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
return isset($_SESSION['flashbag'][$key]) ? true : false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check if flashbag error key exists |
103
|
|
|
* @param string $key |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
public function hasFlashbagErrorsKey($key) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
return isset($_SESSION['flashbag']['errors'][$key]) ? true : false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set flashbag value by key |
113
|
|
|
* @param string|array $key |
114
|
|
|
* @param null|string|array $value |
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
|
|
public function setFlashbag($key, $value = null) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if (is_array($key)) { |
120
|
|
|
|
121
|
|
|
foreach ($key AS $k => $val) { |
122
|
|
|
$_SESSION['flashbag'][$k] = $val; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$_SESSION['flashbag'][$key] = $value; |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get flashbag value by key |
136
|
|
|
* |
137
|
|
|
* @param string $key |
138
|
|
|
* @return null|string|array |
139
|
|
|
*/ |
140
|
|
|
public function getFlashbag(string $key) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if (!isset($_SESSION['flashbag'][$key])) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$result = $_SESSION['flashbag'][$key]; |
147
|
|
|
|
148
|
|
|
unset($_SESSION['flashbag'][$key]); |
149
|
|
|
|
150
|
|
|
return $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get flashbag error by key |
155
|
|
|
* |
156
|
|
|
* @param string $key |
157
|
|
|
* @return null|string|array |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function getFlashbagError(string $key) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
if (!isset($_SESSION['flashbag']['errors'][$key])) { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$result = $_SESSION['flashbag']['errors'][$key]; |
166
|
|
|
|
167
|
|
|
unset($_SESSION['flashbag']['errors'][$key]); |
168
|
|
|
|
169
|
|
|
return $result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set form data in session flashbag |
174
|
|
|
* @param array $formData |
175
|
|
|
*/ |
176
|
|
|
public function setFlashbagFormData(array $formData) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
if (is_array($formData)) { |
179
|
|
|
$_SESSION['flashbag']['formData'] = $formData; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get form data from session flashbag |
185
|
|
|
* @param string $key |
186
|
|
|
* @return null|array|string |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function getFlashbagFormData(string $key) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
if (!isset($_SESSION['flashbag']['formData'][$key])) { |
191
|
|
|
return null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$result = $_SESSION['flashbag']['formData'][$key]; |
195
|
|
|
|
196
|
|
|
unset($_SESSION['flashbag']['formData'][$key]); |
197
|
|
|
|
198
|
|
|
return $result; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
} |
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: