Skip to content

[FrameworkBundle] adds routing/container descriptors #7887

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
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0130339
[FrameworkBundle] added routing descriptors
jfsimon Apr 30, 2013
ed83497
[FrameworkBundle] added text container descriptors
jfsimon May 7, 2013
dc7eb94
[FrameworkBundle] added descriptor helper & missing methods
jfsimon May 7, 2013
821c5b0
[FrameworkBundle] added container service & alias descriptors
jfsimon May 31, 2013
651d809
[FrameworkBundle] added container services description
jfsimon Jun 2, 2013
800d3f5
[FrameworkBundle] added container services description by tag
jfsimon Jun 2, 2013
535c786
[FrameworkBundle] added container parameters description
jfsimon Jun 2, 2013
603c1b2
[FrameworkBundle] added container tags descriptions
jfsimon Jun 3, 2013
3aa7438
[FrameworkBundle] added descriptors usage in router:debug command
jfsimon Jun 4, 2013
036da08
[FrameworkBundle] added container parameters sorting in descriptors
jfsimon Jun 4, 2013
2290a99
[FrameworkBundle] added container services sorting in descriptors
jfsimon Jun 4, 2013
1747d26
[FrameworkBundle] added single service descriptor
jfsimon Jun 4, 2013
ca6e90d
[FrameworkBundle] added descriptors usage in container:debug command
jfsimon Jun 4, 2013
bcc3ae3
[FrameworkBundle] applied advices from gh comments
jfsimon Jun 4, 2013
8e84dac
[FrameworkBundle] fixed json & md decriptors
jfsimon Sep 19, 2013
d6ef568
[FrameworkBundle] fixed text & xml descriptors
jfsimon Sep 20, 2013
52b331b
[FrameworkBundle] added table helper usage to descriptors
jfsimon Sep 23, 2013
b8fa077
[FrameworkBundle] fixed xml route description
jfsimon Sep 27, 2013
29a940d
[FrameworkBundle] changed xml routing
jfsimon Sep 27, 2013
ad939b6
[FrameworkBundle] added route host regex to xml descriptors
jfsimon Sep 29, 2013
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
348 changes: 29 additions & 319 deletions src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Large diffs are not rendered by default.

159 changes: 16 additions & 143 deletions src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -50,7 +51,9 @@ protected function configure()
->setName('router:debug')
->setDefinition(array(
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview')
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output route(s) in other formats'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
))
->setDescription('Displays current routes for an application')
->setHelp(<<<EOF
Expand All @@ -70,151 +73,21 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$helper = new DescriptorHelper();

if ($name) {
$this->outputRoute($output, $name);
} else {
$this->outputRoutes($output, null, $input->getOption('show-controllers'));
}
}

protected function outputRoutes(OutputInterface $output, $routes = null, $showControllers = false)
{
if (null === $routes) {
$routes = $this->getContainer()->get('router')->getRouteCollection()->all();
}

$output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));

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

foreach ($routes as $name => $route) {
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$path = $route->getPath();
$maxName = max($maxName, strlen($name));
$maxMethod = max($maxMethod, strlen($method));
$maxScheme = max($maxScheme, strlen($scheme));
$maxHost = max($maxHost, strlen($host));
$maxPath = max($maxPath, strlen($path));
}

$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %-'.($maxPath + 19).'s';

if ($showControllers) {
$format = str_replace('s %s', 's %-'.$maxPath.'s %s', $format);
$formatHeader = $formatHeader . ' %s';
}

if ($showControllers) {
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>', '<comment>Controller</comment>'));
} else {
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
}

foreach ($routes as $name => $route) {
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
if ($showControllers) {
$defaultData = $route->getDefaults();
$controller = $defaultData['_controller'] ? $defaultData['_controller'] : '';
if ($controller instanceof \Closure) {
$controller = 'Closure';
} else {
if (is_object($controller)) {
$controller = get_class($controller);
}
}
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath(), $controller), OutputInterface::OUTPUT_RAW);
} else {
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
if (!$route) {
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}
$helper->describe($output, $route, $input->getOption('format'), $input->getOption('raw'), array('name' => $name));
} else {
$routes = $this->getContainer()->get('router')->getRouteCollection();
$helper->describe($output, $routes, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
));
}
}

/**
* @throws \InvalidArgumentException When route does not exist
*/
protected function outputRoute(OutputInterface $output, $name)
{
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
if (!$route) {
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->write('<comment>Name</comment> ');
$output->writeln($name, OutputInterface::OUTPUT_RAW);

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

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

$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);
}
}

protected function formatValue($value)
{
if (is_object($value)) {
return sprintf('object(%s)', get_class($value));
}

if (is_string($value)) {
return $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;
}
}
Loading