From c4c721c6f7fb1fa593591517ca025e152fe2a6c9 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 12 Jun 2024 14:41:08 +0000 Subject: [PATCH] Merge pull request #1724 from hydephp/route-list-formats Add a JSON output format option to the route list command https://github.com/hydephp/develop/commit/bb3428279b532f002ead30425cfd55c357bb44ef --- src/Console/Commands/RouteListCommand.php | 18 ++++++-- .../Feature/Commands/RouteListCommandTest.php | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/Console/Commands/RouteListCommand.php b/src/Console/Commands/RouteListCommand.php index 0eb86431..b0e923ec 100644 --- a/src/Console/Commands/RouteListCommand.php +++ b/src/Console/Commands/RouteListCommand.php @@ -10,6 +10,7 @@ use function array_map; use function array_keys; +use function json_encode; use function array_values; /** @@ -18,7 +19,7 @@ class RouteListCommand extends Command { /** @var string */ - protected $signature = 'route:list'; + protected $signature = 'route:list {--format=txt : The output format (txt or json)}'; /** @var string */ protected $description = 'Display all the registered routes'; @@ -27,9 +28,11 @@ public function handle(): int { $routes = $this->generate(); - $this->table($this->makeHeader($routes), $routes); - - return Command::SUCCESS; + return match ($this->option('format')) { + 'txt' => $this->table($this->makeHeader($routes), $routes) ?? Command::SUCCESS, + 'json' => $this->writeRaw(json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) ?? Command::SUCCESS, + default => $this->error("Invalid format provided. Only 'txt' and 'json' are supported.") ?? Command::FAILURE, + }; } /** @return array> */ @@ -43,4 +46,11 @@ protected function makeHeader(array $routes): array { return array_map(Hyde::makeTitle(...), array_keys($routes[0])); } + + /** Write a message without ANSI formatting */ + protected function writeRaw(string $message): void + { + $this->output->setDecorated(false); + $this->output->writeln($message); + } } diff --git a/tests/Feature/Commands/RouteListCommandTest.php b/tests/Feature/Commands/RouteListCommandTest.php index 95edc551..b715501c 100644 --- a/tests/Feature/Commands/RouteListCommandTest.php +++ b/tests/Feature/Commands/RouteListCommandTest.php @@ -98,6 +98,49 @@ public function testConsoleRouteListWithTypeLabel() ]])->assertExitCode(0); } + public function testConsoleRouteListWithTextFormatOption() + { + $this->artisan('route:list --format=txt') + ->expectsTable($this->headers(), [[ + 'BladePage', + '_pages/404.blade.php', + '_site/404.html', + '404', + ], [ + 'BladePage', + '_pages/index.blade.php', + '_site/index.html', + 'index', + ]])->assertExitCode(0); + } + + public function testConsoleRouteListWithJsonFormatOption() + { + $this->artisan('route:list --format=json') + ->expectsOutput(json_encode([ + [ + 'page_type' => 'BladePage', + 'source_file' => '_pages/404.blade.php', + 'output_file' => '_site/404.html', + 'route_key' => '404', + ], + [ + 'page_type' => 'BladePage', + 'source_file' => '_pages/index.blade.php', + 'output_file' => '_site/index.html', + 'route_key' => 'index', + ], + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) + ->assertExitCode(0); + } + + public function testConsoleRouteListWithInvalidFormatOption() + { + $this->artisan('route:list --format=foo') + ->expectsOutput("Invalid format provided. Only 'txt' and 'json' are supported.") + ->assertExitCode(1); + } + protected function headers(): array { return ['Page Type', 'Source File', 'Output File', 'Route Key'];