Yaffs site version 1.1
[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\Process\ProcessBuilder;
14 use Symfony\Component\Finder\Finder;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Utils\ConfigurationManager;
18 use Drupal\Console\Core\Generator\InitGenerator;
19 use Drupal\Console\Core\Utils\ShowFile;
20 use Drupal\Console\Core\Style\DrupalStyle;
21
22 /**
23  * Class InitCommand
24  *
25  * @package Drupal\Console\Core\Command
26  */
27 class InitCommand extends Command
28 {
29     use CommandTrait;
30
31     /**
32      * @var ShowFile
33      */
34     protected $showFile;
35
36     /**
37      * @var ConfigurationManager
38      */
39     protected $configurationManager;
40
41     /**
42      * @var string
43      */
44     protected $appRoot;
45
46     /**
47      * @var string
48      */
49     protected $consoleRoot;
50
51     /**
52      * @var InitGenerator
53      */
54     protected $generator;
55
56     private $configParameters = [
57         'language' => 'en',
58         'temp' => '/tmp',
59         'chain' => false,
60         'sites' => false,
61         'learning' => false,
62         'generate_inline' => false,
63         'generate_chain' => false
64     ];
65
66     /**
67      * InitCommand constructor.
68      *
69      * @param ShowFile             $showFile
70      * @param ConfigurationManager $configurationManager
71      * @param InitGenerator        $generator
72      * @param string               $appRoot
73      * @param string               $consoleRoot
74      */
75     public function __construct(
76         ShowFile $showFile,
77         ConfigurationManager $configurationManager,
78         InitGenerator $generator,
79         $appRoot,
80         $consoleRoot = null
81     ) {
82         $this->showFile = $showFile;
83         $this->configurationManager = $configurationManager;
84         $this->generator = $generator;
85         $this->appRoot = $appRoot;
86         $this->consoleRoot = $consoleRoot;
87         parent::__construct();
88     }
89
90     /**
91      * {@inheritdoc}
92      */
93     protected function configure()
94     {
95         $this
96             ->setName('init')
97             ->setDescription($this->trans('commands.init.description'))
98             ->addOption(
99                 'destination',
100                 null,
101                 InputOption::VALUE_OPTIONAL,
102                 $this->trans('commands.init.options.destination')
103             )
104             ->addOption(
105                 'override',
106                 null,
107                 InputOption::VALUE_NONE,
108                 $this->trans('commands.init.options.override')
109             )
110             ->addOption(
111                 'autocomplete',
112                 null,
113                 InputOption::VALUE_NONE,
114                 $this->trans('commands.init.options.autocomplete')
115             );
116     }
117
118     /**
119      * {@inheritdoc}
120      */
121     protected function interact(InputInterface $input, OutputInterface $output)
122     {
123         $io = new DrupalStyle($input, $output);
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 = $io->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'] = $io->choiceNoList(
143             $this->trans('commands.init.questions.language'),
144             array_keys($configuration->get('application.languages'))
145         );
146
147         $this->configParameters['temp'] = $io->ask(
148             $this->trans('commands.init.questions.temp'),
149             '/tmp'
150         );
151
152         $this->configParameters['learning'] = $io->confirm(
153             $this->trans('commands.init.questions.chain'),
154             false
155         );
156
157         $this->configParameters['sites'] = $io->confirm(
158             $this->trans('commands.init.questions.sites'),
159             false
160         );
161
162         $this->configParameters['chain'] = $io->confirm(
163             $this->trans('commands.init.questions.learning'),
164             false
165         );
166
167         $this->configParameters['generate_inline'] = $io->confirm(
168             $this->trans('commands.init.questions.generate-inline'),
169             false
170         );
171
172         $this->configParameters['generate_chain'] = $io->confirm(
173             $this->trans('commands.init.questions.generate-chain'),
174             false
175         );
176
177         if (!$autocomplete) {
178             $autocomplete = $io->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         $io = new DrupalStyle($input, $output);
192         $copiedFiles = [];
193         $destination = $input->getOption('destination');
194         $autocomplete = $input->getOption('autocomplete');
195         $override = $input->getOption('override');
196         if (!$destination) {
197             $destination = $this->configurationManager->getConsoleDirectory();
198         }
199
200         $finder = new Finder();
201         $finder->in(
202             sprintf(
203                 '%s%s/config/dist/',
204                 $this->configurationManager->getApplicationDirectory(),
205                 DRUPAL_CONSOLE_CORE
206             )
207         );
208         if (!$this->configParameters['chain']) {
209             $finder->exclude('chain');
210         }
211         if (!$this->configParameters['sites']) {
212             $finder->exclude('sites');
213         }
214         $finder->files();
215
216         foreach ($finder as $configFile) {
217             $sourceFile = sprintf(
218                 '%s%s/config/dist/%s',
219                 $this->configurationManager->getApplicationDirectory(),
220                 DRUPAL_CONSOLE_CORE,
221                 $configFile->getRelativePathname()
222             );
223
224             $destinationFile = sprintf(
225                 '%s%s',
226                 $destination,
227                 $configFile->getRelativePathname()
228             );
229
230             if ($this->copyFile($sourceFile, $destinationFile, $override)) {
231                 $copiedFiles[] = $destinationFile;
232             }
233         }
234
235         if ($copiedFiles) {
236             $this->showFile->copiedFiles($io, $copiedFiles, false);
237             $io->newLine();
238         }
239
240         $executableName = null;
241         if ($autocomplete) {
242             $processBuilder = new ProcessBuilder(['bash']);
243             $process = $processBuilder->getProcess();
244             $process->setCommandLine('echo $_');
245             $process->run();
246             $fullPathExecutable = explode('/', $process->getOutput());
247             $executableName = trim(end($fullPathExecutable));
248             $process->stop();
249         }
250
251         $this->generator->generate(
252             $this->configurationManager->getConsoleDirectory(),
253             $executableName,
254             $override,
255             $destination,
256             $this->configParameters
257         );
258
259         $io->writeln($this->trans('application.messages.autocomplete'));
260
261         return 0;
262     }
263
264     /**
265      * @param string $source
266      * @param string $destination
267      * @param string $override
268      * @return bool
269      */
270     private function copyFile($source, $destination, $override)
271     {
272         if (file_exists($destination)) {
273             if ($override) {
274                 copy(
275                     $destination,
276                     $destination . '.old'
277                 );
278             } else {
279                 return false;
280             }
281         }
282
283         $filePath = dirname($destination);
284         if (!is_dir($filePath)) {
285             mkdir($filePath, 0777, true);
286         }
287
288         return copy(
289             $source,
290             $destination
291         );
292     }
293 }