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>
|
2013-03-08 12:06:24 +00:00
|
|
|
* @author Tobias Schultze <http://tobion.de>
|
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
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-06-28 23:18:41 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When route does not exist
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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');
|
2013-03-08 12:06:24 +00:00
|
|
|
$maxScheme = strlen('scheme');
|
2013-01-21 16:57:28 +00:00
|
|
|
$maxHost = strlen('host');
|
2012-04-14 18:21:10 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($routes as $name => $route) {
|
2013-03-08 12:06:24 +00:00
|
|
|
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
|
|
|
|
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
|
2013-01-21 16:57:28 +00:00
|
|
|
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
|
2012-04-14 18:21:10 +01:00
|
|
|
$maxName = max($maxName, strlen($name));
|
|
|
|
$maxMethod = max($maxMethod, strlen($method));
|
2013-03-08 12:06:24 +00:00
|
|
|
$maxScheme = max($maxScheme, strlen($scheme));
|
2013-01-21 16:57:28 +00:00
|
|
|
$maxHost = max($maxHost, strlen($host));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 12:06:24 +00:00
|
|
|
$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>'));
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($routes as $name => $route) {
|
2013-03-08 12:06:24 +00:00
|
|
|
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
|
|
|
|
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
|
2013-01-21 16:57:28 +00:00
|
|
|
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
|
2013-03-08 12:06:24 +00:00
|
|
|
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
|
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));
|
|
|
|
}
|
|
|
|
|
2013-03-08 12:06:24 +00:00
|
|
|
$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';
|
2013-01-21 16:57:28 +00:00
|
|
|
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
|
2012-12-06 11:52:20 +00:00
|
|
|
|
2013-03-08 12:06:24 +00:00
|
|
|
$output->write('<comment>Name</comment> ');
|
|
|
|
$output->writeln($name, OutputInterface::OUTPUT_RAW);
|
2012-04-17 09:08:13 +01:00
|
|
|
|
2013-03-08 12:06:24 +00:00
|
|
|
$output->write('<comment>Path</comment> ');
|
|
|
|
$output->writeln($route->getPath(), OutputInterface::OUTPUT_RAW);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2013-03-08 12:06:24 +00:00
|
|
|
$output->write('<comment>Host</comment> ');
|
|
|
|
$output->writeln($host, OutputInterface::OUTPUT_RAW);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2013-03-08 12:06:24 +00:00
|
|
|
$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);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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
|
|
|
}
|
2013-03-08 12:06:24 +00:00
|
|
|
|
|
|
|
private function formatConfigs(array $array)
|
|
|
|
{
|
|
|
|
$string = '';
|
|
|
|
ksort($array);
|
|
|
|
foreach ($array as $name => $value) {
|
2013-04-02 10:39:57 +01:00
|
|
|
$string .= ($string ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
2013-03-08 12:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|