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

src/Commands/EntityTypeSpellCommands.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Commands;

use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\devel_wizard\Spell\SpellManagerInterface;
use Drupal\devel_wizard\Utils;
use Drush\Attributes as CLI;
use Drush\Boot\DrupalBootLevels;
use Symfony\Component\Console\Input\InputInterface;

class EntityTypeSpellCommands extends SpellCommandsBase {

  protected Utils $utils;

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

  #[CLI\Hook(
    type: HookManager::INTERACT,
    target: 'devel-wizard:spell:devel_wizard_entity_type',
  )]
  public function cmdEntityTypeInteract(InputInterface $input): void {
    $io = $this->io();

    $argName = 'module';
    $module = $input->getArgument($argName);
    if (!$module) {
      // @todo Get dynamically the "command handler class::method".
      // Handler method can be replaced with "@hook replace-command ...".
      // And maybe the new method has no "module" argument.
      $description = $this->getCommandInputDescription('cmdEntityTypeExecute', 'argument', $argName);
      $module = $io->ask(
        "1/1 argument <comment>$argName</comment> - $description",
        NULL,
        $this->utils->getStackedValidator(
          $this->utils->getRequiredValidator("argument $argName is required"),
          $this->utils->getRegexpValidator("format of the $argName is invalid", Utils::MACHINE_NAME_REGEXP),
        ),
      );
      $input->setArgument($argName, (string) $module);
    }
  }

  /**
   * @noinspection PhpUnusedParameterInspection
   */
  #[CLI\Command(name: 'devel-wizard:spell:devel_wizard_entity_type')]
  #[CLI\Help(
    description: 'Generates PHP and YML files for a new fieldable and revisionable entity type.',
  )]
  #[CLI\Bootstrap(level: DrupalBootLevels::FULL)]
  #[CLI\Argument(
    name: 'goal',
    description: 'Allowed values: bundleable, config, content',
    suggestedValues: [
      'bundleable',
      'config',
      'content',
    ],
  )]
  #[CLI\Argument(
    name: 'module',
    description: 'Machine-name of the module to put the code files into.',
  )]
  #[CLI\Argument(
    name: 'contentEntityTypeId',
    description: <<< 'TEXT'
      Machine-name of the new content entity type.
      If the goal is "config", then this argument still has to be provided (e.g.: as an empty string), but will be ignored.
      TEXT,
  )]
  #[CLI\Argument(
    name: 'configEntityTypeId',
    description: 'Machine-name of the new config entity type.',
  )]
  public function cmdEntityTypeExecute(
    string $goal,
    string $module,
    string $contentEntityTypeId = '',
    string $configEntityTypeId = '',
  ): void {
    $this->execute();
  }

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

    $conf = [];
    if ($spellId === 'devel_wizard_entity_type') {
      $conf = [
        'goal' => $input->getArgument('goal'),
        'module' => [
          'machine_name' => $input->getArgument('module'),
        ],
        'config' => [
          'id' => $input->getArgument('configEntityTypeId'),
        ],
        'content' => [
          'id' => $input->getArgument('contentEntityTypeId'),
        ],
      ];

      switch ($conf['goal']) {
        case 'config':
          unset($conf['content']);
          break;

        case 'content':
          unset($conf['config']);
          break;
      }
    }

    return $conf;
  }

}

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

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