2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Command;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2011-10-16 12:56:51 +01:00
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/**
|
2011-03-10 15:09:40 +00:00
|
|
|
* A console command for retrieving information about routes
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
2011-06-20 20:04:55 +01:00
|
|
|
class RouterDebugCommand extends ContainerAwareCommand
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2011-06-28 23:18:41 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isEnabled()
|
|
|
|
{
|
|
|
|
if (!$this->getContainer()->has('router')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$router = $this->getContainer()->get('router');
|
2011-10-16 12:56:51 +01:00
|
|
|
if (!$router instanceof RouterInterface) {
|
2011-06-28 23:18:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2011-06-28 23:18:41 +01:00
|
|
|
return parent::isEnabled();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this
|
2012-02-12 15:37:55 +00:00
|
|
|
->setName('router:debug')
|
2010-05-06 12:25:53 +01:00
|
|
|
->setDefinition(array(
|
|
|
|
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
|
|
|
|
))
|
|
|
|
->setDescription('Displays current routes for an application')
|
|
|
|
->setHelp(<<<EOF
|
2012-02-12 15:37:55 +00:00
|
|
|
The <info>%command.name%</info> displays the configured routes:
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-02-12 15:37:55 +00:00
|
|
|
<info>php %command.full_name%</info>
|
2010-05-06 12:25:53 +01:00
|
|
|
EOF
|
|
|
|
)
|
|
|
|
;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2012-04-17 09:08:13 +01:00
|
|
|
$name = $input->getArgument('name');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-04-17 09:08:13 +01:00
|
|
|
if ($name) {
|
|
|
|
$this->outputRoute($output, $name);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2012-04-17 09:08:13 +01:00
|
|
|
$this->outputRoutes($output);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 17:29:35 +01:00
|
|
|
protected function outputRoutes(OutputInterface $output, $routes = null)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2012-06-28 17:29:35 +01:00
|
|
|
if (null === $routes) {
|
2012-08-24 15:33:39 +01:00
|
|
|
$routes = $this->getContainer()->get('router')->getRouteCollection()->all();
|
2012-04-17 09:08:13 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
|
|
|
|
|
2012-04-14 18:21:10 +01:00
|
|
|
$maxName = strlen('name');
|
|
|
|
$maxMethod = strlen('method');
|
|
|
|
$maxHostname = strlen('hostname');
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($routes as $name => $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$requirements = $route->getRequirements();
|
2012-04-17 09:08:22 +01:00
|
|
|
$method = isset($requirements['_method'])
|
|
|
|
? strtoupper(is_array($requirements['_method'])
|
|
|
|
? implode(', ', $requirements['_method']) : $requirements['_method']
|
|
|
|
)
|
|
|
|
: 'ANY';
|
2012-12-06 11:52:20 +00:00
|
|
|
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
|
2012-04-14 18:21:10 +01:00
|
|
|
$maxName = max($maxName, strlen($name));
|
|
|
|
$maxMethod = max($maxMethod, strlen($method));
|
|
|
|
$maxHostname = max($maxHostname, strlen($hostname));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2012-04-14 18:21:10 +01:00
|
|
|
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxHostname.'s %s';
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
// displays the generated routes
|
2012-04-14 18:21:10 +01:00
|
|
|
$format1 = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxHostname + 19).'s %s';
|
|
|
|
$output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Hostname</comment>', '<comment>Pattern</comment>'));
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($routes as $name => $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$requirements = $route->getRequirements();
|
2012-04-17 09:08:22 +01:00
|
|
|
$method = isset($requirements['_method'])
|
|
|
|
? strtoupper(is_array($requirements['_method'])
|
|
|
|
? implode(', ', $requirements['_method']) : $requirements['_method']
|
|
|
|
)
|
|
|
|
: 'ANY';
|
2012-12-06 11:52:20 +00:00
|
|
|
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
|
2012-04-14 18:21:10 +01:00
|
|
|
$output->writeln(sprintf($format, $name, $method, $hostname, $route->getPattern()));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When route does not exist
|
|
|
|
*/
|
2012-04-17 09:08:13 +01:00
|
|
|
protected function outputRoute(OutputInterface $output, $name)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2012-04-17 09:08:13 +01:00
|
|
|
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
|
|
|
|
if (!$route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
|
|
|
|
}
|
|
|
|
|
2012-12-06 11:52:20 +00:00
|
|
|
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
|
|
|
|
|
2012-04-17 09:08:13 +01:00
|
|
|
$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
|
|
|
|
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
|
2012-12-06 11:52:20 +00:00
|
|
|
$output->writeln(sprintf('<comment>HostnamePattern</comment> %s', $hostname));
|
2010-05-06 12:25:53 +01:00
|
|
|
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
|
|
|
|
|
|
|
|
$defaults = '';
|
|
|
|
$d = $route->getDefaults();
|
|
|
|
ksort($d);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($d as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
|
|
|
}
|
2012-12-06 11:52:20 +00:00
|
|
|
$output->writeln(sprintf('<comment>Defaults</comment> %s', $defaults));
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$requirements = '';
|
|
|
|
$r = $route->getRequirements();
|
|
|
|
ksort($r);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($r as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
|
|
|
}
|
2012-12-06 13:14:56 +00:00
|
|
|
$requirements = '' !== $requirements ? $requirements : 'NONE';
|
2012-12-06 11:52:20 +00:00
|
|
|
$output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements));
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$options = '';
|
|
|
|
$o = $route->getOptions();
|
|
|
|
ksort($o);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($o as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
|
|
|
}
|
2012-12-06 11:52:20 +00:00
|
|
|
$output->writeln(sprintf('<comment>Options</comment> %s', $options));
|
|
|
|
$output->write('<comment>Regex</comment> ');
|
2012-08-24 15:41:40 +01:00
|
|
|
$output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->compile()->getRegex())), OutputInterface::OUTPUT_RAW);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function formatValue($value)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_object($value)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('object(%s)', get_class($value));
|
|
|
|
}
|
2011-02-27 16:24:32 +00:00
|
|
|
|
2011-04-09 23:53:33 +01:00
|
|
|
if (is_string($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2011-02-27 16:24:32 +00:00
|
|
|
return preg_replace("/\n\s*/s", '', var_export($value, true));
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|