-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
157 lines (141 loc) · 3.53 KB
/
Session.php
File metadata and controls
157 lines (141 loc) · 3.53 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
<?php
namespace Colibri\Session;
use Colibri\Pattern\Helper;
use Colibri\Session\Storage\StorageInterface;
use Colibri\Util\Arr;
/**
* Class Session.
*/
class Session extends Helper
{
/**
* @var array flashed between http calls variables
*/
private static $flashedVars = [];
/**
* @var StorageInterface storage driver
*/
private static $storage = null;
/**
* @var string|StorageInterface class name (Native::class, ...)
*/
private static $driver = Storage\Native::class;
/**
* Starts the Session.
*/
public static function start()
{
$driver = self::getDriver();
self::$storage = $driver::getInstance();
self::$flashedVars = self::$storage->remove('flashed');
}
/**
* Returns session ID.
*/
public static function id(): string
{
return self::$storage->id();
}
/**
* Retrieve the value from session.
*
* @param string $dottedKey
* @param mixed $default
*
* @return mixed
*/
public static function get($dottedKey, $default = null)
{
if (self::$flashedVars) {
$flashedValue = Arr::get(self::$flashedVars, $dottedKey, '~no~flashed~value~');
if ($flashedValue !== '~no~flashed~value~') {
return $flashedValue;
}
}
return self::$storage->get($dottedKey, $default);
}
/**
* Store the value into session.
*
* @param string $dottedKey
* @param mixed $value
*
* @return mixed
*/
public static function set($dottedKey, $value)
{
return self::$storage->set($dottedKey, $value);
}
/**
* Removes the value from session by specified key.
*
* @param $dottedKey
*
* @return mixed|null returns removed value or null if key not found
*/
public static function remove($dottedKey)
{
return self::$storage->remove($dottedKey);
}
/**
* Flashes some value to next http call.
*
* @param string $key
* @param mixed $value
*/
public static function flash($key, $value)
{
static::set('flashed.' . $key, $value);
}
/**
* Flashes array of values to next http call.
*
* @param array $keyValues
*/
public static function flashValues(array $keyValues)
{
static::set('flashed', $keyValues);
}
/**
* Closes current session and try to find and open new with <$sessionId>.
*
* @param string $id
* @param bool $saveCurrent
*
* @throws \Colibri\Session\Exception
*/
public static function catch($id, $saveCurrent = true)
{
self::$storage->catch($id, $saveCurrent);
}
/**
* @return string|StorageInterface
*/
public static function getDriver(): string
{
return self::$driver;
}
/**
* @param string $driver name of class that implements \Colibri\Session\Storage\StorageInterface
*
* @throws \InvalidArgumentException
*/
public static function setDriver(string $driver)
{
if ( ! self::isValidDriver($driver)) {
throw new \InvalidArgumentException("invalid session driver '$driver'");
}
self::$driver = $driver;
}
/**
* @param string $driver
*
* @return bool
*/
private static function isValidDriver(string $driver): bool
{
return
class_exists($driver) &&
Arr::contains(class_implements($driver), StorageInterface::class);
}
}