diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php index 12b3454115e2c..c761cd57dbb63 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php @@ -38,6 +38,17 @@ */ 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, ) { @@ -45,40 +56,64 @@ public function __construct( 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 { @@ -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)], @@ -576,6 +611,21 @@ private function formatRouterConfig(array $config): string return trim($configAsString); } + /** + * @param array $methods + */ + private function formatMethods(array $methods): string + { + if (!$methods) { + $methods = ['ANY']; + } + + return implode('|', array_map( + fn (string $method): string => sprintf('%s', self::VERB_COLORS[$method] ?? 'default', $method), + $methods + )); + } + private function formatControllerLink(mixed $controller, string $anchorText, ?callable $getContainer = null): string { if (null === $this->fileLinkFormatter) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTestCase.php index eb18fbcc75b79..92fc04edc491e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTestCase.php @@ -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 = []; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php index 8eb1c438601c2..07c66e9c87523 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php @@ -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 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.json b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.json new file mode 100644 index 0000000000000..7dd4387521924 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.json @@ -0,0 +1,2 @@ +[] + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.md b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.md new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.txt new file mode 100644 index 0000000000000..de24cefb89204 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.txt @@ -0,0 +1,4 @@ + ------ -------- ------ +  Name   Method   Path  + ------ -------- ------ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.xml new file mode 100644 index 0000000000000..ea7517e162a61 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/empty_route_collection.xml @@ -0,0 +1,3 @@ + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.txt index 9814273b7a221..bb70cea61d94b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.txt @@ -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 | diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt index ad7a4c8c844fb..f91cf38806213 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt @@ -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;;\ | diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2.txt index 533409d402add..7bdbb83a01965 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2.txt @@ -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 | diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt index 8e3fe4ca7d65f..1d9276e34d0db 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt @@ -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;;\ | diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.json b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.json index 200108a166aac..b5d584c957462 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.json +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.json @@ -37,3 +37,4 @@ "condition": "context.getMethod() in ['GET', 'HEAD', 'POST']" } } + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.md b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.md index 432001f0247fa..64b38953bd5b7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.md +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.md @@ -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'] \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.txt index 9d06562328908..b787a2d4b9705 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.txt @@ -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 --------- ---------- ------------ ----------- --------------- diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.xml index 6a07e059649c8..c36826d869d48 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_1.xml @@ -34,3 +34,4 @@ context.getMethod() in ['GET', 'HEAD', 'POST'] + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_2.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_2.txt index a9f9ee21b7497..e511a2857354f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_2.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_2.txt @@ -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 --------- ---------- ------------ ----------- --------------- diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_3.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_3.txt index 8822b3c40793a..1d89756d24703 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_3.txt +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_collection_3.txt @@ -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 --------- ---------- ------------ ----------- ----------- diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.json b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.json new file mode 100644 index 0000000000000..bc002e61d9ef6 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.json @@ -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" + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.md b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.md new file mode 100644 index 0000000000000..41466a1646f4a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.md @@ -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 \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.txt new file mode 100644 index 0000000000000..6c34fc464c5ef --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.txt @@ -0,0 +1,6 @@ + ------------ -------- -------- ------------- +  Name   Method   Scheme   Path  + ------------ -------- -------- ------------- + some_route ANY https /some-route + ------------ -------- -------- ------------- + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.xml new file mode 100644 index 0000000000000..de9930fc5cc2e --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_host.xml @@ -0,0 +1,14 @@ + + + + /some-route + https + + Controller + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.json b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.json new file mode 100644 index 0000000000000..811c077732f8c --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.json @@ -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" + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.md b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.md new file mode 100644 index 0000000000000..8a8bad6ea9fd5 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.md @@ -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 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.txt new file mode 100644 index 0000000000000..cb83e325a81e2 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.txt @@ -0,0 +1,6 @@ + ---------------------- -------- ------------- ------------- +  Name   Method   Host   Path  + ---------------------- -------- ------------- ------------- + some_route_with_host ANY symfony.com /some-route + ---------------------- -------- ------------- ------------- + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.xml new file mode 100644 index 0000000000000..fe7d8da8d9a4f --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_with_generic_scheme.xml @@ -0,0 +1,13 @@ + + + + /some-route + symfony.com + + strpos + + + + + +