-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSession.php
More file actions
196 lines (163 loc) · 4.86 KB
/
Copy pathSession.php
File metadata and controls
196 lines (163 loc) · 4.86 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace duncan3dc\Sessions;
use duncan3dc\Sessions\Exceptions\InvalidNameException;
use function strlen;
/**
* A static interface for SessionInstance.
*/
final class Session
{
/**
* @var string $name The name of the session.
*/
private static $name = "";
/**
* @var SessionInterface|null $session The underlying session instance.
*/
private static $session;
/**
* Set the name of the session to use.
*
* @param string $name The name of the session
*/
public static function name(string $name): void
{
self::$name = $name;
self::$session = null;
}
/**
* Inject the session instance to use.
*
* @param SessionInterface $session The instance to use
*
* @return void
*/
public static function setInstance(SessionInterface $session): void
{
self::$session = $session;
}
/**
* Ensure the session instance has been created.
*
* @return SessionInterface
*/
public static function getInstance(): SessionInterface
{
if (self::$session instanceof SessionInterface) {
return self::$session;
}
if (strlen(self::$name) < 1) {
throw new InvalidNameException("Cannot start session, no name has been specified, you must call Session::name() before using this class");
}
self::$session = new SessionInstance(self::$name);
return self::$session;
}
/**
* Create a new namespaced section of this session to avoid clashes.
*
* @param string $name The namespace of the session
*
* @return SessionInterface
*/
public static function createNamespace(string $name): SessionInterface
{
return self::getInstance()->createNamespace($name);
}
/**
* Get a value from the session data cache.
*
* @param string $key The name of the name to retrieve
*
* @return mixed
*/
public static function get(string $key)
{
return self::getInstance()->get($key);
}
/**
* Get all the current session data.
*
* @return array<string, mixed>
*/
public static function getAll(): array
{
return self::getInstance()->getAll();
}
/**
* Set a value within session data.
*
* @param string|array<string, mixed> $data Either the name of the session key to update, or an array of keys to update
* @param mixed $value If $data is a string then store this value in the session data
*
* @return void
*/
public static function set(string|array $data, $value = null): void
{
self::getInstance()->set($data, $value);
}
/**
* This is a convenience method to prevent having to do several checks/set for all persistant variables.
*
* If the key name has been passed via POST then that value is stored in the session and returned.
* If the key name has been passed via GET then that value is stored in the session and returned.
* If there is already a value in the session data then that is returned.
* If all else fails then the default value is returned.
* All checks are truthy/falsy (so a POST value of "0" is ignored), unless the 3rd parameter is set to true.
*
* @param string $key The name of the key to retrieve from session data
* @param mixed $default The value to use if the current session value is falsy
* @param bool $strict Whether to do strict comparisons or not
*
* @return mixed
*/
public static function getSet(string $key, $default = null, bool $strict = false)
{
return self::getInstance()->getSet($key, $default, $strict);
}
/**
* Unset a value within session data.
*
* @param string ...$keys The keys to delete from the session
*/
public static function delete(string ...$keys): void
{
self::getInstance()->delete(...$keys);
}
/**
* Clear all previously set values.
*/
public static function clear(): void
{
self::getInstance()->clear();
}
/**
* Retrieve a one-time value from the session data.
*
* @param string $key The name of the flash value to retrieve
*
* @return mixed
*/
public static function getFlash(string $key)
{
return self::getInstance()->getFlash($key);
}
/**
* Set a one-time value within session data.
*
* @param string $key The name of the flash value to update
* @param mixed $value The value to store against the key
*
* @return void
*/
public static function setFlash(string $key, $value): void
{
self::getInstance()->setFlash($key, $value);
}
/**
* Tear down the session and wipe all it's data.
*/
public static function destroy(): void
{
self::getInstance()->destroy();
}
}