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