作者 pengjch

dev: swrpc客户端代码生成提交

... ... @@ -20,7 +20,7 @@ class SwrpcGenCom extends Command
*
* @var string
*/
protected $signature = 'swrpc:gen {-m|--module=}';
protected $signature = 'swrpc:gen {-m|--module=} {-s|--service=}';
/**
* The console command description.
... ... @@ -55,20 +55,16 @@ class SwrpcGenCom extends Command
$filters = [];
$specifyModules = $this->option('module') ? explode(',', $this->option('module')) : [];
$modules = Module::all();
print_r($modules);exit();
/** @var \Nwidart\Modules\Laravel\Module $module */
foreach ($modules as $module) {
if ($module->getName() == 'Common') {
continue;
}
$loader = require base_path('vendor').'/autoload.php';
// 获取注册的命名空间前缀并筛选出 Composer 包的命名空间
$prefixes = $loader->getPrefixesPsr4();
foreach ($prefixes as $key => $prefix) {
//如果有指定moudle,则只有指定的moudle会生成,否则会生成全部
if (count($specifyModules) > 0 && !in_array($module->getName(), $specifyModules)) {
if (count($specifyModules) > 0 && !in_array($key, $specifyModules)) {
continue;
}
$moduleName = $module->getName();
$modulePath = str_replace('_', '/', $moduleName);
$moduleName = $key;
$modulePath = str_replace('\\', '/', $moduleName);
//create module dir
$basePath = base_path('app/Clients/' . $modulePath);
... ... @@ -77,10 +73,9 @@ class SwrpcGenCom extends Command
}
//target service namespace
$namespace = ltrim(str_replace([base_path(), '/'], ['', '\\'], $module->getPath()), '\\') . '\\Services';
$namespace = $key . 'Http\Service';
//fetch file
$servicePath = $module->getPath() . '/Services';
$servicePath = $prefix[0] . '/Http/Service';
if (!is_dir($servicePath)) {
continue;
}
... ... @@ -111,7 +106,7 @@ class SwrpcGenCom extends Command
$proxyPath = $basePath . '/' . $file;
ob_start();
ob_implicit_flush(false);
include(base_path('app/Clients/client.template.php'));
include(__DIR__.'/client.template.php');
$content = ob_get_clean();
if (!file_put_contents($proxyPath, $content)) {
$this->error($proxyPath . ' import failure.');
... ... @@ -167,7 +162,7 @@ class SwrpcGenCom extends Command
$serviceObj = $rc->newInstance();
if (!($serviceObj instanceof LogicService)) {
$this->warn('没有继承\Swrpc\LogicService类,跳过'.get_class($serviceObj));
$this->warn('没有继承\AukeySwrpc\LogicService类,跳过'.get_class($serviceObj));
return [];
}
... ...
<?php
/** @var $class */
/** @var $funcs */
/** @var $serviceKey */
/** @var $modulePath */
$serviceKey = env('SWRPC_SERVER_NAME');
?>
<?php echo "<?php \n\n"; ?>
namespace App\Clients\<?=rtrim(str_replace('/','\\',$modulePath), '\\')?>;
use AukeySwrpc\BaseService;
/**
* 不要直接修改这个类的代码,用命令行去执行生成
* php artisan swrpc:gen
*
* Class <?=$class;?> <?php echo "\n"?>
<?php foreach ($funcs as $func) { ?>
* @method <?= $func ?>
<?php } ?>
*
* @package App\Clients
* @author pengjch <?=date('Y-m-d H:i:s')?>
*/
class <?=$class;?> extends BaseService
{
/**
* @return <?=$class.PHP_EOL;?>
* @author pengjch
*/
public static function factory(): <?=$class.PHP_EOL;?>
{
return new static();
}
/**
* @author pengjch
*/
public function init()
{
$this->setService('<?=$serviceKey?>');
}
}
... ...