|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Slick\Http\Session\Driver; |
|
6
|
|
|
|
|
7
|
|
|
use Slick\Http\Session\SessionDriverInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractDriver |
|
11
|
|
|
* @package Slick\Http\Session\Driver |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractDriver implements SessionDriverInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var null|string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected ?string $domain = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
protected int $lifetime = 0; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected string $name = 'ID'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected string $prefix = 'slick_'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates a Session Driver |
|
37
|
|
|
* |
|
38
|
|
|
* @param array<string, mixed> $options |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(array $options = []) |
|
41
|
|
|
{ |
|
42
|
|
|
foreach ($options as $option => $value) { |
|
43
|
|
|
if (property_exists(__CLASS__, $option)) { |
|
44
|
|
|
$this->$option = $value; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the value store with provided key or the default value. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $key The key used to store the value in session. |
|
53
|
|
|
* @param string $default The default value if no value was stored. |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed The stored value or the default value if key |
|
56
|
|
|
* was not found. |
|
57
|
10 |
|
*/ |
|
58
|
|
|
public function get($key, $default = null): mixed |
|
59
|
10 |
|
{ |
|
60
|
10 |
|
$sessionKey = $this->prefix . $key; |
|
61
|
10 |
|
if (\array_key_exists($sessionKey, $this->fetchSession())) { |
|
62
|
|
|
$default = $this->fetchSession()[$sessionKey]; |
|
63
|
|
|
} |
|
64
|
|
|
return $default; |
|
65
|
|
|
} |
|
66
|
4 |
|
|
|
67
|
|
|
/** |
|
68
|
4 |
|
* Set/Stores a provided values with a given key. |
|
69
|
4 |
|
* |
|
70
|
2 |
|
* @param string $key The key used to store the value in session. |
|
71
|
2 |
|
* @param mixed $value The value to store under the provided key. |
|
72
|
4 |
|
* |
|
73
|
|
|
* @return self Self instance for |
|
74
|
|
|
* method call chains. |
|
75
|
|
|
* @SuppressWarnings(PHPMD) |
|
76
|
|
|
*/ |
|
77
|
|
|
public function set($key, $value): self |
|
78
|
2 |
|
{ |
|
79
|
|
|
$sessionKey = $this->prefix . $key; |
|
80
|
2 |
|
$_SESSION[$sessionKey] = $value; |
|
81
|
2 |
|
return $this; |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Erases the values stored with the given key. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $key The key used to store the value in session. |
|
88
|
2 |
|
* |
|
89
|
|
|
* @return self Self-instance for |
|
90
|
2 |
|
* method call chains. |
|
91
|
2 |
|
* @SuppressWarnings(PHPMD) |
|
92
|
2 |
|
*/ |
|
93
|
|
|
public function erase($key): self |
|
94
|
|
|
{ |
|
95
|
|
|
$sessionKey = $this->prefix . $key; |
|
96
|
|
|
unset($_SESSION[$sessionKey]); |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array<string, mixed> |
|
102
|
|
|
* @SuppressWarnings(PHPMD) |
|
103
|
|
|
*/ |
|
104
|
|
|
private function fetchSession(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return $_SESSION; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|