Skip to content

Commit

Permalink
[generate:command] Add initialize option. (#3704)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas authored Jan 18, 2018
1 parent cfd8ad6 commit 80cde77
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
20 changes: 18 additions & 2 deletions src/Command/Generate/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand All @@ -156,6 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$extension,
$extensionType,
$name,
$initialize,
$interact,
$class,
$containerAware,
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 13 additions & 3 deletions src/Generator/CommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
26 changes: 18 additions & 8 deletions templates/module/src/Command/command.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down Expand Up @@ -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 -%}

0 comments on commit 80cde77

Please sign in to comment.