-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSessionNamespace.php
More file actions
148 lines (118 loc) · 3.08 KB
/
Copy pathSessionNamespace.php
File metadata and controls
148 lines (118 loc) · 3.08 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace duncan3dc\Sessions;
/**
* A namespaced portion of the session data.
*/
final class SessionNamespace implements SessionInterface
{
use SessionTrait;
private string $name;
private SessionInterface $session;
/**
* Create a new namespaced portion of a session.
*
* @param string $name The namespace of the session
* @param SessionInterface $session The session instance to use
*/
public function __construct(string $name, SessionInterface $session)
{
$this->name = $name;
$this->session = $session;
}
/**
* Get the namespace prefix for keys.
*
* @return string
*/
private function getNamespace(): string
{
return "_ns_{$this->name}_";
}
/**
* Converts the passed session key into a namespaced key.
*
* @param string $key The key to convert
*
* @return string
*/
private function getNamespacedKey(string $key): string
{
return $this->getNamespace() . $key;
}
/**
* Create a new namespaced section of this session to avoid clashes.
*
* @param string $name The namespace of the session
*
* @return SessionInterface
*/
public function createNamespace(string $name): SessionInterface
{
$name = $this->getNamespacedKey($name);
return new SessionNamespace($name, $this->session);
}
/**
* Get a value from the session data cache.
*
* @param string $key The name of the name to retrieve
*
* @return mixed
*/
public function get(string $key)
{
$key = $this->getNamespacedKey($key);
return $this->session->get($key);
}
/**
* Get all the current session data.
*/
public function getAll(): array
{
$namespace = $this->getNamespace();
$length = mb_strlen($namespace);
$values = [];
$data = $this->session->getAll();
foreach ($data as $key => $val) {
if (mb_substr($key, 0, $length) === $namespace) {
$key = mb_substr($key, $length);
$values[$key] = $val;
}
}
return $values;
}
/**
* Set a value within session data.
*/
public function set(string|array $data, $value = null): SessionInterface
{
if (is_array($data)) {
$newData = [];
foreach ($data as $key => $val) {
$key = $this->getNamespacedKey($key);
$newData[$key] = $val;
}
$data = $newData;
} else {
$data = $this->getNamespacedKey($data);
}
$this->session->set($data, $value);
return $this;
}
/**
* Tear down the session and wipe all it's data.
*
* @return SessionInterface
*/
public function clear(): SessionInterface
{
$values = $this->getAll();
if (count($values) > 0) {
$this->delete(...array_keys($values));
}
return $this;
}
public function destroy(): void
{
$this->clear();
}
}