microphp/Core/Session.php

121 lines
3.0 KiB
PHP
Raw Normal View History

2023-04-02 10:00:20 +03:00
<?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;
function __contruct()
{
session_set_cookie_params(strtotime(static::$lifetime));
session_name(static::$cookiename);
Session::$ready = true;
}
function staticStart()
{
session_start();
}
function staticFlush()
{
session_commit();
}
function staticDestroy()
{
session_destroy();
}
function staticReset()
{
session_reset();
}
function staticSet($name, $value)
{
$_SESSION[$name] = $value;
}
function staticGet($name)
{
return $_SESSION[$name];
}
function staticDelete($name)
{
unset($_SESSION[$name]);
}
function staticHas($name)
{
return isset($_SESSION[$name]);
}
function staticAdd($name, $item)
{
if(!Session::has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name][] = $item;
}
function staticPop($name, $item)
{
if(!$this->has($name))
{
$_SESSION[$name] = [];
}
return array_pop($_SESSION[$name]);
}
function staticShift($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
return array_shift($_SESSION[$name]);
}
function staticUnshift($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name] = [
$item,
...$_SESSION[$name]
];
}
function staticMerge($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name] = [
...$_SESSION[$name],
...$item
];
}
function staticPath($path)
{
session_save_path($path);
}
function staticStatus()
{
switch(session_status())
{
case PHP_SESSION_ACTIVE:{
return Session::$active = true;
}
default:{
return Session::$active = false;
}
}
}
function staticInit()
{
if(!Session::$ready)
{
$this->__contruct();
}
if(!$this->staticStatus())
{
$this->staticStart();
}
}
};