|
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 Covered by php |
|
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 flash message key exists |
|
93
|
|
|
* @param string $key |
|
94
|
|
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
|
|
public function hasFlashMessage(string $key) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
return isset($_SESSION['flashMessage'][$key]) ? true : false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set flashbag value by key |
|
103
|
|
|
* @param string|array $key |
|
104
|
|
|
* @param null|string|array $value |
|
105
|
|
|
* @return boolean |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setFlashMessage($key, $value = null) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
if (is_array($key)) { |
|
110
|
|
|
|
|
111
|
|
|
foreach ($key AS $k => $val) { |
|
112
|
|
|
$_SESSION['flashMessage'][$k] = $val; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$_SESSION['flashMessage'][$key] = $value; |
|
120
|
|
|
|
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get flash message value by key |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $key |
|
128
|
|
|
* @return null|string|array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getFlashMessage(string $key) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
if (!isset($_SESSION['flashMessage'][$key])) { |
|
133
|
|
|
return null; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$result = $_SESSION['flashMessage'][$key]; |
|
137
|
|
|
|
|
138
|
|
|
unset($_SESSION['flashMessage'][$key]); |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
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: