Skip to content

Commit

Permalink
Merge pull request #118 from dmouse/router-debug
Browse files Browse the repository at this point in the history
Router debug
  • Loading branch information
jmolivas committed Jul 3, 2014
2 parents aa1dd82 + 3a52333 commit d529df3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/Command/ContainerAwareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ public function getServices()
return $this->services;
}

public function getRoutes()
public function getRouteProvider()
{
if (null === $this->routes) {
$this->routes = [];
$routeProvider = $this->getContainer()->get('router.route_provider');
$this->routes = $routeProvider->getAllRoutes();
if (null === $this->route_provider) {
$this->route_provider = $this->getContainer()->get('router.route_provider');
}

return $this->routes;
return $this->route_provider;
}

/**
Expand Down
56 changes: 51 additions & 5 deletions src/Command/RouterDebugCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\ContainerDebugCommand.
* Contains \Drupal\AppConsole\Command\RouterDebugCommand.
*/

namespace Drupal\AppConsole\Command;
Expand All @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\TableHelper;

class RouterDebugCommand extends ContainerAwareCommand
{
Expand All @@ -19,20 +20,65 @@ protected function configure()
$this
->setName('router:debug')
->setDescription('Displays current routes for an application')
->addArgument('route-name', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Route names')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$routes = $this->getRoutes();

$route_name = $input->getArgument('route-name');
$table = $this->getHelperSet()->get('table');
$table->setHeaders(['Name', 'Path']);
$table->setlayout($table::LAYOUT_COMPACT);
if ($route_name) {
$this->getRouteByNames($route_name, $output, $table);
}
else {
$this->getAllRoutes($output, $table);
}
}

protected function getAllRoutes($output, $table)
{
$rp = $this->getRouteProvider();
$routes = $rp->getAllRoutes();

$table->setHeaders(['Name', 'Path']);
foreach ($routes as $route_name => $route) {
$table->addRow([$route_name, $route->getPath()]);
}
$table->render($output);
}

protected function getRouteByNames($route_name, $output, $table)
{
$rp = $this->getRouteProvider();
$routes = $rp->getRoutesByNames($route_name);
$table->setHeaders(['Route name', 'Options']);
$table->setlayout(TableHelper::LAYOUT_BORDERLESS);

$rows = [];
foreach ($routes as $name => $route) {
$table->addRow(['<info>'.$name.'</info>']);
$table->addRow([' <comment>+ Pattern</comment>', $route->getPath()]);

$table->addRow([' <comment>+ Defaults</comment>']);
$table = $this->addRouteAttributes($route->getDefaults(), $table);

$table->addRow([' <comment>+ Options</comment>']);
$table = $this->addRouteAttributes($route->getOptions(), $table);
}
$table->render($output);
}

protected function addRouteAttributes($attr, $table)
{
foreach ($attr as $key => $value) {
if (is_array($value)) {
$table= $this->addRouteAttributes($value, $table);
}
else {
$table->addRow([' <comment>- </comment>'.$key, $value]);
}
}
return $table;
}
}

0 comments on commit d529df3

Please sign in to comment.