Skip to content

Commit

Permalink
Merge pull request #634 from hydephp/add-a-json-output-format-option-…
Browse files Browse the repository at this point in the history
…to-the-route-list-command

Add a JSON output format option to the route list command
  • Loading branch information
caendesilva authored Jun 30, 2024
2 parents 227cd9a + c4c721c commit 2df0a1a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Console/Commands/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function array_map;
use function array_keys;
use function json_encode;
use function array_values;

/**
Expand All @@ -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';
Expand All @@ -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<integer, array<string, string>> */
Expand All @@ -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);
}
}
43 changes: 43 additions & 0 deletions tests/Feature/Commands/RouteListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down

0 comments on commit 2df0a1a

Please sign in to comment.