Skip to content

Commit 821481d

Browse files
committed
Only show relevant columns in debug:router
1 parent 0dfb1ec commit 821481d

18 files changed

+213
-31
lines changed

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,78 @@
3838
*/
3939
class TextDescriptor extends Descriptor
4040
{
41+
private const VERB_COLORS = [
42+
'ANY' => 'red',
43+
'GET' => 'blue',
44+
'HEAD' => '#6C7280',
45+
'OPTIONS' => '#6C7280',
46+
'POST' => 'yellow',
47+
'PUT' => 'yellow',
48+
'PATCH' => 'yellow',
49+
'DELETE' => 'red',
50+
];
51+
4152
public function __construct(
4253
private ?FileLinkFormatter $fileLinkFormatter = null,
4354
) {
4455
}
4556

4657
protected function describeRouteCollection(RouteCollection $routes, array $options = []): void
4758
{
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-
}
59+
$showAliases = $options['show_aliases'] ?? false;
5860

5961
$tableRows = [];
62+
$shouldShowSchema = false;
63+
$shouldShowHost = false;
6064
foreach ($routes->all() as $name => $route) {
6165
$controller = $route->getDefault('_controller');
6266

67+
$schema = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
68+
$shouldShowSchema = $shouldShowSchema || 'ANY' !== $schema;
69+
70+
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
71+
$shouldShowHost = $shouldShowHost || 'ANY' !== $host;
72+
6373
$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),
74+
'Name' => $name,
75+
'Methods' => $this->formatMethods($route->getMethods()),
76+
'Schema' => $schema,
77+
'Host' => $host,
78+
'Path' => $route->getPath(),
79+
'Controller' => $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '',
6980
];
7081

71-
if ($showControllers) {
72-
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '';
73-
}
74-
7582
if ($showAliases) {
76-
$row[] = implode('|', ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []);
83+
$row['Aliases'] = implode('|', ($reverseAliases ??= $this->getReverseAliases($routes))[$name] ?? []);
7784
}
7885

7986
$tableRows[] = $row;
8087
}
8188

89+
$tableHeaders = ['Name', 'Method'];
90+
91+
if ($shouldShowSchema) {
92+
$tableHeaders[] = 'Schema';
93+
} else {
94+
array_walk($tableRows, function (&$row) {
95+
unset($row['Schema']);
96+
});
97+
}
98+
99+
if ($shouldShowHost) {
100+
$tableHeaders[] = 'Host';
101+
} else {
102+
array_walk($tableRows, function (&$row) {
103+
unset($row['Host']);
104+
});
105+
}
106+
107+
$tableHeaders = array_merge($tableHeaders, ['Path', 'Controller']);
108+
109+
if ($showAliases = $options['show_aliases'] ?? false) {
110+
$tableHeaders[] = 'Aliases';
111+
}
112+
82113
if (isset($options['output'])) {
83114
$options['output']->table($tableHeaders, $tableRows);
84115
} else {
@@ -576,6 +607,18 @@ private function formatRouterConfig(array $config): string
576607
return trim($configAsString);
577608
}
578609

610+
private function formatMethods(array $methods): string
611+
{
612+
if (!$methods) {
613+
$methods = ['ANY'];
614+
}
615+
616+
return implode('<fg=#6C7280>|</>', array_map(
617+
fn (string $method): string => '<fg='.self::VERB_COLORS[$method].'>'.$method.'</>',
618+
$methods
619+
));
620+
}
621+
579622
private function formatControllerLink(mixed $controller, string $anchorText, ?callable $getContainer = null): string
580623
{
581624
if (null === $this->fileLinkFormatter) {

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
1313

14+
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
1415
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\FooUnitEnum;
1516
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Suit;
1617
use Symfony\Component\DependencyInjection\Alias;
@@ -34,7 +35,32 @@ public static function getRouteCollections()
3435
$collection1->add($name, $route);
3536
}
3637

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

4066
public static function getRoutes()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

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   Controller 
3+
------ -------- ------ ------------
4+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<routes/>

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@
3636
},
3737
"condition": "context.getMethod() in ['GET', 'HEAD', 'POST']"
3838
}
39-
}
39+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ route_2
3434
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
3535
- `opt1`: val1
3636
- `opt2`: val2
37-
- Condition: context.getMethod() in ['GET', 'HEAD', 'POST']
38-
37+
- Condition: context.getMethod() in ['GET', 'HEAD', 'POST']
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
--------- ---------- ------------ ----------- ---------------
2-
 Name   Method   Scheme   Host   Path 
