You are here

class CommandGcuSubscriber in Update helper 8

Same name and namespace in other branches
  1. 2.x modules/update_helper_checklist/src/Events/CommandGcuSubscriber.php \Drupal\update_helper_checklist\Events\CommandGcuSubscriber

Subscriber for "generate:configuration:update" command.

Hierarchy

  • class \Drupal\update_helper_checklist\Events\CommandGcuSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CommandGcuSubscriber

1 string reference to 'CommandGcuSubscriber'
console.services.yml in modules/update_helper_checklist/console.services.yml
modules/update_helper_checklist/console.services.yml
1 service uses CommandGcuSubscriber
update_helper_checklist.command_gcu_subscriber in modules/update_helper_checklist/console.services.yml
Drupal\update_helper_checklist\Events\CommandGcuSubscriber

File

modules/update_helper_checklist/src/Events/CommandGcuSubscriber.php, line 18

Namespace

Drupal\update_helper_checklist\Events
View source
class CommandGcuSubscriber implements EventSubscriberInterface {

  /**
   * Key for update version option.
   *
   * @var string
   */
  protected static $updateVersionName = 'update-version';

  /**
   * Key for update description.
   *
   * @var string
   */
  protected static $updateDescription = 'update-description';

  /**
   * Key for success message command option.
   *
   * @var string
   */
  protected static $successMessageName = 'success-message';

  /**
   * Key for failure message command option.
   *
   * @var string
   */
  protected static $failureMessageName = 'failure-message';

  /**
   * Checklist entry generator for configuration update command.
   *
   * @var \Drupal\update_helper_checklist\Generator\ConfigurationUpdateGenerator
   */
  protected $generator;

  /**
   * Console translator manager service.
   *
   * @var \Drupal\Console\Utils\TranslatorManager
   */
  protected $translatorManager;

  /**
   * Update checklist service.
   *
   * @var \Drupal\update_helper_checklist\UpdateChecklist
   */
  protected $updateChecklist;

  /**
   * CommandGcuSubscriber constructor.
   *
   * @param \Drupal\update_helper_checklist\Generator\ConfigurationUpdateGenerator $generator
   *   Code generator service.
   * @param \Drupal\Console\Utils\TranslatorManager $translator_manager
   *   Translator manager service.
   * @param \Drupal\update_helper_checklist\UpdateChecklist $update_checklist
   *   Update checklist service.
   */
  public function __construct(ConfigurationUpdateGenerator $generator, TranslatorManager $translator_manager, UpdateChecklist $update_checklist) {
    $this->generator = $generator;
    $this->translatorManager = $translator_manager;
    $this->updateChecklist = $update_checklist;

    // Init required options for this subscriber to work.
    $translator_manager
      ->addResourceTranslationsByExtension('update_helper_checklist', 'module');
    $this->generator
      ->addSkeletonDir(__DIR__ . '/../../templates/console');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      UpdateHelperEvents::COMMAND_GCU_CONFIGURE => [
        [
          'onConfigure',
          10,
        ],
      ],
      UpdateHelperEvents::COMMAND_GCU_INTERACT => [
        [
          'onInteract',
          10,
        ],
      ],
      UpdateHelperEvents::COMMAND_GCU_EXECUTE => [
        [
          'onExecute',
          10,
        ],
      ],
    ];
  }

  /**
   * Get options for "generate:configuration:update" relevant for checklist.
   *
   * @param \Drupal\update_helper\Events\CommandConfigureEvent $configure_event
   *   Command options event.
   */
  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'));
  }

  /**
   * Handle on interactive mode for getting command options.
   *
   * @param \Drupal\update_helper\Events\CommandInteractEvent $interact_event
   *   Event.
   */
  public function onInteract(CommandInteractEvent $interact_event) {
    $command = $interact_event
      ->getCommand();
    $input = $interact_event
      ->getInput();

    /** @var \Drupal\Console\Core\Style\DrupalStyle $output */
    $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);

    // Get update version.
    if (!$update_version) {
      $update_versions = $this->updateChecklist
        ->getUpdateVersions($input
        ->getOption('module'));

      // Set internal pointer to end, to get last update version.
      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);
    }

    // Get update description for checklist.
    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);
    }

    // Get success message for checklist.
    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);
    }

    // Get failure message for checklist.
    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);
    }
  }

  /**
   * Handles configuration update generation.
   *
   * @param \Drupal\update_helper\Events\CommandExecuteEvent $execute_event
   *   Event.
   */
  public function onExecute(CommandExecuteEvent $execute_event) {

    // If command that triggered this event wasn't successful, then nothing
    // should be created.
    if (!$execute_event
      ->getSuccessful()) {
      return;
    }

    // Get options provided by command as options or in interactive mode.
    $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]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommandGcuSubscriber::$failureMessageName protected static property Key for failure message command option.
CommandGcuSubscriber::$generator protected property Checklist entry generator for configuration update command.
CommandGcuSubscriber::$successMessageName protected static property Key for success message command option.
CommandGcuSubscriber::$translatorManager protected property Console translator manager service.
CommandGcuSubscriber::$updateChecklist protected property Update checklist service.
CommandGcuSubscriber::$updateDescription protected static property Key for update description.
CommandGcuSubscriber::$updateVersionName protected static property Key for update version option.
CommandGcuSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CommandGcuSubscriber::onConfigure public function Get options for "generate:configuration:update" relevant for checklist.
CommandGcuSubscriber::onExecute public function Handles configuration update generation.
CommandGcuSubscriber::onInteract public function Handle on interactive mode for getting command options.
CommandGcuSubscriber::__construct public function CommandGcuSubscriber constructor.