mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
Add console session draft
This commit is contained in:
parent
f704dfdeff
commit
34b9ba3097
212
DataStorage/Session/ConsoleSession.php
Normal file
212
DataStorage/Session/ConsoleSession.php
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\DataStorage\Session
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\DataStorage\Session;
|
||||
|
||||
use phpOMS\DataStorage\LockException;
|
||||
use phpOMS\Uri\UriFactory;
|
||||
use phpOMS\Utils\RnG\StringUtils;
|
||||
|
||||
/**
|
||||
* Console session class.
|
||||
*
|
||||
* @package phpOMS\DataStorage\Session
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
class ConsoleSession implements SessionInterface
|
||||
{
|
||||
/**
|
||||
* Is session locked/already set.
|
||||
*
|
||||
* @var bool
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private bool $isLocked = false;
|
||||
|
||||
/**
|
||||
* Raw session data.
|
||||
*
|
||||
* @var array
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private array $sessionData = [];
|
||||
|
||||
/**
|
||||
* Session ID.
|
||||
*
|
||||
* @var null|int|string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private $sid = null;
|
||||
|
||||
/**
|
||||
* Inactivity Interval.
|
||||
*
|
||||
* @var int
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private int $inactivityInterval = 0;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $liftetime Session life time
|
||||
* @param string $sid Session id
|
||||
* @param int $inactivityInterval Interval for session activity
|
||||
*
|
||||
* @throws LockException throws this exception if the session is alrady locked for further interaction
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct(int $liftetime = 3600, $sid = '', int $inactivityInterval = 0)
|
||||
{
|
||||
if (\session_id()) {
|
||||
\session_write_close(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if ($sid !== '') {
|
||||
\session_id((string) $sid);
|
||||
}
|
||||
|
||||
$this->inactivityInterval = $inactivityInterval;
|
||||
|
||||
if (\session_status() !== \PHP_SESSION_ACTIVE) {
|
||||
\session_set_cookie_params($liftetime, '/', '', false, true); // @codeCoverageIgnore
|
||||
\session_start(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if ($this->inactivityInterval > 0 && ($this->inactivityInterval + ($_SESSION['lastActivity'] ?? 0) < \time())) {
|
||||
$this->destroy(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$this->sessionData = $_SESSION ?? [];
|
||||
$_SESSION = null;
|
||||
$this->sessionData['lastActivity'] = \time();
|
||||
$this->sid = \session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($key, $value, bool $overwrite = false) : bool
|
||||
{
|
||||
if (!$this->isLocked && ($overwrite || !isset($this->sessionData[$key]))) {
|
||||
$this->sessionData[$key] = $value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return $this->sessionData[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lock() : void
|
||||
{
|
||||
$this->isLocked = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if session is locked.
|
||||
*
|
||||
* @return bool Lock status
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function isLocked() : bool
|
||||
{
|
||||
return $this->isLocked;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save() : void
|
||||
{
|
||||
if (!$this->isLocked) {
|
||||
$_SESSION = $this->sessionData;
|
||||
\session_write_close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove($key) : bool
|
||||
{
|
||||
if (!$this->isLocked && isset($this->sessionData[$key])) {
|
||||
unset($this->sessionData[$key]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSID()
|
||||
{
|
||||
return $this->sid;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setSID($sid) : void
|
||||
{
|
||||
$this->sid = $sid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the current session.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
private function destroy() : void
|
||||
{
|
||||
if (\session_status() !== \PHP_SESSION_NONE) {
|
||||
\session_destroy();
|
||||
$this->sessionData = [];
|
||||
\session_start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destruct session.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
166
DataStorage/Session/ConsoleSessionHandler.php
Normal file
166
DataStorage/Session/ConsoleSessionHandler.php
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\DataStorage\Session
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\DataStorage\Session;
|
||||
|
||||
/**
|
||||
* Console session handler.
|
||||
*
|
||||
* @package phpOMS\DataStorage\Session
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.Superglobals)
|
||||
*/
|
||||
final class ConsoleSessionHandler implements \SessionHandlerInterface, \SessionIdInterface
|
||||
{
|
||||
/**
|
||||
* File path for session
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private string $savePath;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $path Path of the session data
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct(string $path)
|
||||
{
|
||||
$this->savePath = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a session id string
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function create_sid() : string
|
||||
{
|
||||
return \session_create_id('console-');
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the session storage
|
||||
*
|
||||
* @param string $savePath Path of the session data
|
||||
* @param string $sessionName Name of the session
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function open($savePath, $sessionName)
|
||||
{
|
||||
$this->savePath = $savePath;
|
||||
if (!\is_dir($this->savePath)) {
|
||||
\mkdir($this->savePath, 0777);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the session
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function close() : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the session data
|
||||
*
|
||||
* @param string $id Session id
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
return (string) @\file_get_contents($this->savePath . '/sess_' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write session data
|
||||
*
|
||||
* @param string $id Session id
|
||||
* @param string $data Session data
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function write($id, $data)
|
||||
{
|
||||
return \file_put_contents($this->savePath . '/sess_' . $id, $data) === false ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the session
|
||||
*
|
||||
* @param string $id Session id
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$file = $this->savePath . '/sess_' . $id;
|
||||
if (\file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Garbage collect session data
|
||||
*
|
||||
* @param int $maxlifetime Maximum session data life time
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function gc($maxlifetime)
|
||||
{
|
||||
$files = \glob("$this->savePath/sess_*");
|
||||
|
||||
if ($files === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (\filemtime($file) + $maxlifetime < \time() && \file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user