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