Files
zhamao-framework/src/ZM/Utils/ZMUtil.php

132 lines
4.6 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
declare(strict_types=1);
2020-03-02 16:14:20 +08:00
namespace ZM\Utils;
2020-09-29 15:07:43 +08:00
use Exception;
use Swoole\Process;
2020-08-31 10:11:06 +08:00
use ZM\Console\Console;
use ZM\Framework;
use ZM\Store\Lock\SpinLock;
2020-11-03 21:02:24 +08:00
use ZM\Store\ZMAtomic;
2020-08-31 10:11:06 +08:00
use ZM\Store\ZMBuf;
2022-08-03 16:37:26 +08:00
2020-03-02 16:14:20 +08:00
class ZMUtil
{
/**
2022-03-29 02:11:14 +08:00
* @param mixed $error_exit
* @throws Exception
*/
2022-03-29 02:11:14 +08:00
public static function stop($error_exit = false)
{
if (SpinLock::tryLock('_stop_signal') === false) {
return;
}
2021-06-16 00:17:30 +08:00
Console::warning(Console::setColor('Stopping server...', 'red'));
if (Console::getLevel() >= 4) {
Console::trace();
}
2022-03-29 02:11:14 +08:00
ZMAtomic::get('stop_signal')->set($error_exit ? 2 : 1);
2020-09-29 15:07:43 +08:00
server()->shutdown();
2020-03-02 16:14:20 +08:00
}
/**
* @throws Exception
*/
public static function reload()
{
Process::kill(server()->master_pid, SIGUSR1);
2020-03-02 16:14:20 +08:00
}
public static function getModInstance($class)
{
if (!isset(ZMBuf::$instance[$class])) {
// Console::debug('Class instance $class not exist, so I created it.');
2020-08-31 10:11:06 +08:00
return ZMBuf::$instance[$class] = new $class();
}
return ZMBuf::$instance[$class];
}
/**
* 在工作进程中返回可以通过reload重新加载的php文件列表
* @return string[]|string[][]
*/
public static function getReloadableFiles(): array
{
2021-09-01 14:14:00 +08:00
$array_map = [];
foreach (array_diff(
get_included_files(),
Framework::$loaded_files
) as $key => $x) {
2021-09-01 14:14:00 +08:00
$array_map[$key] = str_replace(DataProvider::getSourceRootDir() . '/', '', $x);
}
return $array_map;
}
2021-06-16 00:17:30 +08:00
/**
* 使用Psr-4标准获取目录下的所有类
2022-04-03 01:47:38 +08:00
* @param string $dir 目录
* @param string $base_namespace 基础命名空间
* @param null|mixed $rule 规则
* @param bool|string $return_path_value 是否返回文件路径,返回文件路径的话传入字符串
* @return string[]
2021-06-16 00:17:30 +08:00
*/
2022-04-03 01:47:38 +08:00
public static function getClassesPsr4(string $dir, string $base_namespace, $rule = null, $return_path_value = false): array
{
2021-06-16 00:17:30 +08:00
// 预先读取下composer的file列表
$composer = json_decode(file_get_contents(DataProvider::getSourceRootDir() . '/composer.json'), true);
$classes = [];
// 扫描目录使用递归模式相对路径模式因为下面此路径要用作转换成namespace
$files = DataProvider::scanDirFiles($dir, true, true);
foreach ($files as $v) {
$pathinfo = pathinfo($v);
2021-09-01 14:14:00 +08:00
if (($pathinfo['extension'] ?? '') == 'php') {
2022-03-29 00:46:17 +08:00
$path = rtrim($dir, '/') . '/' . rtrim($pathinfo['dirname'], './') . '/' . $pathinfo['basename'];
// 过滤不包含类的文件
$tokens = token_get_all(file_get_contents($path));
$found = false;
foreach ($tokens as $token) {
if (!is_array($token)) {
continue;
}
if ($token[0] === T_CLASS) {
$found = true;
}
}
if (!$found) {
continue;
}
if ($rule === null) { // 规则未设置回调时候,使用默认的识别过滤规则
/*if (substr(file_get_contents($dir . '/' . $v), 6, 6) == '#plain') {
continue;
}*/
if (file_exists($dir . '/' . $pathinfo['basename'] . '.plain')) {
continue;
2021-06-16 00:17:30 +08:00
}
if (mb_substr($pathinfo['basename'], 0, 7) == 'global_' || mb_substr($pathinfo['basename'], 0, 7) == 'script_') {
continue;
}
foreach (($composer['autoload']['files'] ?? []) as $fi) {
if (md5_file(DataProvider::getSourceRootDir() . '/' . $fi) == md5_file($dir . '/' . $v)) {
continue 2;
}
}
2022-08-03 16:37:26 +08:00
} elseif (is_callable($rule) && !$rule($dir, $pathinfo)) {
continue;
}
2021-06-16 00:17:30 +08:00
$dirname = $pathinfo['dirname'] == '.' ? '' : (str_replace('/', '\\', $pathinfo['dirname']) . '\\');
$class_name = $base_namespace . '\\' . $dirname . $pathinfo['filename'];
if (is_string($return_path_value)) {
$classes[$class_name] = $return_path_value . '/' . $v;
} else {
$classes[] = $class_name;
}
2021-06-16 00:17:30 +08:00
}
}
return $classes;
}
}