@@ -18,154 +18,154 @@ |
||
18 | 18 | */ |
19 | 19 | class Session implements \IteratorAggregate |
20 | 20 | { |
21 | - use SingletonTrait; |
|
22 | - |
|
23 | - /** @var array The session. */ |
|
24 | - private $session; |
|
25 | - |
|
26 | - /** |
|
27 | - * Construct a Session object. |
|
28 | - * |
|
29 | - * @global array $_SESSION The session parameters. |
|
30 | - */ |
|
31 | - protected function __construct() |
|
32 | - { |
|
33 | - $this->session = &$_SESSION; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Initialize the session, if none exists. |
|
38 | - * |
|
39 | - * @param $name = null |
|
40 | - * @param $domain bool |
|
41 | - * @return null |
|
42 | - * @throws \RuntimeException on failure. |
|
43 | - */ |
|
44 | - public static function start($name = null, $domain = false) |
|
45 | - { |
|
46 | - if (session_status() !== PHP_SESSION_NONE) { |
|
47 | - throw new \RuntimeException('Can\'t start a new session.'); |
|
48 | - } |
|
49 | - if ($name !== null) { |
|
50 | - session_name($name); |
|
51 | - } |
|
52 | - if ($domain){ |
|
53 | - preg_match("/[^\.\/]+\.[^\.\/]+$/", $_SERVER['HTTP_HOST'], $matches); |
|
54 | - session_set_cookie_params(0, '/', $matches[0]); |
|
55 | - } |
|
56 | - session_start(); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Destroy the session, if one exists. |
|
61 | - * |
|
62 | - * @return null |
|
63 | - * @throws \RuntimeException on failure. |
|
64 | - */ |
|
65 | - public static function destroy() |
|
66 | - { |
|
67 | - if (session_status() !== PHP_SESSION_ACTIVE) { |
|
68 | - throw new \RuntimeException('Can\'t destroy the session. There is no active session.'); |
|
69 | - } |
|
70 | - |
|
71 | - if (!session_destroy()) { |
|
72 | - throw new \RuntimeException('Failed to destroy the active session.'); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * {@inheritdoc} |
|
78 | - */ |
|
79 | - public function getIterator() |
|
80 | - { |
|
81 | - return new \ArrayIterator(static::getInstance()->session); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Returns the number of key-value mappings in the session map. |
|
86 | - * |
|
87 | - * @return int the number of key-value mappings in the session map. |
|
88 | - */ |
|
89 | - public static function count() |
|
90 | - { |
|
91 | - return count(static::getInstance()->session); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Returns true if the session map contains no key-value mappings. |
|
96 | - * |
|
97 | - * @return bool true if the session map contains no key-value mappings. |
|
98 | - */ |
|
99 | - public static function isEmpty() |
|
100 | - { |
|
101 | - return empty(static::getInstance()->session); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Returns true if the session map contains a mapping for the specified key. |
|
106 | - * |
|
107 | - * @param string $key |
|
108 | - * @return bool true if the session map contains a mapping for the specified key. |
|
109 | - */ |
|
110 | - public static function containsKey($key) |
|
111 | - { |
|
112 | - return isset(static::getInstance()->session[$key]); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns true if the session map maps one or more keys to the specified value. |
|
117 | - * |
|
118 | - * @param string $value |
|
119 | - * @return bool true if the session map maps one or more keys to the specified value. |
|
120 | - */ |
|
121 | - public static function containsValue($value) |
|
122 | - { |
|
123 | - return in_array($value, static::getInstance()->session); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
128 | - * |
|
129 | - * @param string $key |
|
130 | - * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
131 | - */ |
|
132 | - public static function get($key) |
|
133 | - { |
|
134 | - return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Associates the specified value with the specified key in the session map. |
|
139 | - * |
|
140 | - * @param string $key |
|
141 | - * @param string $value |
|
142 | - * @return null |
|
143 | - */ |
|
144 | - public static function set($key, $value) |
|
145 | - { |
|
146 | - static::getInstance()->session[$key] = $value; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Removes the mapping for the specified key from the session map if present. |
|
151 | - * |
|
152 | - * @param string $key |
|
153 | - * @return null |
|
154 | - */ |
|
155 | - public static function remove($key) |
|
156 | - { |
|
157 | - if (static::containsKey($key)) { |
|
158 | - unset(static::getInstance()->session[$key]); |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Removes all of the mappings from the session map. |
|
164 | - * |
|
165 | - * @return null |
|
166 | - */ |
|
167 | - public static function clear() |
|
168 | - { |
|
169 | - static::getInstance()->session = []; |
|
170 | - } |
|
21 | + use SingletonTrait; |
|
22 | + |
|
23 | + /** @var array The session. */ |
|
24 | + private $session; |
|
25 | + |
|
26 | + /** |
|
27 | + * Construct a Session object. |
|
28 | + * |
|
29 | + * @global array $_SESSION The session parameters. |
|
30 | + */ |
|
31 | + protected function __construct() |
|
32 | + { |
|
33 | + $this->session = &$_SESSION; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Initialize the session, if none exists. |
|
38 | + * |
|
39 | + * @param $name = null |
|
40 | + * @param $domain bool |
|
41 | + * @return null |
|
42 | + * @throws \RuntimeException on failure. |
|
43 | + */ |
|
44 | + public static function start($name = null, $domain = false) |
|
45 | + { |
|
46 | + if (session_status() !== PHP_SESSION_NONE) { |
|
47 | + throw new \RuntimeException('Can\'t start a new session.'); |
|
48 | + } |
|
49 | + if ($name !== null) { |
|
50 | + session_name($name); |
|
51 | + } |
|
52 | + if ($domain){ |
|
53 | + preg_match("/[^\.\/]+\.[^\.\/]+$/", $_SERVER['HTTP_HOST'], $matches); |
|
54 | + session_set_cookie_params(0, '/', $matches[0]); |
|
55 | + } |
|
56 | + session_start(); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Destroy the session, if one exists. |
|
61 | + * |
|
62 | + * @return null |
|
63 | + * @throws \RuntimeException on failure. |
|
64 | + */ |
|
65 | + public static function destroy() |
|
66 | + { |
|
67 | + if (session_status() !== PHP_SESSION_ACTIVE) { |
|
68 | + throw new \RuntimeException('Can\'t destroy the session. There is no active session.'); |
|
69 | + } |
|
70 | + |
|
71 | + if (!session_destroy()) { |
|
72 | + throw new \RuntimeException('Failed to destroy the active session.'); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * {@inheritdoc} |
|
78 | + */ |
|
79 | + public function getIterator() |
|
80 | + { |
|
81 | + return new \ArrayIterator(static::getInstance()->session); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Returns the number of key-value mappings in the session map. |
|
86 | + * |
|
87 | + * @return int the number of key-value mappings in the session map. |
|
88 | + */ |
|
89 | + public static function count() |
|
90 | + { |
|
91 | + return count(static::getInstance()->session); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Returns true if the session map contains no key-value mappings. |
|
96 | + * |
|
97 | + * @return bool true if the session map contains no key-value mappings. |
|
98 | + */ |
|
99 | + public static function isEmpty() |
|
100 | + { |
|
101 | + return empty(static::getInstance()->session); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Returns true if the session map contains a mapping for the specified key. |
|
106 | + * |
|
107 | + * @param string $key |
|
108 | + * @return bool true if the session map contains a mapping for the specified key. |
|
109 | + */ |
|
110 | + public static function containsKey($key) |
|
111 | + { |
|
112 | + return isset(static::getInstance()->session[$key]); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns true if the session map maps one or more keys to the specified value. |
|
117 | + * |
|
118 | + * @param string $value |
|
119 | + * @return bool true if the session map maps one or more keys to the specified value. |
|
120 | + */ |
|
121 | + public static function containsValue($value) |
|
122 | + { |
|
123 | + return in_array($value, static::getInstance()->session); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
128 | + * |
|
129 | + * @param string $key |
|
130 | + * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
131 | + */ |
|
132 | + public static function get($key) |
|
133 | + { |
|
134 | + return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Associates the specified value with the specified key in the session map. |
|
139 | + * |
|
140 | + * @param string $key |
|
141 | + * @param string $value |
|
142 | + * @return null |
|
143 | + */ |
|
144 | + public static function set($key, $value) |
|
145 | + { |
|
146 | + static::getInstance()->session[$key] = $value; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Removes the mapping for the specified key from the session map if present. |
|
151 | + * |
|
152 | + * @param string $key |
|
153 | + * @return null |
|
154 | + */ |
|
155 | + public static function remove($key) |
|
156 | + { |
|
157 | + if (static::containsKey($key)) { |
|
158 | + unset(static::getInstance()->session[$key]); |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Removes all of the mappings from the session map. |
|
164 | + * |
|
165 | + * @return null |
|
166 | + */ |
|
167 | + public static function clear() |
|
168 | + { |
|
169 | + static::getInstance()->session = []; |
|
170 | + } |
|
171 | 171 | } |