-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[FrameworkBundle] Add flag to sort debug:router output
#57651
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ protected function configure(): void | |
| new InputOption('show-aliases', null, InputOption::VALUE_NONE, 'Show aliases in overview'), | ||
| new InputOption('format', null, InputOption::VALUE_REQUIRED, \sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'txt'), | ||
| new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'), | ||
| new InputOption('sort', null, InputOption::VALUE_REQUIRED, 'Sort by column name'), | ||
| ]) | ||
| ->setHelp(<<<'EOF' | ||
| The <info>%command.name%</info> displays the configured routes: | ||
|
|
@@ -117,9 +118,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
| 'container' => $container, | ||
| ]); | ||
| } else { | ||
| $sortBy = $input->getOption('sort'); | ||
| if ($sortBy) { | ||
| $routes = $this->sortBy($routes, \strtolower($sortBy)); | ||
| } | ||
| $helper->describe($io, $routes, [ | ||
| 'format' => $input->getOption('format'), | ||
| 'raw_text' => $input->getOption('raw'), | ||
| 'sort' => $input->getOption('sort'), | ||
| 'show_controllers' => $input->getOption('show-controllers'), | ||
| 'show_aliases' => $input->getOption('show-aliases'), | ||
| 'output' => $io, | ||
|
|
@@ -172,4 +178,59 @@ private function getAvailableFormatOptions(): array | |
| { | ||
| return (new DescriptorHelper())->getFormats(); | ||
| } | ||
|
|
||
| private function sortBy(RouteCollection $routeCollection, string $column): RouteCollection | ||
| { | ||
| $routesArray = $routeCollection->all(); | ||
| $sortedRoutes = new RouteCollection(); | ||
|
|
||
| if ('name' === $column) { | ||
| \ksort($routesArray); | ||
| foreach ($routesArray as $name => $route) { | ||
| $sortedRoutes->add($name, $route); | ||
| $sortedRoutes->addAlias($route->getDefault('_controller'), $name); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get why you add an alias to the controller for every routes. Not all routes have a controller. Example:
|
||
| } | ||
|
|
||
| return $sortedRoutes; | ||
| } | ||
|
|
||
| $helperArray = []; | ||
|
|
||
| if ('method' === $column) { | ||
| foreach ($routesArray as $name => $route) { | ||
| $methods = empty($route->getMethods()) ? 'ANY' : \implode('|', $route->getMethods()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the logic for formatting a list of methods is in the descriptors, it would make sense to move the sorting method into |
||
| $helperArray[$methods][$name] = $route; | ||
| } | ||
| } | ||
|
|
||
| if ('scheme' === $column) { | ||
| foreach ($routesArray as $name => $route) { | ||
| $scheme = empty($route->getSchemes()) ? 'ANY' : \implode('|', $route->getSchemes()); | ||
| $helperArray[$scheme][$name] = $route; | ||
| } | ||
| } | ||
|
|
||
| if ('host' === $column) { | ||
| foreach ($routesArray as $name => $route) { | ||
| $helperArray[$route->getHost()][$name] = $route; | ||
|
|
||
| } | ||
| } | ||
|
|
||
| if ('path' === $column) { | ||
| foreach ($routesArray as $name => $route) { | ||
| $helperArray[$route->getPath()][$name] = $route; | ||
| } | ||
| } | ||
|
|
||
| \ksort($helperArray); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating an array and using |
||
| foreach ($helperArray as $array) { | ||
| foreach ($array as $name => $route) { | ||
| $sortedRoutes->add($name, $route); | ||
| $sortedRoutes->addAlias($route->getDefault('_controller'), $name); | ||
| } | ||
| } | ||
|
|
||
| return $sortedRoutes; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the list of possible values as last parameter, for bash completion.