Skip to content

Commit 22f9bc8

Browse files
jfsimonfabpot
authored andcommitted
[FrameworkBundle] adds routing/container descriptors
1 parent 77d3a85 commit 22f9bc8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2158
-462
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

+29-319
Large diffs are not rendered by default.

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

+16-143
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
1415
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
@@ -50,7 +51,9 @@ protected function configure()
5051
->setName('router:debug')
5152
->setDefinition(array(
5253
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
53-
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview')
54+
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
55+
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output route(s) in other formats'),
56+
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
5457
))
5558
->setDescription('Displays current routes for an application')
5659
->setHelp(<<<EOF
@@ -70,151 +73,21 @@ protected function configure()
7073
protected function execute(InputInterface $input, OutputInterface $output)
7174
{
7275
$name = $input->getArgument('name');
76+
$helper = new DescriptorHelper();
7377

7478
if ($name) {
75-
$this->outputRoute($output, $name);
76-
} else {
77-
$this->outputRoutes($output, null, $input->getOption('show-controllers'));
78-
}
79-
}
80-
81-
protected function outputRoutes(OutputInterface $output, $routes = null, $showControllers = false)
82-
{
83-
if (null === $routes) {
84-
$routes = $this->getContainer()->get('router')->getRouteCollection()->all();
85-
}
86-
87-
$output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
88-
89-
$maxName = strlen('name');
90-
$maxMethod = strlen('method');
91-
$maxScheme = strlen('scheme');
92-
$maxHost = strlen('host');
93-
$maxPath = strlen('path');
94-
95-
foreach ($routes as $name => $route) {
96-
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
97-
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
98-
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
99-
$path = $route->getPath();
100-
$maxName = max($maxName, strlen($name));
101-
$maxMethod = max($maxMethod, strlen($method));
102-
$maxScheme = max($maxScheme, strlen($scheme));
103-
$maxHost = max($maxHost, strlen($host));
104-
$maxPath = max($maxPath, strlen($path));
105-
}
106-
107-
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
108-
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %-'.($maxPath + 19).'s';
109-
110-
if ($showControllers) {
111-
$format = str_replace('s %s', 's %-'.$maxPath.'s %s', $format);
112-
$formatHeader = $formatHeader . ' %s';
113-
}
114-
115-
if ($showControllers) {
116-
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>', '<comment>Controller</comment>'));
117-
} else {
118-
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
119-
}
120-
121-
foreach ($routes as $name => $route) {
122-
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
123-
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
124-
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
125-
if ($showControllers) {
126-
$defaultData = $route->getDefaults();
127-
$controller = $defaultData['_controller'] ? $defaultData['_controller'] : '';
128-
if ($controller instanceof \Closure) {
129-
$controller = 'Closure';
130-
} else {
131-
if (is_object($controller)) {
132-
$controller = get_class($controller);
133-
}
134-
}
135-
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath(), $controller), OutputInterface::OUTPUT_RAW);
136-
} else {
137-
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
79+
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
80+
if (!$route) {
81+
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
13882
}
83+
$helper->describe($output, $route, $input->getOption('format'), $input->getOption('raw'), array('name' => $name));
84+
} else {
85+
$routes = $this->getContainer()->get('router')->getRouteCollection();
86+
$helper->describe($output, $routes, array(
87+
'format' => $input->getOption('format'),
88+
'raw_text' => $input->getOption('raw'),
89+
'show_controllers' => $input->getOption('show-controllers'),
90+
));
13991
}
14092
}
141-
142-
/**
143-
* @throws \InvalidArgumentException When route does not exist
144-
*/
145-
protected function outputRoute(OutputInterface $output, $name)
146-
{
147-
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
148-
if (!$route) {
149-
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
150-
}
151-
152-
$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
153-
154-
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
155-
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
156-
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
157-
158-
$output->write('<comment>Name</comment> ');
159-
$output->writeln($name, OutputInterface::OUTPUT_RAW);
160-
161-
$output->write('<comment>Path</comment> ');
162-
$output->writeln($route->getPath(), OutputInterface::OUTPUT_RAW);
163-
164-
$output->write('<comment>Host</comment> ');
165-
$output->writeln($host, OutputInterface::OUTPUT_RAW);
166-
167-
$output->write('<comment>Scheme</comment> ');
168-
$output->writeln($scheme, OutputInterface::OUTPUT_RAW);
169-
170-
$output->write('<comment>Method</comment> ');
171-
$output->writeln($method, OutputInterface::OUTPUT_RAW);
172-
173-
$output->write('<comment>Class</comment> ');
174-
$output->writeln(get_class($route), OutputInterface::OUTPUT_RAW);
175-
176-
$output->write('<comment>Defaults</comment> ');
177-
$output->writeln($this->formatConfigs($route->getDefaults()), OutputInterface::OUTPUT_RAW);
178-
179-
$output->write('<comment>Requirements</comment> ');
180-
// we do not want to show the schemes and methods again that are also in the requirements for BC
181-
$requirements = $route->getRequirements();
182-
unset($requirements['_scheme'], $requirements['_method']);
183-
$output->writeln($this->formatConfigs($requirements) ?: 'NO CUSTOM', OutputInterface::OUTPUT_RAW);
184-
185-
$output->write('<comment>Options</comment> ');
186-
$output->writeln($this->formatConfigs($route->getOptions()), OutputInterface::OUTPUT_RAW);
187-
188-
$output->write('<comment>Path-Regex</comment> ');
189-
$output->writeln($route->compile()->getRegex(), OutputInterface::OUTPUT_RAW);
190-
191-
if (null !== $route->compile()->getHostRegex()) {
192-
$output->write('<comment>Host-Regex</comment> ');
193-
$output->writeln($route->compile()->getHostRegex(), OutputInterface::OUTPUT_RAW);
194-
}
195-
}
196-
197-
protected function formatValue($value)
198-
{
199-
if (is_object($value)) {
200-
return sprintf('object(%s)', get_class($value));
201-
}
202-
203-
if (is_string($value)) {
204-
return $value;
205-
}
206-
207-
return preg_replace("/\n\s*/s", '', var_export($value, true));
208-
}
209-
210-
private function formatConfigs(array $array)
211-
{
212-
$string = '';
213-
ksort($array);
214-
foreach ($array as $name => $value) {
215-
$string .= ($string ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
216-
}
217-
218-
return $string;
219-
}
22093
}

0 commit comments

Comments
 (0)