From c6c72ca0104ef4db45e132d924af723bde2d1d2c Mon Sep 17 00:00:00 2001 From: yaozm Date: Thu, 9 May 2024 18:29:24 +0800 Subject: [PATCH] refactor(command): update TestCommand signature and handle method - Updated TestCommand signature to include optional channels parameter - Updated handle method to set default channels and configure notify client extender - Improved exception handling and logging in handle method --- src/Commands/TestCommand.php | 80 +++++++++++------------------------- 1 file changed, 24 insertions(+), 56 deletions(-) diff --git a/src/Commands/TestCommand.php b/src/Commands/TestCommand.php index cffde6d..78d63d4 100644 --- a/src/Commands/TestCommand.php +++ b/src/Commands/TestCommand.php @@ -14,77 +14,45 @@ namespace Guanguans\LaravelExceptionNotify\Commands; use Guanguans\LaravelExceptionNotify\DefaultNotifyClientExtender; -use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager; use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException; use Illuminate\Console\Command; -use Illuminate\Support\Arr; class TestCommand extends Command { - protected $signature = 'exception-notify:test'; + protected $signature = 'exception-notify:test {--c|channels=* : Specify channels to test}'; protected $description = 'Test for exception-notify'; - public function handle(ExceptionNotifyManager $exceptionNotifyManager): void + public function handle(): void { - collect(config('exception-notify.channels'))->each(static function (array $config, string $name): void { - if ('notify' === $config['driver']) { - config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class); - } - }); + if ($channels = $this->option('channels')) { + config()->set('exception-notify.defaults', $channels); + } + + collect(config('exception-notify.channels')) + ->only($defaults = config('exception-notify.defaults')) + ->each(static function (array $config, string $name): void { + if ('notify' === $config['driver']) { + config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class); + } + }); $this->output->note('Test for exception-notify start.'); - $this->output->section('The main configuration is as follows.'); - $this->output->listing($this->getMainConfigurations()); + $this->output->section('Current default channels:'); + $this->output->listing($defaults); try { - $runtimeException = new RuntimeException('Test for exception-notify.'); - - if ($exceptionNotifyManager->shouldReport($runtimeException)) { - throw $runtimeException; - } - - $warning = sprintf( - 'The exception [%s] should not be reported. Please check the configuration.', - \get_class($runtimeException) - ); - } finally { - $this->output->warning( - $warning ?? <<<'warning' - Please check whether your channels received the exception notification report. + $this->output->warning(sprintf( + <<<'warning' + Let's throw an exception to trigger exception notification monitoring. + Please check whether your channels(%s) received the exception notification reports. If not, please find reason in the default log. - warning - ); + warning, + implode('、', $defaults) + )); + throw new RuntimeException('Test for exception-notify.'); + } finally { $this->output->note('Test for exception-notify done.'); } } - - private function getMainConfigurations(): array - { - $mainConfigurations = collect(config('exception-notify')) - ->except(['collectors', 'title', 'channels']) - ->transform(static function ($item) { - if (\is_array($item)) { - if ([] === $item) { - return '[]'; - } - - if (array_is_list($item)) { - return implode(',', $item); - } - - return $item; - } - - /** @noinspection DebugFunctionUsageInspection */ - return var_export($item, true); - }); - - return collect(Arr::dot($mainConfigurations)) - ->transform(static fn (?string $item, string $key): string => sprintf( - "$key: %s", - $item ?? 'null' - )) - ->all(); - } }