3-
--------- ---------- ------------ ----------- ---------------
4-
route_1 GET|HEAD http|https localhost /hello/{name}
5-
route_2 PUT|POST http|https localhost /name/add
6-
--------- ---------- ------------ ----------- ---------------
1+
--------- ---------- ------------ ----------- --------------- ------------
2+
 Name   Method   Schema   Host   Path   Controller 
3+
--------- ---------- ------------ ----------- --------------- ------------
4+
route_1 GET|HEAD http|https localhost /hello/{name}
5+
route_2 PUT|POST http|https localhost /name/add
6+
--------- ---------- ------------ ----------- --------------- ------------
77

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
</options>
3434
<condition>context.getMethod() in ['GET', 'HEAD', 'POST']</condition>
3535
</route>
36-
</routes>
36+
</routes>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"some_route": {
3+
"path": "\/some-route",
4+
"pathRegex": "#PATH_REGEX#",
5+
"host": "ANY",
6+
"hostRegex": "",
7+
"scheme": "https",
8+
"method": "ANY",
9+
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
10+
"defaults": {
11+
"_controller": "Controller"
12+
},
13+
"requirements": "NO CUSTOM",
14+
"options": {
15+
"compiler_class": "Symfony\\Component\\Routing\\RouteCompiler"
16+
}
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
some_route
2+
----------
3+
4+
- Path: /some-route
5+
- Path Regex: #PATH_REGEX#
6+
- Host: ANY
7+
- Host Regex:
8+
- Scheme: https
9+
- Method: ANY
10+
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
11+
- Defaults:
12+
- `_controller`: Controller
13+
- Requirements: NO CUSTOM
14+
- Options:
15+
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
------------ -------- -------- ------------- --------------
2+
 Name   Method   Schema   Path   Controller 
3+
------------ -------- -------- ------------- --------------
4+
some_route ANY https /some-route Controller()
5+
------------ -------- -------- ------------- --------------
6+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<routes>
3+
<route name="some_route" class="Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub">
4+
<path regex="#PATH_REGEX#">/some-route</path>
5+
<scheme>https</scheme>
6+
<defaults>
7+
<default key="_controller">Controller</default>
8+
</defaults>
9+
<options>
10+
<option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
11+
</options>
12+
</route>
13+
</routes>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"some_route_with_host": {
3+
"path": "\/some-route",
4+
"pathRegex": "#PATH_REGEX#",
5+
"host": "localhost",
6+
"hostRegex": "#HOST_REGEX#",
7+
"scheme": "ANY",
8+
"method": "ANY",
9+
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
10+
"defaults": {
11+
"_controller": [
12+
"Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController",
13+
"redirectAction"
14+
]
15+
},
16+
"requirements": "NO CUSTOM",
17+
"options": {
18+
"compiler_class": "Symfony\\Component\\Routing\\RouteCompiler"
19+
}
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
some_route_with_host
2+
--------------------
3+
4+
- Path: /some-route
5+
- Path Regex: #PATH_REGEX#
6+
- Host: localhost
7+
- Host Regex: #HOST_REGEX#
8+
- Scheme: ANY
9+
- Method: ANY
10+
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
11+
- Defaults:
12+
- `_controller`: array (0 => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController',1 => 'redirectAction',)
13+
- Requirements: NO CUSTOM
14+
- Options:
15+
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---------------------- -------- ------------- ------------- --------------------------------------------------------------------------------
2+
 Name   Method   Host   Path   Controller 
3+
---------------------- -------- ------------- ------------- --------------------------------------------------------------------------------
4+
some_route_with_host ANY symfony.com /some-route ]8;;myeditor://open?file=/home/mamazu/packages/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php&line=53\Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction()]8;;\
5+
---------------------- -------- ------------- ------------- --------------------------------------------------------------------------------
6+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<routes>
3+
<route name="some_route_with_host" class="Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub">
4+
<path regex="#PATH_REGEX#">/some-route</path>
5+
<host regex="#HOST_REGEX#">localhost</host>
6+
<defaults>
7+
<default key="_controller">array (0 =&gt; 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController',1 =&gt; 'redirectAction',)</default>
8+
</defaults>
9+
<options>
10+
<option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
11+
</options>
12+
</route>
13+
</routes>

0 commit comments

Comments
 (0)