20231010#4
This commit is contained in:
@@ -1,111 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: Zuev Yuri
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Cache;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
class Cache implements CacheInterface {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param int $defaultTime
|
|
||||||
* @param int $subLength
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
private string $path,
|
|
||||||
private readonly int $defaultTime = 120,
|
|
||||||
private readonly int $subLength = 2
|
|
||||||
){
|
|
||||||
$this->path = rtrim($path, "/");
|
|
||||||
if(!is_dir($this->path) && !mkdir($this->path, 0777, true)) throw new Exception("Error create path");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function save(string $name, $value, int $expires = 0) : bool {
|
|
||||||
if(empty($expires)) $expires = time() + $this->defaultTime;
|
|
||||||
if(!$this->fileSave($name, $expires."::".$name."::".serialize($value))) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function getItem(string $key) {
|
|
||||||
if($this->hasItem($key)){
|
|
||||||
$data = explode("::", $this->fileRead($key), 3);
|
|
||||||
return unserialize($data[2]);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function hasItem(string $key) : bool {
|
|
||||||
if (!empty($fileValue = $this->fileRead($key))){
|
|
||||||
if(count($data = explode("::", $fileValue, 3)) != 3) return false;
|
|
||||||
if(time() < (int)$data[0]) return true;
|
|
||||||
}
|
|
||||||
$this->fileDelete($key);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function get(string $name, callable $function, int $expires = 0) : mixed {
|
|
||||||
if($this->hasItem($name)){
|
|
||||||
$data = explode("::", $this->fileRead($name), 3);
|
|
||||||
return unserialize($data[2]);
|
|
||||||
}
|
|
||||||
$value = $function();
|
|
||||||
if(empty($expires)) $expires = time() + $this->defaultTime;
|
|
||||||
$this->fileSave($name, $expires."::".$name."::".serialize($value));
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return object
|
|
||||||
*/
|
|
||||||
private function getPath(string $name) : object {
|
|
||||||
$hexName = md5($name);
|
|
||||||
$subPath = substr($hexName, 0, $this->subLength);
|
|
||||||
return (object)[
|
|
||||||
"path" => $this->path."/".$subPath,
|
|
||||||
"file" => $this->path."/".$subPath."/".$hexName,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param string $data
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function fileSave(string $name, string $data) : bool {
|
|
||||||
$pathObject = $this->getPath($name);
|
|
||||||
if(!is_dir($pathObject->path)) mkdir($pathObject->path, 0777, true);
|
|
||||||
if(!file_put_contents($pathObject->file, $data)) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function fileRead(string $name) : string {
|
|
||||||
$pathObject = $this->getPath($name);
|
|
||||||
if (file_exists($pathObject->file) && $fileValue = file_get_contents($pathObject->file)) return $fileValue;
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function fileDelete($key) : bool {
|
|
||||||
$pathObject = $this->getPath($key);
|
|
||||||
if(file_exists($pathObject->file)) unlink($pathObject->file);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: Zuev Yuri
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Cache;
|
|
||||||
|
|
||||||
interface CacheInterface {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param $value
|
|
||||||
* @param int $expires
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function save(string $name, $value, int $expires = 0) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function getItem(string $key);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasItem(string $key) : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param callable $function
|
|
||||||
* @param int $expires
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function get(string $name, callable $function, int $expires = 0): mixed;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Session;
|
|
||||||
|
|
||||||
|
|
||||||
class Session implements SessionInterface {
|
|
||||||
|
|
||||||
const INT = "INT";
|
|
||||||
const STRING = "STRING";
|
|
||||||
|
|
||||||
public function __construct(string $name = "usi") {
|
|
||||||
if(session_status() == PHP_SESSION_NONE) {
|
|
||||||
if(in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)){
|
|
||||||
session_id("cli");
|
|
||||||
} elseif(!empty($name)) {
|
|
||||||
session_name($name);
|
|
||||||
}
|
|
||||||
session_start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function has(string $name = "") : bool {
|
|
||||||
return (!empty($name)) ? isset($this->getSession()[$name]) : !empty($this->getSession());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function get(string $name = "", string $type = ""): mixed {
|
|
||||||
$session = $this->getSession();
|
|
||||||
if(!empty($name = strtolower($name))) {
|
|
||||||
if (!isset($session[$name])) return null;
|
|
||||||
elseif ($type == self::STRING) {
|
|
||||||
return (!empty($session[$name])) ? (string)$session[$name] : null;
|
|
||||||
}
|
|
||||||
elseif ($type == self::INT) {
|
|
||||||
return (!empty((int)$session[$name]) || $session[$name]==0) ? (int)$session[$name] : null;
|
|
||||||
}
|
|
||||||
return $session[$name];
|
|
||||||
}
|
|
||||||
return $session;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function set(string $name, $value = null) : void {
|
|
||||||
$_SESSION[$name] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
public function clear(string $name = null) : void {
|
|
||||||
if (isset($name)) unset($_SESSION[$name]);
|
|
||||||
else $_SESSION = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
private function getSession() : array {
|
|
||||||
return $_SESSION;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: Zuev Yuri
|
|
||||||
* Date: 10.10.2023
|
|
||||||
* Time: 14:34
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Session;
|
|
||||||
|
|
||||||
interface SessionInterface {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function has(string $name = "") : bool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param string $type
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function get(string $name = "", string $type = ""): mixed;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param $value
|
|
||||||
*/
|
|
||||||
public function set(string $name, $value = null) : void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|null $name
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function clear(string $name = null) : void;
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user