summaryrefslogtreecommitdiff
path: root/include/MySession.class.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-10-11 14:01:29 +0200
committeremkael <emkael@tlen.pl>2016-10-11 14:01:29 +0200
commit51609351f2c4b5082b7e6f0744cd3811c325303f (patch)
tree739015e9ec69bc185ebe30db21369ae0b8b692ce /include/MySession.class.php
parent8d1b0dad63e3906efa9393ef01d08b77d83417b5 (diff)
* initial template
Diffstat (limited to 'include/MySession.class.php')
-rw-r--r--include/MySession.class.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/include/MySession.class.php b/include/MySession.class.php
new file mode 100644
index 0000000..b57f7c8
--- /dev/null
+++ b/include/MySession.class.php
@@ -0,0 +1,61 @@
+<?php
+
+class MySession {
+
+ private static $_initiated = FALSE;
+ private static $_dataContainer = array();
+
+ public static function init() {
+ if (!self::$_initiated) {
+ session_start();
+ if (!isset($_SESSION['__data'])) {
+ $_SESSION['__data'] = [];
+ }
+ self::$_dataContainer = &$_SESSION['__data'];
+ }
+ self::$_initiated = TRUE;
+ }
+
+ private static function _getRealm() {
+ $backtrace = debug_backtrace();
+ while (count($backtrace) && isset($backtrace[0]['class']) && $backtrace[0]['class'] == get_class()) {
+ array_shift($backtrace);
+ }
+ if (!count($backtrace) || (!isset($backtrace[0]['class']))) {
+ throw new SessionRealmException('Session realm not provided');
+ }
+ return $backtrace[0]['class'];
+ }
+
+ public static function get($variable, $realm = NULL) {
+ if (!self::$_initiated) {
+ self::init();
+ }
+ if (!$realm) {
+ $realm = self::_getRealm();
+ }
+ if (!isset(self::$_dataContainer[$realm])) {
+ throw new SessionVariableException('Realm '.$realm.' doesn\'t exist');
+ }
+ if (!isset(self::$_dataContainer[$realm][$variable])) {
+ throw new SessionVariableException('Variable '.$realm.'::'.$variable.' not set');
+ }
+ return self::$_dataContainer[$realm][$variable];
+ }
+
+ public static function set($variable, $value, $realm = NULL) {
+ if (!self::$_initiated) {
+ self::init();
+ }
+ if (!$realm) {
+ $realm = self::_getRealm();
+ }
+ if (!isset(self::$_dataContainer[$realm])) {
+ self::$_dataContainer[$realm] = [];
+ }
+ return self::$_dataContainer[$realm][$variable] = $value;
+ }
+
+}
+
+?>