Skip to content

Commit

Permalink
Applied the new styles to the router: commands
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz authored and fabpot committed Sep 28, 2015
1 parent 078f953 commit 13d0da2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 45 deletions.
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
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
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);

$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
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 @@ -382,23 +387,24 @@ private function renderEventListenerTable(array $eventListeners)
}

/**
* @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

0 comments on commit 13d0da2

Please sign in to comment.