From b3089c1bba3c8448efbde275fa8473f49f7eb817 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Mon, 21 Mar 2022 01:24:07 +0800 Subject: [PATCH] add composer module support (build 449, 2.7.2) --- src/ZM/Annotation/AnnotationParser.php | 4 ++ src/ZM/Command/Module/ModuleListCommand.php | 19 +++++++ src/ZM/ConsoleApplication.php | 4 +- src/ZM/Event/SwooleEvent/OnWorkerStart.php | 12 +++- src/ZM/Utils/Manager/ModuleManager.php | 62 +++++++++++++++++++++ 5 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index b6162f5c..1a00bab5 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -24,6 +24,7 @@ use ZM\Console\Console; use ZM\Exception\AnnotationException; use ZM\Utils\Manager\RouteManager; use ZM\Utils\ZMUtil; +use function server; class AnnotationParser { @@ -211,6 +212,9 @@ class AnnotationParser */ public function addRegisterPath($path, $indoor_name) { + if (server()->worker_id === 0) { + Console::verbose('Add register path: ' . $path . ' => ' . $indoor_name); + } $this->path_list[] = [$path, $indoor_name]; } diff --git a/src/ZM/Command/Module/ModuleListCommand.php b/src/ZM/Command/Module/ModuleListCommand.php index 4b3976e4..8c736f3f 100644 --- a/src/ZM/Command/Module/ModuleListCommand.php +++ b/src/ZM/Command/Module/ModuleListCommand.php @@ -79,6 +79,25 @@ class ModuleListCommand extends Command if ($list === []) { echo Console::setColor('没有发现已打包且装载的模块!', 'yellow') . PHP_EOL; } + + $list = ModuleManager::getComposerModules(); + foreach ($list as $v) { + echo '[' . Console::setColor($v['name'], 'blue') . ']' . PHP_EOL; + $out_list = ['类型' => 'Composer库(composer)']; + $out_list['包名'] = $v['composer-name']; + $out_list['目录'] = str_replace(DataProvider::getSourceRootDir() . '/', '', $v['module-path']); + if (isset($v['version'])) { + $out_list['版本'] = $v['version']; + } + if (isset($v['description'])) { + $out_list['描述'] = $v['description']; + } + $out_list['命名空间'] = $v['namespace']; + $this->printList($out_list); + } + if ($list === []) { + echo Console::setColor('没有发现Composer模块!', 'yellow') . PHP_EOL; + } return 0; } diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 2aa1636a..458b4acf 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -28,9 +28,9 @@ use ZM\Exception\InitException; class ConsoleApplication extends Application { - public const VERSION_ID = 448; + public const VERSION_ID = 449; - public const VERSION = '2.7.1'; + public const VERSION = '2.7.2'; private static $obj; diff --git a/src/ZM/Event/SwooleEvent/OnWorkerStart.php b/src/ZM/Event/SwooleEvent/OnWorkerStart.php index 41f29eb9..b27730d9 100644 --- a/src/ZM/Event/SwooleEvent/OnWorkerStart.php +++ b/src/ZM/Event/SwooleEvent/OnWorkerStart.php @@ -165,9 +165,6 @@ class OnWorkerStart implements SwooleEvent if (trim($k, '\\') == 'ZM') { continue; } - if (\server()->worker_id == 0) { - Console::verbose('Add ' . $v . ":{$k} to register path"); - } $parser->addRegisterPath(DataProvider::getSourceRootDir() . '/' . $v . '/', trim($k, '\\')); } } @@ -187,6 +184,15 @@ class OnWorkerStart implements SwooleEvent } } + // 检查所有的Composer模块,并加载注解 + $list = ModuleManager::getComposerModules(); + foreach ($list as $k => $v) { + if (\server()->worker_id === 0) { + Console::info('Loading composer module: ' . $k); + } + $parser->addRegisterPath($v['module-path'], $v['namespace']); + } + $parser->registerMods(); EventManager::loadEventByParser($parser); // 加载事件 diff --git a/src/ZM/Utils/Manager/ModuleManager.php b/src/ZM/Utils/Manager/ModuleManager.php index 8a63d88f..386ea197 100644 --- a/src/ZM/Utils/Manager/ModuleManager.php +++ b/src/ZM/Utils/Manager/ModuleManager.php @@ -111,6 +111,34 @@ class ModuleManager return $modules; } + public static function getComposerModules() + { + $vendor_file = DataProvider::getSourceRootDir() . '/vendor/composer/installed.json'; + $obj = json_decode(file_get_contents($vendor_file), true); + if ($obj === null) { + return []; + } + $modules = []; + foreach ($obj['packages'] as $v) { + if (isset($v['extra']['zm']['module-path'])) { + if (is_array($v['extra']['zm']['module-path'])) { + foreach ($v['extra']['zm']['module-path'] as $module_path) { + $m = self::getComposerModuleInfo($v, $module_path); + if ($m !== null) { + $modules[$m['name']] = $m; + } + } + } elseif (is_string($v['extra']['zm']['module-path'])) { + $m = self::getComposerModuleInfo($v, $v['extra']['zm']['module-path']); + if ($m !== null) { + $modules[$m['name']] = $m; + } + } + } + } + return $modules; + } + /** * 打包模块 * @param $module @@ -152,4 +180,38 @@ class ModuleManager return false; } } + + private static function getComposerModuleInfo($v, $module_path) + { + $module_root_path = realpath(DataProvider::getSourceRootDir() . '/vendor/composer/' . $v['install-path'] . '/' . $module_path); + if ($module_root_path === false) { + Console::warning(zm_internal_errcode('E00055') . '无法找到Composer发布的插件配置路径在包 `' . $v['name'] . '` 中!'); + return null; + } + $json = json_decode(file_get_contents($module_root_path . '/zm.json'), true); + if ($json === null) { + Console::warning(zm_internal_errcode('E00054') . 'Composer包内无法正常读取 ' . $v['name'] . ' 的内的配置文件(zm.json)!'); + return null; + } + if (!isset($json['name'])) { + return null; + } + $json['composer-name'] = $v['name']; + $json['module-root-path'] = realpath(DataProvider::getSourceRootDir() . '/vendor/composer/' . $v['install-path']); + $json['module-path'] = realpath($json['module-root-path'] . '/' . $module_path); + if (isset($v['autoload']['psr-4'])) { + foreach ($v['autoload']['psr-4'] as $ks => $vs) { + $vs = trim($vs, '/'); + if (strpos($module_path, $vs) === 0) { + $json['namespace'] = trim($ks . str_replace('/', '\\', trim(substr($module_path, strlen($vs)), '/')), '\\'); + break; + } + } + } + if (!isset($json['namespace'])) { + Console::warning(zm_internal_errcode('E00055') . '无法获取Composer发布的模块命名空间!'); + return null; + } + return $json; + } }