Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Module / DownloadCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Module\DownloadCommand.
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\Console\Command\Command;
16 use Drupal\Console\Command\Shared\ProjectDownloadTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Utils\DrupalApi;
19 use GuzzleHttp\Client;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Utils\Validator;
22 use Drupal\Console\Utils\Site;
23 use Drupal\Console\Core\Utils\ConfigurationManager;
24 use Drupal\Console\Core\Utils\ShellProcess;
25
26 class DownloadCommand extends Command
27 {
28     use CommandTrait;
29     use ProjectDownloadTrait;
30
31     /**
32  * @var DrupalApi
33 */
34     protected $drupalApi;
35
36     /**
37  * @var Client
38 */
39     protected $httpClient;
40
41     /**
42      * @var string
43      */
44     protected $appRoot;
45
46     /**
47  * @var Manager
48 */
49     protected $extensionManager;
50
51     /**
52  * @var Validator
53 */
54     protected $validator;
55
56     /**
57  * @var ConfigurationManager
58 */
59     protected $configurationManager;
60
61     /**
62  * @var ShellProcess
63 */
64     protected $shellProcess;
65
66     /**
67      * @var string
68      */
69     protected $root;
70
71     /**
72      * DownloadCommand constructor.
73      *
74      * @param DrupalApi            $drupalApi
75      * @param Client               $httpClient
76      * @param $appRoot
77      * @param Manager              $extensionManager
78      * @param Validator            $validator
79      * @param Site                 $site
80      * @param ConfigurationManager $configurationManager
81      * @param ShellProcess         $shellProcess
82      * @param $root
83      */
84     public function __construct(
85         DrupalApi $drupalApi,
86         Client $httpClient,
87         $appRoot,
88         Manager $extensionManager,
89         Validator $validator,
90         Site $site,
91         ConfigurationManager $configurationManager,
92         ShellProcess $shellProcess,
93         $root
94     ) {
95         $this->drupalApi = $drupalApi;
96         $this->httpClient = $httpClient;
97         $this->appRoot = $appRoot;
98         $this->extensionManager = $extensionManager;
99         $this->validator = $validator;
100         $this->site = $site;
101         $this->configurationManager = $configurationManager;
102         $this->shellProcess = $shellProcess;
103         $this->root = $root;
104         parent::__construct();
105     }
106
107     protected function configure()
108     {
109         $this
110             ->setName('module:download')
111             ->setDescription($this->trans('commands.module.download.description'))
112             ->addArgument(
113                 'module',
114                 InputArgument::IS_ARRAY,
115                 $this->trans('commands.module.download.arguments.module')
116             )
117             ->addOption(
118                 'path',
119                 null,
120                 InputOption::VALUE_OPTIONAL,
121                 $this->trans('commands.module.download.options.path')
122             )
123             ->addOption(
124                 'latest',
125                 '',
126                 InputOption::VALUE_NONE,
127                 $this->trans('commands.module.download.options.latest')
128             )
129             ->addOption(
130                 'composer',
131                 '',
132                 InputOption::VALUE_NONE,
133                 $this->trans('commands.module.install.options.composer')
134             )
135             ->addOption(
136                 'unstable',
137                 '',
138                 InputOption::VALUE_NONE,
139                 $this->trans('commands.module.install.options.unstable')
140             );
141     }
142
143     /**
144      * {@inheritdoc}
145      */
146     protected function interact(InputInterface $input, OutputInterface $output)
147     {
148         $io = new DrupalStyle($input, $output);
149         $composer = $input->getOption('composer');
150         $module = $input->getArgument('module');
151
152         if (!$module) {
153             $module = $this->modulesQuestion($io);
154             $input->setArgument('module', $module);
155         }
156
157         if (!$composer) {
158             $path = $input->getOption('path');
159             if (!$path) {
160                 $path = $io->ask(
161                     $this->trans('commands.module.download.questions.path'),
162                     'modules/contrib'
163                 );
164                 $input->setOption('path', $path);
165             }
166         }
167     }
168
169     /**
170      * {@inheritdoc}
171      */
172     protected function execute(InputInterface $input, OutputInterface $output)
173     {
174         $io = new DrupalStyle($input, $output);
175
176         $modules = $input->getArgument('module');
177         $latest = $input->getOption('latest');
178         $path = $input->getOption('path');
179         $composer = $input->getOption('composer');
180         $unstable = true;
181
182         if ($composer) {
183             foreach ($modules as $module) {
184                 if (!$latest) {
185                     $versions = $this->drupalApi
186                         ->getPackagistModuleReleases($module, 10, $unstable);
187
188                     if (!$versions) {
189                         $io->error(
190                             sprintf(
191                                 $this->trans(
192                                     'commands.module.download.messages.no-releases'
193                                 ),
194                                 $module
195                             )
196                         );
197
198                         return 1;
199                     } else {
200                         $version = $io->choice(
201                             sprintf(
202                                 $this->trans(
203                                     'commands.site.new.questions.composer-release'
204                                 ),
205                                 $module
206                             ),
207                             $versions
208                         );
209                     }
210                 } else {
211                     $versions = $this->drupalApi
212                         ->getPackagistModuleReleases($module, 10, $unstable);
213
214                     if (!$versions) {
215                         $io->error(
216                             sprintf(
217                                 $this->trans(
218                                     'commands.module.download.messages.no-releases'
219                                 ),
220                                 $module
221                             )
222                         );
223                         return 1;
224                     } else {
225                         $version = current(
226                             $this->drupalApi
227                                 ->getPackagistModuleReleases($module, 1, $unstable)
228                         );
229                     }
230                 }
231
232                 // Register composer repository
233                 $command = "composer config repositories.drupal composer https://packagist.drupal-composer.org";
234                 $this->shellProcess->exec($command, $this->root);
235
236                 $command = sprintf(
237                     'composer require drupal/%s:%s --prefer-dist --optimize-autoloader --sort-packages --update-no-dev',
238                     $module,
239                     $version
240                 );
241
242                 if ($this->shellProcess->exec($command, $this->root)) {
243                     $io->success(
244                         sprintf(
245                             $this->trans('commands.module.download.messages.composer'),
246                             $module
247                         )
248                     );
249                 }
250             }
251         } else {
252             $this->downloadModules($io, $modules, $latest, $path);
253         }
254
255         return true;
256     }
257 }