作者 pengjch

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

@@ -20,7 +20,7 @@ class SwrpcGenCom extends Command @@ -20,7 +20,7 @@ class SwrpcGenCom extends Command
20 * 20 *
21 * @var string 21 * @var string
22 */ 22 */
23 - protected $signature = 'swrpc:gen {-m|--module=}'; 23 + protected $signature = 'swrpc:gen {-m|--module=} {-s|--service=}';
24 24
25 /** 25 /**
26 * The console command description. 26 * The console command description.
@@ -55,20 +55,16 @@ class SwrpcGenCom extends Command @@ -55,20 +55,16 @@ class SwrpcGenCom extends Command
55 55
56 $filters = []; 56 $filters = [];
57 $specifyModules = $this->option('module') ? explode(',', $this->option('module')) : []; 57 $specifyModules = $this->option('module') ? explode(',', $this->option('module')) : [];
58 - $modules = Module::all();  
59 - print_r($modules);exit();  
60 - /** @var \Nwidart\Modules\Laravel\Module $module */  
61 - foreach ($modules as $module) {  
62 - if ($module->getName() == 'Common') {  
63 - continue;  
64 - } 58 + $loader = require base_path('vendor').'/autoload.php';
  59 +// 获取注册的命名空间前缀并筛选出 Composer 包的命名空间
  60 + $prefixes = $loader->getPrefixesPsr4();
  61 + foreach ($prefixes as $key => $prefix) {
65 //如果有指定moudle,则只有指定的moudle会生成,否则会生成全部 62 //如果有指定moudle,则只有指定的moudle会生成,否则会生成全部
66 - if (count($specifyModules) > 0 && !in_array($module->getName(), $specifyModules)) { 63 + if (count($specifyModules) > 0 && !in_array($key, $specifyModules)) {
67 continue; 64 continue;
68 } 65 }
69 -  
70 - $moduleName = $module->getName();  
71 - $modulePath = str_replace('_', '/', $moduleName); 66 + $moduleName = $key;
  67 + $modulePath = str_replace('\\', '/', $moduleName);
72 68
73 //create module dir 69 //create module dir
74 $basePath = base_path('app/Clients/' . $modulePath); 70 $basePath = base_path('app/Clients/' . $modulePath);
@@ -77,10 +73,9 @@ class SwrpcGenCom extends Command @@ -77,10 +73,9 @@ class SwrpcGenCom extends Command
77 } 73 }
78 74
79 //target service namespace 75 //target service namespace
80 - $namespace = ltrim(str_replace([base_path(), '/'], ['', '\\'], $module->getPath()), '\\') . '\\Services';  
81 - 76 + $namespace = $key . 'Http\Service';
82 //fetch file 77 //fetch file
83 - $servicePath = $module->getPath() . '/Services'; 78 + $servicePath = $prefix[0] . '/Http/Service';
84 if (!is_dir($servicePath)) { 79 if (!is_dir($servicePath)) {
85 continue; 80 continue;
86 } 81 }
@@ -111,7 +106,7 @@ class SwrpcGenCom extends Command @@ -111,7 +106,7 @@ class SwrpcGenCom extends Command
111 $proxyPath = $basePath . '/' . $file; 106 $proxyPath = $basePath . '/' . $file;
112 ob_start(); 107 ob_start();
113 ob_implicit_flush(false); 108 ob_implicit_flush(false);
114 - include(base_path('app/Clients/client.template.php')); 109 + include(__DIR__.'/client.template.php');
115 $content = ob_get_clean(); 110 $content = ob_get_clean();
116 if (!file_put_contents($proxyPath, $content)) { 111 if (!file_put_contents($proxyPath, $content)) {
117 $this->error($proxyPath . ' import failure.'); 112 $this->error($proxyPath . ' import failure.');
@@ -167,7 +162,7 @@ class SwrpcGenCom extends Command @@ -167,7 +162,7 @@ class SwrpcGenCom extends Command
167 162
168 $serviceObj = $rc->newInstance(); 163 $serviceObj = $rc->newInstance();
169 if (!($serviceObj instanceof LogicService)) { 164 if (!($serviceObj instanceof LogicService)) {
170 - $this->warn('没有继承\Swrpc\LogicService类,跳过'.get_class($serviceObj)); 165 + $this->warn('没有继承\AukeySwrpc\LogicService类,跳过'.get_class($serviceObj));
171 return []; 166 return [];
172 } 167 }
173 168
  1 +<?php
  2 +/** @var $class */
  3 +/** @var $funcs */
  4 +/** @var $serviceKey */
  5 +/** @var $modulePath */
  6 +$serviceKey = env('SWRPC_SERVER_NAME');
  7 +?>
  8 +<?php echo "<?php \n\n"; ?>
  9 +namespace App\Clients\<?=rtrim(str_replace('/','\\',$modulePath), '\\')?>;
  10 +
  11 +
  12 +use AukeySwrpc\BaseService;
  13 +
  14 +/**
  15 + * 不要直接修改这个类的代码,用命令行去执行生成
  16 + * php artisan swrpc:gen
  17 + *
  18 + * Class <?=$class;?> <?php echo "\n"?>
  19 +<?php foreach ($funcs as $func) { ?>
  20 + * @method <?= $func ?>
  21 +<?php } ?>
  22 + *
  23 + * @package App\Clients
  24 + * @author pengjch <?=date('Y-m-d H:i:s')?>
  25 + */
  26 +class <?=$class;?> extends BaseService
  27 +{
  28 + /**
  29 + * @return <?=$class.PHP_EOL;?>
  30 + * @author pengjch
  31 + */
  32 + public static function factory(): <?=$class.PHP_EOL;?>
  33 + {
  34 + return new static();
  35 + }
  36 +
  37 + /**
  38 + * @author pengjch
  39 + */
  40 + public function init()
  41 + {
  42 + $this->setService('<?=$serviceKey?>');
  43 + }
  44 +}