Skip to content
Permalink
Browse files

Add a new option pattern. Improve the code styling (#3913)

  • Loading branch information...
LOBsTerr committed Apr 6, 2019
1 parent e952107 commit ee8ebb5dabc2e84e71d3b64467dabb791b9e8a89
Showing with 24 additions and 15 deletions.
  1. +24 −15 src/Command/Debug/RouterCommand.php
@@ -7,12 +7,14 @@
namespace Drupal\Console\Command\Debug;
use Drupal\Console\Core\Command\Command;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Routing\RouteProviderInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Component\Serialization\Yaml;
class RouterCommand extends Command
{
@@ -42,15 +44,24 @@ protected function configure()
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
$this->trans('commands.debug.router.arguments.route-name')
)
->addOption(
'pattern',
null,
InputArgument::OPTIONAL,
$this->trans('commands.debug.router.options.pattern')
)
->setAliases(['dr']);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$route_name = $input->getArgument('route-name');
$pattern = $input->getOption('pattern');
if ($route_name) {
$this->getRouteByNames($route_name);
if (!empty($route_name)) {
$this->getRoutesTables($this->routeProvider->getRoutesByNames($route_name));
} elseif (!empty($pattern)) {
$this->getRoutesTables($this->routeProvider->getRoutesByPattern($pattern));
} else {
$this->getAllRoutes();
}
@@ -73,35 +84,33 @@ protected function getAllRoutes()
$this->getIo()->table($tableHeader, $tableRows, 'compact');
}
protected function getRouteByNames($route_name)
protected function getRoutesTables($routes)
{
$routes = $this->routeProvider->getRoutesByNames($route_name);
foreach ($routes as $name => $route) {
$tableHeader = [
$this->trans('commands.debug.router.messages.route'),
'<info>'.$name.'</info>'
'<info>' . $name . '</info>'
];
$tableRows = [];
$tableRows[] = [
'<comment>'.$this->trans('commands.debug.router.messages.path').'</comment>',
'<comment>' . $this->trans('commands.debug.router.messages.path') . '</comment>',
$route->getPath(),
];
$tableRows[] = ['<comment>'.$this->trans('commands.debug.router.messages.defaults').'</comment>'];
$tableRows[] = ['<comment>' . $this->trans('commands.debug.router.messages.defaults') . '</comment>'];
$attributes = $this->addRouteAttributes($route->getDefaults());
foreach ($attributes as $attribute) {
$tableRows[] = $attribute;
}
$tableRows[] = ['<comment>'.$this->trans('commands.debug.router.messages.requirements').'</comment>'];
$tableRows[] = ['<comment>' . $this->trans('commands.debug.router.messages.requirements') . '</comment>'];
$requirements = $this->addRouteAttributes($route->getRequirements());
foreach ($requirements as $requirement) {
$tableRows[] = $requirement;
}
$tableRows[] = ['<comment>'.$this->trans('commands.debug.router.messages.options').'</comment>'];
$tableRows[] = ['<comment>' . $this->trans('commands.debug.router.messages.options') . '</comment>'];
$options = $this->addRouteAttributes($route->getOptions());
foreach ($options as $option) {
$tableRows[] = $option;
@@ -116,15 +125,15 @@ protected function addRouteAttributes($attr, $attributes = null)
foreach ($attr as $key => $value) {
if (is_array($value)) {
$attributes[] = [
' '.$key,
' ' . $key,
str_replace(
'- ',
'',
Yaml::encode($value)
)
];
} else {
$attributes[] = [' '.$key, $value];
$attributes[] = [' ' . $key, $value];
}
}

0 comments on commit ee8ebb5

Please sign in to comment.
You can’t perform that action at this time.