mirror of
https://gitee.com/wonderful-code/buildadmin
synced 2024-11-21 14:41:29 +00:00
refactor:完善 extend 中的类的类型定义
This commit is contained in:
parent
45cbabadef
commit
b7eead8321
@ -26,7 +26,7 @@ class Account extends Frontend
|
||||
|
||||
public function overview()
|
||||
{
|
||||
$sevenDays = Date::unixtime('day', -6);
|
||||
$sevenDays = Date::unixTime('day', -6);
|
||||
$score = $money = $days = [];
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
$days[$i] = date("Y-m-d", $sevenDays + ($i * 86400));
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace ba;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Throwable;
|
||||
use DateTimeZone;
|
||||
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
|
||||
* <http://php.net/timezones>.
|
||||
*
|
||||
* @param string $remote timezone that to find the offset of
|
||||
* @param string|null $local timezone used as the baseline
|
||||
* @param mixed|null $now UNIX timestamp or date string
|
||||
* @return integer
|
||||
* @throws Exception
|
||||
* @param string|int|null $now UNIX timestamp or date string
|
||||
* @return int
|
||||
* @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) {
|
||||
// Use the default timezone
|
||||
@ -63,10 +61,10 @@ class Date
|
||||
* @param int $remote timestamp to find the span of
|
||||
* @param int|null $local timestamp to use as the baseline
|
||||
* @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
|
||||
*/
|
||||
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
|
||||
$output = trim(strtolower($output));
|
||||
@ -120,23 +118,24 @@ class Date
|
||||
* 格式化 UNIX 时间戳为人易读的字符串
|
||||
*
|
||||
* @param int $remote Unix 时间戳
|
||||
* @param mixed $local 本地时间
|
||||
*
|
||||
* @param ?int $local 本地时间
|
||||
* @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;
|
||||
$chunks = array(
|
||||
array(60 * 60 * 24 * 365, 'year'),
|
||||
array(60 * 60 * 24 * 30, 'month'),
|
||||
array(60 * 60 * 24 * 7, 'week'),
|
||||
array(60 * 60 * 24, 'day'),
|
||||
array(60 * 60, 'hour'),
|
||||
array(60, 'minute'),
|
||||
array(1, 'second')
|
||||
);
|
||||
$chunks = [
|
||||
[60 * 60 * 24 * 365, 'year'],
|
||||
[60 * 60 * 24 * 30, 'month'],
|
||||
[60 * 60 * 24 * 7, 'week'],
|
||||
[60 * 60 * 24, 'day'],
|
||||
[60 * 60, 'hour'],
|
||||
[60, 'minute'],
|
||||
[1, 'second'],
|
||||
];
|
||||
|
||||
$count = 0;
|
||||
$name = '';
|
||||
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
|
||||
$seconds = $chunks[$i][0];
|
||||
$name = $chunks[$i][1];
|
||||
@ -160,45 +159,28 @@ class Date
|
||||
* @param int|null $minute 基准分钟,默认为null,即以当前分钟为基准
|
||||
* @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;
|
||||
$month = is_null($month) ? date('m') : $month;
|
||||
$day = is_null($day) ? date('d') : $day;
|
||||
$hour = is_null($hour) ? date('H') : $hour;
|
||||
$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) {
|
||||
case 'minute':
|
||||
$time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year);
|
||||
break;
|
||||
case 'hour':
|
||||
$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 ?
|
||||
return match ($type) {
|
||||
'minute' => $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),
|
||||
'day' => $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year),
|
||||
'week' => $position ?
|
||||
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);
|
||||
break;
|
||||
case 'month':
|
||||
$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(23, 59, 59, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 7 - 7 * (-$offset), $year),
|
||||
'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),
|
||||
'quarter' => $position ?
|
||||
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);
|
||||
break;
|
||||
case '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;
|
||||
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),
|
||||
'year' => $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset),
|
||||
default => mktime($hour, $minute, 0, $month, $day, $year),
|
||||
};
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace ba;
|
||||
|
||||
use Throwable;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
@ -9,34 +10,24 @@ use think\Exception;
|
||||
*/
|
||||
class Depends
|
||||
{
|
||||
/**
|
||||
* 类型
|
||||
* @value npm | composer
|
||||
*/
|
||||
protected $type = '';
|
||||
|
||||
/**
|
||||
* json 文件完整路径
|
||||
*/
|
||||
protected $json = null;
|
||||
|
||||
/**
|
||||
* 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 文件内容
|
||||
* @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)) {
|
||||
throw new Exception($this->json . ' file does not exist!');
|
||||
@ -53,9 +44,9 @@ class Depends
|
||||
/**
|
||||
* 设置 json 文件内容
|
||||
* @param array $content
|
||||
* @throws Exception
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function setContent(array $content = [])
|
||||
public function setContent(array $content = []): void
|
||||
{
|
||||
if (!$content) $content = $this->jsonContent;
|
||||
if (!isset($content['name'])) {
|
||||
@ -71,13 +62,14 @@ class Depends
|
||||
/**
|
||||
* 获取依赖项
|
||||
* @param bool $devEnv 是否是获取开发环境依赖
|
||||
* @throws Exception
|
||||
* @return array
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getDepends(bool $devEnv = false)
|
||||
public function getDepends(bool $devEnv = false): array
|
||||
{
|
||||
try {
|
||||
$content = $this->getContent();
|
||||
} catch (Exception $e) {
|
||||
} catch (Throwable) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@ -92,9 +84,10 @@ class Depends
|
||||
* 是否存在某个依赖
|
||||
* @param string $name 依赖名称
|
||||
* @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);
|
||||
return $depends[$name] ?? false;
|
||||
@ -105,16 +98,17 @@ class Depends
|
||||
* @param array $depends 要添加的依赖数组["xxx" => ">=7.1.0",]
|
||||
* @param bool $devEnv 是否添加为开发环境依赖
|
||||
* @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);
|
||||
$dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require');
|
||||
if (!$cover) {
|
||||
foreach ($depends as $key => $item) {
|
||||
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 是否为开发环境删除依赖
|
||||
* @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);
|
||||
$dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require');
|
||||
|
@ -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 长度
|
||||
* @return string
|
||||
*/
|
||||
@ -32,21 +32,14 @@ class Random
|
||||
case 'alpha':
|
||||
case 'alnum':
|
||||
case 'numeric':
|
||||
case 'nozero':
|
||||
switch ($type) {
|
||||
case 'alpha':
|
||||
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
break;
|
||||
case 'alnum':
|
||||
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
break;
|
||||
case 'numeric':
|
||||
$pool = '0123456789';
|
||||
break;
|
||||
case 'nozero':
|
||||
$pool = '123456789';
|
||||
break;
|
||||
}
|
||||
case 'noZero':
|
||||
$pool = match ($type) {
|
||||
'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
||||
'alnum' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
||||
'numeric' => '0123456789',
|
||||
'noZero' => '123456789',
|
||||
default => '',
|
||||
};
|
||||
return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
|
||||
case 'unique':
|
||||
case 'md5':
|
||||
|
@ -11,22 +11,20 @@
|
||||
|
||||
namespace ba;
|
||||
|
||||
use Throwable;
|
||||
use think\Response;
|
||||
use app\admin\library\module\Manage;
|
||||
use think\facade\Config;
|
||||
use think\facade\Cookie;
|
||||
use app\admin\library\Auth;
|
||||
use app\admin\library\module\Manage;
|
||||
use think\exception\HttpResponseException;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
|
||||
class Terminal
|
||||
{
|
||||
/**
|
||||
* @var Terminal|null 对象实例
|
||||
* @var ?Terminal 对象实例
|
||||
*/
|
||||
protected static Terminal|null $instance = null;
|
||||
protected static ?Terminal $instance = null;
|
||||
|
||||
/**
|
||||
* @var string 当前执行的命令 $command 的 key
|
||||
@ -39,7 +37,7 @@ class Terminal
|
||||
protected array $descriptorsPec = [];
|
||||
|
||||
/**
|
||||
* @var resource|false proc_open 返回的 resource
|
||||
* @var resource|bool proc_open 返回的 resource
|
||||
*/
|
||||
protected $process = false;
|
||||
|
||||
@ -129,7 +127,7 @@ class Terminal
|
||||
/**
|
||||
* 获取命令
|
||||
* @param string $key 命令key
|
||||
* @return array|false
|
||||
* @return array|bool
|
||||
*/
|
||||
public static function getCommand(string $key): bool|array
|
||||
{
|
||||
@ -168,9 +166,7 @@ class Terminal
|
||||
/**
|
||||
* 执行命令
|
||||
* @param bool $authentication 是否鉴权
|
||||
* @throws ModelNotFoundException
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function exec(bool $authentication = true): void
|
||||
{
|
||||
@ -222,6 +218,10 @@ class Terminal
|
||||
$this->outputFlag('exec-completed');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取执行状态
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getProcStatus(): bool
|
||||
{
|
||||
$status = proc_get_status($this->process);
|
||||
@ -289,6 +289,7 @@ class Terminal
|
||||
/**
|
||||
* 成功后回调
|
||||
* @return bool
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function successCallback(): bool
|
||||
{
|
||||
|
@ -8,21 +8,22 @@ namespace ba;
|
||||
class Tree
|
||||
{
|
||||
/**
|
||||
* @var Tree
|
||||
* 实例
|
||||
* @var ?Tree
|
||||
*/
|
||||
protected static $instance;
|
||||
protected static ?Tree $instance = null;
|
||||
|
||||
/**
|
||||
* 生成树型结构所需修饰符号
|
||||
* @var array
|
||||
*/
|
||||
public static $icon = array('│', '├', '└');
|
||||
public static array $icon = array('│', '├', '└');
|
||||
|
||||
/**
|
||||
* 子级数据(树枝)
|
||||
* @var array
|
||||
*/
|
||||
protected $childrens = [];
|
||||
protected array $children = [];
|
||||
|
||||
|
||||
/**
|
||||
@ -44,7 +45,7 @@ class Tree
|
||||
* @param array $arr 要改为树状的数组
|
||||
* @param string $field '树枝'字段
|
||||
* @param int $level 递归数组层次,无需手动维护
|
||||
* @param false $superiorEnd 递归上一级树枝是否结束,无需手动维护
|
||||
* @param bool $superiorEnd 递归上一级树枝是否结束,无需手动维护
|
||||
* @return array
|
||||
*/
|
||||
public static function getTreeArray(array $arr, string $field = 'name', int $level = 0, bool $superiorEnd = false): array
|
||||
@ -100,12 +101,12 @@ class Tree
|
||||
|
||||
$pks = [];
|
||||
$topLevelData = []; // 顶级数据
|
||||
$this->childrens = []; // 置空子级数据
|
||||
$this->children = []; // 置空子级数据
|
||||
foreach ($data as $item) {
|
||||
$pks[] = $item[$pk];
|
||||
|
||||
// 以pid组成children
|
||||
$this->childrens[$item[$pid]][] = $item;
|
||||
$this->children[$item[$pid]][] = $item;
|
||||
}
|
||||
// 上级不存在的就是顶级,只获取它们的 children
|
||||
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) {
|
||||
$topLevelData[$key]['children'] = $this->getChildren($this->childrens[$item[$pk]] ?? [], $pk);
|
||||
$topLevelData[$key]['children'] = $this->getChildren($this->children[$item[$pk]] ?? [], $pk);
|
||||
}
|
||||
return $topLevelData;
|
||||
} else {
|
||||
@ -135,8 +136,8 @@ class Tree
|
||||
{
|
||||
if (!$data) return [];
|
||||
foreach ($data as $key => $item) {
|
||||
if (array_key_exists($item[$pk], $this->childrens)) {
|
||||
$data[$key]['children'] = $this->getChildren($this->childrens[$item[$pk]], $pk);
|
||||
if (array_key_exists($item[$pk], $this->children)) {
|
||||
$data[$key]['children'] = $this->getChildren($this->children[$item[$pk]], $pk);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
|
Loading…
Reference in New Issue
Block a user