Permalink
Browse files

refactored code to use get() when outputting a single route

this is useful for a CMS, where in most cases there will be too many routes to make it feasible to load all of them. here a router implementation will be used that will return an empty collection for ->all(). with this refactoring the given routes will not be listed via router:debug, but would still be shown when using router:debug [name]
  • Loading branch information...
1 parent 48af0ba commit 0c3536c7fd119c0b3d930478d545f29d9bf4fd9a @lsmith77 committed Apr 16, 2012
Showing with 16 additions and 15 deletions.
  1. +16 −15 src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
View
31 src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
@@ -48,22 +48,22 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $router = $this->getContainer()->get('router');
+ $name = $input->getArgument('name');
- $routes = array();
- foreach ($router->getRouteCollection()->all() as $name => $route) {
- $routes[$name] = $route->compile();
- }
-
- if ($input->getArgument('name')) {
- $this->outputRoute($output, $routes, $input->getArgument('name'));
+ if ($name) {
+ $this->outputRoute($output, $name);
} else {
- $this->outputRoutes($output, $routes);
+ $this->outputRoutes($output);
}
}
- protected function outputRoutes(OutputInterface $output, $routes)
+ protected function outputRoutes(OutputInterface $output)
{
+ $routes = array();
+ foreach ($this->getContainer()->get('router')->getRouteCollection()->all() as $name => $route) {
+ $routes[$name] = $route->compile();
+ }
+
$output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
$maxName = 4;
@@ -95,15 +95,16 @@ protected function outputRoutes(OutputInterface $output, $routes)
/**
* @throws \InvalidArgumentException When route does not exist
*/
- protected function outputRoute(OutputInterface $output, $routes, $name)
+ protected function outputRoute(OutputInterface $output, $name)
{
- $output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
-
- if (!isset($routes[$name])) {
+ $route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
+ if (!isset($route)) {
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}
- $route = $routes[$name];
+ $output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
+
+ $route = $route->compile();
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));

0 comments on commit 0c3536c

Please sign in to comment.