Skip to content

Applied the new styles to the router: commands #14132

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

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
use Symfony\Component\Routing\RouterInterface;

Expand Down Expand Up @@ -74,9 +75,11 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$formatter = $this->getHelper('formatter');
$output = new SymfonyStyle($input, $output);

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

$output->caution('The router:dump-apache command is deprecated since version 2.5 and will be removed in 3.0.');

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Route;

Expand Down Expand Up @@ -77,8 +78,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);

if (false !== strpos($input->getFirstArgument(), ':d')) {
$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>');
$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.');
}

$name = $input->getArgument('name');
Expand All @@ -89,11 +92,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (!$route) {
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}

$this->convertController($route);

$helper->describe($output, $route, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'name' => $name,
'output' => $output,
));
} else {
$routes = $this->getContainer()->get('router')->getRouteCollection();
Expand All @@ -106,6 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
'output' => $output,
));
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;

Expand Down Expand Up @@ -60,7 +61,7 @@ protected function configure()
The <info>%command.name%</info> shows which routes match a given request and which don't and for what reason:

<info>php %command.full_name% /foo</info>

or

<info>php %command.full_name% /foo --method POST --scheme https --host symfony.com --verbose</info>
Expand All @@ -75,6 +76,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use a different variable name ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need another name, since we're replacing the output we're using by a wrapped one, which implements the exact same interface (and an additional one, StyleInterface).
This output behaves exactly like the previous one which is decorated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding a method argument imho is no good style (see also #14595 (comment) for the same).


$router = $this->getContainer()->get('router');
$context = $router->getContext();
if (null !== $method = $input->getOption('method')) {
Expand All @@ -91,25 +94,26 @@ protected function execute(InputInterface $input, OutputInterface $output)

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

$output->newLine();

$matches = false;
foreach ($traces as $trace) {
if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
$output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
$output->text(sprintf('Route <info>"%s"</> almost matches but %s', $trace['name'], lcfirst($trace['log'])));
} elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
$output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
$output->success(sprintf('Route "%s" matches', $trace['name']));

$routerDebugcommand = $this->getApplication()->find('debug:router');
$output->writeln('');
$routerDebugcommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
$routerDebugCommand = $this->getApplication()->find('debug:router');
$routerDebugCommand->run(new ArrayInput(array('name' => $trace['name'])), $output);

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

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

return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class TextDescriptor extends Descriptor
protected function describeRouteCollection(RouteCollection $routes, array $options = array())
{
$showControllers = isset($options['show_controllers']) && $options['show_controllers'];
$headers = array('Name', 'Method', 'Scheme', 'Host', 'Path');
$table = new Table($this->getOutput());
$table->setStyle('compact');
$table->setHeaders($showControllers ? array_merge($headers, array('Controller')) : $headers);

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

$tableRows = array();
foreach ($routes->all() as $name => $route) {
$row = array(
$name,
Expand All @@ -58,11 +60,16 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$row[] = $controller;
}

$table->addRow($row);
$tableRows[] = $row;
}

$this->writeText($this->formatSection('router', 'Current routes')."\n", $options);
$table->render();
if (isset($options['output'])) {
$options['output']->table($tableHeaders, $tableRows);
} else {
$table = new Table($this->getOutput());
$table->setHeaders($tableHeaders)->setRows($tableRows);
$table->render();
}
}

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

// fixme: values were originally written as raw
$description = array(
'<comment>Path</comment> '.$route->getPath(),
'<comment>Path Regex</comment> '.$route->compile()->getRegex(),
'<comment>Host</comment> '.('' !== $route->getHost() ? $route->getHost() : 'ANY'),
'<comment>Host Regex</comment> '.('' !== $route->getHost() ? $route->compile()->getHostRegex() : ''),
'<comment>Scheme</comment> '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'),
'<comment>Method</comment> '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'),
'<comment>Class</comment> '.get_class($route),
'<comment>Defaults</comment> '.$this->formatRouterConfig($route->getDefaults()),
'<comment>Requirements</comment> '.($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM'),
'<comment>Options</comment> '.$this->formatRouterConfig($route->getOptions()),
$tableHeaders = array('Property', 'Value');
$tableRows = array(
array('Route Name', $options['name']),
array('Path', $route->getPath()),
array('Path Regex', $route->compile()->getRegex()),
array('Host', ('' !== $route->getHost() ? $route->getHost() : 'ANY')),
array('Host Regex', ('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')),
array('Scheme', ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')),
array('Method', ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')),
array('Requirements', ($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM')),
array('Class', get_class($route)),
array('Defaults', $this->formatRouterConfig($route->getDefaults())),
array('Options', $this->formatRouterConfig($route->getOptions())),
);

if (isset($options['name'])) {
array_unshift($description, '<comment>Name</comment> '.$options['name']);
array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
}

$this->writeText(implode("\n", $description)."\n", $options);
$table = new Table($this->getOutput());
$table->setHeaders($tableHeaders)->setRows($tableRows);
$table->render();
}

/**
Expand Down Expand Up @@ -174,7 +179,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$maxTags = array();

foreach ($serviceIds as $key => $serviceId) {
foreach ($serviceIds as $key => $serviceId) {
$definition = $this->resolveServiceDefinition($builder, $serviceId);
if ($definition instanceof Definition) {
// filter out private services unless shown explicitly
Expand Down Expand Up @@ -212,7 +217,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
foreach ($definition->getTag($showTag) as $key => $tag) {
$tagValues = array();
foreach ($tagsNames as $tagName) {
$tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : "";
$tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : '';
}
if (0 === $key) {
$table->addRow(array_merge(array($serviceId), $tagValues, array($definition->getClass())));
Expand All @@ -225,10 +230,10 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
}
} elseif ($definition instanceof Alias) {
$alias = $definition;
$table->addRow(array_merge(array($serviceId, sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, "") : array()));
$table->addRow(array_merge(array($serviceId, sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, '') : array()));
} else {
// we have no information (happens with "service_container")
$table->addRow(array_merge(array($serviceId, get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, "") : array()));
$table->addRow(array_merge(array($serviceId, get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, '') : array()));
}
}

Expand All @@ -245,7 +250,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
: array();

$description[] = sprintf('<comment>Service Id</comment> %s', isset($options['id']) ? $options['id'] : '-');
$description[] = sprintf('<comment>Class</comment> %s', $definition->getClass() ?: "-");
$description[] = sprintf('<comment>Class</comment> %s', $definition->getClass() ?: '-');

$tags = $definition->getTags();
if (count($tags)) {
Expand Down Expand Up @@ -376,23 +381,24 @@ protected function describeCallable($callable, array $options = array())
}

/**
* @param array $array
* @param array $config
*
* @return string
*/
private function formatRouterConfig(array $array)
private function formatRouterConfig(array $config)
{
if (!count($array)) {
if (empty($config)) {
return 'NONE';
}

$string = '';
ksort($array);
foreach ($array as $name => $value) {
$string .= ($string ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
ksort($config);

$configAsString = '';
foreach ($config as $key => $value) {
$configAsString .= sprintf("\n%s: %s", $key, $this->formatValue($value));
}

return $string;
return trim($configAsString);
}

/**
Expand Down