WebhookMappingCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Module\ThirdParty\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\ThirdParty\Services\WebhookDispatchService;
  5. /**
  6. * Webhook映射管理命令
  7. * php artisan thirdparty:webhook-mapping list
  8. */
  9. class WebhookMappingCommand extends Command
  10. {
  11. /**
  12. * 命令签名
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'thirdparty:webhook-mapping
  17. {action : 操作类型 (list|show|test)}
  18. {package? : 包名}
  19. {route? : 路由}';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '管理ThirdParty Webhook映射关系';
  26. /**
  27. * 执行命令
  28. *
  29. * @return int
  30. */
  31. public function handle()
  32. {
  33. $action = $this->argument('action');
  34. $package = $this->argument('package');
  35. $route = $this->argument('route');
  36. switch ($action) {
  37. case 'list':
  38. return $this->listMappings();
  39. case 'show':
  40. return $this->showPackage($package);
  41. case 'test':
  42. return $this->testRoute($package, $route);
  43. default:
  44. $this->error("未知操作: {$action}");
  45. return 1;
  46. }
  47. }
  48. /**
  49. * 列出所有映射关系
  50. *
  51. * @return int
  52. */
  53. protected function listMappings(): int
  54. {
  55. $service = new WebhookDispatchService();
  56. $packages = $service->getRegisteredPackages();
  57. if (empty($packages)) {
  58. $this->info('没有注册的包');
  59. return 0;
  60. }
  61. $this->info('已注册的包列表:');
  62. $this->newLine();
  63. $headers = ['包名', '处理器数量', '映射类型', '描述'];
  64. $rows = [];
  65. foreach ($packages as $packageName => $info) {
  66. $rows[] = [
  67. $packageName,
  68. $info['handler_count'],
  69. '一对一精确匹配',
  70. "包含 {$info['handler_count']} 个专门的Webhook处理器",
  71. ];
  72. }
  73. $this->table($headers, $rows);
  74. return 0;
  75. }
  76. /**
  77. * 显示指定包的详细信息
  78. *
  79. * @param string|null $package 包名
  80. * @return int
  81. */
  82. protected function showPackage(?string $package): int
  83. {
  84. if (!$package) {
  85. $this->error('请指定包名');
  86. return 1;
  87. }
  88. $service = new WebhookDispatchService();
  89. $packages = $service->getRegisteredPackages();
  90. if (!isset($packages[$package])) {
  91. $this->error("包 {$package} 不存在");
  92. return 1;
  93. }
  94. $packageInfo = $packages[$package];
  95. $this->info("包信息: {$package}");
  96. $this->newLine();
  97. // 显示基本信息
  98. $this->line("名称: {$packageInfo['name']}");
  99. $this->line("处理器数量: {$packageInfo['handler_count']}");
  100. $this->newLine();
  101. // 显示处理器列表
  102. $this->info('注册的处理器 (一对一映射):');
  103. if (empty($packageInfo['handlers'])) {
  104. $this->line(' 无');
  105. } else {
  106. $handlerHeaders = ['路由', '处理器类', '专门处理'];
  107. $handlerRows = [];
  108. // 使用反射获取处理器映射
  109. $reflection = new \ReflectionClass(WebhookDispatchService::class);
  110. $property = $reflection->getProperty('packageHandlers');
  111. $property->setAccessible(true);
  112. $handlers = $property->getValue()[$package] ?? [];
  113. foreach ($handlers as $route => $handlerClass) {
  114. $handlerRows[] = [
  115. $route,
  116. $handlerClass,
  117. $this->getHandlerDescription($route),
  118. ];
  119. }
  120. $this->table($handlerHeaders, $handlerRows);
  121. }
  122. return 0;
  123. }
  124. /**
  125. * 获取处理器描述
  126. *
  127. * @param string $route 路由
  128. * @return string
  129. */
  130. protected function getHandlerDescription(string $route): string
  131. {
  132. $descriptions = [
  133. 'register' => '用户注册通知',
  134. 'deposit' => '充值操作通知',
  135. 'withdraw' => '提取操作通知',
  136. 'check' => '余额检查通知',
  137. ];
  138. return $descriptions[$route] ?? '未知操作';
  139. }
  140. /**
  141. * 测试路由匹配
  142. *
  143. * @param string|null $package 包名
  144. * @param string|null $route 路由
  145. * @return int
  146. */
  147. protected function testRoute(?string $package, ?string $route): int
  148. {
  149. if (!$package || !$route) {
  150. $this->error('请指定包名和路由');
  151. return 1;
  152. }
  153. $this->info("测试路由匹配: {$package}/{$route}");
  154. $this->newLine();
  155. try {
  156. $service = new WebhookDispatchService();
  157. // 使用反射获取处理器映射
  158. $reflection = new \ReflectionClass($service);
  159. $property = $reflection->getProperty('packageHandlers');
  160. $property->setAccessible(true);
  161. $handlers = $property->getValue()[$package] ?? [];
  162. if (isset($handlers[$route])) {
  163. $handlerClass = $handlers[$route];
  164. $this->info('找到匹配的处理器:');
  165. $this->line("路由: {$route}");
  166. $this->line("处理器类: {$handlerClass}");
  167. $this->line("映射类型: 一对一精确匹配");
  168. $this->line("支持方法: POST (Webhook标准)");
  169. $this->line("专门处理: " . $this->getHandlerDescription($route));
  170. // 检查处理器类是否存在
  171. if (class_exists($handlerClass)) {
  172. $this->line("类状态: 已加载");
  173. } else {
  174. $this->error("类状态: 未找到");
  175. }
  176. } else {
  177. $this->error('没有找到匹配的处理器');
  178. $this->line('可用路由: ' . implode(', ', array_keys($handlers)));
  179. }
  180. } catch (\Exception $e) {
  181. $this->error("测试失败: {$e->getMessage()}");
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. }