<?php 

	class Session {
		private static $session;

		private static function loadSessionData(){
			session_start();
			self::$session = $_SESSION;
			session_write_close();

			unset($_SESSION);
		}

		public static function getAll(){
			self::loadSessionData();

			return self::$session;
		}

		public static function get($param){
			self::loadSessionData();

			$params = explode(".", $param);
			$aux = self::$session;

			for ($i=0; $i < count($params); $i++) {
				$aux = $aux[$params[$i]];
			}

			return $aux;
		}

		public static function set($param, $value){
			self::loadSessionData();

			$params = explode(".", $param);
			$aux = self::$session;
			$aux2 = self::rec($params, $value, $aux);

			session_start();
			$_SESSION[$params[0]] = $aux2[$params[0]];
			session_write_close();

			unset($_SESSION);
		}

		private static function rec($p, $v, $a, $i = 0, $n = []){
			if ($i+1 == count($p)) {
				$n[$p[$i]] = $v;
			}
			else{
				$x = self::rec($p, $v, $a, $i+1, $a[$p[$i]]);
				$n[$p[$i]] = is_array($n[$p[$i]]) ? $x + $n[$p[$i]] : $x;
			}

			return $n;
		}

		public static function exist($param){
			$result = self::get($param);

			return isset($result);
		}

		public static function getID(){
			return session_id();
		}

		public static function setID($id){
			return session_id($id);
		}

		public static function regenerateID(){
			session_start();
			session_regenerate_id();
			session_write_close();

			return self::getID();
		}

		public static function getName(){
			return session_name();
		}

		public static function setName($name){
			return session_name($name);
		}

		public static function unset(){
			session_start();
			$status = session_unset();
			session_write_close();

			return $status;
		}

		public static function destroy($reset_id = true){
			session_start();
			$status = session_destroy();
			session_write_close();

			if ($reset_id == true) {
				self::regenerateID();
			}

			return $status;
		}
	}

?>