Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / PluginDebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\PluginDebugCommand.
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\Command\Command;
14 use Symfony\Component\Yaml\Yaml;
15 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class DebugCommand
20  *
21  * @package Drupal\Console\Command
22  */
23 class PluginDebugCommand extends Command
24 {
25     use ContainerAwareCommandTrait;
26     /**
27      * {@inheritdoc}
28      */
29     protected function configure()
30     {
31         $this->setName('plugin:debug')
32             ->setDescription($this->trans('commands.plugin.debug.description'))
33             ->setHelp($this->trans('commands.plugin.debug.help'))
34             ->addArgument(
35                 'type',
36                 InputArgument::OPTIONAL,
37                 $this->trans('commands.plugin.debug.arguments.type')
38             )
39             ->addArgument(
40                 'id',
41                 InputArgument::OPTIONAL,
42                 $this->trans('commands.plugin.debug.arguments.id')
43             );
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     protected function execute(InputInterface $input, OutputInterface $output)
50     {
51         $io = new DrupalStyle($input, $output);
52
53         $pluginType = $input->getArgument('type');
54         $pluginId = $input->getArgument('id');
55
56         // No plugin type specified, show a list of plugin types.
57         if (!$pluginType) {
58             $tableHeader = [
59                 $this->trans('commands.plugin.debug.table-headers.plugin-type-name'),
60                 $this->trans('commands.plugin.debug.table-headers.plugin-type-class')
61             ];
62             $tableRows = [];
63             $serviceDefinitions = $this->container
64                 ->getParameter('console.service_definitions');
65
66             foreach ($serviceDefinitions as $serviceId => $serviceDefinition) {
67                 if (strpos($serviceId, 'plugin.manager.') === 0) {
68                     $serviceName = substr($serviceId, 15);
69                     $tableRows[$serviceName] = [
70                         $serviceName,
71                         $serviceDefinition->getClass()
72                     ];
73                 }
74             }
75
76             ksort($tableRows);
77             $io->table($tableHeader, array_values($tableRows));
78
79             return true;
80         }
81
82         $service = $this->container->get('plugin.manager.' . $pluginType);
83         if (!$service) {
84             $io->error(
85                 sprintf(
86                     $this->trans('commands.plugin.debug.errors.plugin-type-not-found'),
87                     $pluginType
88                 )
89             );
90             return false;
91         }
92
93         // Valid plugin type specified, no ID specified, show list of instances.
94         if (!$pluginId) {
95             $tableHeader = [
96                 $this->trans('commands.plugin.debug.table-headers.plugin-id'),
97                 $this->trans('commands.plugin.debug.table-headers.plugin-class')
98             ];
99             $tableRows = [];
100             foreach ($service->getDefinitions() as $definition) {
101                 $pluginId = $definition['id'];
102                 $className = $definition['class'];
103                 $tableRows[$pluginId] = [$pluginId, $className];
104             }
105             ksort($tableRows);
106             $io->table($tableHeader, array_values($tableRows));
107             return true;
108         }
109
110         // Valid plugin type specified, ID specified, show the definition.
111         $definition = $service->getDefinition($pluginId);
112         $tableHeader = [
113             $this->trans('commands.plugin.debug.table-headers.definition-key'),
114             $this->trans('commands.plugin.debug.table-headers.definition-value')
115         ];
116         $tableRows = [];
117         foreach ($definition as $key => $value) {
118             if (is_object($value) && method_exists($value, '__toString')) {
119                 $value = (string) $value;
120             } elseif (is_array($value) || is_object($value)) {
121                 $value = Yaml::dump($value);
122             } elseif (is_bool($value)) {
123                 $value = ($value) ? 'TRUE' : 'FALSE';
124             }
125             $tableRows[$key] = [$key, $value];
126         }
127         ksort($tableRows);
128         $io->table($tableHeader, array_values($tableRows));
129         return true;
130     }
131 }