|
14 | 14 | namespace Guanguans\LaravelExceptionNotify\Commands;
|
15 | 15 |
|
16 | 16 | use Guanguans\LaravelExceptionNotify\DefaultNotifyClientExtender;
|
17 |
| -use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager; |
18 | 17 | use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
|
19 | 18 | use Illuminate\Console\Command;
|
20 |
| -use Illuminate\Support\Arr; |
21 | 19 |
|
22 | 20 | class TestCommand extends Command
|
23 | 21 | {
|
24 |
| - protected $signature = 'exception-notify:test'; |
| 22 | + protected $signature = 'exception-notify:test {--c|channels=* : Specify channels to test}'; |
25 | 23 | protected $description = 'Test for exception-notify';
|
26 | 24 |
|
27 |
| - public function handle(ExceptionNotifyManager $exceptionNotifyManager): void |
| 25 | + public function handle(): void |
28 | 26 | {
|
29 |
| - collect(config('exception-notify.channels'))->each(static function (array $config, string $name): void { |
30 |
| - if ('notify' === $config['driver']) { |
31 |
| - config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class); |
32 |
| - } |
33 |
| - }); |
| 27 | + if ($channels = $this->option('channels')) { |
| 28 | + config()->set('exception-notify.defaults', $channels); |
| 29 | + } |
| 30 | + |
| 31 | + collect(config('exception-notify.channels')) |
| 32 | + ->only($defaults = config('exception-notify.defaults')) |
| 33 | + ->each(static function (array $config, string $name): void { |
| 34 | + if ('notify' === $config['driver']) { |
| 35 | + config()->set("exception-notify.channels.$name.client.extender", DefaultNotifyClientExtender::class); |
| 36 | + } |
| 37 | + }); |
34 | 38 |
|
35 | 39 | $this->output->note('Test for exception-notify start.');
|
36 |
| - $this->output->section('The main configuration is as follows.'); |
37 |
| - $this->output->listing($this->getMainConfigurations()); |
| 40 | + $this->output->section('Current default channels:'); |
| 41 | + $this->output->listing($defaults); |
38 | 42 |
|
39 | 43 | try {
|
40 |
| - $runtimeException = new RuntimeException('Test for exception-notify.'); |
41 |
| - |
42 |
| - if ($exceptionNotifyManager->shouldReport($runtimeException)) { |
43 |
| - throw $runtimeException; |
44 |
| - } |
45 |
| - |
46 |
| - $warning = sprintf( |
47 |
| - 'The exception [%s] should not be reported. Please check the configuration.', |
48 |
| - \get_class($runtimeException) |
49 |
| - ); |
50 |
| - } finally { |
51 |
| - $this->output->warning( |
52 |
| - $warning ?? <<<'warning' |
53 |
| - Please check whether your channels received the exception notification report. |
| 44 | + $this->output->warning(sprintf( |
| 45 | + <<<'warning' |
| 46 | + Let's throw an exception to trigger exception notification monitoring. |
| 47 | + Please check whether your channels(%s) received the exception notification reports. |
54 | 48 | If not, please find reason in the default log.
|
55 |
| - warning |
56 |
| - ); |
| 49 | + warning, |
| 50 | + implode('、', $defaults) |
| 51 | + )); |
57 | 52 |
|
| 53 | + throw new RuntimeException('Test for exception-notify.'); |
| 54 | + } finally { |
58 | 55 | $this->output->note('Test for exception-notify done.');
|
59 | 56 | }
|
60 | 57 | }
|
61 |
| - |
62 |
| - private function getMainConfigurations(): array |
63 |
| - { |
64 |
| - $mainConfigurations = collect(config('exception-notify')) |
65 |
| - ->except(['collectors', 'title', 'channels']) |
66 |
| - ->transform(static function ($item) { |
67 |
| - if (\is_array($item)) { |
68 |
| - if ([] === $item) { |
69 |
| - return '[]'; |
70 |
| - } |
71 |
| - |
72 |
| - if (array_is_list($item)) { |
73 |
| - return implode(',', $item); |
74 |
| - } |
75 |
| - |
76 |
| - return $item; |
77 |
| - } |
78 |
| - |
79 |
| - /** @noinspection DebugFunctionUsageInspection */ |
80 |
| - return var_export($item, true); |
81 |
| - }); |
82 |
| - |
83 |
| - return collect(Arr::dot($mainConfigurations)) |
84 |
| - ->transform(static fn (?string $item, string $key): string => sprintf( |
85 |
| - "$key: <fg=yellow>%s</>", |
86 |
| - $item ?? 'null' |
87 |
| - )) |
88 |
| - ->all(); |
89 |
| - } |
90 | 58 | }
|
0 commit comments