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