|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Console\Commands; |
| 6 | + |
| 7 | +use Stringable; |
| 8 | +use Tempest\Console\Console; |
| 9 | +use Tempest\Console\ConsoleArgument; |
| 10 | +use Tempest\Console\ConsoleCommand; |
| 11 | +use Tempest\Console\ExitCode; |
| 12 | +use Tempest\Container\Container; |
| 13 | +use Tempest\Core\AppConfig; |
| 14 | +use Tempest\Core\Insight; |
| 15 | +use Tempest\Core\InsightsProvider; |
| 16 | +use Tempest\Support\Arr; |
| 17 | +use Tempest\Support\Json; |
| 18 | +use Tempest\Support\Str; |
| 19 | + |
| 20 | +final readonly class AboutCommand |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly Console $console, |
| 24 | + private readonly Container $container, |
| 25 | + private readonly AppConfig $appConfig, |
| 26 | + ) {} |
| 27 | + |
| 28 | + #[ConsoleCommand(name: 'about', description: 'Shows insights about the application', aliases: ['insights'])] |
| 29 | + public function __invoke( |
| 30 | + #[ConsoleArgument(description: 'Formats the outpuyt to JSON', aliases: ['--json'])] |
| 31 | + ?bool $json = null, |
| 32 | + ): ExitCode { |
| 33 | + if ($json) { |
| 34 | + $this->writeInsightsAsJson(); |
| 35 | + } else { |
| 36 | + $this->writeFormattedInsights(); |
| 37 | + } |
| 38 | + |
| 39 | + return ExitCode::SUCCESS; |
| 40 | + } |
| 41 | + |
| 42 | + private function writeFormattedInsights(): void |
| 43 | + { |
| 44 | + foreach ($this->appConfig->insightsProviders as $class) { |
| 45 | + /** @var InsightsProvider $provider */ |
| 46 | + $provider = $this->container->get($class); |
| 47 | + |
| 48 | + $this->console->header($provider->name); |
| 49 | + |
| 50 | + foreach ($provider->getInsights() as $key => $value) { |
| 51 | + $this->console->keyValue($key, $this->formatInsight($value)); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private function writeInsightsAsJson(): void |
| 57 | + { |
| 58 | + $json = []; |
| 59 | + |
| 60 | + foreach ($this->appConfig->insightsProviders as $class) { |
| 61 | + /** @var InsightsProvider $provider */ |
| 62 | + $provider = $this->container->get($class); |
| 63 | + |
| 64 | + $json[Str\to_snake_case($provider->name)] = Arr\map_with_keys( |
| 65 | + array: $provider->getInsights(), |
| 66 | + map: fn (mixed $value, string $key) => yield Str\to_snake_case($key) => $this->rawInsight($value), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + $this->console->writeRaw(Json\encode($json)); |
| 71 | + } |
| 72 | + |
| 73 | + private function formatInsight(Stringable|Insight|string $value): string |
| 74 | + { |
| 75 | + if ($value instanceof Insight) { |
| 76 | + return $value->formattedValue; |
| 77 | + } |
| 78 | + |
| 79 | + return (string) $value; |
| 80 | + } |
| 81 | + |
| 82 | + private function rawInsight(Stringable|Insight|string $value): string |
| 83 | + { |
| 84 | + if ($value instanceof Insight) { |
| 85 | + return $value->value; |
| 86 | + } |
| 87 | + |
| 88 | + return Str\strip_tags((string) $value); |
| 89 | + } |
| 90 | +} |
0 commit comments