Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Module / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Module\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Module;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Utils\Site;
18 use GuzzleHttp\Client;
19 use Drupal\Console\Core\Utils\ConfigurationManager;
20
21 class DebugCommand extends Command
22 {
23     use CommandTrait;
24
25     /**
26      * @var ConfigurationManager
27      */
28     protected $configurationManager;
29
30     /**
31      * @var Site
32      */
33     protected $site;
34
35     /**
36      * DebugCommand constructor.
37      *
38      * @param Client  $httpClient
39      */
40
41     protected $httpClient;
42
43     /**
44      * ChainDebugCommand constructor.
45      *
46      * @param ConfigurationManager $configurationManager
47      * @param Site                 $site
48      */
49     public function __construct(
50         ConfigurationManager $configurationManager,
51         Site $site,
52         Client $httpClient
53     ) {
54         $this->configurationManager = $configurationManager;
55         $this->site = $site;
56         $this->httpClient = $httpClient;
57         parent::__construct();
58     }
59
60     protected function configure()
61     {
62         $this
63             ->setName('module:debug')
64             ->setDescription($this->trans('commands.module.debug.description'))
65             ->addArgument(
66                 'module',
67                 InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
68                 $this->trans('commands.module.debug.module')
69             )
70             ->addOption(
71                 'status',
72                 null,
73                 InputOption::VALUE_OPTIONAL,
74                 $this->trans('commands.module.debug.options.status')
75             )
76             ->addOption(
77                 'type',
78                 null,
79                 InputOption::VALUE_OPTIONAL,
80                 $this->trans('commands.module.debug.options.type')
81             );
82     }
83
84     protected function execute(InputInterface $input, OutputInterface $output)
85     {
86         $io = new DrupalStyle($input, $output);
87
88         $this->site->loadLegacyFile('/core/modules/system/system.module');
89
90         $status = strtolower($input->getOption('status'));
91         $type = strtolower($input->getOption('type'));
92         $modules = strtolower($input->getArgument('module'));
93
94         if ($modules) {
95             $config = $this->configurationManager->getConfiguration();
96             $repo = $config->get('application.composer.repositories.default');
97
98             foreach ($modules as $module) {
99                 $url = sprintf(
100                     '%s/packages/drupal/%s.json',
101                     $config->get('application.composer.packages.default'),
102                     $module
103                 );
104
105                 try {
106                     $data = $this->httpClient->getUrlAsJson($repo . $url);
107                 } catch (\Exception $e) {
108                     $io->error(
109                         sprintf(
110                             $this->trans('commands.module.debug.messages.no-results'),
111                             $module
112                         )
113                     );
114
115                     return 1;
116                 }
117
118                 $tableHeader = [
119                   '<info>'.$data->package->name.'</info>'
120                 ];
121
122                 $tableRows = [];
123
124                 $tableRows[] = [
125                   $data->package->description
126                 ];
127
128                 $tableRows[] = [
129                   '<comment>'.$this->trans('commands.module.debug.messages.total-downloads').'</comment>',
130                   $data->package->downloads->total
131                 ];
132
133                 $tableRows[] = [
134                   '<comment>'.$this->trans('commands.module.debug.messages.total-monthly').'</comment>',
135                   $data->package->downloads->monthly
136                 ];
137
138                 $tableRows[] = [
139                   '<comment>'.$this->trans('commands.module.debug.messages.total-daily').'</comment>',
140                   $data->package->downloads->daily
141                 ];
142
143                 $io->table($tableHeader, $tableRows, 'compact');
144             }
145             return 0;
146         }
147
148         if ($status == 'installed') {
149             $status = 1;
150         } elseif ($status == 'uninstalled') {
151             $status = 0;
152         } else {
153             $status = -1;
154         }
155
156         if ($type == 'core') {
157             $type = 'core';
158         } elseif ($type == 'no-core') {
159             $type = '';
160         } else {
161             $type = null;
162         }
163
164         $tableHeader = [
165           $this->trans('commands.module.debug.messages.id'),
166           $this->trans('commands.module.debug.messages.name'),
167           $this->trans('commands.module.debug.messages.package'),
168           $this->trans('commands.module.debug.messages.version'),
169           $this->trans('commands.module.debug.messages.schema-version'),
170           $this->trans('commands.module.debug.messages.status'),
171           $this->trans('commands.module.debug.messages.origin'),
172         ];
173
174         $tableRows = [];
175         $modules = system_rebuild_module_data();
176         foreach ($modules as $module_id => $module) {
177             if ($status >= 0 && $status != $module->status) {
178                 continue;
179             }
180
181             if ($type !== null && $type !== $module->origin) {
182                 continue;
183             }
184
185             $module_status = ($module->status) ? $this->trans('commands.module.debug.messages.installed') : $this->trans('commands.module.debug.messages.uninstalled');
186             $module_origin = ($module->origin) ? $module->origin : 'no core';
187             $schema_version = (drupal_get_installed_schema_version($module_id)!= -1?drupal_get_installed_schema_version($module_id): '');
188
189             $tableRows [] = [
190               $module_id,
191               $module->info['name'],
192               $module->info['package'],
193               $module->info['version'],
194               $schema_version,
195               $module_status,
196               $module_origin,
197             ];
198         }
199
200         $io->table($tableHeader, $tableRows, 'compact');
201     }
202 }