17cd7dc02639c7a0fc69ffaabf7dcb55a0b3a0d0
[yaffs-website] / vendor / drupal / console / src / Command / Debug / RouterCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Debug\RouterCommand.
6  */
7
8 namespace Drupal\Console\Command\Debug;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Routing\RouteProviderInterface;
15 use Drupal\Component\Serialization\Yaml;
16
17 class RouterCommand extends Command
18 {
19     /**
20      * @var RouteProviderInterface
21      */
22     protected $routeProvider;
23
24     /**
25      * DebugCommand constructor.
26      *
27      * @param RouteProviderInterface $routeProvider
28      */
29     public function __construct(RouteProviderInterface $routeProvider)
30     {
31         $this->routeProvider = $routeProvider;
32         parent::__construct();
33     }
34
35     protected function configure()
36     {
37         $this
38             ->setName('debug:router')
39             ->setDescription($this->trans('commands.debug.router.description'))
40             ->addArgument(
41                 'route-name',
42                 InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
43                 $this->trans('commands.debug.router.arguments.route-name')
44             )
45             ->setAliases(['dr']);
46     }
47
48     protected function execute(InputInterface $input, OutputInterface $output)
49     {
50         $route_name = $input->getArgument('route-name');
51
52         if ($route_name) {
53             $this->getRouteByNames($route_name);
54         } else {
55             $this->getAllRoutes();
56         }
57     }
58
59     protected function getAllRoutes()
60     {
61         $routes = $this->routeProvider->getAllRoutes();
62
63         $tableHeader = [
64             $this->trans('commands.debug.router.messages.name'),
65             $this->trans('commands.debug.router.messages.path'),
66         ];
67
68         $tableRows = [];
69         foreach ($routes as $route_name => $route) {
70             $tableRows[] = [$route_name, $route->getPath()];
71         }
72
73         $this->getIo()->table($tableHeader, $tableRows, 'compact');
74     }
75
76     protected function getRouteByNames($route_name)
77     {
78         $routes = $this->routeProvider->getRoutesByNames($route_name);
79
80         foreach ($routes as $name => $route) {
81             $tableHeader = [
82                 $this->trans('commands.debug.router.messages.route'),
83                 '<info>'.$name.'</info>'
84             ];
85             $tableRows = [];
86
87             $tableRows[] = [
88                 '<comment>'.$this->trans('commands.debug.router.messages.path').'</comment>',
89                 $route->getPath(),
90             ];
91
92             $tableRows[] = ['<comment>'.$this->trans('commands.debug.router.messages.defaults').'</comment>'];
93             $attributes = $this->addRouteAttributes($route->getDefaults());
94             foreach ($attributes as $attribute) {
95                 $tableRows[] = $attribute;
96             }
97
98             $tableRows[] = ['<comment>'.$this->trans('commands.debug.router.messages.requirements').'</comment>'];
99             $requirements = $this->addRouteAttributes($route->getRequirements());
100             foreach ($requirements as $requirement) {
101                 $tableRows[] = $requirement;
102             }
103
104             $tableRows[] = ['<comment>'.$this->trans('commands.debug.router.messages.options').'</comment>'];
105             $options = $this->addRouteAttributes($route->getOptions());
106             foreach ($options as $option) {
107                 $tableRows[] = $option;
108             }
109
110             $this->getIo()->table($tableHeader, $tableRows, 'compact');
111         }
112     }
113
114     protected function addRouteAttributes($attr, $attributes = null)
115     {
116         foreach ($attr as $key => $value) {
117             if (is_array($value)) {
118                 $attributes[] = [
119                   ' '.$key,
120                   str_replace(
121                       '- ',
122                       '',
123                       Yaml::encode($value)
124                   )
125                 ];
126             } else {
127                 $attributes[] = [' '.$key, $value];
128             }
129         }
130
131         return $attributes;
132     }
133 }