View source
<?php
namespace Drupal\update_helper_checklist\Events;
use Drupal\Console\Utils\TranslatorManager;
use Drupal\update_helper\Events\CommandConfigureEvent;
use Drupal\update_helper\Events\CommandExecuteEvent;
use Drupal\update_helper\Events\UpdateHelperEvents;
use Drupal\update_helper\Events\CommandInteractEvent;
use Drupal\update_helper_checklist\Generator\ConfigurationUpdateGenerator;
use Drupal\update_helper_checklist\UpdateChecklist;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CommandGcuSubscriber implements EventSubscriberInterface {
protected static $updateVersionName = 'update-version';
protected static $updateDescription = 'update-description';
protected static $successMessageName = 'success-message';
protected static $failureMessageName = 'failure-message';
protected $generator;
protected $translatorManager;
protected $updateChecklist;
public function __construct(ConfigurationUpdateGenerator $generator, TranslatorManager $translator_manager, UpdateChecklist $update_checklist) {
$this->generator = $generator;
$this->translatorManager = $translator_manager;
$this->updateChecklist = $update_checklist;
$translator_manager
->addResourceTranslationsByExtension('update_helper_checklist', 'module');
$this->generator
->addSkeletonDir(__DIR__ . '/../../templates/console');
}
public static function getSubscribedEvents() {
return [
UpdateHelperEvents::COMMAND_GCU_CONFIGURE => [
[
'onConfigure',
10,
],
],
UpdateHelperEvents::COMMAND_GCU_INTERACT => [
[
'onInteract',
10,
],
],
UpdateHelperEvents::COMMAND_GCU_EXECUTE => [
[
'onExecute',
10,
],
],
];
}
public function onConfigure(CommandConfigureEvent $configure_event) {
$configure_event
->addOption(static::$updateVersionName, NULL, InputOption::VALUE_OPTIONAL, $configure_event
->getCommand()
->trans('commands.generate.configuration.update.checklist.options.update-version'), '');
$configure_event
->addOption(static::$updateDescription, NULL, InputOption::VALUE_REQUIRED, $configure_event
->getCommand()
->trans('commands.generate.configuration.update.checklist.options.update-description'));
$configure_event
->addOption(static::$successMessageName, NULL, InputOption::VALUE_REQUIRED, $configure_event
->getCommand()
->trans('commands.generate.configuration.update.checklist.options.success-message'));
$configure_event
->addOption(static::$failureMessageName, NULL, InputOption::VALUE_REQUIRED, $configure_event
->getCommand()
->trans('commands.generate.configuration.update.checklist.options.failure-message'));
}
public function onInteract(CommandInteractEvent $interact_event) {
$command = $interact_event
->getCommand();
$input = $interact_event
->getInput();
$output = $interact_event
->getOutput();
$update_version = $input
->getOption(static::$updateVersionName);
$update_description = $input
->getOption(static::$updateDescription);
$success_message = $input
->getOption(static::$successMessageName);
$failure_message = $input
->getOption(static::$failureMessageName);
if (!$update_version) {
$update_versions = $this->updateChecklist
->getUpdateVersions($input
->getOption('module'));
end($update_versions);
$update_version = $output
->ask($command
->trans('commands.generate.configuration.update.checklist.questions.update-version'), empty($update_versions) ? '8.x-1.0' : current($update_versions));
$input
->setOption(static::$updateVersionName, $update_version);
}
if (!$update_description) {
$update_description = $output
->ask($command
->trans('commands.generate.configuration.update.checklist.questions.update-description'), $command
->trans('commands.generate.configuration.update.checklist.defaults.update-description'));
$input
->setOption(static::$updateDescription, $update_description);
}
if (!$success_message) {
$success_message = $output
->ask($command
->trans('commands.generate.configuration.update.checklist.questions.success-message'), $command
->trans('commands.generate.configuration.update.checklist.defaults.success-message'));
$input
->setOption(static::$successMessageName, $success_message);
}
if (!$failure_message) {
$failure_message = $output
->ask($command
->trans('commands.generate.configuration.update.checklist.questions.failure-message'), $command
->trans('commands.generate.configuration.update.checklist.defaults.failure-message'));
$input
->setOption(static::$failureMessageName, $failure_message);
}
}
public function onExecute(CommandExecuteEvent $execute_event) {
if (!$execute_event
->getSuccessful()) {
return;
}
$options = $execute_event
->getOptions();
$this->generator
->generate($execute_event
->getModule(), $execute_event
->getUpdateNumber(), $options[static::$updateVersionName], $options['description'], $options[static::$updateDescription], $options[static::$successMessageName], $options[static::$failureMessageName]);
}
}