microphp/Core/Session.php

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