Skip to content

Commit c2cfbcf

Browse files
committed
feature #14132 Applied the new styles to the router: commands (javiereguiluz)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #14132). Discussion ---------- Applied the new styles to the router: commands | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Here are some screenshots comparing these commands before/after. ## debug:router ![debug_router_all](https://cloud.githubusercontent.com/assets/73419/6919169/f9c44c14-d7b4-11e4-8470-fcaae35f334f.png) ## debug:router --show-controllers ![debug_router_all_and_controllers](https://cloud.githubusercontent.com/assets/73419/6919172/ff9aa610-d7b4-11e4-9d9b-07e9ba00a948.png) ## debug:router admin_post_delete ![debug_router_route](https://cloud.githubusercontent.com/assets/73419/6919177/08b1cfee-d7b5-11e4-8310-93a37e68faa5.png) ## router:match - no match ![router_match_no_match](https://cloud.githubusercontent.com/assets/73419/6919186/1cda7afc-d7b5-11e4-9833-2083b4f7b3b2.png) ## router:match - almost match ![router_match_almost](https://cloud.githubusercontent.com/assets/73419/6919190/2095ba8a-d7b5-11e4-8d19-d51fa52cd3f5.png) ## router:match - exact match ![router_match_exact](https://cloud.githubusercontent.com/assets/73419/6919193/236ddb48-d7b5-11e4-9be0-53ec95ce940f.png) ## Deprecated router:dump-apache ![dump_apache](https://cloud.githubusercontent.com/assets/73419/6919164/ebe3651c-d7b4-11e4-9f73-ddf2db825974.png) Commits ------- 13d0da2 Applied the new styles to the router: commands
2 parents 078f953 + 13d0da2 commit c2cfbcf

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/RouterApacheDumperCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
1819
use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
1920
use Symfony\Component\Routing\RouterInterface;
2021

@@ -74,9 +75,11 @@ protected function configure()
7475
*/
7576
protected function execute(InputInterface $input, OutputInterface $output)
7677
{
77-
$formatter = $this->getHelper('formatter');
78+
$output = new SymfonyStyle($input, $output);
7879

79-
$output->writeln($formatter->formatSection('warning', 'The router:dump-apache command is deprecated since version 2.5 and will be removed in 3.0', 'comment'));
80+
$output->title('Router Apache Dumper');
81+
82+
$output->caution('The router:dump-apache command is deprecated since version 2.5 and will be removed in 3.0.');
8083

8184
$router = $this->getContainer()->get('router');
8285

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Symfony\Component\Routing\RouterInterface;
2021
use Symfony\Component\Routing\Route;
2122

@@ -77,8 +78,10 @@ protected function configure()
7778
*/
7879
protected function execute(InputInterface $input, OutputInterface $output)
7980
{
81+
$output = new SymfonyStyle($input, $output);
82+
8083
if (false !== strpos($input->getFirstArgument(), ':d')) {
81-
$output->writeln('<comment>The use of "router:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:router" instead.</comment>');
84+
$output->caution('The use of "router:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:router" instead.');
8285
}
8386

8487
$name = $input->getArgument('name');
@@ -89,11 +92,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
8992
if (!$route) {
9093
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
9194
}
95+
9296
$this->convertController($route);
97+
9398
$helper->describe($output, $route, array(
9499
'format' => $input->getOption('format'),
95100
'raw_text' => $input->getOption('raw'),
96101
'name' => $name,
102+
'output' => $output,
97103
));
98104
} else {
99105
$routes = $this->getContainer()->get('router')->getRouteCollection();
@@ -106,6 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
106112
'format' => $input->getOption('format'),
107113
'raw_text' => $input->getOption('raw'),
108114
'show_controllers' => $input->getOption('show-controllers'),
115+
'output' => $output,
109116
));
110117
}
111118
}

src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Component\Console\Input\ArrayInput;
1415
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
18-
use Symfony\Component\Console\Input\ArrayInput;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Symfony\Component\Routing\RouterInterface;
2021
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
2122

