0330fb564137d5eb07df744131dd1ed422ec88d5
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ModuleCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\ModuleCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Config\Definition\Exception\Exception;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Generator\ModuleGenerator;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Utils\Validator;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Utils\StringConverter;
21 use Drupal\Console\Utils\DrupalApi;
22
23 class ModuleCommand extends Command
24 {
25     use ConfirmationTrait;
26     use CommandTrait;
27
28     /**
29      * @var ModuleGenerator
30      */
31     protected $generator;
32
33     /**
34      * @var Validator
35      */
36     protected $validator;
37
38     /**
39      * @var string
40      */
41     protected $appRoot;
42
43     /**
44      * @var StringConverter
45      */
46     protected $stringConverter;
47
48     /**
49      * @var DrupalApi
50      */
51     protected $drupalApi;
52
53     /**
54      * @var string
55      */
56     protected $twigtemplate;
57
58
59     /**
60      * ModuleCommand constructor.
61      *
62      * @param ModuleGenerator $generator
63      * @param Validator       $validator
64      * @param $appRoot
65      * @param StringConverter $stringConverter
66      * @param DrupalApi       $drupalApi
67      * @param $twigtemplate
68      */
69     public function __construct(
70         ModuleGenerator $generator,
71         Validator $validator,
72         $appRoot,
73         StringConverter $stringConverter,
74         DrupalApi $drupalApi,
75         $twigtemplate = null
76     ) {
77         $this->generator = $generator;
78         $this->validator = $validator;
79         $this->appRoot = $appRoot;
80         $this->stringConverter = $stringConverter;
81         $this->drupalApi = $drupalApi;
82         $this->twigtemplate = $twigtemplate;
83         parent::__construct();
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     protected function configure()
90     {
91         $this
92             ->setName('generate:module')
93             ->setDescription($this->trans('commands.generate.module.description'))
94             ->setHelp($this->trans('commands.generate.module.help'))
95             ->addOption(
96                 'module',
97                 null,
98                 InputOption::VALUE_REQUIRED,
99                 $this->trans('commands.generate.module.options.module')
100             )
101             ->addOption(
102                 'machine-name',
103                 null,
104                 InputOption::VALUE_REQUIRED,
105                 $this->trans('commands.generate.module.options.machine-name')
106             )
107             ->addOption(
108                 'module-path',
109                 null,
110                 InputOption::VALUE_REQUIRED,
111                 $this->trans('commands.generate.module.options.module-path')
112             )
113             ->addOption(
114                 'description',
115                 null,
116                 InputOption::VALUE_OPTIONAL,
117                 $this->trans('commands.generate.module.options.description')
118             )
119             ->addOption(
120                 'core',
121                 null,
122                 InputOption::VALUE_OPTIONAL,
123                 $this->trans('commands.generate.module.options.core')
124             )
125             ->addOption(
126                 'package',
127                 null,
128                 InputOption::VALUE_OPTIONAL,
129                 $this->trans('commands.generate.module.options.package')
130             )
131             ->addOption(
132                 'module-file',
133                 null,
134                 InputOption::VALUE_NONE,
135                 $this->trans('commands.generate.module.options.module-file')
136             )
137             ->addOption(
138                 'features-bundle',
139                 null,
140                 InputOption::VALUE_REQUIRED,
141                 $this->trans('commands.generate.module.options.features-bundle')
142             )
143             ->addOption(
144                 'composer',
145                 null,
146                 InputOption::VALUE_NONE,
147                 $this->trans('commands.generate.module.options.composer')
148             )
149             ->addOption(
150                 'dependencies',
151                 null,
152                 InputOption::VALUE_OPTIONAL,
153                 $this->trans('commands.generate.module.options.dependencies'),
154                 ''
155             )
156             ->addOption(
157                 'test',
158                 null,
159                 InputOption::VALUE_OPTIONAL,
160                 $this->trans('commands.generate.module.options.test')
161             )
162             ->addOption(
163                 'twigtemplate',
164                 null,
165                 InputOption::VALUE_OPTIONAL,
166                 $this->trans('commands.generate.module.options.twigtemplate')
167             );
168     }
169
170     /**
171      * {@inheritdoc}
172      */
173     protected function execute(InputInterface $input, OutputInterface $output)
174     {
175         $io = new DrupalStyle($input, $output);
176         $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
177
178         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
179         if (!$this->confirmGeneration($io, $yes)) {
180             return 1;
181         }
182
183         $module = $this->validator->validateModuleName($input->getOption('module'));
184
185         $modulePath = $this->appRoot . $input->getOption('module-path');
186         $modulePath = $this->validator->validateModulePath($modulePath, true);
187
188         $machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
189         $description = $input->getOption('description');
190         $core = $input->getOption('core');
191         $package = $input->getOption('package');
192         $moduleFile = $input->getOption('module-file');
193         $featuresBundle = $input->getOption('features-bundle');
194         $composer = $input->getOption('composer');
195         $dependencies = $this->validator->validateExtensions(
196             $input->getOption('dependencies'),
197             'module',
198             $io
199         );
200         $test = $input->getOption('test');
201         $twigTemplate = $input->getOption('twigtemplate');
202
203         $this->generator->generate(
204             $module,
205             $machineName,
206             $modulePath,
207             $description,
208             $core,
209             $package,
210             $moduleFile,
211             $featuresBundle,
212             $composer,
213             $dependencies,
214             $test,
215             $twigTemplate
216         );
217
218         return 0;
219     }
220
221     /**
222      * {@inheritdoc}
223      */
224     protected function interact(InputInterface $input, OutputInterface $output)
225     {
226         $io = new DrupalStyle($input, $output);
227
228         $validator = $this->validator;
229
230         try {
231             $module = $input->getOption('module') ?
232               $this->validator->validateModuleName(
233                   $input->getOption('module')
234               ) : null;
235         } catch (\Exception $error) {
236             $io->error($error->getMessage());
237
238             return 1;
239         }
240
241         if (!$module) {
242             $module = $io->ask(
243                 $this->trans('commands.generate.module.questions.module'),
244                 null,
245                 function ($module) use ($validator) {
246                     return $validator->validateModuleName($module);
247                 }
248             );
249             $input->setOption('module', $module);
250         }
251
252         try {
253             $machineName = $input->getOption('machine-name') ?
254               $this->validator->validateModuleName(
255                   $input->getOption('machine-name')
256               ) : null;
257         } catch (\Exception $error) {
258             $io->error($error->getMessage());
259         }
260
261         if (!$machineName) {
262             $machineName = $io->ask(
263                 $this->trans('commands.generate.module.questions.machine-name'),
264                 $this->stringConverter->createMachineName($module),
265                 function ($machine_name) use ($validator) {
266                     return $validator->validateMachineName($machine_name);
267                 }
268             );
269             $input->setOption('machine-name', $machineName);
270         }
271
272         $modulePath = $input->getOption('module-path');
273         if (!$modulePath) {
274             $drupalRoot = $this->appRoot;
275             $modulePath = $io->ask(
276                 $this->trans('commands.generate.module.questions.module-path'),
277                 '/modules/custom',
278                 function ($modulePath) use ($drupalRoot, $machineName) {
279                     $modulePath = ($modulePath[0] != '/' ? '/' : '').$modulePath;
280                     $fullPath = $drupalRoot.$modulePath.'/'.$machineName;
281                     if (file_exists($fullPath)) {
282                         throw new \InvalidArgumentException(
283                             sprintf(
284                                 $this->trans('commands.generate.module.errors.directory-exists'),
285                                 $fullPath
286                             )
287                         );
288                     }
289
290                     return $modulePath;
291                 }
292             );
293         }
294         $input->setOption('module-path', $modulePath);
295
296         $description = $input->getOption('description');
297         if (!$description) {
298             $description = $io->ask(
299                 $this->trans('commands.generate.module.questions.description'),
300                 'My Awesome Module'
301             );
302         }
303         $input->setOption('description', $description);
304
305         $package = $input->getOption('package');
306         if (!$package) {
307             $package = $io->ask(
308                 $this->trans('commands.generate.module.questions.package'),
309                 'Custom'
310             );
311         }
312         $input->setOption('package', $package);
313
314         $core = $input->getOption('core');
315         if (!$core) {
316             $core = $io->ask(
317                 $this->trans('commands.generate.module.questions.core'), '8.x',
318                 function ($core) {
319                     // Only allow 8.x and higher as core version.
320                     if (!preg_match('/^([0-9]+)\.x$/', $core, $matches) || ($matches[1] < 8)) {
321                         throw new \InvalidArgumentException(
322                             sprintf(
323                                 $this->trans('commands.generate.module.errors.invalid-core'),
324                                 $core
325                             )
326                         );
327                     }
328
329                     return $core;
330                 }
331             );
332             $input->setOption('core', $core);
333         }
334
335         $moduleFile = $input->getOption('module-file');
336         if (!$moduleFile) {
337             $moduleFile = $io->confirm(
338                 $this->trans('commands.generate.module.questions.module-file'),
339                 true
340             );
341             $input->setOption('module-file', $moduleFile);
342         }
343
344         $featuresBundle = $input->getOption('features-bundle');
345         if (!$featuresBundle) {
346             $featuresSupport = $io->confirm(
347                 $this->trans('commands.generate.module.questions.features-support'),
348                 false
349             );
350             if ($featuresSupport) {
351                 $featuresBundle = $io->ask(
352                     $this->trans('commands.generate.module.questions.features-bundle'),
353                     'default'
354                 );
355             }
356             $input->setOption('features-bundle', $featuresBundle);
357         }
358
359         $composer = $input->getOption('composer');
360         if (!$composer) {
361             $composer = $io->confirm(
362                 $this->trans('commands.generate.module.questions.composer'),
363                 true
364             );
365             $input->setOption('composer', $composer);
366         }
367
368         $dependencies = $input->getOption('dependencies');
369         if (!$dependencies) {
370             $addDependencies = $io->confirm(
371                 $this->trans('commands.generate.module.questions.dependencies'),
372                 false
373             );
374             if ($addDependencies) {
375                 $dependencies = $io->ask(
376                     $this->trans('commands.generate.module.options.dependencies')
377                 );
378             }
379             $input->setOption('dependencies', $dependencies);
380         }
381
382         $test = $input->getOption('test');
383         if (!$test) {
384             $test = $io->confirm(
385                 $this->trans('commands.generate.module.questions.test'),
386                 true
387             );
388             $input->setOption('test', $test);
389         }
390
391         $twigtemplate = $input->getOption('twigtemplate');
392         if (!$twigtemplate) {
393             $twigtemplate = $io->confirm(
394                 $this->trans('commands.generate.module.questions.twigtemplate'),
395                 true
396             );
397             $input->setOption('twigtemplate', $twigtemplate);
398         }
399     }
400
401     /**
402      * @return ModuleGenerator
403      */
404     protected function createGenerator()
405     {
406         return new ModuleGenerator();
407     }
408 }