5 * Contains \Drupal\Console\Command\Generate\ModuleCommand.
8 namespace Drupal\Console\Command\Generate;
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;
23 class ModuleCommand extends Command
25 use ConfirmationTrait;
29 * @var ModuleGenerator
44 * @var StringConverter
46 protected $stringConverter;
56 protected $twigtemplate;
60 * ModuleCommand constructor.
62 * @param ModuleGenerator $generator
63 * @param Validator $validator
65 * @param StringConverter $stringConverter
66 * @param DrupalApi $drupalApi
67 * @param $twigtemplate
69 public function __construct(
70 ModuleGenerator $generator,
73 StringConverter $stringConverter,
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();
89 protected function configure()
92 ->setName('generate:module')
93 ->setDescription($this->trans('commands.generate.module.description'))
94 ->setHelp($this->trans('commands.generate.module.help'))
98 InputOption::VALUE_REQUIRED,
99 $this->trans('commands.generate.module.options.module')
104 InputOption::VALUE_REQUIRED,
105 $this->trans('commands.generate.module.options.machine-name')
110 InputOption::VALUE_REQUIRED,
111 $this->trans('commands.generate.module.options.module-path')
116 InputOption::VALUE_OPTIONAL,
117 $this->trans('commands.generate.module.options.description')
122 InputOption::VALUE_OPTIONAL,
123 $this->trans('commands.generate.module.options.core')
128 InputOption::VALUE_OPTIONAL,
129 $this->trans('commands.generate.module.options.package')
134 InputOption::VALUE_NONE,
135 $this->trans('commands.generate.module.options.module-file')
140 InputOption::VALUE_REQUIRED,
141 $this->trans('commands.generate.module.options.features-bundle')
146 InputOption::VALUE_NONE,
147 $this->trans('commands.generate.module.options.composer')
152 InputOption::VALUE_OPTIONAL,
153 $this->trans('commands.generate.module.options.dependencies'),
159 InputOption::VALUE_OPTIONAL,
160 $this->trans('commands.generate.module.options.test')
165 InputOption::VALUE_OPTIONAL,
166 $this->trans('commands.generate.module.options.twigtemplate')
173 protected function execute(InputInterface $input, OutputInterface $output)
175 $io = new DrupalStyle($input, $output);
176 $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
178 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
179 if (!$this->confirmGeneration($io, $yes)) {
183 $module = $this->validator->validateModuleName($input->getOption('module'));
185 $modulePath = $this->appRoot . $input->getOption('module-path');
186 $modulePath = $this->validator->validateModulePath($modulePath, true);
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'),
200 $test = $input->getOption('test');
201 $twigTemplate = $input->getOption('twigtemplate');
203 $this->generator->generate(
224 protected function interact(InputInterface $input, OutputInterface $output)
226 $io = new DrupalStyle($input, $output);
228 $validator = $this->validator;
231 $module = $input->getOption('module') ?
232 $this->validator->validateModuleName(
233 $input->getOption('module')
235 } catch (\Exception $error) {
236 $io->error($error->getMessage());
243 $this->trans('commands.generate.module.questions.module'),
245 function ($module) use ($validator) {
246 return $validator->validateModuleName($module);
249 $input->setOption('module', $module);
253 $machineName = $input->getOption('machine-name') ?
254 $this->validator->validateModuleName(
255 $input->getOption('machine-name')
257 } catch (\Exception $error) {
258 $io->error($error->getMessage());
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);
269 $input->setOption('machine-name', $machineName);
272 $modulePath = $input->getOption('module-path');
274 $drupalRoot = $this->appRoot;
275 $modulePath = $io->ask(
276 $this->trans('commands.generate.module.questions.module-path'),
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(
284 $this->trans('commands.generate.module.errors.directory-exists'),
294 $input->setOption('module-path', $modulePath);
296 $description = $input->getOption('description');
298 $description = $io->ask(
299 $this->trans('commands.generate.module.questions.description'),
303 $input->setOption('description', $description);
305 $package = $input->getOption('package');
308 $this->trans('commands.generate.module.questions.package'),
312 $input->setOption('package', $package);
314 $core = $input->getOption('core');
317 $this->trans('commands.generate.module.questions.core'), '8.x',
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(
323 $this->trans('commands.generate.module.errors.invalid-core'),
332 $input->setOption('core', $core);
335 $moduleFile = $input->getOption('module-file');
337 $moduleFile = $io->confirm(
338 $this->trans('commands.generate.module.questions.module-file'),
341 $input->setOption('module-file', $moduleFile);
344 $featuresBundle = $input->getOption('features-bundle');
345 if (!$featuresBundle) {
346 $featuresSupport = $io->confirm(
347 $this->trans('commands.generate.module.questions.features-support'),
350 if ($featuresSupport) {
351 $featuresBundle = $io->ask(
352 $this->trans('commands.generate.module.questions.features-bundle'),
356 $input->setOption('features-bundle', $featuresBundle);
359 $composer = $input->getOption('composer');
361 $composer = $io->confirm(
362 $this->trans('commands.generate.module.questions.composer'),
365 $input->setOption('composer', $composer);
368 $dependencies = $input->getOption('dependencies');
369 if (!$dependencies) {
370 $addDependencies = $io->confirm(
371 $this->trans('commands.generate.module.questions.dependencies'),
374 if ($addDependencies) {
375 $dependencies = $io->ask(
376 $this->trans('commands.generate.module.options.dependencies')
379 $input->setOption('dependencies', $dependencies);
382 $test = $input->getOption('test');
384 $test = $io->confirm(
385 $this->trans('commands.generate.module.questions.test'),
388 $input->setOption('test', $test);
391 $twigtemplate = $input->getOption('twigtemplate');
392 if (!$twigtemplate) {
393 $twigtemplate = $io->confirm(
394 $this->trans('commands.generate.module.questions.twigtemplate'),
397 $input->setOption('twigtemplate', $twigtemplate);
402 * @return ModuleGenerator
404 protected function createGenerator()
406 return new ModuleGenerator();