WebhookMappingCommand.php 6.1 KB

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