-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathSessionStorage.php
61 lines (43 loc) · 1.08 KB
/
SessionStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace Milo\Github\Storages;
use Milo\Github;
/**
* Session storage which uses $_SESSION directly. Session must be started already before use.
*
* @author Miloslav Hůla (https://github.com/milo)
*/
class SessionStorage implements ISessionStorage
{
use Github\Strict;
public const SESSION_KEY = 'milo.github-api';
public function __construct(
private string $sessionKey = self::SESSION_KEY
) {}
public function set(string $name, mixed $value): static
{
if ($value === null) {
return $this->remove($name);
}
$this->check(__METHOD__);
$_SESSION[$this->sessionKey][$name] = $value;
return $this;
}
public function get(string $name): mixed
{
$this->check(__METHOD__);
return $_SESSION[$this->sessionKey][$name] ?? null;
}
public function remove(string $name): static
{
$this->check(__METHOD__);
unset($_SESSION[$this->sessionKey][$name]);
return $this;
}
private function check(string $method): void
{
if (!isset($_SESSION)) {
trigger_error("Start session before using $method().", E_USER_WARNING);
}
}
}