add composer module support (build 449, 2.7.2)

This commit is contained in:
crazywhalecc 2022-03-21 01:24:07 +08:00
parent c5a6f1fea4
commit b3089c1bba
5 changed files with 96 additions and 5 deletions

View File

@ -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];
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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); // 加载事件

View File

@ -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;
}
}