8000 [FrameworkBundle] Add --show-aliases option to debug:router command by fancyweb · Pull Request #50109 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Add --show-aliases option to debug:router command #50109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[FrameworkBundle] Add --show-aliases option to debug:router command
  • Loading branch information
fancyweb committed Sep 22, 2023
commit 33d692f310cd67f19f97162d4aee1c5c8766d359
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CHANGELOG
* Deprecate the `Http\Client\HttpClient` service, use `Psr\Http\Client\ClientInterface` instead
* Add `stop_worker_on_signals` configuration option to `messenger` to define signals which would stop a worker
* Add support for `--all` option to clear all cache pools with `cache:pool:clear` command
* Add `--show-aliases` option to `debug:router` command

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function configure(): void
->setDefinition([
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
new InputOption('show-aliases', null, InputOption::VALUE_NONE, 'Show aliases in overview'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'txt'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
])
Expand Down Expand Up @@ -92,6 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
'show_aliases' => $input->getOption('show-aliases'),
'output' => $io,
]);

Expand Down Expand Up @@ -120,6 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
'show_aliases' => $input->getOption('show-aliases'),
'output' => $io,
'container' => $container,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ protected function sortByPriority(array $tag): array
return $tag;
}

/**
* @return array<string, string[]>
*/
protected function getReverseAliases(RouteCollection $routes): array
{
$reverseAliases = [];
foreach ($routes->getAliases() as $name => $alias) {
$reverseAliases[$alias->getId()][] = $name;
}

return $reverseAliases;
}

public static function getClassDescription(string $class, string &$resolvedClass = null): string
{
$resolvedClass = $class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$data = [];
foreach ($routes->all() as $name => $route) {
$data[$name] = $this->getRouteData($route);
if (($showAliases ??= $options['show_aliases'] ?? false) && $aliases = ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []) {
$data[$name]['aliases'] = $aliases;
}
}

$this->writeData($data, $options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$this->write("\n\n");
}
$this->describeRoute($route, ['name' => $name]);
if (($showAliases ??= $options['show_aliases'] ?? false) && $aliases = ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []) {
$this->write(sprintf("- Aliases: \n%s", implode("\n", array_map(static fn (string $alias): string => sprintf(' - %s', $alias), $aliases))));
}
}
$this->write("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$tableHeaders[] = 'Controller';
}

if ($showAliases = $options['show_aliases'] ?? false) {
$tableHeaders[] = 'Aliases';
}

$tableRows = [];
foreach ($routes->all() as $name => $route) {
$controller = $route->getDefault('_controller');
Expand All @@ -69,6 +73,10 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '';
}

if ($showAliases) {
$row[] = implode('|', ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []);
}

$tableRows[] = $row;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class XmlDescriptor extends Descriptor
{
protected function describeRouteCollection(RouteCollection $routes, array $options = []): void
{
$this->writeDocument($this->getRouteCollectionDocument($routes));
$this->writeDocument($this->getRouteCollectionDocument($routes, $options));
}

protected function describeRoute(Route $route, array $options = []): void
Expand Down Expand Up @@ -141,13 +141,21 @@ private function writeDocument(\DOMDocument $dom): void
$this->write($dom->saveXML());
}

private function getRouteCollectionDocument(RouteCollection $routes): \DOMDocument
private function getRouteCollectionDocument(RouteCollection $routes, array $options): \DOMDocument
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($routesXML = $dom->createElement('routes'));

foreach ($routes->all() as $name => $route) {
$routeXML = $this->getRouteDocument($route, $name);
if (($showAliases ??= $options['show_aliases'] ?? false) && $aliases = ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []) {
$routeXML->firstChild->appendChild($aliasesXML = $routeXML->createElement('aliases'));
foreach ($aliases as $alias) {
$aliasesXML->appendChild($aliasXML = $routeXML->createElement('alias'));
$aliasXML->appendChild(new \DOMText($alias));
}
}

$routesXML->appendChild($routesXML->ownerDocument->importNode($routeXML->childNodes->item(0), true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ public function testComplete(array $input, array $expectedSuggestions)
$this->assertSame($expectedSuggestions, $tester->complete($input));
}

/**
* @testWith ["txt"]
* ["xml"]
* ["json"]
* ["md"]
*/
public function testShowAliases(string $format)
{
$tester = $this->createCommandTester();

$this->assertSame(0, $tester->execute(['--show-aliases' => true, '--format' => $format]));
$this->assertStringContainsString('my_custom_alias', $tester->getDisplay());
}

public static function provideCompletionSuggestions()
{
yield 'option --format' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ routerdebug_session_logout:
routerdebug_test:
path: /test
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }

my_custom_alias:
alias: routerdebug_test
0