Skip to content

[FrameworkBundle] router debug command fix #7301

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

Merged
merged 3 commits into from
Mar 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Finder\Finder;

/**
Expand All @@ -26,7 +25,7 @@
class AssetsInstallCommand extends ContainerAwareCommand
{
/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand Down Expand Up @@ -62,9 +61,9 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*
* @throws \InvalidArgumentException When the target directory does not exist
* @throws \InvalidArgumentException When the target directory does not exist or symlink cannot be used
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CacheClearCommand extends ContainerAwareCommand
protected $name;

/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class CacheWarmupCommand extends ContainerAwareCommand
{
/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class ConfigDumpReferenceCommand extends ContainerDebugCommand
{
/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand Down Expand Up @@ -53,7 +53,9 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*
* @throws \LogicException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
abstract class ContainerAwareCommand extends Command implements ContainerAwareInterface
{
/**
* @var ContainerInterface
* @var ContainerInterface|null
*/
private $container;

Expand All @@ -40,7 +40,7 @@ protected function getContainer()
}

/**
* @see ContainerAwareInterface::setContainer()
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
class ContainerDebugCommand extends ContainerAwareCommand
{
/**
* @var ContainerBuilder
* @var ContainerBuilder|null
*/
protected $containerBuilder;

/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand Down Expand Up @@ -74,7 +74,9 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*
* @throws \LogicException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down Expand Up @@ -307,7 +309,7 @@ protected function getContainerBuilder()
*
* @param string $serviceId The service id to resolve
*
* @return \Symfony\Component\DependencyInjection\Definition|\Symfony\Component\DependencyInjection\Alias
* @return Definition|Alias
*/
protected function resolveServiceDefinition($serviceId)
{
Expand All @@ -328,7 +330,7 @@ protected function resolveServiceDefinition($serviceId)
* Renders list of tagged services grouped by tag
*
* @param OutputInterface $output
* @param bool $showPrivate
* @param Boolean $showPrivate
*/
protected function outputTags(OutputInterface $output, $showPrivate = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class RouterApacheDumperCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function isEnabled()
{
Expand All @@ -42,7 +42,7 @@ public function isEnabled()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand All @@ -65,7 +65,7 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
114 changes: 65 additions & 49 deletions src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
* A console command for retrieving information about routes
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
*/
class RouterDebugCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function isEnabled()
{
Expand All @@ -40,7 +41,7 @@ public function isEnabled()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand All @@ -60,7 +61,9 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*
* @throws \InvalidArgumentException When route does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -83,34 +86,28 @@ protected function outputRoutes(OutputInterface $output, $routes = null)

$maxName = strlen('name');
$maxMethod = strlen('method');
$maxScheme = strlen('scheme');
$maxHost = strlen('host');

foreach ($routes as $name => $route) {
$requirements = $route->getRequirements();
$method = isset($requirements['_method'])
? strtoupper(is_array($requirements['_method'])
? implode(', ', $requirements['_method']) : $requirements['_method']
)
: 'ANY';
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$maxName = max($maxName, strlen($name));
$maxMethod = max($maxMethod, strlen($method));
$maxScheme = max($maxScheme, strlen($scheme));
$maxHost = max($maxHost, strlen($host));
}
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxHost.'s %s';

// displays the generated routes
$format1 = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxHost + 19).'s %s';
$output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Host</comment>', '<comment>Pattern</comment>'));
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %s';
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));

foreach ($routes as $name => $route) {
$requirements = $route->getRequirements();
$method = isset($requirements['_method'])
? strtoupper(is_array($requirements['_method'])
? implode(', ', $requirements['_method']) : $requirements['_method']
)
: 'ANY';
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$output->writeln(sprintf($format, $name, $method, $host, $route->getPath()));
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
}
}

Expand All @@ -124,41 +121,49 @@ protected function outputRoute(OutputInterface $output, $name)
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}

$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));

$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';

$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
$output->write('<comment>Name</comment> ');
$output->writeln($name, OutputInterface::OUTPUT_RAW);

$output->writeln(sprintf('<comment>Name</comment> %s', $name));
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPath()));
$output->writeln(sprintf('<comment>Host</comment> %s', $host));
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
$output->write('<comment>Path</comment> ');
$output->writeln($route->getPath(), OutputInterface::OUTPUT_RAW);

$defaults = '';
$d = $route->getDefaults();
ksort($d);
foreach ($d as $name => $value) {
$defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
}
$output->writeln(sprintf('<comment>Defaults</comment> %s', $defaults));
$output->write('<comment>Host</comment> ');
$output->writeln($host, OutputInterface::OUTPUT_RAW);

$requirements = '';
$r = $route->getRequirements();
ksort($r);
foreach ($r as $name => $value) {
$requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
}
$requirements = '' !== $requirements ? $requirements : 'NONE';
$output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements));

$options = '';
$o = $route->getOptions();
ksort($o);
foreach ($o as $name => $value) {
$options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
$output->write('<comment>Scheme</comment> ');
$output->writeln($scheme, OutputInterface::OUTPUT_RAW);

$output->write('<comment>Method</comment> ');
$output->writeln($method, OutputInterface::OUTPUT_RAW);

$output->write('<comment>Class</comment> ');
$output->writeln(get_class($route), OutputInterface::OUTPUT_RAW);

$output->write('<comment>Defaults</comment> ');
$output->writeln($this->formatConfigs($route->getDefaults()), OutputInterface::OUTPUT_RAW);

$output->write('<comment>Requirements</comment> ');
// we do not want to show the schemes and methods again that are also in the requirements for BC
$requirements = $route->getRequirements();
unset($requirements['_scheme'], $requirements['_method']);
$output->writeln($this->formatConfigs($requirements) ?: 'NO CUSTOM', OutputInterface::OUTPUT_RAW);

$output->write('<comment>Options</comment> ');
$output->writeln($this->formatConfigs($route->getOptions()), OutputInterface::OUTPUT_RAW);

$output->write('<comment>Path-Regex</comment> ');
$output->writeln($route->compile()->getRegex(), OutputInterface::OUTPUT_RAW);

if (null !== $route->compile()->getHostRegex()) {
$output->write('<comment>Host-Regex</comment> ');
$output->writeln($route->compile()->getHostRegex(), OutputInterface::OUTPUT_RAW);
}
$output->writeln(sprintf('<comment>Options</comment> %s', $options));
$output->write('<comment>Regex</comment> ');
$output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->compile()->getRegex())), OutputInterface::OUTPUT_RAW);
}

protected function formatValue($value)
Expand All @@ -173,4 +178,15 @@ protected function formatValue($value)

return preg_replace("/\n\s*/s", '', var_export($value, true));
}

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

return $string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class RouterMatchCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function isEnabled()
{
Expand All @@ -41,7 +41,7 @@ public function isEnabled()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand All @@ -61,7 +61,7 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class ServerRunCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function isEnabled()
{
Expand All @@ -37,7 +37,7 @@ public function isEnabled()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function configure()
{
Expand Down Expand Up @@ -74,7 +74,7 @@ protected function configure()
}

/**
* @see Command
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
Loading