835afe165ca59de233927754396c190447f06306
[yaffs-website] / vendor / drupal / console-core / src / Command / Generate / SiteAliasCommand.php
1 <?php
2
3 namespace Drupal\Console\Core\Command\Generate;
4
5 use Drupal\Console\Core\Command\Command;
6 use Drupal\Console\Core\Utils\ConfigurationManager;
7 use Drupal\Console\Core\Utils\DrupalFinder;
8 use Drupal\Console\Core\Generator\SiteAliasGenerator;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Output\OutputInterface;
12
13 /**
14  * Class SiteAliasCommand
15  *
16  * @package Drupal\Console\Command\Generate
17  */
18 class SiteAliasCommand extends Command
19 {
20     /**
21      * @var SiteAliasGenerator
22      */
23     protected $generator;
24
25     /**
26      * @var DrupalFinder
27      */
28     protected $drupalFinder;
29
30     /**
31      * @var ConfigurationManager
32      */
33     protected $configurationManager;
34
35     /**
36      * @var array
37      */
38     private $types = [
39         'local',
40         'ssh',
41         'container'
42     ];
43
44     /**
45      * @var array
46      */
47     private $extraOptions = [
48         'ssh' => [
49             'none' => '',
50             'vagrant' => '-o PasswordAuthentication=no -i ~/.vagrant.d/insecure_private_key',
51         ],
52         'container' => [
53             'none' => '',
54             'drupal4docker' => 'docker-compose exec --user=82 php'
55         ]
56     ];
57
58     public function __construct(
59         SiteAliasGenerator $generator,
60         ConfigurationManager $configurationManager,
61         DrupalFinder $drupalFinder
62     ) {
63         $this->generator = $generator;
64         $this->configurationManager = $configurationManager;
65         $this->drupalFinder = $drupalFinder;
66         parent::__construct();
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function configure()
73     {
74         $this
75             ->setName('generate:site:alias')
76             ->setDescription(
77                 $this->trans('commands.generate.site.alias.description')
78             )
79             ->setHelp($this->trans('commands.generate.site.alias.help'))
80             ->addOption(
81                 'site',
82                 null,
83                 InputOption::VALUE_NONE,
84                 $this->trans('commands.generate.site.alias.options.site')
85             )
86             ->addOption(
87                 'name',
88                 null,
89                 InputOption::VALUE_REQUIRED,
90                 $this->trans('commands.generate.site.alias.options.name')
91             )
92             ->addOption(
93                 'environment',
94                 null,
95                 InputOption::VALUE_REQUIRED,
96                 $this->trans('commands.generate.site.alias.options.environment')
97             )
98             ->addOption(
99                 'type',
100                 null,
101                 InputOption::VALUE_REQUIRED,
102                 $this->trans('commands.generate.site.alias.options.type')
103             )
104             ->addOption(
105                 'composer-root',
106                 null,
107                 InputOption::VALUE_OPTIONAL,
108                 $this->trans('commands.generate.site.alias.options.composer-root')
109             )
110             ->addOption(
111                 'site-uri',
112                 null,
113                 InputOption::VALUE_OPTIONAL,
114                 $this->trans('commands.generate.site.alias.options.site-uri')
115             )
116             ->addOption(
117                 'host',
118                 null,
119                 InputOption::VALUE_OPTIONAL,
120                 $this->trans('commands.generate.site.alias.options.host')
121             )
122             ->addOption(
123                 'user',
124                 null,
125                 InputOption::VALUE_OPTIONAL,
126                 $this->trans('commands.generate.site.alias.options.user')
127             )
128             ->addOption(
129                 'port',
130                 null,
131                 InputOption::VALUE_OPTIONAL,
132                 $this->trans('commands.generate.site.alias.options.port')
133             )
134             ->addOption(
135                 'extra-options',
136                 null,
137                 InputOption::VALUE_OPTIONAL,
138                 $this->trans('commands.generate.site.alias.options.extra-options')
139             )
140             ->addOption(
141                 'directory',
142                 null,
143                 InputOption::VALUE_REQUIRED,
144                 $this->trans('commands.generate.site.alias.options.directory')
145             )
146             ->setAliases(['gsa']);
147     }
148
149     /**
150      * {@inheritdoc}
151      */
152     protected function interact(
153         InputInterface $input,
154         OutputInterface $output
155     ) {
156         $site = $input->getOption('site');
157         $name = $input->getOption('name');
158         if (!$name) {
159             $sites = $this->configurationManager->getSites();
160             if (!empty($sites)) {
161                 $sites = array_keys($this->configurationManager->getSites());
162                 $name = $this->getIo()->choiceNoList(
163                     $this->trans('commands.generate.site.alias.questions.name'),
164                     $sites,
165                     current($sites),
166                     TRUE
167                 );
168
169                 if (is_numeric($name)) {
170                     $name = $sites[$name];
171                 }
172             } else {
173                 $name = $this->getIo()->ask(
174                     $this->trans('commands.generate.site.alias.questions.name')
175                 );
176             }
177
178             $input->setOption('name', $name);
179         }
180
181         $environment = $input->getOption('environment');
182         if (!$environment) {
183             $environment = $this->getIo()->ask(
184                 $this->trans('commands.generate.site.alias.questions.environment'),
185                 null
186             );
187
188             $input->setOption('environment', $environment);
189         }
190
191         $type = $input->getOption('type');
192         if (!$type) {
193             $type = $this->getIo()->choiceNoList(
194                 $this->trans('commands.generate.site.alias.questions.type'),
195                 $this->types
196             );
197
198             $input->setOption('type', $type);
199         }
200
201         $composerRoot = $input->getOption('composer-root');
202         if (!$composerRoot) {
203             $root = $this->drupalFinder->getComposerRoot();
204             if ($type === 'container') {
205                 $root = '/var/www/html';
206             }
207             if ($type === 'ssh') {
208                 $root = '/var/www/'.$name;
209             }
210             $composerRoot = $this->getIo()->ask(
211                 $this->trans('commands.generate.site.alias.questions.composer-root'),
212                 $root
213             );
214
215             $input->setOption('composer-root', $composerRoot);
216         }
217
218         $siteUri = $input->getOption('site-uri');
219         if (!$siteUri) {
220             $uri = explode('.', $environment);
221             if (count($uri)>1) {
222                 $uri = $uri[1];
223             } else {
224                 $uri = 'default';
225             }
226             $siteUri = $this->getIo()->askEmpty(
227                 $this->trans('commands.generate.site.alias.questions.site-uri'),
228                 $uri
229             );
230
231             $input->setOption('site-uri', $siteUri);
232         }
233
234         if ($type !== 'local') {
235             $extraOptions = $input->getOption('extra-options');
236             if (!$extraOptions) {
237                 $options = array_values($this->extraOptions[$type]);
238
239                 $extraOptions = $this->getIo()->choiceNoList(
240                     $this->trans(
241                         'commands.generate.site.alias.questions.extra-options'
242                     ),
243                     $options,
244                     current($options)
245                 );
246
247                 $input->setOption('extra-options', $extraOptions);
248             }
249
250             $host = $input->getOption('host');
251             if (!$host) {
252                 $host = $this->getIo()->askEmpty(
253                     $this->trans('commands.generate.site.alias.questions.host')
254                 );
255
256                 $input->setOption('host', $host);
257             }
258
259             $user = $input->getOption('user');
260             if (!$user) {
261                 $user = $this->getIo()->askEmpty(
262                     $this->trans('commands.generate.site.alias.questions.user')
263                 );
264
265                 $input->setOption('user', $user);
266             }
267
268             $port = $input->getOption('port');
269             if (!$port) {
270                 $port = $this->getIo()->askEmpty(
271                     $this->trans('commands.generate.site.alias.questions.port')
272                 );
273
274                 $input->setOption('port', $port);
275             }
276         }
277
278         $directory = $input->getOption('directory');
279         if ($site && $this->drupalFinder->getComposerRoot()) {
280             $directory = $this->drupalFinder->getComposerRoot() . '/console/';
281         }
282
283         if (!$directory) {
284             $directory = $this->getIo()->choice(
285                 $this->trans('commands.generate.site.alias.questions.directory'),
286                 $this->configurationManager->getConfigurationDirectories()
287             );
288
289             $input->setOption('directory', $directory);
290         }
291     }
292
293     /**
294      * {@inheritdoc}
295      */
296     protected function execute(
297         InputInterface $input,
298         OutputInterface $output
299     ) {
300         $site = $input->getOption('site');
301         $directory = $input->getOption('directory');
302         if ($site && $this->drupalFinder->isValidDrupal()) {
303             $directory = $this->drupalFinder->getComposerRoot() . '/console/';
304         }
305         $this->generator->generate(
306             [
307                 'name' => $input->getOption('name'),
308                 'environment' => $input->getOption('environment'),
309                 'type' => $input->getOption('type'),
310                 'extra_options' => $input->getOption('extra-options'),
311                 'root' => $input->getOption('composer-root'),
312                 'uri' => $input->getOption('site-uri'),
313                 'port' => $input->getOption('port'),
314                 'user' => $input->getOption('user'),
315                 'host' => $input->getOption('host'),
316                 'directory' => $directory
317             ]
318         );
319     }
320 }