Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / drupal / console-core / src / Command / InitCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\InitCommand.
6  */
7
8 namespace Drupal\Console\Core\Command;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Filesystem\Filesystem;
14 use Symfony\Component\Process\ProcessBuilder;
15 use Symfony\Component\Finder\Finder;
16 use Drupal\Console\Core\Utils\ConfigurationManager;
17 use Drupal\Console\Core\Generator\InitGenerator;
18 use Drupal\Console\Core\Utils\ShowFile;
19
20 /**
21  * Class InitCommand
22  *
23  * @package Drupal\Console\Core\Command
24  */
25 class InitCommand extends Command
26 {
27     /**
28      * @var ShowFile
29      */
30     protected $showFile;
31
32     /**
33      * @var ConfigurationManager
34      */
35     protected $configurationManager;
36
37     /**
38      * @var string
39      */
40     protected $appRoot;
41
42     /**
43      * @var string
44      */
45     protected $consoleRoot;
46
47     /**
48      * @var InitGenerator
49      */
50     protected $generator;
51
52     private $configParameters = [
53         'language' => 'en',
54         'temp' => '/tmp',
55         'chain' => false,
56         'sites' => false,
57         'learning' => false,
58         'generate_inline' => false,
59         'generate_chain' => false
60     ];
61
62     private $directories = [
63       'chain',
64       'sites',
65     ];
66
67     /**
68      * InitCommand constructor.
69      *
70      * @param ShowFile             $showFile
71      * @param ConfigurationManager $configurationManager
72      * @param InitGenerator        $generator
73      * @param string               $appRoot
74      * @param string               $consoleRoot
75      */
76     public function __construct(
77         ShowFile $showFile,
78         ConfigurationManager $configurationManager,
79         InitGenerator $generator,
80         $appRoot,
81         $consoleRoot = null
82     ) {
83         $this->showFile = $showFile;
84         $this->configurationManager = $configurationManager;
85         $this->generator = $generator;
86         $this->appRoot = $appRoot;
87         $this->consoleRoot = $consoleRoot;
88         parent::__construct();
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     protected function configure()
95     {
96         $this
97             ->setName('init')
98             ->setDescription($this->trans('commands.init.description'))
99             ->addOption(
100                 'destination',
101                 null,
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.init.options.destination')
104             )
105             ->addOption(
106                 'override',
107                 null,
108                 InputOption::VALUE_NONE,
109                 $this->trans('commands.init.options.override')
110             )
111             ->addOption(
112                 'autocomplete',
113                 null,
114                 InputOption::VALUE_NONE,
115                 $this->trans('commands.init.options.autocomplete')
116             );
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     protected function interact(InputInterface $input, OutputInterface $output)
123     {
124         $destination = $input->getOption('destination');
125         $autocomplete = $input->getOption('autocomplete');
126         $configuration = $this->configurationManager->getConfiguration();
127
128         if (!$destination) {
129             if ($this->appRoot && $this->consoleRoot) {
130                 $destination = $this->getIo()->choice(
131                     $this->trans('commands.init.questions.destination'),
132                     $this->configurationManager->getConfigurationDirectories()
133                 );
134             } else {
135                 $destination = $this->configurationManager
136                     ->getConsoleDirectory();
137             }
138
139             $input->setOption('destination', $destination);
140         }
141
142         $this->configParameters['language'] = $this->getIo()->choiceNoList(
143             $this->trans('commands.init.questions.language'),
144             array_keys($configuration->get('application.languages'))
145         );
146
147         $this->configParameters['temp'] = $this->getIo()->ask(
148             $this->trans('commands.init.questions.temp'),
149             '/tmp'
150         );
151
152         $this->configParameters['chain'] = $this->getIo()->confirm(
153             $this->trans('commands.init.questions.chain'),
154             false
155         );
156
157         $this->configParameters['sites'] = $this->getIo()->confirm(
158             $this->trans('commands.init.questions.sites'),
159             false
160         );
161
162         $this->configParameters['learning'] = $this->getIo()->confirm(
163             $this->trans('commands.init.questions.learning'),
164             false
165         );
166
167         $this->configParameters['generate_inline'] = $this->getIo()->confirm(
168             $this->trans('commands.init.questions.generate-inline'),
169             false
170         );
171
172         $this->configParameters['generate_chain'] = $this->getIo()->confirm(
173             $this->trans('commands.init.questions.generate-chain'),
174             false
175         );
176
177         if (!$autocomplete) {
178             $autocomplete = $this->getIo()->confirm(
179                 $this->trans('commands.init.questions.autocomplete'),
180                 false
181             );
182             $input->setOption('autocomplete', $autocomplete);
183         }
184     }
185
186     /**
187      * {@inheritdoc}
188      */
189     protected function execute(InputInterface $input, OutputInterface $output)
190     {
191         $copiedFiles = [];
192         $destination = $input->getOption('destination');
193         $autocomplete = $input->getOption('autocomplete');
194         $override = $input->getOption('override');
195         if (!$destination) {
196             $destination = $this->configurationManager->getConsoleDirectory();
197         }
198
199         $finder = new Finder();
200         $finder->in(
201             sprintf(
202                 '%sdist/',
203                 $this->configurationManager->getVendorCoreRoot()
204             )
205         );
206         if (!$this->configParameters['chain']) {
207             $finder->exclude('chain');
208         }
209         if (!$this->configParameters['sites']) {
210             $finder->exclude('sites');
211         }
212         $finder->files();
213
214         foreach ($finder as $configFile) {
215             $sourceFile = sprintf(
216                 '%sdist/%s',
217                 $this->configurationManager->getVendorCoreRoot(),
218                 $configFile->getRelativePathname()
219             );
220
221             $destinationFile = sprintf(
222                 '%s%s',
223                 $destination,
224                 $configFile->getRelativePathname()
225             );
226
227             $fs = new Filesystem();
228             foreach ($this->directories as $directory) {
229                 if (!$fs->exists($destination.$directory)) {
230                     $fs->mkdir($destination.$directory);
231                 }
232             }
233
234             if ($this->copyFile($sourceFile, $destinationFile, $override)) {
235                 $copiedFiles[] = $destinationFile;
236             }
237         }
238
239         if ($copiedFiles) {
240             $this->showFile->copiedFiles($this->getIo(), $copiedFiles, false);
241             $this->getIo()->newLine();
242         }
243
244         $executableName = null;
245         if ($autocomplete) {
246             $processBuilder = new ProcessBuilder(['bash']);
247             $process = $processBuilder->getProcess();
248             $process->setCommandLine('echo $_');
249             $process->run();
250             $fullPathExecutable = explode('/', $process->getOutput());
251             $executableName = trim(end($fullPathExecutable));
252             $process->stop();
253         }
254
255         $this->generator->generate([
256           'user_home' => $this->configurationManager->getConsoleDirectory(),
257           'executable_name' => $executableName,
258           'override' => $override,
259           'destination' => $destination,
260           'config_parameters' => $this->configParameters,
261         ]);
262
263         $this->getIo()->writeln($this->trans('application.messages.autocomplete'));
264
265         return 0;
266     }
267
268     /**
269      * @param string $source
270      * @param string $destination
271      * @param string $override
272      * @return bool
273      */
274     private function copyFile($source, $destination, $override)
275     {
276         if (file_exists($destination)) {
277             if ($override) {
278                 copy(
279                     $destination,
280                     $destination . '.old'
281                 );
282             } else {
283                 return false;
284             }
285         }
286
287         $filePath = dirname($destination);
288         if (!is_dir($filePath)) {
289             mkdir($filePath, 0777, true);
290         }
291
292         return copy(
293             $source,
294             $destination
295         );
296     }
297 }