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