| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace App\Module\ThirdParty\Commands;
- use Illuminate\Console\Command;
- use App\Module\ThirdParty\Services\WebhookDispatchService;
- /**
- * Webhook映射管理命令
- * php artisan thirdparty:webhook-mapping list
- */
- class WebhookMappingCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'thirdparty:webhook-mapping
- {action : 操作类型 (list|show|test)}
- {package? : 包名}
- {route? : 路由}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '管理ThirdParty Webhook映射关系';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $action = $this->argument('action');
- $package = $this->argument('package');
- $route = $this->argument('route');
- switch ($action) {
- case 'list':
- return $this->listMappings();
- case 'show':
- return $this->showPackage($package);
- case 'test':
- return $this->testRoute($package, $route);
- default:
- $this->error("未知操作: {$action}");
- return 1;
- }
- }
- /**
- * 列出所有映射关系
- *
- * @return int
- */
- protected function listMappings(): int
- {
- $service = new WebhookDispatchService();
- $packages = $service->getRegisteredPackages();
- if (empty($packages)) {
- $this->info('没有注册的包');
- return 0;
- }
- $this->info('已注册的包列表:');
- $this->newLine();
- $headers = ['包名', '处理器数量', '映射类型', '描述'];
- $rows = [];
- foreach ($packages as $packageName => $info) {
- $rows[] = [
- $packageName,
- $info['handler_count'],
- '一对一精确匹配',
- "包含 {$info['handler_count']} 个专门的Webhook处理器",
- ];
- }
- $this->table($headers, $rows);
- return 0;
- }
- /**
- * 显示指定包的详细信息
- *
- * @param string|null $package 包名
- * @return int
- */
- protected function showPackage(?string $package): int
- {
- if (!$package) {
- $this->error('请指定包名');
- return 1;
- }
- $service = new WebhookDispatchService();
- $packages = $service->getRegisteredPackages();
- if (!isset($packages[$package])) {
- $this->error("包 {$package} 不存在");
- return 1;
- }
- $packageInfo = $packages[$package];
- $this->info("包信息: {$package}");
- $this->newLine();
- // 显示基本信息
- $this->line("名称: {$packageInfo['name']}");
- $this->line("处理器数量: {$packageInfo['handler_count']}");
- $this->newLine();
- // 显示处理器列表
- $this->info('注册的处理器 (一对一映射):');
- if (empty($packageInfo['handlers'])) {
- $this->line(' 无');
- } else {
- $handlerHeaders = ['路由', '处理器类', '专门处理'];
- $handlerRows = [];
- // 使用反射获取处理器映射
- $reflection = new \ReflectionClass(WebhookDispatchService::class);
- $property = $reflection->getProperty('packageHandlers');
- $property->setAccessible(true);
- $handlers = $property->getValue()[$package] ?? [];
- foreach ($handlers as $route => $handlerClass) {
- $handlerRows[] = [
- $route,
- $handlerClass,
- $this->getHandlerDescription($route),
- ];
- }
- $this->table($handlerHeaders, $handlerRows);
- }
- return 0;
- }
- /**
- * 获取处理器描述
- *
- * @param string $route 路由
- * @return string
- */
- protected function getHandlerDescription(string $route): string
- {
- $descriptions = [
- 'register' => '用户注册通知',
- 'deposit' => '充值操作通知',
- 'withdraw' => '提取操作通知',
- 'check' => '余额检查通知',
- ];
- return $descriptions[$route] ?? '未知操作';
- }
- /**
- * 测试路由匹配
- *
- * @param string|null $package 包名
- * @param string|null $route 路由
- * @return int
- */
- protected function testRoute(?string $package, ?string $route): int
- {
- if (!$package || !$route) {
- $this->error('请指定包名和路由');
- return 1;
- }
- $this->info("测试路由匹配: {$package}/{$route}");
- $this->newLine();
- try {
- $service = new WebhookDispatchService();
- // 使用反射获取处理器映射
- $reflection = new \ReflectionClass($service);
- $property = $reflection->getProperty('packageHandlers');
- $property->setAccessible(true);
- $handlers = $property->getValue()[$package] ?? [];
- if (isset($handlers[$route])) {
- $handlerClass = $handlers[$route];
- $this->info('找到匹配的处理器:');
- $this->line("路由: {$route}");
- $this->line("处理器类: {$handlerClass}");
- $this->line("映射类型: 一对一精确匹配");
- $this->line("支持方法: POST (Webhook标准)");
- $this->line("专门处理: " . $this->getHandlerDescription($route));
- // 检查处理器类是否存在
- if (class_exists($handlerClass)) {
- $this->line("类状态: 已加载");
- } else {
- $this->error("类状态: 未找到");
- }
- } else {
- $this->error('没有找到匹配的处理器');
- $this->line('可用路由: ' . implode(', ', array_keys($handlers)));
- }
- } catch (\Exception $e) {
- $this->error("测试失败: {$e->getMessage()}");
- return 1;
- }
- return 0;
- }
- }
|