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