Version 1
[yaffs-website] / vendor / drupal / console / src / Command / ContainerDebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\ContainerDebugCommand.
6  */
7
8 namespace Drupal\Console\Command;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputArgument;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Yaml\Yaml;
16 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 /**
20  * Class ContainerDebugCommand
21  *
22  * @package Drupal\Console\Command
23  */
24 class ContainerDebugCommand extends Command
25 {
26     use ContainerAwareCommandTrait;
27
28     /**
29      * {@inheritdoc}
30      */
31     protected function configure()
32     {
33         $this
34             ->setName('container:debug')
35             ->setDescription($this->trans('commands.container.debug.description'))
36             ->addOption(
37                 'parameters',
38                 null,
39                 InputOption::VALUE_NONE,
40                 $this->trans('commands.container.debug.arguments.service')
41             )
42             ->addArgument(
43                 'service',
44                 InputArgument::OPTIONAL,
45                 $this->trans('commands.container.debug.arguments.service')
46             );
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     protected function execute(InputInterface $input, OutputInterface $output)
53     {
54         $io = new DrupalStyle($input, $output);
55         $service = $input->getArgument('service');
56         $parameters = $input->getOption('parameters');
57
58         if ($parameters) {
59             $parameterList = $this->getParameterList();
60             ksort($parameterList);
61             $io->write(Yaml::dump(['parameters' => $parameterList], 4, 2));
62
63             return 0;
64         }
65
66         $tableHeader = [];
67         if ($service) {
68             $tableRows = $this->getServiceDetail($service);
69             $io->table($tableHeader, $tableRows, 'compact');
70
71             return 0;
72         }
73
74         $tableHeader = [
75             $this->trans('commands.container.debug.messages.service_id'),
76             $this->trans('commands.container.debug.messages.class_name')
77         ];
78
79         $tableRows = $this->getServiceList();
80         $io->table($tableHeader, $tableRows, 'compact');
81
82         return 0;
83     }
84
85     private function getServiceList()
86     {
87         $services = [];
88         $serviceDefinitions = $this->container
89             ->getParameter('console.service_definitions');
90
91         foreach ($serviceDefinitions as $serviceId => $serviceDefinition) {
92             $services[] = [$serviceId, $serviceDefinition->getClass()];
93         }
94         return $services;
95     }
96
97     private function getServiceDetail($service)
98     {
99         $serviceInstance = $this->get($service);
100         $serviceDetail = [];
101
102         if ($serviceInstance) {
103             $serviceDetail[] = [
104                 $this->trans('commands.container.debug.messages.service'),
105                 $service
106             ];
107             $serviceDetail[] = [
108                 $this->trans('commands.container.debug.messages.class'),
109                 get_class($serviceInstance)
110             ];
111             $serviceDetail[] = [
112                 $this->trans('commands.container.debug.messages.interface'),
113                 Yaml::dump(class_implements($serviceInstance))
114             ];
115             if ($parent = get_parent_class($serviceInstance)) {
116                 $serviceDetail[] = [
117                     $this->trans('commands.container.debug.messages.parent'),
118                     $parent
119                 ];
120             }
121             if ($vars = get_class_vars($serviceInstance)) {
122                 $serviceDetail[] = [
123                     $this->trans('commands.container.debug.messages.variables'),
124                     Yaml::dump($vars)
125                 ];
126             }
127             if ($methods = get_class_methods($serviceInstance)) {
128                 $serviceDetail[] = [
129                     $this->trans('commands.container.debug.messages.methods'),
130                     Yaml::dump($methods)
131                 ];
132             }
133         }
134
135         return $serviceDetail;
136     }
137
138     private function getParameterList()
139     {
140         $parameters = array_filter(
141             $this->container->getParameterBag()->all(), function ($name) {
142                 if (preg_match('/^container\./', $name)) {
143                     return false;
144                 }
145                 if (preg_match('/^drupal\./', $name)) {
146                     return false;
147                 }
148                 if (preg_match('/^console\./', $name)) {
149                     return false;
150                 }
151                 return true;
152             }, ARRAY_FILTER_USE_KEY
153         );
154
155         return $parameters;
156     }
157 }