1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alkemann\h2l; |
4
|
|
|
|
5
|
|
|
use alkemann\h2l\util\ArrayManipulations; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Session implementation that access $_SESSION directly |
9
|
|
|
* |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
* @package alkemann\h2l |
12
|
|
|
*/ |
13
|
|
|
class Session implements interfaces\Session |
14
|
|
|
{ |
15
|
|
|
public function startIfNotStarted(): void |
16
|
|
|
{ |
17
|
|
|
if ($this->active() === false) { |
18
|
|
|
session_start(); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns the value stored in `$key`, may return null if no value is set |
24
|
|
|
* |
25
|
|
|
* `$key` may be a 'dot.notation.string' to access ['dot']['notation']['string'] |
26
|
|
|
* |
27
|
|
|
* @param string $key |
28
|
|
|
* @return mixed|null |
29
|
|
|
*/ |
30
|
|
|
public function get(string $key) |
31
|
|
|
{ |
32
|
|
|
$this->startIfNotStarted(); |
33
|
|
|
if (isset($_SESSION[$key])) { |
34
|
|
|
return $_SESSION[$key]; |
35
|
|
|
} |
36
|
|
|
if (strpos($key, '.') !== false && $_SESSION != null) { |
37
|
|
|
return ArrayManipulations::getFromArrayByKey($key, $_SESSION); |
38
|
|
|
} |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set `$value` into the session array |
44
|
|
|
* |
45
|
|
|
* `$key` may be a 'dot.notation.string' to access ['dot']['notation']['string'] |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* @param mixed $value |
49
|
|
|
*/ |
50
|
|
|
public function set(string $key, $value): void |
51
|
|
|
{ |
52
|
|
|
$this->startIfNotStarted(); |
53
|
|
|
if (strpos($key, '.') === false) { |
54
|
|
|
$_SESSION[$key] = $value; |
55
|
|
|
} else { |
56
|
|
|
/** @psalm-suppress NullReference */ |
57
|
|
|
ArrayManipulations::setToArrayByKey($key, $value, $_SESSION); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Remove value at `$key` from session array |
63
|
|
|
* |
64
|
|
|
* @param string $key |
65
|
|
|
* @throws \InvalidArgumentException `$key` does not support "dot" notation |
66
|
|
|
*/ |
67
|
|
|
public function unset(string $key): void |
68
|
|
|
{ |
69
|
|
|
if (strpos($key, '.') !== false) { |
70
|
|
|
throw new \InvalidArgumentException(__METHOD__ . ' does not support "dot" notation'); |
71
|
|
|
} |
72
|
|
|
unset($_SESSION[$key]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns `true` if the PHP session is active |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
private function active(): bool |
81
|
|
|
{ |
82
|
|
|
return session_status() === \PHP_SESSION_ACTIVE; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Unsets and destroys any active session, returns `false` if no active session |
87
|
|
|
* |
88
|
|
|
* also returns `false` if session was not successfully destroyed. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function destroy(): bool |
93
|
|
|
{ |
94
|
|
|
session_destroy(); |
95
|
|
|
if ($this->active()) { |
96
|
|
|
session_destroy(); |
97
|
|
|
unset($_SESSION); |
98
|
|
|
|
99
|
|
|
return $this->active() === false; |
100
|
|
|
} |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check if `$key` is set in session on root of the session array |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* @return bool |
109
|
|
|
* @throws \InvalidArgumentException `$key` does not support "dot" notation |
110
|
|
|
*/ |
111
|
|
|
public function check(string $key): bool |
112
|
|
|
{ |
113
|
|
|
if (strpos($key, '.') !== false) { |
114
|
|
|
throw new \InvalidArgumentException(__METHOD__ . ' does not support "dot" notation'); |
115
|
|
|
} |
116
|
|
|
return isset($_SESSION[$key]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|