Commit 1259961b authored by Carsten  Rose's avatar Carsten Rose
Browse files

Session.php: introduced close(). This will unlock the current session. Take...

Session.php: introduced close(). This will unlock the current session. Take care on subsequent calls to reopen primary session again.
parent 4f1b0bd4
......@@ -14,24 +14,54 @@ class Session {
private static $instance = null;
private static $phpUnit = null;
private static $sessionLocal = array();
private static $sessionId = null;
private static $sessionOpen = false;
/**
* @param bool|false $phpUnit
* @throws CodeException
*/
private function __construct($phpUnit = false) {
if (self::$phpUnit !== null)
if (self::$phpUnit !== null) {
throw new CodeException("Try to set flag phpunit again - that should not happen.", ERROR_CODE_SHOULD_NOT_HAPPEN);
}
self::$phpUnit = $phpUnit;
if (self::$phpUnit === true) {
self::$sessionLocal = array();
} else {
session_name(SESSION_NAME);
session_start();
self::$sessionId = session_id();
}
self::$sessionOpen = true;
self::checkFeUserUid();
}
/**
* Free a lock on the current session
*/
public static function close() {
session_write_close();
self::$sessionOpen = false;
}
/**
* By default the session is opened during bootstrap. In case the session has been closed manually, reopen it here.
* This code has never been tested - it should not happen that a session needs manually closed and reopen than.
* Nevertheless: This code is therefore a fallback in case the reopen really happens somewhat. Maybe this happens in
* upcoming logic.
*
*/
public static function open() {
if (self::$sessionOpen != true && self::$sessionId != null) {
session_id(self::$sessionId);
session_start();
}
}
/**
......@@ -82,6 +112,10 @@ class Session {
*/
public static function get($key) {
if (!self::$sessionOpen) {
self::open();
}
if (self::$phpUnit) {
if (isset(self::$sessionLocal[$key]))
$value = self::$sessionLocal[$key];
......@@ -103,6 +137,10 @@ class Session {
*/
public static function clearAll() {
if (!self::$sessionOpen) {
self::open();
}
if (self::$phpUnit) {
self::$sessionLocal = array();
} else {
......@@ -116,6 +154,10 @@ class Session {
*/
public static function set($key, $value) {
if (!self::$sessionOpen) {
self::open();
}
if (self::$phpUnit) {
self::$sessionLocal[$key] = $value;
} else {
......@@ -130,6 +172,10 @@ class Session {
*/
public static function unsetItem($key) {
if (!self::$sessionOpen) {
self::open();
}
if (isset($_SESSION[SESSION_NAME][$key])) {
unset($_SESSION[SESSION_NAME][$key]);
}
......@@ -147,6 +193,10 @@ class Session {
self::$instance = new self($phpUnit);
}
if (!self::$sessionOpen) {
self::open();
}
return self::$instance;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment