Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Router / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\RouterDebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Router;
9
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;
18
19 class DebugCommand extends Command
20 {
21     use CommandTrait;
22
23     /**
24      * @var RouteProviderInterface
25      */
26     protected $routeProvider;
27
28     /**
29      * DebugCommand constructor.
30      *
31      * @param RouteProviderInterface $routeProvider
32      */
33     public function __construct(RouteProviderInterface $routeProvider)
34     {
35         $this->routeProvider = $routeProvider;
36         parent::__construct();
37     }
38
39     protected function configure()
40     {
41         $this
42             ->setName('router:debug')
43             ->setDescription($this->trans('commands.router.debug.description'))
44             ->addArgument(
45                 'route-name',
46                 InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
47                 $this->trans('commands.router.debug.arguments.route-name')
48             );
49     }
50
51     protected function execute(InputInterface $input, OutputInterface $output)
52     {
53         $io = new DrupalStyle($input, $output);
54
55         $route_name = $input->getArgument('route-name');
56
57         if ($route_name) {
58             $this->getRouteByNames($io, $route_name);
59         } else {
60             $this->getAllRoutes($io);
61         }
62     }
63
64     protected function getAllRoutes(DrupalStyle $io)
65     {
66         $routes = $this->routeProvider->getAllRoutes();
67
68         $tableHeader = [
69             $this->trans('commands.router.debug.messages.name'),
70             $this->trans('commands.router.debug.messages.path'),
71         ];
72
73         $tableRows = [];
74         foreach ($routes as $route_name => $route) {
75             $tableRows[] = [$route_name, $route->getPath()];
76         }
77
78         $io->table($tableHeader, $tableRows, 'compact');
79     }
80
81     protected function getRouteByNames(DrupalStyle $io, $route_name)
82     {
83         $routes = $this->routeProvider->getRoutesByNames($route_name);
84
85         foreach ($routes as $name => $route) {
86             $tableHeader = [
87                 $this->trans('commands.router.debug.messages.route'),
88                 '<info>'.$name.'</info>'
89             ];
90             $tableRows = [];
91
92             $tableRows[] = [
93                 '<comment>'.$this->trans('commands.router.debug.messages.path').'</comment>',
94                 $route->getPath(),
95             ];
96
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;
101             }
102
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;
107             }
108
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;
113             }
114
115             $io->table($tableHeader, $tableRows, 'compact');
116         }
117     }
118
119     protected function addRouteAttributes($attr, $attributes = null)
120     {
121         foreach ($attr as $key => $value) {
122             if (is_array($value)) {
123                 $attributes[] = [
124                   ' '.$key,
125                   str_replace(
126                       '- ',
127                       '',
128                       Yaml::encode($value)
129                   )
130                 ];
131             } else {
132                 $attributes[] = [' '.$key, $value];
133             }
134         }
135
136         return $attributes;
137     }
138 }