@@ -60,7 +61,7 @@ protected function configure()
6061
The <info>%command.name%</info> shows which routes match a given request and which don't and for what reason:
6162
6263
<info>php %command.full_name% /foo</info>
63-
64+
6465
or
6566
6667
<info>php %command.full_name% /foo --method POST --scheme https --host symfony.com --verbose</info>
@@ -75,6 +76,8 @@ protected function configure()
7576
*/
7677
protected function execute(InputInterface $input, OutputInterface $output)
7778
{
79+
$output = new SymfonyStyle($input, $output);
80+
7881
$router = $this->getContainer()->get('router');
7982
$context = $router->getContext();
8083
if (null !== $method = $input->getOption('method')) {
@@ -91,25 +94,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
9194

9295
$traces = $matcher->getTraces($input->getArgument('path_info'));
9396

97+
$output->newLine();
98+
9499
$matches = false;
95100
foreach ($traces as $trace) {
96101
if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
97-
$output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
102+
$output->text(sprintf('Route <info>"%s"</> almost matches but %s', $trace['name'], lcfirst($trace['log'])));
98103
} elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
99-
$output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
104+
$output->success(sprintf('Route "%s" matches', $trace['name']));
100105

101-
$routerDebugcommand = $this->getApplication()->find('debug:router');
102-
$output->writeln('');
103-
$routerDebugcommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
106+
$routerDebugCommand = $this->getApplication()->find('debug:router');
107+
$routerDebugCommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
104108

105109
$matches = true;
106110
} elseif ($input->getOption('verbose')) {
107-
$output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
111+
$output->text(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
108112
}
109113
}
110114

111115
if (!$matches) {
112-
$output->writeln(sprintf('<fg=red>None of the routes match the path "%s"</>', $input->getArgument('path_info')));
116+
$output->error(sprintf('None of the routes match the path "%s"', $input->getArgument('path_info')));
113117

114118
return 1;
115119
}

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

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ class TextDescriptor extends Descriptor
3434
protected function describeRouteCollection(RouteCollection $routes, array $options = array())
3535
{
3636
$showControllers = isset($options['show_controllers']) && $options['show_controllers'];
37-
$headers = array('Name', 'Method', 'Scheme', 'Host', 'Path');
38-
$table = new Table($this->getOutput());
39-
$table->setStyle('compact');
40-
$table->setHeaders($showControllers ? array_merge($headers, array('Controller')) : $headers);
4137

38+
$tableHeaders = array('Name', 'Method', 'Scheme', 'Host', 'Path');
39+
if ($showControllers) {
40+
$tableHeaders[] = 'Controller';
41+
}
42+
43+
$tableRows = array();
4244
foreach ($routes->all() as $name => $route) {
4345
$row = array(
4446
$name,
@@ -58,11 +60,16 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
5860
$row[] = $controller;
5961
}
6062

61-
$table->addRow($row);
63+
$tableRows[] = $row;
6264
}
6365

64-
$this->writeText($this->formatSection('router', 'Current routes')."\n", $options);
65-
$table->render();
66+
if (isset($options['output'])) {
67+
$options['output']->table($tableHeaders, $tableRows);
68+
} else {
69+
$table = new Table($this->getOutput());
70+
$table->setHeaders($tableHeaders)->setRows($tableRows);
71+
$table->render();
72+
}
6673
}
6774

6875
/**
@@ -73,26 +80,24 @@ protected function describeRoute(Route $route, array $options = array())
7380
$requirements = $route->getRequirements();
7481
unset($requirements['_scheme'], $requirements['_method']);
7582

76-
// fixme: values were originally written as raw
77-
$description = array(
78-
'<comment>Path</comment> '.$route->getPath(),
79-
'<comment>Path Regex</comment> '.$route->compile()->getRegex(),
80-
'<comment>Host</comment> '.('' !== $route->getHost() ? $route->getHost() : 'ANY'),
81-
'<comment>Host Regex</comment> '.('' !== $route->getHost() ? $route->compile()->getHostRegex() : ''),
82-
'<comment>Scheme</comment> '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'),
83-
'<comment>Method</comment> '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'),
84-
'<comment>Class</comment> '.get_class($route),
85-
'<comment>Defaults</comment> '.$this->formatRouterConfig($route->getDefaults()),
86-
'<comment>Requirements</comment> '.($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM'),
87-
'<comment>Options</comment> '.$this->formatRouterConfig($route->getOptions()),
83+
$tableHeaders = array('Property', 'Value');
84+
$tableRows = array(
85+
array('Route Name', $options['name']),
86+
array('Path', $route->getPath()),
87+
array('Path Regex', $route->compile()->getRegex()),
88+
array('Host', ('' !== $route->getHost() ? $route->getHost() : 'ANY')),
89+
array('Host Regex', ('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')),
90+
array('Scheme', ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')),
91+
array('Method', ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')),
92+
array('Requirements', ($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM')),
93+
array('Class', get_class($route)),
94+
array('Defaults', $this->formatRouterConfig($route->getDefaults())),
95+
array('Options', $this->formatRouterConfig($route->getOptions())),
8896
);
8997

90-
if (isset($options['name'])) {
91-
array_unshift($description, '<comment>Name</comment> '.$options['name']);
92-
array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
93-
}
94-
95-
$this->writeText(implode("\n", $description)."\n", $options);
98+
$table = new Table($this->getOutput());
99+
$table->setHeaders($tableHeaders)->setRows($tableRows);
100+
$table->render();
96101
}
97102

98103
/**
@@ -382,23 +387,24 @@ private function renderEventListenerTable(array $eventListeners)
382387
}
383388

384389
/**
385-
* @param array $array
390+
* @param array $config
386391
*
387392
* @return string
388393
*/
389-
private function formatRouterConfig(array $array)
394+
private function formatRouterConfig(array $config)
390395
{
391-
if (!count($array)) {
396+
if (empty($config)) {
392397
return 'NONE';
393398
}
394399

395-
$string = '';
396-
ksort($array);
397-
foreach ($array as $name => $value) {
398-
$string .= ($string ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
400+
ksort($config);
401+
402+
$configAsString = '';
403+
foreach ($config as $key => $value) {
404+
$configAsString .= sprintf("\n%s: %s", $key, $this->formatValue($value));
399405
}
400406

401-
return $string;
407+
return trim($configAsString);
402408
}
403409

404410
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy