devel_wizard-2.x-dev/src/Commands/ProjectDrupalDrushSpellCommands.php

src/Commands/ProjectDrupalDrushSpellCommands.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Commands;

use Consolidation\AnnotatedCommand\AnnotationData;
use Drupal\devel_wizard\Spell\SpellManagerInterface;
use Drupal\devel_wizard\Utils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

class ProjectDrupalDrushSpellCommands extends SpellCommandsBase {

  protected Utils $utils;

  public function __construct(
    SpellManagerInterface $spellManager,
    Utils $utils,
  ) {
    $this->utils = $utils;
    parent::__construct($spellManager);
  }

  /**
   * @phpstan-param \Consolidation\AnnotatedCommand\AnnotationData<string, mixed> $annotationData
   *
   * @hook interact devel-wizard:spell:devel_wizard_project_drupal_drush
   */
  public function cmdProjectDrupalDrushInteract(
    InputInterface $input,
    OutputInterface $output,
    AnnotationData $annotationData,
  ): void {
    $io = $this->io();
    $config = $this->getConfig();

    $type = 'module';
    $rootProjectDir = $config->get('runtime.project');
    $drupalRootDir = $config->get('options.root');

    $argName = 'machineName';
    $machineName = $input->getArgument($argName);
    if (!$machineName) {
      $machineName = $io->ask(
        "argument <comment>$argName</comment> - Machine-name of the new module",
        NULL,
        $this->utils->getStackedValidator(
          $this->utils->getRequiredValidator(
            "$argName argument is required",
          ),
          $this->utils->getRegexpValidator(
            "format of the $argName is invalid",
            Utils::MACHINE_NAME_REGEXP,
          ),
        ),
      );

      $input->setArgument($argName, $machineName);
    }

    $optionName = 'parent-dir';
    $parentDir = $input->getOption($optionName);
    if (!$parentDir) {
      $choices = $this->utils->projectDestinationChoices($type, $rootProjectDir, $drupalRootDir);
      $question = new ChoiceQuestion(
        "option <comment>--{$optionName}</comment> - Parent directory",
        $choices,
      );
      $parentDir = $io->askQuestion($question);
      $input->setOption($optionName, $parentDir);
    }

    $optionName = 'git-template';
    $gitTemplate = $input->getOption($optionName);
    if ($gitTemplate === '?' && !$input->isInteractive()) {
      // @todo This is an error.
      $input->setOption($optionName, '');
    }

    if ($gitTemplate === '?' && $input->isInteractive()) {
      $gitTemplate = $io->choice(
        'Git template',
        $this->utils->gitTemplateChoices($config->get('env.HOME')),
      );
      $input->setOption('git-template', $gitTemplate);
    }
  }

  /**
   * Creates a new 'drupal/foo' package with type 'drupal-drush'.
   *
   * @param string $machineName
   *   Machine-name of the new project.
   * @param array $options
   *
   * @command devel-wizard:spell:devel_wizard_project_drupal_drush
   *
   * @option string $type
   *    Allowed values: sub_module, standalone.
   * @option string $parent-dir
   *   Most of the time it is: DRUPAL_ROOT/../drush/Commands/contrib
   *   Default: ../drush/Commands/contrib
   * @option string $git-template
   *   Git template directory to use for `git init`.
   *
   * @aliases dw:s:p-drush
   * @bootstrap full
   *
   * @noinspection PhpUnusedParameterInspection
   */
  public function cmdProjectDrupalDrushExecute(
    string $machineName,
    array $options = [
      'type' => 'sub_module',
      'parent-dir' => '',
      'git-template' => '',
    ],
  ) {
    $this->execute();
  }

  protected function buildSpellConfigurationFromInput(): array {
    $input = $this->input();
    $spellId = $this->getSpellIdFromInput();

    return match ($spellId) {
      'devel_wizard_project_drupal_drush' => [
        'type' => $input->getOption('type'),
        'machine_name' => $input->getArgument('machineName'),
        'parent_dir' => $input->getOption('parent-dir'),
        'git_template' => $input->getOption('git-template'),
      ],
      default => [],
    };
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc