Skip to content

Commit

Permalink
RouteDumpCommand: restyled output
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and f3l1x committed May 19, 2020
1 parent cc1394f commit f123a4c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
64 changes: 46 additions & 18 deletions src/Command/RouteDumpCommand.php
Expand Up @@ -8,14 +8,14 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class RouteDumpCommand extends Command
{

public const TABLE_HEADER = ['Method', 'Path', 'Handler', 'Parameters'];
private const TABLE_HEADER = ['Method', 'Path', 'Handler', ' ', 'Parameters'];

/** @var string */
protected static $defaultName = 'apitte:route:dump';
Expand Down Expand Up @@ -46,34 +46,49 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$io = new SymfonyStyle($input, $output);

$io->title('All registered endpoints');

$table = new Table($output);
$table->setHeaders(self::TABLE_HEADER);

$style = new TableStyle();
$style
->setDefaultCrossingChar(' ')
->setVerticalBorderChars(' ')
->setHorizontalBorderChars('', '─')
->setCellRowContentFormat('%s');
$table->setStyle($style);

/** @var Endpoint[][] $endpointsByHandler */
$endpointsByHandler = [];
foreach ($endpoints as $endpoint) {
$endpointsByHandler[$endpoint->getHandler()->getClass()][] = $endpoint;
}

foreach ($endpointsByHandler as $handler) {
foreach ($handler as $endpoint) {
foreach ($endpointsByHandler as $groupedEndpoints) {
$previousClass = null;

foreach ($groupedEndpoints as $endpoint) {
$handler = $endpoint->getHandler();
$currentClass = $class = $handler->getClass();

if ($previousClass === $class) {
$currentClass = '';
}

$table->addRow([
implode('|', $endpoint->getMethods()),
$endpoint->getMask(),
sprintf(
'%s::%s()',
$endpoint->getHandler()->getClass(),
$endpoint->getHandler()->getMethod()
'<fg=cyan>%s</>',
implode('|', $endpoint->getMethods())
),
$endpoint->getMask(),
$currentClass,
$handler->getMethod(),
$this->formatParameters($endpoint->getParameters()),
]);

$previousClass = $class;
}

if ($handler !== end($endpointsByHandler)) {
if ($groupedEndpoints !== end($endpointsByHandler)) {
$table->addRow(new TableSeparator());
}
}
Expand All @@ -88,11 +103,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
private function formatParameters(array $parameters): string
{
$params = array_map(function (EndpointParameter $parameter): string {
return sprintf('%s (%s)', $parameter->getName(), $parameter->getType());
}, $parameters);
$paramsByIn = [];

foreach ($parameters as $parameter) {
$paramsByIn[$parameter->getIn()][] = $parameter->getName();
}

ksort($paramsByIn);

$result = '';

foreach ($paramsByIn as $in => $params) {
$result .= sprintf('<fg=cyan>%s</>', $in) . ': ' . implode(', ', $params);
if ($params !== end($paramsByIn)) {
$result .= ' | ';
}
}

return implode(', ', $params);
return $result;
}

}
25 changes: 12 additions & 13 deletions tests/cases/Command/RouteDumpCommand.phpt
Expand Up @@ -58,19 +58,18 @@ test(function (): void {

$command->run($input, $output);

$result = <<<EOD
All registered endpoints
========================
+--------+---------------+-------------------+-------------+
| Method | Path | Handler | Parameters |
+--------+---------------+-------------------+-------------+
| GET | /example/foo | class1::method1() | |
| GET | /example/{id} | class1::method2() | id (string) |
+--------+---------------+-------------------+-------------+
| GET | /lorem-ipsum | class2::method1() | |
+--------+---------------+-------------------+-------------+
$result = trim(implode("\n", array_map(static function (string $line): string {
return rtrim($line);
}, explode("\n", $output->fetch()))));

$expected = <<<EOD
Method Path Handler Parameters
GET /example/foo class1 method1
GET /example/{id} method2 path: id
────── ───────────── ─────── ─────── ──────────
GET /lorem-ipsum class2 method1
EOD;

Assert::equal($result, trim($output->fetch()));
Assert::equal($expected, $result);
});

0 comments on commit f123a4c

Please sign in to comment.