|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\StateAgent; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\BlackBox\StateAgentInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Smith implements StateAgentInterface |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
private static ?StateAgentInterface $instance = null; |
|
11
|
|
|
|
|
12
|
|
|
// IS-54-16 : Behold, I have created the smith who blows the fire of coals |
|
13
|
|
|
// $options : https://www.php.net/manual/fr/session.configuration.php |
|
14
|
|
|
public static function getInstance(array $options = []): StateAgentInterface |
|
15
|
|
|
{ |
|
16
|
|
|
if (is_null(self::$instance)) { |
|
17
|
|
|
self::$instance = new Smith($options); |
|
18
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
return self::$instance; |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function start(): string |
|
25
|
|
|
{ |
|
26
|
|
|
if (self::hasNoSession()) { |
|
27
|
|
|
session_start(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return session_name(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function set(string $key, $value): void |
|
34
|
|
|
{ |
|
35
|
|
|
$_SESSION[$key] = $value; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function has(string $key): bool |
|
39
|
|
|
{ |
|
40
|
|
|
return isset($_SESSION[$key]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function get(string $key) |
|
44
|
|
|
{ |
|
45
|
|
|
return $_SESSION[$key] ?? null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function __construct(array $options = []) |
|
49
|
|
|
{ |
|
50
|
|
|
if (self::sessionsAreDisabled()) { |
|
51
|
|
|
throw new \UnexpectedValueException(__CLASS__ . '::PHP_SESSION_DISABLED'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (self::hasNoSession()) { |
|
55
|
|
|
session_name($options['session_name'] ?? StateAgentInterface::DEFAULT_SESSION_NAME); |
|
56
|
|
|
unset($options['session_name']); |
|
57
|
|
|
session_start($options); // https://www.php.net/manual/fr/function.session-start.php |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// camelCase wrapper for setcookie, coherent with getCookie |
|
63
|
|
|
public function setCookie( |
|
64
|
|
|
string $name, |
|
65
|
|
|
string $value = "", |
|
66
|
|
|
int $expires_in = 365 * 24 * 60 * 60, |
|
67
|
|
|
string $path = "/", |
|
68
|
|
|
string $domain = "", |
|
69
|
|
|
bool $secure = false, |
|
70
|
|
|
bool $httponly = false |
|
71
|
|
|
): bool { |
|
72
|
|
|
return setcookie($name, $value, time() + $expires_in, $path, $domain, $secure, $httponly); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// returns the value stored or null |
|
76
|
|
|
public function getCookie(string $name): ?string |
|
77
|
|
|
{ |
|
78
|
|
|
return $_COOKIE[$name] ?? null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// IS-54-16 : I have also created the ravager to destroy |
|
82
|
|
|
public function destroy(): bool |
|
83
|
|
|
{ |
|
84
|
|
|
|
|
85
|
|
|
if (ini_get("session.use_cookies")) { |
|
86
|
|
|
$params = session_get_cookie_params(); |
|
87
|
|
|
setcookie( |
|
88
|
|
|
session_name(), |
|
89
|
|
|
'', |
|
90
|
|
|
time() - 42000, |
|
91
|
|
|
$params["path"], |
|
92
|
|
|
$params["domain"], |
|
93
|
|
|
$params["secure"], |
|
94
|
|
|
$params["httponly"] |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return session_destroy(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
private static function sessionsAreDisabled(): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return session_status() === PHP_SESSION_DISABLED; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private static function hasNoSession(): bool |
|
108
|
|
|
{ |
|
109
|
|
|
return session_status() === PHP_SESSION_NONE; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|