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