Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Module / InstallCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Module\InstallCommand.
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\Process\ProcessBuilder;
15 use Drupal\Console\Core\Command\Command;
16 use Drupal\Console\Command\Shared\ProjectDownloadTrait;
17 use Drupal\Console\Command\Shared\ModuleTrait;
18 use Drupal\Console\Utils\Site;
19 use Drupal\Console\Utils\Validator;
20 use Drupal\Core\Extension\ModuleInstallerInterface;
21 use Drupal\Console\Utils\DrupalApi;
22 use Drupal\Console\Extension\Manager;
23 use Drupal\Console\Core\Utils\ChainQueue;
24
25 /**
26  * Class InstallCommand
27  *
28  * @package Drupal\Console\Command\Module
29  */
30 class InstallCommand extends Command
31 {
32     use ProjectDownloadTrait;
33     use ModuleTrait;
34
35     /**
36      * @var Site
37      */
38     protected $site;
39
40     /**
41      * @var Validator
42      */
43     protected $validator;
44
45     /**
46      * @var ModuleInstaller
47      */
48     protected $moduleInstaller;
49
50     /**
51      * @var DrupalApi
52      */
53     protected $drupalApi;
54
55     /**
56      * @var Manager
57      */
58     protected $extensionManager;
59
60     /**
61      * @var string
62      */
63     protected $appRoot;
64
65     /**
66      * @var ChainQueue
67      */
68     protected $chainQueue;
69
70     /**
71      * InstallCommand constructor.
72      *
73      * @param Site            $site
74      * @param Validator       $validator
75      * @param ModuleInstaller $moduleInstaller
76      * @param DrupalApi       $drupalApi
77      * @param Manager         $extensionManager
78      * @param $appRoot
79      * @param ChainQueue      $chainQueue
80      */
81     public function __construct(
82         Site $site,
83         Validator $validator,
84         ModuleInstallerInterface $moduleInstaller,
85         DrupalApi $drupalApi,
86         Manager $extensionManager,
87         $appRoot,
88         ChainQueue $chainQueue
89     ) {
90         $this->site = $site;
91         $this->validator = $validator;
92         $this->moduleInstaller = $moduleInstaller;
93         $this->drupalApi = $drupalApi;
94         $this->extensionManager = $extensionManager;
95         $this->appRoot = $appRoot;
96         $this->chainQueue = $chainQueue;
97         parent::__construct();
98     }
99
100     /**
101      * {@inheritdoc}
102      */
103     protected function configure()
104     {
105         $this
106             ->setName('module:install')
107             ->setDescription($this->trans('commands.module.install.description'))
108             ->addArgument(
109                 'module',
110                 InputArgument::IS_ARRAY,
111                 $this->trans('commands.module.install.arguments.module')
112             )
113             ->addOption(
114                 'latest',
115                 null,
116                 InputOption::VALUE_NONE,
117                 $this->trans('commands.module.install.options.latest')
118             )
119             ->addOption(
120                 'composer',
121                 null,
122                 InputOption::VALUE_NONE,
123                 $this->trans('commands.module.uninstall.options.composer')
124             )
125             ->setAliases(['moi']);
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     protected function interact(InputInterface $input, OutputInterface $output)
132     {
133         $module = $input->getArgument('module');
134         if (!$module) {
135             $module = $this->modulesQuestion();
136             $input->setArgument('module', $module);
137         }
138     }
139
140     /**
141      * {@inheritdoc}
142      */
143     protected function execute(InputInterface $input, OutputInterface $output)
144     {
145         $module = $input->getArgument('module');
146         $latest = $input->getOption('latest');
147         $composer = $input->getOption('composer');
148
149         $this->site->loadLegacyFile('/core/includes/bootstrap.inc');
150
151         // check module's requirements
152         $this->moduleRequirement($module);
153
154         if ($composer) {
155             foreach ($module as $moduleItem) {
156                 $command = sprintf(
157                     'composer show drupal/%s ',
158                     $moduleItem
159                 );
160
161                 $processBuilder = new ProcessBuilder([]);
162                 $processBuilder->setWorkingDirectory($this->appRoot);
163                 $processBuilder->setArguments(explode(" ", $command));
164                 $process = $processBuilder->getProcess();
165                 $process->setTty('true');
166                 $process->run();
167
168                 if ($process->isSuccessful()) {
169                     $this->getIo()->info(
170                         sprintf(
171                             $this->trans('commands.module.install.messages.download-with-composer'),
172                             $moduleItem
173                         )
174                     );
175                 } else {
176                     $this->getIo()->error(
177                         sprintf(
178                             $this->trans('commands.module.install.messages.not-installed-with-composer'),
179                             $moduleItem
180                         )
181                     );
182                     throw new \RuntimeException($process->getErrorOutput());
183                 }
184             }
185
186             $unInstalledModules = $module;
187         } else {
188             $resultList = $this->downloadModules($module, $latest);
189
190             $invalidModules = $resultList['invalid'];
191             $unInstalledModules = $resultList['uninstalled'];
192
193             if ($invalidModules) {
194                 foreach ($invalidModules as $invalidModule) {
195                     unset($module[array_search($invalidModule, $module)]);
196                     $this->getIo()->error(
197                         sprintf(
198                             $this->trans('commands.module.install.messages.invalid-name'),
199                             $invalidModule
200                         )
201                     );
202                 }
203             }
204
205             if (!$unInstalledModules) {
206                 $this->getIo()->warning($this->trans('commands.module.install.messages.nothing'));
207
208                 return 0;
209             }
210         }
211
212         try {
213             $this->getIo()->comment(
214                 sprintf(
215                     $this->trans('commands.module.install.messages.installing'),
216                     implode(', ', $unInstalledModules)
217                 )
218             );
219
220             drupal_static_reset('system_rebuild_module_data');
221
222             $this->moduleInstaller->install($unInstalledModules, true);
223             $this->getIo()->success(
224                 sprintf(
225                     $this->trans('commands.module.install.messages.success'),
226                     implode(', ', $unInstalledModules)
227                 )
228             );
229         } catch (\Exception $e) {
230             $this->getIo()->error($e->getMessage());
231
232             return 1;
233         }
234
235         $this->site->removeCachedServicesFile();
236         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
237     }
238 }