2020-03-02 16:14:20 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2021-09-01 14:14:00 +08:00
|
|
|
|
use function file_get_contents;
|
|
|
|
|
|
use function get_included_files;
|
|
|
|
|
|
use function is_callable;
|
|
|
|
|
|
use function is_string;
|
|
|
|
|
|
use function json_decode;
|
|
|
|
|
|
use function mb_substr;
|
|
|
|
|
|
use function md5_file;
|
|
|
|
|
|
use function pathinfo;
|
|
|
|
|
|
use function server;
|
|
|
|
|
|
use function str_replace;
|
|
|
|
|
|
use function substr;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
class ZMUtil
|
|
|
|
|
|
{
|
2021-03-18 14:56:35 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2020-09-29 15:07:43 +08:00
|
|
|
|
public static function stop() {
|
2021-06-16 00:17:30 +08:00
|
|
|
|
if (SpinLock::tryLock('_stop_signal') === false) return;
|
|
|
|
|
|
Console::warning(Console::setColor('Stopping server...', 'red'));
|
2021-03-08 00:48:51 +08:00
|
|
|
|
if (Console::getLevel() >= 4) Console::trace();
|
2021-06-16 00:17:30 +08:00
|
|
|
|
ZMAtomic::get('stop_signal')->set(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
|
|
|
|
|
|
*/
|
2021-03-24 23:34:46 +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
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
|
public static function getModInstance($class) {
|
|
|
|
|
|
if (!isset(ZMBuf::$instance[$class])) {
|
2021-06-16 00:17:30 +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
|
|
|
|
} else {
|
|
|
|
|
|
return ZMBuf::$instance[$class];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-01-29 22:27:10 +08:00
|
|
|
|
|
2021-03-24 23:34:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在工作进程中返回可以通过reload重新加载的php文件列表
|
|
|
|
|
|
* @return string[]|string[][]
|
|
|
|
|
|
*/
|
2021-09-01 14:14:00 +08:00
|
|
|
|
public static function getReloadableFiles(): array {
|
|
|
|
|
|
$array_map = [];
|
|
|
|
|
|
foreach (array_diff(
|
|
|
|
|
|
get_included_files(),
|
|
|
|
|
|
Framework::$loaded_files
|
|
|
|
|
|
) as $key => $x) {
|
|
|
|
|
|
$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标准获取目录下的所有类
|
|
|
|
|
|
* @param $dir
|
|
|
|
|
|
* @param $base_namespace
|
|
|
|
|
|
* @param null|mixed $rule
|
2021-09-01 14:14:00 +08:00
|
|
|
|
* @param bool $return_path_value
|
2021-06-16 00:17:30 +08:00
|
|
|
|
* @return String[]
|
|
|
|
|
|
*/
|
2021-09-01 14:14:00 +08:00
|
|
|
|
public static function getClassesPsr4($dir, $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') {
|
2021-06-16 00:17:30 +08:00
|
|
|
|
if ($rule === null) { //规则未设置回调时候,使用默认的识别过滤规则
|
|
|
|
|
|
if (substr(file_get_contents($dir . '/' . $v), 6, 6) == '#plain') continue;
|
2021-10-06 18:01:40 +08:00
|
|
|
|
elseif (mb_substr($pathinfo["basename"], 0, 7) == 'global_' || mb_substr($pathinfo["basename"], 0, 7) == 'script_') continue;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
foreach (($composer['autoload']['files'] ?? []) as $fi) {
|
|
|
|
|
|
if (md5_file(DataProvider::getSourceRootDir().'/'.$fi) == md5_file($dir.'/'.$v)) continue 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
} elseif (is_callable($rule) && !($rule($dir, $pathinfo))) continue;
|
|
|
|
|
|
$dirname = $pathinfo['dirname'] == '.' ? '' : (str_replace('/', '\\', $pathinfo['dirname']) . '\\');
|
|
|
|
|
|
$class_name = $base_namespace . '\\' . $dirname . $pathinfo['filename'];
|
2021-09-01 14:14:00 +08:00
|
|
|
|
if (is_string($return_path_value)) $classes[$class_name] = $return_path_value . "/" .$v;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
else $classes[] = $class_name;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return $classes;
|
|
|
|
|
|
}
|
2020-04-29 15:29:56 +08:00
|
|
|
|
}
|