2020-03-02 16:14:20 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use Swoole\Process;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Console\Console;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Framework;
|
2021-03-06 17:22:42 +08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2021-03-18 14:56:35 +08:00
|
|
|
|
/**
|
2022-03-29 02:11:14 +08:00
|
|
|
|
* @param mixed $error_exit
|
2021-03-18 14:56:35 +08:00
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2022-03-29 02:11:14 +08:00
|
|
|
|
public static function stop($error_exit = false)
|
2022-03-15 18:05:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (SpinLock::tryLock('_stop_signal') === false) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-06-16 00:17:30 +08:00
|
|
|
|
Console::warning(Console::setColor('Stopping server...', 'red'));
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-18 14:56:35 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public static function reload()
|
|
|
|
|
|
{
|
2021-03-29 15:34:24 +08:00
|
|
|
|
Process::kill(server()->master_pid, SIGUSR1);
|
2020-03-02 16:14:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public static function getModInstance($class)
|
|
|
|
|
|
{
|
2020-04-29 15:29:56 +08:00
|
|
|
|
if (!isset(ZMBuf::$instance[$class])) {
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// 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();
|
2020-04-29 15:29:56 +08:00
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return ZMBuf::$instance[$class];
|
2020-04-29 15:29:56 +08:00
|
|
|
|
}
|
2021-01-29 22:27:10 +08:00
|
|
|
|
|
2021-03-24 23:34:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在工作进程中返回可以通过reload重新加载的php文件列表
|
|
|
|
|
|
* @return string[]|string[][]
|
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public static function getReloadableFiles(): array
|
|
|
|
|
|
{
|
2021-09-01 14:14:00 +08:00
|
|
|
|
$array_map = [];
|
|
|
|
|
|
foreach (array_diff(
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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-03-24 23:34:46 +08:00
|
|
|
|
}
|
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 是否返回文件路径,返回文件路径的话传入字符串
|
2022-03-15 18:05:33 +08:00
|
|
|
|
* @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
|
2022-03-15 18:05:33 +08:00
|
|
|
|
{
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-20 16:23:07 +08:00
|
|
|
|
if ($rule === null) { // 规则未设置回调时候,使用默认的识别过滤规则
|
2022-03-15 18:05:33 +08:00
|
|
|
|
/*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
|
|
|
|
}
|
2022-03-15 18:05:33 +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)) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2021-06-16 00:17:30 +08:00
|
|
|
|
$dirname = $pathinfo['dirname'] == '.' ? '' : (str_replace('/', '\\', $pathinfo['dirname']) . '\\');
|
|
|
|
|
|
$class_name = $base_namespace . '\\' . $dirname . $pathinfo['filename'];
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2020-04-29 15:29:56 +08:00
|
|
|
|
}
|