microphp/Core/Session.php

123 lines
3.1 KiB
PHP
Executable File

<?php
include_once "Reflection.php";
include_once "RequestCookie.php";
class Session {
use ReflectionHook;
static $active = false;
static $lifetime = '+7 days';
static $cookiename = 'session';
static $ready = false;
public function __contruct()
{
session_set_cookie_params(strtotime(static::$lifetime));
session_name(static::$cookiename);
Session::$ready = true;
}
public function staticStart()
{
session_start();
}
public function staticFlush()
{
session_commit();
}
public function staticDestroy()
{
session_destroy();
}
public function staticReset()
{
session_reset();
}
public function staticSet($name, $value)
{
$_SESSION[$name] = $value;
}
public function staticGet($name)
{
return $_SESSION[$name];
}
public function staticDelete($name)
{
unset($_SESSION[$name]);
}
public function staticHas($name)
{
return isset($_SESSION[$name]);
}
public function staticAdd($name, $item)
{
if(!Session::has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name][] = $item;
}
public function staticPop($name, $item)
{
if(!$this->has($name))
{
$_SESSION[$name] = [];
}
return array_pop($_SESSION[$name]);
}
public function staticShift($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
return array_shift($_SESSION[$name]);
}
public function staticUnshift($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name] = [
$item,
...$_SESSION[$name]
];
}
public function staticMerge($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name] = [
...$_SESSION[$name],
...$item
];
}
public function staticPath($path)
{
session_save_path($path);
}
public function staticStatus()
{
switch(session_status())
{
case PHP_SESSION_ACTIVE:{
return Session::$active = true;
}
default:{
return Session::$active = false;
}
}
}
public function staticInit()
{
if(!Session::$ready)
{
$this->__contruct();
}
if(!$this->staticStatus())
{
$this->staticStart();
}
}
};
define("session", new Session(), false);