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

src/Commands/SpellManagerCommands.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandResult;
use Drupal\devel_wizard\Spell\SpellManagerInterface;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;

class SpellManagerCommands extends DrushCommands {

  protected SpellManagerInterface $spellManager;

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

  /**
   * Lists all spells.
   *
   * @command devel-wizard:spell-list
   *
   * @option string $format
   *   Default: yaml
   */
  public function cmdSpellListExecute(
    array $options = [
      'format' => 'yaml',
    ],
  ): CommandResult {
    $data = [];
    foreach ($this->spellManager->getDefinitions() as $spellDefinition) {
      $data[$spellDefinition->id()] = $spellDefinition->jsonSerialize();
    }

    return CommandResult::data($data);
  }

  /**
   * @hook validate devel-wizard:spell-definition
   */
  public function cmdSpellDefinitionValidate(CommandData $commandData): void {
    $input = $commandData->input();
    $spellId = $input->getArgument('spellId');
    if (!$this->spellManager->hasDefinition($spellId)) {
      throw new \InvalidArgumentException("Spell ID $spellId is not exists");
    }
  }

  /**
   * @param array<string, mixed> $options
   */
  #[CLI\Help(
    description: 'Provides information about a spell definition.',
  )]
  #[CLI\Command(name: 'devel-wizard:spell-definition')]
  #[CLI\Argument(
    name: 'spellId',
    description: 'ID of the spell.',
  )]
  #[CLI\Option(
    name: 'format',
    description: 'Output format.'
  )]
  public function cmdSpellDefinitionExecute(
    string $spellId,
    array $options = [
      'format' => 'yaml',
    ],
  ): CommandResult {
    /* @noinspection PhpUnhandledExceptionInspection */
    $data = $this
      ->spellManager
      ->getDefinition($spellId)
      ->jsonSerialize();

    return CommandResult::data($data);
  }

}

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

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