|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the miBadger package. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Michael Webbers <[email protected]> |
|
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
|
8
|
|
|
* @version 1.0.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace miBadger\Http; |
|
12
|
|
|
|
|
13
|
|
|
use miBadger\Singleton\SingletonTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The session class |
|
17
|
|
|
* |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Session implements \IteratorAggregate |
|
21
|
|
|
{ |
|
22
|
|
|
use SingletonTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array The session. */ |
|
25
|
|
|
private $session; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Construct a Sesssion object. |
|
29
|
|
|
* |
|
30
|
|
|
* @global array $_SESSION The session parameters. |
|
31
|
|
|
*/ |
|
32
|
13 |
|
protected function __construct() |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
13 |
|
$this->session = &$_SESSION; |
|
35
|
13 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize the session, if none exists. |
|
39
|
|
|
* |
|
40
|
|
|
* @param $name = null |
|
41
|
|
|
* @return null |
|
42
|
|
|
* @throws \RuntimeException on failure. |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public static function start($name = null) |
|
45
|
|
|
{ |
|
46
|
4 |
|
if ($name !== null) { |
|
47
|
2 |
|
session_name($name); |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
if (session_status() !== PHP_SESSION_NONE || !session_start()) { |
|
51
|
1 |
|
throw new \RuntimeException('Can\'t start session.'); |
|
52
|
|
|
} |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Destroy the session, if one exists. |
|
57
|
|
|
* |
|
58
|
|
|
* @return null |
|
59
|
|
|
* @throws \RuntimeException on failure. |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public static function destroy() |
|
62
|
|
|
{ |
|
63
|
2 |
|
if (session_status() !== PHP_SESSION_ACTIVE || !session_destroy()) { |
|
64
|
1 |
|
throw new \RuntimeException('Can\'t destroy session.'); |
|
65
|
|
|
} |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function getIterator() |
|
72
|
|
|
{ |
|
73
|
1 |
|
return new \ArrayIterator(static::getInstance()->session); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the number of key-value mappings in the session map. |
|
78
|
|
|
* |
|
79
|
|
|
* @return int the number of key-value mappings in the session map. |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public static function count() |
|
82
|
|
|
{ |
|
83
|
1 |
|
return count(static::getInstance()->session); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns true if the session map contains no key-value mappings. |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool true if the session map contains no key-value mappings. |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public static function isEmpty() |
|
92
|
|
|
{ |
|
93
|
2 |
|
return empty(static::getInstance()->session); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns true if the session map contains a mapping for the specified key. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* @return bool true if the session map contains a mapping for the specified key. |
|
101
|
|
|
*/ |
|
102
|
4 |
|
public static function containsKey($key) |
|
103
|
|
|
{ |
|
104
|
4 |
|
return isset(static::getInstance()->session[$key]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns true if the session map maps one or more keys to the specified value. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $value |
|
111
|
|
|
* @return bool true if the session map maps one or more keys to the specified value. |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function containsValue($value) |
|
114
|
|
|
{ |
|
115
|
1 |
|
return in_array($value, static::getInstance()->session); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $key |
|
122
|
|
|
* @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public static function get($key) |
|
125
|
|
|
{ |
|
126
|
2 |
|
return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Associates the specified value with the specified key in the session map. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $key |
|
133
|
|
|
* @param string $value |
|
134
|
|
|
* @return null |
|
135
|
|
|
*/ |
|
136
|
8 |
|
public static function set($key, $value) |
|
137
|
|
|
{ |
|
138
|
8 |
|
static::getInstance()->session[$key] = $value; |
|
139
|
8 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Removes the mapping for the specified key from the session map if present. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $key |
|
145
|
|
|
* @return null |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public static function remove($key) |
|
148
|
|
|
{ |
|
149
|
1 |
|
if (static::containsKey($key)) { |
|
150
|
1 |
|
unset(static::getInstance()->session[$key]); |
|
151
|
1 |
|
} |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Removes all of the mappings from the session map. |
|
156
|
|
|
* |
|
157
|
|
|
* @return null |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public static function clear() |
|
160
|
|
|
{ |
|
161
|
1 |
|
static::getInstance()->session = []; |
|
162
|
1 |
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: