This commit is contained in:
me
2026-07-23 15:50:56 +07:00
parent 9ec08a3e44
commit 7448b5ea2a
9 changed files with 172 additions and 705 deletions
-21
View File
@@ -1,21 +0,0 @@
<?php
namespace Nightmare\Cache\Driver;
use Nightmare\Fs;
class File
{
private $opt;
public function __construct() {
}
public function has($key) {}
public function get($key, $default = null) {}
public function set($key, $value, $ttl) {}
public function unset($key) {}
public function clear() {}
public function clear_prefix($prefix) {}
}
+172 -213
View File
@@ -1,213 +1,172 @@
<?php <?php
namespace Nightmare; namespace Nightmare;
use Exception; use Exception;
use FilesystemIterator; use FilesystemIterator;
use RecursiveCallbackFilterIterator; use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use SplFileInfo; use SplFileInfo;
// file system // file system
class Fs class Fs
{ {
/* /*
* file, file1, file2... * file, file1, file2...
*/ */
/** /**
* @param string $file_name_body * @param string $file_name_body
* @param string $file_ext * @param string $file_ext
* @return string * @return string
*/ */
public function name_increment($file_name_body, $file_ext) public function name_increment($file_name_body, $file_ext)
{ {
$i = 1; $i = 1;
$file_exists = true; $file_exists = true;
do { do {
$file_save = $file_name_body . $i . '.' . $file_ext; $file_save = $file_name_body . $i . '.' . $file_ext;
if (!file_exists($file_save)) { if (!file_exists($file_save)) {
$file_exists = false; $file_exists = false;
} }
$i++; $i++;
} while ($file_exists); } while ($file_exists);
return $file_save; return $file_save;
} }
/** /**
* @param string $name * @param string $name
* @return string * @return string
*/ */
public static function get_extension($name) public static function get_extension($name)
{ {
return (new SplFileInfo($name))->getExtension(); return (new SplFileInfo($name))->getExtension();
} }
/** /**
* @param string $path * @param string $path
* @return int * @return int
*/ */
public static function size($path) public static function size($path)
{ {
if (!is_dir($path)) { if (!is_dir($path)) {
return filesize($path); return filesize($path);
} }
$size = 0; $size = 0;
$iterator = new RecursiveIteratorIterator( $iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS), new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
RecursiveIteratorIterator::LEAVES_ONLY RecursiveIteratorIterator::LEAVES_ONLY
); );
foreach ($iterator as $file) { foreach ($iterator as $file) {
if ($file->isFile()) { if ($file->isFile()) {
$size += $file->getSize(); $size += $file->getSize();
} }
} }
return $size; return $size;
} }
/** /**
* @param int $fileSize * @param int $fileSize
* @return string * @return string
*/ */
public static function sizen($fileSize) public static function sizen($fileSize)
{ {
$size = floatval($fileSize); $size = floatval($fileSize);
if ($size < 1024) { if ($size < 1024) {
$s = $size . ' B'; $s = $size . ' B';
} elseif ($size < 1048576) { } elseif ($size < 1048576) {
$s = round($size / 1024, 2) . ' KB'; $s = round($size / 1024, 2) . ' KB';
} elseif ($size < 1073741824) { } elseif ($size < 1073741824) {
$s = round($size / 1048576, 2) . ' MB'; $s = round($size / 1048576, 2) . ' MB';
} elseif ($size < 1099511627776) { } elseif ($size < 1099511627776) {
$s = round($size / 1073741824, 2) . ' GB'; $s = round($size / 1073741824, 2) . ' GB';
} elseif ($size < 1125899906842624) { } elseif ($size < 1125899906842624) {
$s = round($size / 1099511627776, 2) . ' TB'; $s = round($size / 1099511627776, 2) . ' TB';
} elseif ($size < 1152921504606846976) { } elseif ($size < 1152921504606846976) {
$s = round($size / 1125899906842624, 2) . ' PB'; $s = round($size / 1125899906842624, 2) . ' PB';
} elseif ($size < 1.1805916207174E+21) { } elseif ($size < 1.1805916207174E+21) {
$s = round($size / 1152921504606846976, 2) . ' EB'; $s = round($size / 1152921504606846976, 2) . ' EB';
} elseif ($size < 1.2089258196146E+24) { } elseif ($size < 1.2089258196146E+24) {
$s = round($size / 1.1805916207174E+21, 2) . ' ZB'; $s = round($size / 1.1805916207174E+21, 2) . ' ZB';
} else { } else {
$s = round($size / 1.2089258196146E+24, 2) . ' YB'; $s = round($size / 1.2089258196146E+24, 2) . ' YB';
} }
return $s; return $s;
} }
/** /**
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
public static function remove($path) public static function remove($path)
{ {
if (is_link($path)) { if (is_link($path)) {
return unlink($path); return unlink($path);
} }
if (is_file($path)) { if (is_file($path)) {
return unlink($path); return unlink($path);
} }
if (is_dir($path)) { if (is_dir($path)) {
$files = array_diff(scandir($path), ['.', '..']); $files = array_diff(scandir($path), ['.', '..']);
foreach ($files as $file) { foreach ($files as $file) {
$filePath = $path . DIRECTORY_SEPARATOR . $file; $filePath = $path . DIRECTORY_SEPARATOR . $file;
if (!self::remove($filePath)) { if (!self::remove($filePath)) {
return false; return false;
} }
} }
return rmdir($path); return rmdir($path);
} }
if (!file_exists($path)) { if (!file_exists($path)) {
return true; return true;
} }
throw new Exception('remove error, not match file type'); throw new Exception('remove error, not match file type');
} }
/** /**
* @param string $path * @param string ...$paths
* @param array $excludes * @return string
* @return RecursiveIteratorIterator */
*/ public static function join_path(...$paths)
public static function read_full_dir($path, $excludes = []) {
{ return preg_replace('#/+#', '/', implode('/', $paths));
$directory = new RecursiveDirectoryIterator( }
$path,
FilesystemIterator::UNIX_PATHS /**
| FilesystemIterator::SKIP_DOTS * Lấy tên chủ sở hữu file theo tên file
); * @param string $filename
* @return string
$filter = new RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) use ($path, $excludes) { */
$relativePath = Str::replace_first($path, '', $current->getPathname()); public static function get_owner_name($filename)
{
foreach ($excludes as $exclude) { $owner_id = @fileowner($filename);
if (empty($exclude)) { if ($owner_id === false) {
continue; return '';
} }
//var_dump($relativePath); return self::get_owner_name_by_id($owner_id);
//var_dump($exclude); }
$exclude = trim($exclude); /**
$exclude = trim($exclude, '/'); * Lấy tên chủ sở hữu theo user ID
$relativePath = trim($relativePath, '/'); * @param int $id
* @return string
if (str_ends_with($relativePath, $exclude)) { */
return false; public static function get_owner_name_by_id($id)
} {
} $info = @posix_getpwuid($id);
return $info['name'] ?? '';
return true; }
}); }
return new RecursiveIteratorIterator(
$filter,
RecursiveIteratorIterator::SELF_FIRST
);
}
/**
* @param string ...$paths
* @return string
*/
public static function join_path(...$paths)
{
return preg_replace('#/+#', '/', implode('/', $paths));
}
/**
* Lấy tên chủ sở hữu file theo tên file
* @param string $filename
* @return string
*/
public static function get_owner_name($filename)
{
$owner_id = @fileowner($filename);
if ($owner_id === false) {
return '';
}
return self::get_owner_name_by_id($owner_id);
}
/**
* Lấy tên chủ sở hữu theo user ID
* @param int $id
* @return string
*/
public static function get_owner_name_by_id($id)
{
$info = @posix_getpwuid($id);
return $info['name'] ?? '';
}
}
-57
View File
@@ -1,57 +0,0 @@
<?php
namespace Nightmare\Session\Storage;
use SessionHandlerInterface;
use ReturnTypeWillChange;
class Apcu implements SessionHandlerInterface
{
private string $prefix;
private int $ttl;
public function __construct(string $prefix = 'sess_', int $ttl = 86400)
{
$this->prefix = $prefix;
$this->ttl = $ttl;
}
#[ReturnTypeWillChange]
public function close()
{
return true;
}
#[ReturnTypeWillChange]
public function destroy($id)
{
$key = $this->prefix . $id;
return apcu_delete($key);
}
#[ReturnTypeWillChange]
public function gc($max_lifetime)
{
return 1;
}
#[ReturnTypeWillChange]
public function open($path, $name)
{
return true;
}
#[ReturnTypeWillChange]
public function read($id)
{
$key = $this->prefix . $id;
return apcu_exists($key) ? apcu_fetch($key) : '';
}
#[ReturnTypeWillChange]
public function write($id, $data)
{
$key = $this->prefix . $id;
return apcu_store($key, $data, $this->ttl);
}
}
View File
-207
View File
@@ -1,207 +0,0 @@
<?php
namespace nightmare;
class cache
{
/**
* @var bool
*/
private static $debug = false;
/**
* @var int|null
*/
private static $expire = null;
/**
* @var string
*/
private static $prefix = '';
/**
* @var mixed
*/
private static $adapter;
/**
* @var array
*/
private static $adapters = [];
// common
/**
* @param bool $debug
* @return void
*/
public static function set_debug($debug)
{
self::$debug = $debug;
}
/**
* @param string $prefix
* @return void
*/
public static function set_prefix($prefix)
{
self::$prefix = $prefix;
}
/**
* @param int|null $ttl
* @return void
*/
public static function set_expire($ttl = null)
{
self::$expire = $ttl;
}
// adapter
/**
* @param string $key
* @return void
*/
public static function set_adapter($key)
{
self::$adapter = self::$adapters[$key];
}
/**
* @param string $key
* @return mixed
*/
public static function get_adapter($key)
{
return self::$adapters[$key];
}
/**
* @param string $key
* @param mixed $adapter
* @return void
*/
public static function add_adapter($key, $adapter)
{
self::$adapters = array_merge(self::$adapters, [$key => $adapter]);
}
/**
* @param string $key
* @return void
*/
public function remove_adapter($key)
{
unset(self::$adapters[$key]);
}
/**
* @return array
*/
public static function get_adapters()
{
return self::$adapters;
}
// cache
/**
* @param string $key
* @return bool
*/
public static function has($key)
{
return self::$adapter->hasItem(self::$prefix . $key);
}
// truyen 1 tham so - lay binh thuong
// truyen 2 tham so tro len - luu cache cho lan sau
/**
* @param string $key
* @param mixed $default
* @param array $opt
* @return mixed
*/
public static function get($key, $default = null, $opt = [])
{
$opt += [
'expire' => self::$expire,
'debug' => false,
'save' => true,
'save_if' => null, // ?callable
];
if (self::$debug || $opt['debug']) {
self::unset(self::$prefix . $key);
}
// get cache
$item = self::$adapter->getItem(self::$prefix . $key);
if ($item->isHit()) {
return $item->get();
} else {
if (is_callable($default)) {
$default = call_user_func($default, $opt);
}
if ($opt['save']) {
$save = false;
if (is_callable($opt['save_if'])) {
if (call_user_func($opt['save_if'], $default)) {
$save = true;
}
} else {
$save = true;
}
if ($save) {
self::set($key, $default, $opt['expire']);
}
}
return $default;
}
}
/**
* @param string $key
* @param mixed $value
* @param int|null $expire
* @return bool
*/
public static function set($key, $value, $expire = null)
{
$item = self::$adapter->getItem(self::$prefix . $key);
if ($expire !== null) {
$item->expiresAfter($expire);
} elseif (self::$expire !== null) {
$item->expiresAfter(self::$expire);
}
$item->set($value);
return self::$adapter->save($item);
}
/**
* @param string $key
* @return bool
*/
public static function unset($key)
{
return self::$adapter->deleteItem(self::$prefix . $key);
}
/**
* @param string $prefix
* @return bool
*/
public static function clear($prefix = '')
{
return self::$adapter->clear($prefix);
}
}
View File
-207
View File
@@ -1,207 +0,0 @@
<?php
namespace nightmare;
class cache
{
/**
* @var bool
*/
private $debug = false;
/**
* @var int|null
*/
private $expire = null;
/**
* @var string
*/
private $prefix = '';
/**
* @var mixed
*/
private $adapter;
/**
* @var array
*/
private $adapters = [];
// common
/**
* @param bool $debug
* @return void
*/
public function set_debug($debug)
{
self::$debug = $debug;
}
/**
* @param string $prefix
* @return void
*/
public function set_prefix($prefix)
{
self::$prefix = $prefix;
}
/**
* @param int|null $ttl
* @return void
*/
public function set_expire($ttl = null)
{
self::$expire = $ttl;
}
// adapter
/**
* @param string $key
* @return void
*/
public function set_adapter($key)
{
self::$adapter = self::$adapters[$key];
}
/**
* @param string $key
* @return mixed
*/
public function get_adapter($key)
{
return self::$adapters[$key];
}
/**
* @param string $key
* @param mixed $adapter
* @return void
*/
public function add_adapter($key, $adapter)
{
self::$adapters = array_merge(self::$adapters, [$key => $adapter]);
}
/**
* @param string $key
* @return void
*/
public function remove_adapter($key)
{
unset(self::$adapters[$key]);
}
/**
* @return array
*/
public function get_adapters()
{
return self::$adapters;
}
// cache
/**
* @param string $key
* @return bool
*/
public function has($key)
{
return self::$adapter->hasItem(self::$prefix . $key);
}
// truyen 1 tham so - lay binh thuong
// truyen 2 tham so tro len - luu cache cho lan sau
/**
* @param string $key
* @param mixed $default
* @param array $opt
* @return mixed
*/
public function get($key, $default = null, $opt = [])
{
$opt += [
'expire' => self::$expire,
'debug' => false,
'save' => true,
'save_if' => null, // ?callable
];
if (self::$debug || $opt['debug']) {
self::unset(self::$prefix . $key);
}
// get cache
$item = self::$adapter->getItem(self::$prefix . $key);
if ($item->isHit()) {
return $item->get();
} else {
if (is_callable($default)) {
$default = call_user_func($default, $opt);
}
if ($opt['save']) {
$save = false;
if (is_callable($opt['save_if'])) {
if (call_user_func($opt['save_if'], $default)) {
$save = true;
}
} else {
$save = true;
}
if ($save) {
self::set($key, $default, $opt['expire']);
}
}
return $default;
}
}
/**
* @param string $key
* @param mixed $value
* @param int|null $expire
* @return bool
*/
public function set($key, $value, $expire = null)
{
$item = self::$adapter->getItem(self::$prefix . $key);
if ($expire !== null) {
$item->expiresAfter($expire);
} elseif (self::$expire !== null) {
$item->expiresAfter(self::$expire);
}
$item->set($value);
return self::$adapter->save($item);
}
/**
* @param string $key
* @return bool
*/
public function unset($key)
{
return self::$adapter->deleteItem(self::$prefix . $key);
}
/**
* @param string $prefix
* @return bool
*/
public function clear($prefix = '')
{
return self::$adapter->clear($prefix);
}
}
View File
View File