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