refactor:完善 extend 中的类的类型定义

This commit is contained in:
妙码生花 2023-06-25 12:01:03 +08:00
parent 45cbabadef
commit b7eead8321
6 changed files with 103 additions and 131 deletions

View File

@ -26,7 +26,7 @@ class Account extends Frontend
public function overview() public function overview()
{ {
$sevenDays = Date::unixtime('day', -6); $sevenDays = Date::unixTime('day', -6);
$score = $money = $days = []; $score = $money = $days = [];
for ($i = 0; $i < 7; $i++) { for ($i = 0; $i < 7; $i++) {
$days[$i] = date("Y-m-d", $sevenDays + ($i * 86400)); $days[$i] = date("Y-m-d", $sevenDays + ($i * 86400));

View File

@ -3,7 +3,7 @@
namespace ba; namespace ba;
use DateTime; use DateTime;
use Exception; use Throwable;
use DateTimeZone; use DateTimeZone;
use DateTimeInterface; use DateTimeInterface;
@ -23,18 +23,16 @@ class Date
/** /**
* 计算两个时区间相差的时长,单位为秒 * 计算两个时区间相差的时长,单位为秒
* *
* $seconds = self::offset('America/Chicago', 'GMT');
*
* [!!] A list of time zones that PHP supports can be found at * [!!] A list of time zones that PHP supports can be found at
* <http://php.net/timezones>. * <http://php.net/timezones>.
*
* @param string $remote timezone that to find the offset of * @param string $remote timezone that to find the offset of
* @param string|null $local timezone used as the baseline * @param string|null $local timezone used as the baseline
* @param mixed|null $now UNIX timestamp or date string * @param string|int|null $now UNIX timestamp or date string
* @return integer * @return int
* @throws Exception * @throws Throwable
* @example $seconds = self::offset('America/Chicago', 'GMT');
*/ */
public static function offset(string $remote, string $local = null, $now = null): int public static function offset(string $remote, string $local = null, string|int $now = null): int
{ {
if ($local === null) { if ($local === null) {
// Use the default timezone // Use the default timezone
@ -63,10 +61,10 @@ class Date
* @param int $remote timestamp to find the span of * @param int $remote timestamp to find the span of
* @param int|null $local timestamp to use as the baseline * @param int|null $local timestamp to use as the baseline
* @param string $output formatting string * @param string $output formatting string
* @return array|string associative list of all outputs requested|when only a single output is requested * @return bool|array|string associative list of all outputs requested|when only a single output is requested
* @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php * @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php
*/ */
public static function span(int $remote, int $local = null, string $output = 'years,months,weeks,days,hours,minutes,seconds') public static function span(int $remote, int $local = null, string $output = 'years,months,weeks,days,hours,minutes,seconds'): bool|array|string
{ {
// Normalize output // Normalize output
$output = trim(strtolower($output)); $output = trim(strtolower($output));
@ -120,23 +118,24 @@ class Date
* 格式化 UNIX 时间戳为人易读的字符串 * 格式化 UNIX 时间戳为人易读的字符串
* *
* @param int $remote Unix 时间戳 * @param int $remote Unix 时间戳
* @param mixed $local 本地时间 * @param ?int $local 本地时间
*
* @return string 格式化的日期字符串 * @return string 格式化的日期字符串
*/ */
public static function human(int $remote, $local = null): string public static function human(int $remote, ?int $local = null): string
{ {
$timeDiff = (is_null($local) ? time() : $local) - $remote; $timeDiff = (is_null($local) ? time() : $local) - $remote;
$chunks = array( $chunks = [
array(60 * 60 * 24 * 365, 'year'), [60 * 60 * 24 * 365, 'year'],
array(60 * 60 * 24 * 30, 'month'), [60 * 60 * 24 * 30, 'month'],
array(60 * 60 * 24 * 7, 'week'), [60 * 60 * 24 * 7, 'week'],
array(60 * 60 * 24, 'day'), [60 * 60 * 24, 'day'],
array(60 * 60, 'hour'), [60 * 60, 'hour'],
array(60, 'minute'), [60, 'minute'],
array(1, 'second') [1, 'second'],
); ];
$count = 0;
$name = '';
for ($i = 0, $j = count($chunks); $i < $j; $i++) { for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0]; $seconds = $chunks[$i][0];
$name = $chunks[$i][1]; $name = $chunks[$i][1];
@ -160,45 +159,28 @@ class Date
* @param int|null $minute 基准分钟默认为null即以当前分钟为基准 * @param int|null $minute 基准分钟默认为null即以当前分钟为基准
* @return int 处理后的Unix时间戳 * @return int 处理后的Unix时间戳
*/ */
public static function unixtime(string $type = 'day', int $offset = 0, string $position = 'begin', int $year = null, int $month = null, int $day = null, int $hour = null, int $minute = null): int public static function unixTime(string $type = 'day', int $offset = 0, string $position = 'begin', int $year = null, int $month = null, int $day = null, int $hour = null, int $minute = null): int
{ {
$year = is_null($year) ? date('Y') : $year; $year = is_null($year) ? date('Y') : $year;
$month = is_null($month) ? date('m') : $month; $month = is_null($month) ? date('m') : $month;
$day = is_null($day) ? date('d') : $day; $day = is_null($day) ? date('d') : $day;
$hour = is_null($hour) ? date('H') : $hour; $hour = is_null($hour) ? date('H') : $hour;
$minute = is_null($minute) ? date('i') : $minute; $minute = is_null($minute) ? date('i') : $minute;
$position = in_array($position, array('begin', 'start', 'first', 'front')); $position = in_array($position, ['begin', 'start', 'first', 'front']);
switch ($type) { return match ($type) {
case 'minute': 'minute' => $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year),
$time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year); 'hour' => $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year),
break; 'day' => $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year),
case 'hour': 'week' => $position ?
$time = $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year);
break;
case 'day':
$time = $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year);
break;
case 'week':
$time = $position ?
mktime(0, 0, 0, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 1 - 7 * (-$offset), $year) : mktime(0, 0, 0, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 1 - 7 * (-$offset), $year) :
mktime(23, 59, 59, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 7 - 7 * (-$offset), $year); mktime(23, 59, 59, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 7 - 7 * (-$offset), $year),
break; 'month' => $position ? mktime(0, 0, 0, $month + $offset, 1, $year) : mktime(23, 59, 59, $month + $offset, cal_days_in_month(CAL_GREGORIAN, $month + $offset, $year), $year),
case 'month': 'quarter' => $position ?
$time = $position ? mktime(0, 0, 0, $month + $offset, 1, $year) : mktime(23, 59, 59, $month + $offset, cal_days_in_month(CAL_GREGORIAN, $month + $offset, $year), $year);
break;
case 'quarter':
$time = $position ?
mktime(0, 0, 0, 1 + ((ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) - 1) * 3, 1, $year) : mktime(0, 0, 0, 1 + ((ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) - 1) * 3, 1, $year) :
mktime(23, 59, 59, (ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, cal_days_in_month(CAL_GREGORIAN, (ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, $year), $year); mktime(23, 59, 59, (ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, cal_days_in_month(CAL_GREGORIAN, (ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, $year), $year),
break; 'year' => $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset),
case 'year': default => mktime($hour, $minute, 0, $month, $day, $year),
$time = $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset); };
break;
default:
$time = mktime($hour, $minute, 0, $month, $day, $year);
break;
}
return $time;
} }
} }

View File

@ -2,6 +2,7 @@
namespace ba; namespace ba;
use Throwable;
use think\Exception; use think\Exception;
/** /**
@ -9,34 +10,24 @@ use think\Exception;
*/ */
class Depends class Depends
{ {
/**
* 类型
* @value npm | composer
*/
protected $type = '';
/**
* json 文件完整路径
*/
protected $json = null;
/** /**
* json 文件内容 * json 文件内容
* @var array
*/ */
protected $jsonContent = null; protected array $jsonContent = [];
public function __construct(string $json, string $type = 'npm') public function __construct(protected string $json, protected string $type = 'npm')
{ {
$this->json = $json;
$this->type = $type;
} }
/** /**
* 获取 json 文件内容 * 获取 json 文件内容
* @param bool $realTime 获取实时内容 * @param bool $realTime 获取实时内容
* @throws Exception * @return array
* @throws Throwable
*/ */
public function getContent(bool $realTime = false) public function getContent(bool $realTime = false): array
{ {
if (!file_exists($this->json)) { if (!file_exists($this->json)) {
throw new Exception($this->json . ' file does not exist!'); throw new Exception($this->json . ' file does not exist!');
@ -53,9 +44,9 @@ class Depends
/** /**
* 设置 json 文件内容 * 设置 json 文件内容
* @param array $content * @param array $content
* @throws Exception * @throws Throwable
*/ */
public function setContent(array $content = []) public function setContent(array $content = []): void
{ {
if (!$content) $content = $this->jsonContent; if (!$content) $content = $this->jsonContent;
if (!isset($content['name'])) { if (!isset($content['name'])) {
@ -71,13 +62,14 @@ class Depends
/** /**
* 获取依赖项 * 获取依赖项
* @param bool $devEnv 是否是获取开发环境依赖 * @param bool $devEnv 是否是获取开发环境依赖
* @throws Exception * @return array
* @throws Throwable
*/ */
public function getDepends(bool $devEnv = false) public function getDepends(bool $devEnv = false): array
{ {
try { try {
$content = $this->getContent(); $content = $this->getContent();
} catch (Exception $e) { } catch (Throwable) {
return []; return [];
} }
@ -92,9 +84,10 @@ class Depends
* 是否存在某个依赖 * 是否存在某个依赖
* @param string $name 依赖名称 * @param string $name 依赖名称
* @param bool $devEnv 是否是获取开发环境依赖 * @param bool $devEnv 是否是获取开发环境依赖
* @throws Exception * @return bool|string false或者依赖版本号
* @throws Throwable
*/ */
public function hasDepend(string $name, bool $devEnv = false) public function hasDepend(string $name, bool $devEnv = false): bool|string
{ {
$depends = $this->getDepends($devEnv); $depends = $this->getDepends($devEnv);
return $depends[$name] ?? false; return $depends[$name] ?? false;
@ -105,16 +98,17 @@ class Depends
* @param array $depends 要添加的依赖数组["xxx" => ">=7.1.0",] * @param array $depends 要添加的依赖数组["xxx" => ">=7.1.0",]
* @param bool $devEnv 是否添加为开发环境依赖 * @param bool $devEnv 是否添加为开发环境依赖
* @param bool $cover 覆盖模式 * @param bool $cover 覆盖模式
* @throws Exception * @return void
* @throws Throwable
*/ */
public function addDepends(array $depends, bool $devEnv = false, bool $cover = false) public function addDepends(array $depends, bool $devEnv = false, bool $cover = false): void
{ {
$content = $this->getContent(true); $content = $this->getContent(true);
$dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require'); $dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require');
if (!$cover) { if (!$cover) {
foreach ($depends as $key => $item) { foreach ($depends as $key => $item) {
if (isset($content[$dKey][$key])) { if (isset($content[$dKey][$key])) {
throw new Exception($key . ' dependencie already exists!'); throw new Exception($key . ' depend already exists!');
} }
} }
} }
@ -124,11 +118,12 @@ class Depends
/** /**
* 删除依赖 * 删除依赖
* @param array $depends 要删除的依赖数组["php", "w7corp/easywechat"] * @param array $depends 要删除的依赖数组["php", "w7corp/easyWechat"]
* @param bool $devEnv 是否为开发环境删除依赖 * @param bool $devEnv 是否为开发环境删除依赖
* @throws Exception * @return void
* @throws Throwable
*/ */
public function removeDepends(array $depends, bool $devEnv = false) public function removeDepends(array $depends, bool $devEnv = false): void
{ {
$content = $this->getContent(true); $content = $this->getContent(true);
$dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require'); $dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require');

View File

@ -22,7 +22,7 @@ class Random
/** /**
* 随机字符生成 * 随机字符生成
* @param string $type 类型 alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1 * @param string $type 类型 alpha/alnum/numeric/noZero/unique/md5/encrypt/sha1
* @param int $len 长度 * @param int $len 长度
* @return string * @return string
*/ */
@ -32,21 +32,14 @@ class Random
case 'alpha': case 'alpha':
case 'alnum': case 'alnum':
case 'numeric': case 'numeric':
case 'nozero': case 'noZero':
switch ($type) { $pool = match ($type) {
case 'alpha': 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 'alnum' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
break; 'numeric' => '0123456789',
case 'alnum': 'noZero' => '123456789',
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; default => '',
break; };
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
}
return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len); return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
case 'unique': case 'unique':
case 'md5': case 'md5':

View File

@ -11,22 +11,20 @@
namespace ba; namespace ba;
use Throwable;
use think\Response; use think\Response;
use app\admin\library\module\Manage;
use think\facade\Config; use think\facade\Config;
use think\facade\Cookie; use think\facade\Cookie;
use app\admin\library\Auth; use app\admin\library\Auth;
use app\admin\library\module\Manage;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
class Terminal class Terminal
{ {
/** /**
* @var Terminal|null 对象实例 * @var ?Terminal 对象实例
*/ */
protected static Terminal|null $instance = null; protected static ?Terminal $instance = null;
/** /**
* @var string 当前执行的命令 $command key * @var string 当前执行的命令 $command key
@ -39,7 +37,7 @@ class Terminal
protected array $descriptorsPec = []; protected array $descriptorsPec = [];
/** /**
* @var resource|false proc_open 返回的 resource * @var resource|bool proc_open 返回的 resource
*/ */
protected $process = false; protected $process = false;
@ -129,7 +127,7 @@ class Terminal
/** /**
* 获取命令 * 获取命令
* @param string $key 命令key * @param string $key 命令key
* @return array|false * @return array|bool
*/ */
public static function getCommand(string $key): bool|array public static function getCommand(string $key): bool|array
{ {
@ -168,9 +166,7 @@ class Terminal
/** /**
* 执行命令 * 执行命令
* @param bool $authentication 是否鉴权 * @param bool $authentication 是否鉴权
* @throws ModelNotFoundException * @throws Throwable
* @throws DataNotFoundException
* @throws DbException
*/ */
public function exec(bool $authentication = true): void public function exec(bool $authentication = true): void
{ {
@ -222,6 +218,10 @@ class Terminal
$this->outputFlag('exec-completed'); $this->outputFlag('exec-completed');
} }
/**
* 获取执行状态
* @throws Throwable
*/
public function getProcStatus(): bool public function getProcStatus(): bool
{ {
$status = proc_get_status($this->process); $status = proc_get_status($this->process);
@ -289,6 +289,7 @@ class Terminal
/** /**
* 成功后回调 * 成功后回调
* @return bool * @return bool
* @throws Throwable
*/ */
public function successCallback(): bool public function successCallback(): bool
{ {
@ -369,7 +370,7 @@ class Terminal
* 执行一个命令并以字符串的方式返回执行输出 * 执行一个命令并以字符串的方式返回执行输出
* 代替 exec 使用,这样就只需要解除 proc_open 的函数禁用了 * 代替 exec 使用,这样就只需要解除 proc_open 的函数禁用了
* @param $commandKey * @param $commandKey
* @return string | bool * @return string|bool
*/ */
public static function getOutputFromProc($commandKey): bool|string public static function getOutputFromProc($commandKey): bool|string
{ {

View File

@ -8,21 +8,22 @@ namespace ba;
class Tree class Tree
{ {
/** /**
* @var Tree * 实例
* @var ?Tree
*/ */
protected static $instance; protected static ?Tree $instance = null;
/** /**
* 生成树型结构所需修饰符号 * 生成树型结构所需修饰符号
* @var array * @var array
*/ */
public static $icon = array('│', '├', '└'); public static array $icon = array('│', '├', '└');
/** /**
* 子级数据(树枝) * 子级数据(树枝)
* @var array * @var array
*/ */
protected $childrens = []; protected array $children = [];
/** /**
@ -44,7 +45,7 @@ class Tree
* @param array $arr 要改为树状的数组 * @param array $arr 要改为树状的数组
* @param string $field '树枝'字段 * @param string $field '树枝'字段
* @param int $level 递归数组层次,无需手动维护 * @param int $level 递归数组层次,无需手动维护
* @param false $superiorEnd 递归上一级树枝是否结束,无需手动维护 * @param bool $superiorEnd 递归上一级树枝是否结束,无需手动维护
* @return array * @return array
*/ */
public static function getTreeArray(array $arr, string $field = 'name', int $level = 0, bool $superiorEnd = false): array public static function getTreeArray(array $arr, string $field = 'name', int $level = 0, bool $superiorEnd = false): array
@ -100,12 +101,12 @@ class Tree
$pks = []; $pks = [];
$topLevelData = []; // 顶级数据 $topLevelData = []; // 顶级数据
$this->childrens = []; // 置空子级数据 $this->children = []; // 置空子级数据
foreach ($data as $item) { foreach ($data as $item) {
$pks[] = $item[$pk]; $pks[] = $item[$pk];
// 以pid组成children // 以pid组成children
$this->childrens[$item[$pid]][] = $item; $this->children[$item[$pid]][] = $item;
} }
// 上级不存在的就是顶级,只获取它们的 children // 上级不存在的就是顶级,只获取它们的 children
foreach ($data as $item) { foreach ($data as $item) {
@ -114,9 +115,9 @@ class Tree
} }
} }
if (count($this->childrens) > 0) { if (count($this->children) > 0) {
foreach ($topLevelData as $key => $item) { foreach ($topLevelData as $key => $item) {
$topLevelData[$key]['children'] = $this->getChildren($this->childrens[$item[$pk]] ?? [], $pk); $topLevelData[$key]['children'] = $this->getChildren($this->children[$item[$pk]] ?? [], $pk);
} }
return $topLevelData; return $topLevelData;
} else { } else {
@ -135,8 +136,8 @@ class Tree
{ {
if (!$data) return []; if (!$data) return [];
foreach ($data as $key => $item) { foreach ($data as $key => $item) {
if (array_key_exists($item[$pk], $this->childrens)) { if (array_key_exists($item[$pk], $this->children)) {
$data[$key]['children'] = $this->getChildren($this->childrens[$item[$pk]], $pk); $data[$key]['children'] = $this->getChildren($this->children[$item[$pk]], $pk);
} }
} }
return $data; return $data;