Skip to content

[FrameworkBundle] Only show relevant columns in debug:router call #59780

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

Open
wants to merge 13 commits into
base: 7.4
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,82 @@
*/
class TextDescriptor extends Descriptor
{
private const VERB_COLORS = [
'ANY' => 'default',
'GET' => 'blue',
'HEAD' => 'magenta',
'OPTIONS' => 'blue',
'POST' => 'green',
'PUT' => 'yellow',
'PATCH' => 'yellow',
'DELETE' => 'red',
];

public function __construct(
private ?FileLinkFormatter $fileLinkFormatter = null,
) {
}

protected function describeRouteCollection(RouteCollection $routes, array $options = []): void
{
$showControllers = isset($options['show_controllers']) && $options['show_controllers'];

$tableHeaders = ['Name', 'Method', 'Scheme', 'Host', 'Path'];
if ($showControllers) {
$tableHeaders[] = 'Controller';
}

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

$tableRows = [];
$shouldShowScheme = false;
$shouldShowHost = false;
foreach ($routes->all() as $name => $route) {
$controller = $route->getDefault('_controller');

$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$shouldShowScheme = $shouldShowScheme || 'ANY' !== $scheme;

$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$shouldShowHost = $shouldShowHost || 'ANY' !== $host;

$row = [
$name,
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
'' !== $route->getHost() ? $route->getHost() : 'ANY',
$this->formatControllerLink($controller, $route->getPath(), $options['container'] ?? null),
'Name' => $name,
'Methods' => $this->formatMethods($route->getMethods()),
'Scheme' => $scheme,
'Host' => $host,
'Path' => $route->getPath(),
];

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

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

$tableRows[] = $row;
}

$tableHeaders = ['Name', 'Method'];

if ($shouldShowScheme) {
$tableHeaders[] = 'Scheme';
} else {
array_walk($tableRows, function (&$row) { unset($row['Scheme']); });
}

if ($shouldShowHost) {
$tableHeaders[] = 'Host';
} else {
array_walk($tableRows, function (&$row) { unset($row['Host']); });
}

$tableHeaders[] = 'Path';

if ($showControllers) {
$tableHeaders[] = 'Controller';
}

if ($showAliases) {
$tableHeaders[] = 'Aliases';
}

if (isset($options['output'])) {
$options['output']->table($tableHeaders, $tableRows);
} else {
Expand All @@ -103,7 +138,7 @@ protected function describeRoute(Route $route, array $options = []): void
['Host', '' !== $route->getHost() ? $route->getHost() : 'ANY'],
['Host Regex', '' !== $route->getHost() ? $route->compile()->getHostRegex() : ''],
['Scheme', $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'],
['Method', $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'],
['Method', $this->formatMethods($route->getMethods())],
['Requirements', $route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM'],
['Class', $route::class],
['Defaults', $this->formatRouterConfig($defaults)],
Expand Down Expand Up @@ -576,6 +611,21 @@ private function formatRouterConfig(array $config): string
return trim($configAsString);
}

/**
* @param array<string> $methods
*/
private function formatMethods(array $methods): string
{
if (!$methods) {
$methods = ['ANY'];
}

return implode('|', array_map(
fn (string $method): string => sprintf('<fg=%s>%s</>', self::VERB_COLORS[$method] ?? 'default', $method),
$methods
));
}

private function formatControllerLink(mixed $controller, string $anchorText, ?callable $getContainer = null): string
{
if (null === $this->fileLinkFormatter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ private static function getContainerBuilderDescriptionTestData(array $objects):
'public' => ['show_hidden' => false],
'tag1' => ['show_hidden' => true, 'tag' => 'tag1'],
'tags' => ['group_by' => 'tags', 'show_hidden' => true],
'arguments' => ['show_hidden' => false],
];

$data = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,32 @@ public static function getRouteCollections()
$collection1->add($name, $route);
}

return ['route_collection_1' => $collection1];
$routesWithGenericHost = new RouteCollection();
$routesWithGenericHost->add('some_route', new RouteStub(
'/some-route',
['_controller' => 'Controller'],
[],
[],
null,
['https'],
));

$routesWithGenericScheme = new RouteCollection();
$routesWithGenericScheme->add('some_route_with_host', new RouteStub(
'/some-route',
['_controller' => 'strpos'],
[],
[],
'symfony.com',
[],
));

return [
'empty_route_collection' => new RouteCollection(),
'route_collection_1' => $collection1,
'route_with_generic_host' => $routesWithGenericHost,
'route_with_generic_scheme' => $routesWithGenericScheme,
];
}

public static function getRouteCollectionsByHttpMethod(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[]

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
------ -------- ------
 Name   Method   Path 
------ -------- ------

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes/>

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Host | localhost |
| Host Regex | #HOST_REGEX# |
| Scheme | http|https |
| Method | GET|HEAD |
| Method | GET|HEAD |
| Requirements | name: [a-z]+ |
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
| Defaults | name: Joseph |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Host | localhost |
| Host Regex | #HOST_REGEX# |
| Scheme | http|https |
| Method | GET|HEAD |
| Method | GET|HEAD |
| Requirements | name: [a-z]+ |
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
| Defaults | _controller: ]8;;myeditor://open?file=[:file:]&line=58\Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\MyController::__invoke()]8;;\ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Host | localhost |
| Host Regex | #HOST_REGEX# |
| Scheme | http|https |
| Method | PUT|POST |
| Method | PUT|POST |
| Requirements | NO CUSTOM |
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
| Defaults | NONE |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Host | localhost |
| Host Regex | #HOST_REGEX# |
| Scheme | http|https |
| Method | PUT|POST |
| Method | PUT|POST |
| Requirements | NO CUSTOM |
| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub |
| Defaults | _controller: ]8;;myeditor://open?file=[:file:]&line=58\Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\MyController::__invoke()]8;;\ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
"condition": "context.getMethod() in ['GET', 'HEAD', 'POST']"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ route_2
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
- `opt1`: val1
- `opt2`: val2
- Condition: context.getMethod() in ['GET', 'HEAD', 'POST']

- Condition: context.getMethod() in ['GET', 'HEAD', 'POST']
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--------- ---------- ------------ ----------- ---------------
 Name   Method   Scheme   Host   Path 
--------- ---------- ------------ ----------- ---------------
route_1 GET|HEAD http|https localhost /hello/{name}
route_2 PUT|POST http|https localhost /name/add
route_1 GET|HEAD http|https localhost /hello/{name}
route_2 PUT|POST http|https localhost /name/add
--------- ---------- ------------ ----------- ---------------

Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
<condition>context.getMethod() in ['GET', 'HEAD', 'POST']</condition>
</route>
</routes>

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--------- ---------- ------------ ----------- ---------------
 Name   Method   Scheme   Host   Path 
--------- ---------- ------------ ----------- ---------------
route_1 GET|HEAD http|https localhost /hello/{name}
route_3 ANY http|https localhost /other/route
route_1 GET|HEAD http|https localhost /hello/{name}
route_3 ANY http|https localhost /other/route
--------- ---------- ------------ ----------- ---------------

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--------- ---------- ------------ ----------- -----------
 Name   Method   Scheme   Host   Path 
--------- ---------- ------------ ----------- -----------
route_2 PUT|POST http|https localhost /name/add
route_2 PUT|POST http|https localhost /name/add
--------- ---------- ------------ ----------- -----------

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"some_route": {
"path": "\/some-route",
"pathRegex": "#PATH_REGEX#",
"host": "ANY",
"hostRegex": "",
"scheme": "https",
"method": "ANY",
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
"defaults": {
"_controller": "Controller"
},
"requirements": "NO CUSTOM",
"options": {
"compiler_class": "Symfony\\Component\\Routing\\RouteCompiler"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
some_route
----------

- Path: /some-route
- Path Regex: #PATH_REGEX#
- Host: ANY
- Host Regex:
- Scheme: https
- Method: ANY
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
- Defaults:
- `_controller`: Controller
- Requirements: NO CUSTOM
- Options:
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
------------ -------- -------- -------------
 Name   Method   Scheme   Path 
------------ -------- -------- -------------
some_route ANY https /some-route
------------ -------- -------- -------------

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes>
<route name="some_route" class="Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub">
<path regex="#PATH_REGEX#">/some-route</path>
<scheme>https</scheme>
<defaults>
<default key="_controller">Controller</default>
</defaults>
<options>
<option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
</options>
</route>
</routes>

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"some_route_with_host": {
"path": "\/some-route",
"pathRegex": "#PATH_REGEX#",
"host": "symfony.com",
"hostRegex": "#HOST_REGEX#",
"scheme": "ANY",
"method": "ANY",
"class": "Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\RouteStub",
"defaults": {
"_controller": "strpos"
},
"requirements": "NO CUSTOM",
"options": {
"compiler_class": "Symfony\\Component\\Routing\\RouteCompiler"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
some_route_with_host
--------------------

- Path: /some-route
- Path Regex: #PATH_REGEX#
- Host: symfony.com
- Host Regex: #HOST_REGEX#
- Scheme: ANY
- Method: ANY
- Class: Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub
- Defaults:
- `_controller`: strpos
- Requirements: NO CUSTOM
- Options:
- `compiler_class`: Symfony\Component\Routing\RouteCompiler
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---------------------- -------- ------------- -------------
 Name   Method   Host   Path 
---------------------- -------- ------------- -------------
some_route_with_host ANY symfony.com /some-route
---------------------- -------- ------------- -------------

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes>
<route name="some_route_with_host" class="Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub">
<path regex="#PATH_REGEX#">/some-route</path>
<host regex="#HOST_REGEX#">symfony.com</host>
<defaults>
<default key="_controller">strpos</default>
</defaults>
<options>
<option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
</options>
</route>
</routes>
Loading