Merge pull request #10754 from cetra3/master

Refactor internal session handler to write directly to $_SESSION
This commit is contained in:
Lukas Reschke 2014-09-16 15:48:19 +02:00
commit 2f10b60c9e
1 changed files with 37 additions and 7 deletions

View File

@ -15,46 +15,76 @@ namespace OC\Session;
* *
* @package OC\Session * @package OC\Session
*/ */
class Internal extends Memory { class Internal extends Session {
public function __construct($name) { public function __construct($name) {
session_name($name); session_name($name);
session_start(); session_start();
if (!isset($_SESSION)) { if (!isset($_SESSION)) {
throw new \Exception('Failed to start session'); throw new \Exception('Failed to start session');
} }
$this->data = $_SESSION;
} }
public function __destruct() { public function __destruct() {
$this->close(); $this->close();
} }
/**
* @param string $key
* @param integer $value
*/
public function set($key, $value) {
$this->validateSession();
$_SESSION[$key] = $value;
}
/**
* @param string $key
* @return mixed
*/
public function get($key) {
if (!$this->exists($key)) {
return null;
}
return $_SESSION[$key];
}
/**
* @param string $key
* @return bool
*/
public function exists($key) {
return isset($_SESSION[$key]);
}
/** /**
* @param string $key * @param string $key
*/ */
public function remove($key) { public function remove($key) {
// also remove it from $_SESSION to prevent re-setting the old value during the merge
if (isset($_SESSION[$key])) { if (isset($_SESSION[$key])) {
unset($_SESSION[$key]); unset($_SESSION[$key]);
} }
parent::remove($key);
} }
public function clear() { public function clear() {
session_unset(); session_unset();
@session_regenerate_id(true); @session_regenerate_id(true);
@session_start(); @session_start();
$this->data = $_SESSION = array(); $_SESSION = array();
} }
public function close() { public function close() {
$_SESSION = array_merge($_SESSION, $this->data);
session_write_close(); session_write_close();
parent::close(); parent::close();
} }
public function reopen() { public function reopen() {
throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.'); throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
} }
private function validateSession() {
if ($this->sessionClosed) {
throw new \Exception('Session has been closed - no further changes to the session as allowed');
}
}
} }