Skip to content

Commit cd7cfec

Browse files
committed
feature #59780 [FrameworkBundle] Only show relevant columns in debug:router call and adding colors (mamazu)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [FrameworkBundle] Only show relevant columns in `debug:router` call and adding colors | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Issues | Fix #59767 | License | MIT ## What I did * Adding more tests for the descriptor (empty route collection) * ~Always showing the controllers in the list view (we but not removing the `--show-controllers` so it's not a BC break)~ * Only show the "Host" and "Schema" columns when the values are != "ANY" * Adding colors to the HTTP methods (it's the same colors Swagger uses) ## Why * Color coding the HTTP methods gives you more info at a quick glance * Hiding columns that don't provide values reduces the noise of the table (for more info there is still detail view) ## Screenshots Before: ![image](https://github.com/user-attachments/assets/12353289-1737-4715-8a9a-9e49b8b07e8b) After: ![image](https://github.com/user-attachments/assets/de383ee0-3992-4b7f-84a2-e26b09e8075e) Commits ------- 75728d2 [FrameworkBundle] Only show relevant columns in `debug:router` call and adding colors
2 parents 44f6c85 + 75728d2 commit cd7cfec

24 files changed

+221
-30
lines changed

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

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,83 @@
3838
*/
3939
class TextDescriptor extends Descriptor
4040
{
41+
private const VERB_COLORS = [
42+
'ANY' => 'default',
43+
'GET' => 'blue',
44+
'QUERY' => 'blue',
45+
'HEAD' => 'magenta',
46+
'OPTIONS' => 'blue',
47+
'POST' => 'green',
48+
'PUT' => 'yellow',
49+
'PATCH' => 'yellow',
50+
'DELETE' => 'red',
51+
];
52+
4153
public function __construct(
4254
private ?FileLinkFormatter $fileLinkFormatter = null,
4355
) {
4456
}
4557

4658
protected function describeRouteCollection(RouteCollection $routes, array $options = []): void
4759
{
48-
$showControllers = isset($options['show_controllers']) && $options['show_controllers'];
49-
50-
$tableHeaders = ['Name', 'Method', 'Scheme', 'Host', 'Path'];
51-
if ($showControllers) {
52-
$tableHeaders[] = 'Controller';
53-
}
54-
55-
if ($showAliases = $options['show_aliases'] ?? false) {
56-
$tableHeaders[] = 'Aliases';
57-
}
60+
$showAliases = $options['show_aliases'] ?? false;
61+
$showControllers = $options['show_controllers'] ?? false;
5862

5963
$tableRows = [];
64+
$shouldShowScheme = false;
65+
$shouldShowHost = false;
6066
foreach ($routes->all() as $name => $route) {
6167
$controller = $route->getDefault('_controller');
6268

69+
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
70+
$shouldShowScheme = $shouldShowScheme || 'ANY' !== $scheme;
71+
72+
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
73+
$shouldShowHost = $shouldShowHost || 'ANY' !== $host;
74+
6375
$row = [
64-
$name,
65-
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
66-
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
67-
'' !== $route->getHost() ? $route->getHost() : 'ANY',
68-
$this->formatControllerLink($controller, $route->getPath(), $options['container'] ?? null),
76+
'Name' => $name,
77+
'Methods' => $this->formatMethods($route->getMethods()),
78+
'Scheme' => $scheme,
79+
'Host' => $host,
80+
'Path' => $route->getPath(),
6981
];
7082

7183
if ($showControllers) {
72-
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '';
84+
$row['Controller'] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '';
7385
}
7486

7587
if ($showAliases) {
76-
$row[] = implode('|', ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []);
88+
$row['Aliases'] = implode('|', $this->getReverseAliases($routes)[$name] ?? []);
7789
}
7890

7991
$tableRows[] = $row;
8092
}
8193

94+
$tableHeaders = ['Name', 'Method'];
95+
96+
if ($shouldShowScheme) {
97+
$tableHeaders[] = 'Scheme';
98+
} else {
99+
array_walk($tableRows, function (&$row) { unset($row['Scheme']); });
100+
}
101+
102+
if ($shouldShowHost) {
103+
$tableHeaders[] = 'Host';
104+
} else {
105+
array_walk($tableRows, function (&$row) { unset($row['Host']); });
106+
}
107+
108+
$tableHeaders[] = 'Path';
109+
110+
if ($showControllers) {
111+
$tableHeaders[] = 'Controller';
112+
}
113+
114+
if ($showAliases) {
115+
$tableHeaders[] = 'Aliases';
116+
}
117+
82118
if (isset($options['output'])) {
83119
$options['output']->table($tableHeaders, $tableRows);
84120
} else {
@@ -103,7 +139,7 @@ protected function describeRoute(Route $route, array $options = []): void
103139
['Host', '' !== $route->getHost() ? $route->getHost() : 'ANY'],
104140
['Host Regex', '' !== $route->getHost() ? $route->compile()->getHostRegex() : ''],
105141
['Scheme', $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'],
106-
['Method', $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'],
142+
['Method', $this->formatMethods($route->getMethods())],
107143
['Requirements', $route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM'],
108144
['Class', $route::class],
109145
['Defaults', $this->formatRouterConfig($defaults)],
@@ -576,6 +612,21 @@ private function formatRouterConfig(array $config): string
576612
return trim($configAsString);
577613
}
578614

615+
/**
616+
* @param array<string> $methods
617+
*/
618+
private function formatMethods(array $methods): string
619+
{
620+
if ([] === $methods) {
621+
$methods = ['ANY'];
622+
}
623+
624+
return implode('|', array_map(
625+
fn (string $method): string => \sprintf('<fg=%s>%s</>', self::VERB_COLORS[$method] ?? 'default', $method),
626+
$methods
627+
));
628+
}
629+
579630
/**
580631
* @param (callable():ContainerBuilder)|null $getContainer
581632
*/

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,32 @@ public static function getRouteCollections()
3434
$collection1->add($name, $route);
3535
}
3636

37-
return ['route_collection_1' => $collection1];
37+
$routesWithGenericHost = new RouteCollection();
38+
$routesWithGenericHost->add('some_route', new RouteStub(
39+
'/some-route',
40+
['_controller' => 'Controller'],
41+
[],
42+
[],
43+
null,
44+
['https'],
45+
));
46+
47+
$routesWithGenericScheme = new RouteCollection();
48+
$routesWithGenericScheme->add('some_route_with_host', new RouteStub(
49+
'/some-route',
50+
['_controller' => 'strpos'],
51+
[],
52+
[],
53+
'symfony.com',
54+
[],
55+
));
56+
57+
return [
58+
'empty_route_collection' => new RouteCollection(),
59+
'route_collection_1' => $collection1,
60+
'route_with_generic_host' => $routesWithGenericHost,
61+
'route_with_generic_scheme' => $routesWithGenericScheme,
62+
];
3863
}
3964

4065
public static function getRouteCollectionsByHttpMethod(): array
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[]
2+

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.md

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
------ -------- ------
2+
 Name   Method   Path 
3+
------ -------- ------
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<routes/>
3+

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| Host | localhost |
88
| Host Regex | #HOST_REGEX# |
99
| Scheme | http|https |
10-
| Method | GET|HEAD |
10+
| Method | GET|HEAD |
1111
| Requirements | name: [a-z]+ |
1212
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
1313
| Defaults | name: Joseph |

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| Host | localhost |
88
| Host Regex | #HOST_REGEX# |
99
| Scheme | http|https |
10-
| Method | GET|HEAD |
10+
| Method | GET|HEAD |
1111
| Requirements | name: [a-z]+ |
1212
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
1313
| Defaults | _controller: ]8;;myeditor://open?file=[:file:]&line=59\Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\MyController::__invoke()]8;;\ |

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| Host | localhost |
88
| Host Regex | #HOST_REGEX# |
99
| Scheme | http|https |
10-
| Method | PUT|POST |
10+
| Method | PUT|POST |
1111
| Requirements | NO CUSTOM |
1212
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
1313
| Defaults | NONE |

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| Host | localhost |
88
| Host Regex | #HOST_REGEX# |
99
| Scheme | http|https |
10-
| Method | PUT|POST |
10+
| Method | PUT|POST |
1111
| Requirements | NO CUSTOM |
1212
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
1313
| Defaults | _controller: ]8;;myeditor://open?file=[:file:]&line=59\Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\MyController::__invoke()]8;;\ |

0 commit comments

Comments
 (0)