Added the Search API Synonym module to deal specifically with licence and license...
[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                 'site',
107                 null,
108                 InputOption::VALUE_NONE,
109                 $this->trans('commands.init.options.site')
110             )
111             ->addOption(
112                 'override',
113                 null,
114                 InputOption::VALUE_NONE,
115                 $this->trans('commands.init.options.override')
116             )
117             ->addOption(
118                 'autocomplete',
119                 null,
120                 InputOption::VALUE_NONE,
121                 $this->trans('commands.init.options.autocomplete')
122             );
123     }
124
125     /**
126      * {@inheritdoc}
127      */
128     protected function interact(InputInterface $input, OutputInterface $output)
129     {
130         $destination = $input->getOption('destination');
131         $site = $input->getOption('site');
132         $autocomplete = $input->getOption('autocomplete');
133         $configuration = $this->configurationManager->getConfiguration();
134
135         if ($site && $this->appRoot && $this->consoleRoot) {
136             $destination = $this->consoleRoot . '/console/';
137         }
138
139         if (!$destination) {
140             if ($this->appRoot && $this->consoleRoot) {
141                 $destination = $this->getIo()->choice(
142                     $this->trans('commands.init.questions.destination'),
143                     $this->configurationManager->getConfigurationDirectories()
144                 );
145             } else {
146                 $destination = $this->configurationManager
147                     ->getConsoleDirectory();
148             }
149
150             $input->setOption('destination', $destination);
151         }
152
153         $this->configParameters['language'] = $this->getIo()->choiceNoList(
154             $this->trans('commands.init.questions.language'),
155             array_keys($configuration->get('application.languages'))
156         );
157
158         $this->configParameters['temp'] = $this->getIo()->ask(
159             $this->trans('commands.init.questions.temp'),
160             '/tmp'
161         );
162
163         $this->configParameters['chain'] = $this->getIo()->confirm(
164             $this->trans('commands.init.questions.chain'),
165             false
166         );
167
168         $this->configParameters['sites'] = $this->getIo()->confirm(
169             $this->trans('commands.init.questions.sites'),
170             false
171         );
172
173         $this->configParameters['learning'] = $this->getIo()->confirm(
174             $this->trans('commands.init.questions.learning'),
175             false
176         );
177
178         $this->configParameters['generate_inline'] = $this->getIo()->confirm(
179             $this->trans('commands.init.questions.generate-inline'),
180             false
181         );
182
183         $this->configParameters['generate_chain'] = $this->getIo()->confirm(
184             $this->trans('commands.init.questions.generate-chain'),
185             false
186         );
187
188         if (!$autocomplete) {
189             $autocomplete = $this->getIo()->confirm(
190                 $this->trans('commands.init.questions.autocomplete'),
191                 false
192             );
193             $input->setOption('autocomplete', $autocomplete);
194         }
195     }
196
197     /**
198      * {@inheritdoc}
199      */
200     protected function execute(InputInterface $input, OutputInterface $output)
201     {
202         $copiedFiles = [];
203         $destination = $input->getOption('destination');
204         $site = $input->getOption('site');
205         $autocomplete = $input->getOption('autocomplete');
206         $override = $input->getOption('override');
207
208         if ($site && $this->appRoot && $this->consoleRoot) {
209             $destination = $this->consoleRoot . '/console/';
210         }
211
212         if (!$destination) {
213             $destination = $this->configurationManager->getConsoleDirectory();
214         }
215
216         $finder = new Finder();
217         $finder->in(
218             sprintf(
219                 '%sdist/',
220                 $this->configurationManager->getVendorCoreRoot()
221             )
222         );
223         if (!$this->configParameters['chain']) {
224             $finder->exclude('chain');
225         }
226         if (!$this->configParameters['sites']) {
227             $finder->exclude('sites');
228         }
229         $finder->files();
230
231         foreach ($finder as $configFile) {
232             $sourceFile = sprintf(
233                 '%sdist/%s',
234                 $this->configurationManager->getVendorCoreRoot(),
235                 $configFile->getRelativePathname()
236             );
237
238             $destinationFile = sprintf(
239                 '%s%s',
240                 $destination,
241                 $configFile->getRelativePathname()
242             );
243
244             $fs = new Filesystem();
245             foreach ($this->directories as $directory) {
246                 if (!$fs->exists($destination.$directory)) {
247                     $fs->mkdir($destination.$directory);
248                 }
249             }
250
251             if ($this->copyFile($sourceFile, $destinationFile, $override)) {
252                 $copiedFiles[] = $destinationFile;
253             }
254         }
255
256         if ($copiedFiles) {
257             $this->showFile->copiedFiles($this->getIo(), $copiedFiles, false);
258             $this->getIo()->newLine();
259         }
260
261         $executableName = null;
262         if ($autocomplete) {
263             $processBuilder = new ProcessBuilder(['bash']);
264             $process = $processBuilder->getProcess();
265             $process->setCommandLine('echo $_');
266             $process->run();
267             $fullPathExecutable = explode('/', $process->getOutput());
268             $executableName = trim(end($fullPathExecutable));
269             $process->stop();
270         }
271
272         $this->generator->generate([
273           'user_home' => $this->configurationManager->getConsoleDirectory(),
274           'executable_name' => $executableName,
275           'override' => $override,
276           'destination' => $destination,
277           'config_parameters' => $this->configParameters,
278         ]);
279
280         $this->getIo()->writeln($this->trans('application.messages.autocomplete'));
281
282         return 0;
283     }
284
285     /**
286      * @param string $source
287      * @param string $destination
288      * @param string $override
289      * @return bool
290      */
291     private function copyFile($source, $destination, $override)
292     {
293         if (file_exists($destination)) {
294             if ($override) {
295                 copy(
296                     $destination,
297                     $destination . '.old'
298                 );
299             } else {
300                 return false;
301             }
302         }
303
304         $filePath = dirname($destination);
305         if (!is_dir($filePath)) {
306             mkdir($filePath, 0777, true);
307         }
308
309         return copy(
310             $source,
311             $destination
312         );
313     }
314 }