8000 [FrameworkBundle] Add --show-aliases option to debug:router command · symfony/symfony@7b171d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b171d4

Browse files
committed
[FrameworkBundle] Add --show-aliases option to debug:router command
1 parent c7cab07 commit 7b171d4

File tree

9 files changed

+58
-2
lines changed

9 files changed

+58
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CHANGELOG
2222
* Deprecate the `Http\Client\HttpClient` service, use `Psr\Http\Client\ClientInterface` instead
2323
* Add `stop_worker_on_signals` configuration option to `messenger` to define signals which would stop a worker
2424
* Add support for `--all` option to clear all cache pools with `cache:pool:clear` command
25+
* Add `--show-aliases` option to `debug:router` command
2526

2627
6.2
2728
---

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ protected function configure(): void
5656
->setDefinition([
5757
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
5858
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
59+
new InputOption('show-aliases', null, InputOption::VALUE_NONE, 'Show aliases in overview'),
5960
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'txt'),
6061
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
6162
])
@@ -92,6 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9293
'format' => $input->getOption('format'),
9394
'raw_text' => $input->getOption('raw'),
9495
'show_controllers' => $input->getOption('show-controllers'),
96+
'show_aliases' => $input->getOption('show-aliases'),
9597
'output' => $io,
9698
]);
9799

@@ -120,6 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
120122
'format' => $input->getOption('format'),
121123
'raw_text' => $input->getOption('raw'),
122124
'show_controllers' => $input->getOption('show-controllers'),
125+
'show_aliases' => $input->getOption('show-aliases'),
123126
'output' => $io,
124127
'container' => $container,
125128
]);

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ protected function sortByPriority(array $tag): array
263263
return $tag;
264264
}
265265

266+
/**
267+
* @return array<string, string[]>
268+
*/
269+
protected function getReverseAliases(RouteCollection $routes): array
270+
{
271+
$reverseAliases = [];
272+
foreach ($routes->getAliases() as $name => $alias) {
273+
$reverseAliases[$alias->getId()][] = $name;
274+
}
275+
276+
return $reverseAliases;
277+
}
278+
266279
public static function getClassDescription(string $class, string &$resolvedClass = null): string
267280
{
268281
$resolvedClass = $class;

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
3737
$data = [];
3838
foreach ($routes->all() as $name => $route) {
3939
$data[$name] = $this->getRouteData($route);
40+
if (($showAliases ??= $options['show_aliases'] ?? false) && $aliases = ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []) {
41+
$data[$name]['aliases'] = $aliases;
42+
}
4043
}
4144

4245
$this->writeData($data, $options);

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
3939
$this->write("\n\n");
4040
}
4141
$this->describeRoute($route, ['name' => $name]);
42+
if (($showAliases ??= $options['show_aliases'] ?? false) && $aliases = ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []) {
43+
$this->write(sprintf("- Aliases: \n%s", implode("\n", array_map(static fn (string $alias): string => sprintf(' - %s', $alias), $aliases))));
44+
}
4245
}
4346
$this->write("\n");
4447
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
5353
$tableHeaders[] = 'Controller';
5454
}
5555

56+
if ($showAliases = $options['show_aliases'] ?? false) {
57+
$tableHeaders[] = 'Aliases';
58+
}
59+
5660
$tableRows = [];
5761
foreach ($routes->all() as $name => $route) {
5862
$controller = $route->getDefault('_controller');
@@ -69,6 +73,10 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
6973
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '';
7074
}
7175

76+
if ($showAliases) {
77+
$row[] = implode('|', ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []);
78+
}
79+
7280
$tableRows[] = $row;
7381
}
7482

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class XmlDescriptor extends Descriptor
3535
{
3636
protected function describeRouteCollection(RouteCollection $routes, array $options = []): void
3737
{
38-
$this->writeDocument($this->getRouteCollectionDocument($routes));
38+
$this->writeDocument($this->getRouteCollectionDocument($routes, $options));
3939
}
4040

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

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

149149
foreach ($routes->all() as $name => $route) {
150150
$routeXML = $this->getRouteDocument($route, $name);
151+
if (($showAliases ??= $options['show_aliases'] ?? false) && $aliases = ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []) {
152+
$routeXML->firstChild->appendChild($aliasesXML = $routeXML->createElement('aliases'));
153+
foreach ($aliases as $alias) {
154+
$aliasesXML->appendChild($aliasXML = $routeXML->createElement('alias'));
155+
$aliasXML->appendChild(new \DOMText($alias));
156+
}
157+
}
158+
151159
$routesXML->appendChild($routesXML->ownerDocument->importNode($routeXML->childNodes->item(0), true));
152160
}
153161

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ public function testComplete(array $input, array $expectedSuggestions)
9696
$this->assertSame($expectedSuggestions, $tester->complete($input));
9797
}
9898

99+
/**
100+
* @testWith ["txt"]
101+
* ["xml"]
102+
* ["json"]
103+
* ["md"]
104+
*/
105+
public function testShowAliases(string $format)
106+
{
107+
$tester = $this->createCommandTester();
108+
109+
$this->assertSame(0, $tester->execute(['--show-aliases' => true, '--format' => $format]));
110+
$this->assertStringContainsString('my_custom_alias', $tester->getDisplay());
111+
}
112+
99113
public static function provideCompletionSuggestions()
100114
{
101115
yield 'option --format' => [

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ routerdebug_session_logout:
1313
routerdebug_test:
1414
path: /test
1515
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }
16+
17+
my_custom_alias:
18+
alias: routerdebug_test

0 commit comments

Comments
 (0)
0