From 33d692f310cd67f19f97162d4aee1c5c8766d359 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Fri, 21 Apr 2023 18:48:29 +0200 Subject: [PATCH] [FrameworkBundle] Add --show-aliases option to debug:router command --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../FrameworkBundle/Command/RouterDebugCommand.php | 3 +++ .../Console/Descriptor/Descriptor.php | 13 +++++++++++++ .../Console/Descriptor/JsonDescriptor.php | 3 +++ .../Console/Descriptor/MarkdownDescriptor.php | 3 +++ .../Console/Descriptor/TextDescriptor.php | 8 ++++++++ .../Console/Descriptor/XmlDescriptor.php | 12 ++++++++++-- .../Tests/Functional/RouterDebugCommandTest.php | 14 ++++++++++++++ .../Tests/Functional/app/RouterDebug/routing.yml | 3 +++ 9 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index f81adef331734..ed8e9ef604c35 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php index 27d16e636d9c0..87482e9e5d5bc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php @@ -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)'), ]) @@ -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, ]); @@ -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, ]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php index 260601908949a..54a66a1b93e12 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php @@ -260,6 +260,19 @@ protected function sortByPriority(array $tag): array return $tag; } + /** + * @return array + */ + 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; diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php index 332e0eccd7b9d..8b109de5219e5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php @@ -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); diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php index 7423a28550c90..c1cc8fcaa4817 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php @@ -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"); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php index 06afc5ceacdc9..e2f89fd09fa11 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php @@ -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'); @@ -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; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php index 7c03aeba1301a..a6f9ec47d3153 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php @@ -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 @@ -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)); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php index ff12535e10e17..314915d8afcea 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php @@ -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' => [ diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml index 1b9a6c2725ab8..52abd5c2610be 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml @@ -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