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