diff --git a/src/Command/Generate/CommandCommand.php b/src/Command/Generate/CommandCommand.php index b5b1f22ce..887d6e5f7 100644 --- a/src/Command/Generate/CommandCommand.php +++ b/src/Command/Generate/CommandCommand.php @@ -110,6 +110,12 @@ protected function configure() InputOption::VALUE_REQUIRED, $this->trans('commands.generate.command.options.name') ) + ->addOption( + 'initialize', + null, + InputOption::VALUE_NONE, + $this->trans('commands.generate.command.options.initialize') + ) ->addOption( 'interact', null, @@ -140,6 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $extensionType = $input->getOption('extension-type'); $class = $this->validator->validateCommandName($input->getOption('class')); $name = $input->getOption('name'); + $initialize = $input->getOption('initialize'); $interact = $input->getOption('interact'); $containerAware = $input->getOption('container-aware'); $services = $input->getOption('services'); @@ -156,6 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $extension, $extensionType, $name, + $initialize, $interact, $class, $containerAware, @@ -194,12 +202,20 @@ protected function interact(InputInterface $input, OutputInterface $output) $input->setOption('name', $name); } - $interact = $input->getOption('interact'); + $initialize = $input->getOption('initialize'); + if (!$initialize) { + $initialize = $this->getIo()->confirm( + $this->trans('commands.generate.command.questions.initialize'), + false + ); + $input->setOption('initialize', $initialize); + } + $interact = $input->getOption('interact'); if (!$interact) { $interact = $this->getIo()->confirm( $this->trans('commands.generate.command.questions.interact'), - true + false ); $input->setOption('interact', $interact); } diff --git a/src/Generator/CommandGenerator.php b/src/Generator/CommandGenerator.php index 8e7618750..132185a45 100644 --- a/src/Generator/CommandGenerator.php +++ b/src/Generator/CommandGenerator.php @@ -48,13 +48,22 @@ public function __construct( * @param string $extension Extension name * @param string $extensionType Extension type * @param string $name Command name - * @param string $interact Interact + * @param string $initialize Initialize method + * @param string $interact Interact method * @param string $class Class name * @param boolean $containerAware Container Aware command * @param array $services Services array */ - public function generate($extension, $extensionType, $name, $interact, $class, $containerAware, $services) - { + public function generate( + $extension, + $extensionType, + $name, + $initialize, + $interact, + $class, + $containerAware, + $services + ) { $command_key = str_replace(':', '.', $name); $extensionObject = $this->extensionManager->getDrupalExtension($extensionType, $extension); @@ -64,6 +73,7 @@ public function generate($extension, $extensionType, $name, $interact, $class, $ 'extensionType' => $extensionType, 'name' => $name, 'interact' => $interact, + 'initialize' => $initialize, 'class_name' => $class, 'container_aware' => $containerAware, 'command_key' => $command_key, diff --git a/templates/module/src/Command/command.php.twig b/templates/module/src/Command/command.php.twig index 0f970d536..2b22077e9 100644 --- a/templates/module/src/Command/command.php.twig +++ b/templates/module/src/Command/command.php.twig @@ -16,7 +16,6 @@ use Drupal\Console\Core\Command\ContainerAwareCommand; {% else %} use Drupal\Console\Core\Command\Command; {% endif %} -use Drupal\Console\Core\Style\DrupalStyle; use Drupal\Console\Annotations\DrupalCommand; {% endblock %} @@ -53,20 +52,31 @@ class {{ class_name }} extends {% if container_aware %}ContainerAwareCommand{% e ->setDescription($this->trans('commands.{{ command_key }}.description')); } - /** - * {@inheritdoc} - */ - protected function execute(InputInterface $input, OutputInterface $output) { - $this->getIo()->info($this->trans('commands.{{ command_key }}.messages.success')); +{% if initialize %} + /** + * {@inheritdoc} + */ + protected function initialize(InputInterface $input, OutputInterface $output) { + parent::initialize($input, $output); + $this->getIo()->info('initialize'); } +{% endif %} {% if interact %} /** * {@inheritdoc} */ - protected function interact(InputInterface $input, OutputInterface $output) { + protected function interact(InputInterface $input, OutputInterface $output) { + $this->getIo()->info('interact'); + } - } {% endif %} + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $this->getIo()->info('execute'); + $this->getIo()->info($this->trans('commands.{{ command_key }}.messages.success')); + } {%- endblock -%}