@@ -9,163 +9,163 @@ |
||
| 9 | 9 | class HybridSession extends BaseStore |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * List of session handlers |
|
| 14 | - * |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $handlers = []; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * True if this session store has been initialised |
|
| 21 | - * |
|
| 22 | - * @var bool |
|
| 23 | - */ |
|
| 24 | - protected static $enabled = false; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @param SessionHandlerInterface[] |
|
| 28 | - * |
|
| 29 | - * @return $this |
|
| 30 | - */ |
|
| 31 | - public function setHandlers($handlers) |
|
| 32 | - { |
|
| 33 | - $this->handlers = $handlers; |
|
| 34 | - $this->setKey($this->getKey()); |
|
| 35 | - |
|
| 36 | - return $this; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param string |
|
| 41 | - * |
|
| 42 | - * @return $this |
|
| 43 | - */ |
|
| 44 | - public function setKey($key) |
|
| 45 | - { |
|
| 46 | - parent::setKey($key); |
|
| 47 | - |
|
| 48 | - foreach ($this->handlers as $handler) { |
|
| 49 | - $handler->setKey($key); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - return $this; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @return SessionHandlerInterface[] |
|
| 57 | - */ |
|
| 58 | - public function getHandlers() |
|
| 59 | - { |
|
| 60 | - return $this->handlers; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param string $save_path |
|
| 65 | - * @param string $name |
|
| 66 | - * |
|
| 67 | - * @return bool |
|
| 68 | - */ |
|
| 69 | - public function open($save_path, $name) |
|
| 70 | - { |
|
| 71 | - if ($this->handlers) { |
|
| 72 | - foreach ($this->handlers as $handler) { |
|
| 73 | - $handler->open($save_path, $name); |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return true; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @return bool |
|
| 82 | - */ |
|
| 83 | - public function close() |
|
| 84 | - { |
|
| 85 | - if ($this->handlers) { |
|
| 86 | - foreach ($this->handlers as $handler) { |
|
| 87 | - $handler->close(); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $session_id |
|
| 96 | - * |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - public function read($session_id) |
|
| 100 | - { |
|
| 101 | - if ($this->handlers) { |
|
| 102 | - foreach ($this->handlers as $handler) { |
|
| 103 | - if ($data = $handler->read($session_id)) { |
|
| 104 | - return $data; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return ''; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - public function write($session_id, $session_data) |
|
| 114 | - { |
|
| 115 | - if ($this->handlers) { |
|
| 116 | - foreach ($this->handlers as $handler) { |
|
| 117 | - if ($handler->write($session_id, $session_data)) { |
|
| 118 | - return true; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function destroy($session_id) |
|
| 126 | - { |
|
| 127 | - if ($this->handlers) { |
|
| 128 | - foreach ($this->handlers as $handler) { |
|
| 129 | - $handler->destroy($session_id); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - return true; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - public function gc($maxlifetime) |
|
| 136 | - { |
|
| 137 | - if ($this->handlers) { |
|
| 138 | - foreach ($this->handlers as $handler) { |
|
| 139 | - $handler->gc($maxlifetime); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Register the session handler as the default |
|
| 146 | - * |
|
| 147 | - * @param string $key Desired session key |
|
| 148 | - */ |
|
| 149 | - public static function init($key = null) |
|
| 150 | - { |
|
| 151 | - $instance = Injector::inst()->get(__CLASS__); |
|
| 152 | - |
|
| 153 | - if (empty($key)) { |
|
| 154 | - user_error( |
|
| 155 | - 'HybridSession::init() was not given a $key. Disabling cookie-based storage', |
|
| 156 | - E_USER_WARNING |
|
| 157 | - ); |
|
| 158 | - } else { |
|
| 159 | - $instance->setKey($key); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - session_set_save_handler($instance, true); |
|
| 163 | - |
|
| 164 | - self::$enabled = true; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - public static function is_enabled() |
|
| 168 | - { |
|
| 169 | - return self::$enabled; |
|
| 170 | - } |
|
| 12 | + /** |
|
| 13 | + * List of session handlers |
|
| 14 | + * |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $handlers = []; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * True if this session store has been initialised |
|
| 21 | + * |
|
| 22 | + * @var bool |
|
| 23 | + */ |
|
| 24 | + protected static $enabled = false; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @param SessionHandlerInterface[] |
|
| 28 | + * |
|
| 29 | + * @return $this |
|
| 30 | + */ |
|
| 31 | + public function setHandlers($handlers) |
|
| 32 | + { |
|
| 33 | + $this->handlers = $handlers; |
|
| 34 | + $this->setKey($this->getKey()); |
|
| 35 | + |
|
| 36 | + return $this; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param string |
|
| 41 | + * |
|
| 42 | + * @return $this |
|
| 43 | + */ |
|
| 44 | + public function setKey($key) |
|
| 45 | + { |
|
| 46 | + parent::setKey($key); |
|
| 47 | + |
|
| 48 | + foreach ($this->handlers as $handler) { |
|
| 49 | + $handler->setKey($key); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + return $this; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @return SessionHandlerInterface[] |
|
| 57 | + */ |
|
| 58 | + public function getHandlers() |
|
| 59 | + { |
|
| 60 | + return $this->handlers; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param string $save_path |
|
| 65 | + * @param string $name |
|
| 66 | + * |
|
| 67 | + * @return bool |
|
| 68 | + */ |
|
| 69 | + public function open($save_path, $name) |
|
| 70 | + { |
|
| 71 | + if ($this->handlers) { |
|
| 72 | + foreach ($this->handlers as $handler) { |
|
| 73 | + $handler->open($save_path, $name); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return true; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @return bool |
|
| 82 | + */ |
|
| 83 | + public function close() |
|
| 84 | + { |
|
| 85 | + if ($this->handlers) { |
|
| 86 | + foreach ($this->handlers as $handler) { |
|
| 87 | + $handler->close(); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return true; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $session_id |
|
| 96 | + * |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + public function read($session_id) |
|
| 100 | + { |
|
| 101 | + if ($this->handlers) { |
|
| 102 | + foreach ($this->handlers as $handler) { |
|
| 103 | + if ($data = $handler->read($session_id)) { |
|
| 104 | + return $data; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return ''; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + public function write($session_id, $session_data) |
|
| 114 | + { |
|
| 115 | + if ($this->handlers) { |
|
| 116 | + foreach ($this->handlers as $handler) { |
|
| 117 | + if ($handler->write($session_id, $session_data)) { |
|
| 118 | + return true; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function destroy($session_id) |
|
| 126 | + { |
|
| 127 | + if ($this->handlers) { |
|
| 128 | + foreach ($this->handlers as $handler) { |
|
| 129 | + $handler->destroy($session_id); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + return true; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + public function gc($maxlifetime) |
|
| 136 | + { |
|
| 137 | + if ($this->handlers) { |
|
| 138 | + foreach ($this->handlers as $handler) { |
|
| 139 | + $handler->gc($maxlifetime); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Register the session handler as the default |
|
| 146 | + * |
|
| 147 | + * @param string $key Desired session key |
|
| 148 | + */ |
|
| 149 | + public static function init($key = null) |
|
| 150 | + { |
|
| 151 | + $instance = Injector::inst()->get(__CLASS__); |
|
| 152 | + |
|
| 153 | + if (empty($key)) { |
|
| 154 | + user_error( |
|
| 155 | + 'HybridSession::init() was not given a $key. Disabling cookie-based storage', |
|
| 156 | + E_USER_WARNING |
|
| 157 | + ); |
|
| 158 | + } else { |
|
| 159 | + $instance->setKey($key); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + session_set_save_handler($instance, true); |
|
| 163 | + |
|
| 164 | + self::$enabled = true; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + public static function is_enabled() |
|
| 168 | + { |
|
| 169 | + return self::$enabled; |
|
| 170 | + } |
|
| 171 | 171 | } |