5 * Contains \Drupal\Console\Command\RouterDebugCommand.
8 namespace Drupal\Console\Command\Router;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Core\Routing\RouteProviderInterface;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Component\Serialization\Yaml;
19 class DebugCommand extends Command
24 * @var RouteProviderInterface
26 protected $routeProvider;
29 * DebugCommand constructor.
31 * @param RouteProviderInterface $routeProvider
33 public function __construct(RouteProviderInterface $routeProvider)
35 $this->routeProvider = $routeProvider;
36 parent::__construct();
39 protected function configure()
42 ->setName('router:debug')
43 ->setDescription($this->trans('commands.router.debug.description'))
46 InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
47 $this->trans('commands.router.debug.arguments.route-name')
51 protected function execute(InputInterface $input, OutputInterface $output)
53 $io = new DrupalStyle($input, $output);
55 $route_name = $input->getArgument('route-name');
58 $this->getRouteByNames($io, $route_name);
60 $this->getAllRoutes($io);
64 protected function getAllRoutes(DrupalStyle $io)
66 $routes = $this->routeProvider->getAllRoutes();
69 $this->trans('commands.router.debug.messages.name'),
70 $this->trans('commands.router.debug.messages.path'),
74 foreach ($routes as $route_name => $route) {
75 $tableRows[] = [$route_name, $route->getPath()];
78 $io->table($tableHeader, $tableRows, 'compact');
81 protected function getRouteByNames(DrupalStyle $io, $route_name)
83 $routes = $this->routeProvider->getRoutesByNames($route_name);
85 foreach ($routes as $name => $route) {
87 $this->trans('commands.router.debug.messages.route'),
88 '<info>'.$name.'</info>'
93 '<comment>'.$this->trans('commands.router.debug.messages.path').'</comment>',
97 $tableRows[] = ['<comment>'.$this->trans('commands.router.debug.messages.defaults').'</comment>'];
98 $attributes = $this->addRouteAttributes($route->getDefaults());
99 foreach ($attributes as $attribute) {
100 $tableRows[] = $attribute;
103 $tableRows[] = ['<comment>'.$this->trans('commands.router.debug.messages.requirements').'</comment>'];
104 $requirements = $this->addRouteAttributes($route->getRequirements());
105 foreach ($requirements as $requirement) {
106 $tableRows[] = $requirement;
109 $tableRows[] = ['<comment>'.$this->trans('commands.router.debug.messages.options').'</comment>'];
110 $options = $this->addRouteAttributes($route->getOptions());
111 foreach ($options as $option) {
112 $tableRows[] = $option;
115 $io->table($tableHeader, $tableRows, 'compact');
119 protected function addRouteAttributes($attr, $attributes = null)
121 foreach ($attr as $key => $value) {
122 if (is_array($value)) {
132 $attributes[] = [' '.$key, $value];