devel_wizard-2.x-dev/src/Commands/ModuleSpellCommands.php
src/Commands/ModuleSpellCommands.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 Symfony\Component\Console\Input\InputInterface;
class ModuleSpellCommands extends SpellCommandsBase {
protected Utils $utils;
public function __construct(
SpellManagerInterface $spellManager,
Utils $utils,
) {
$this->utils = $utils;
parent::__construct($spellManager);
}
#[CLI\Hook(
type: HookManager::INTERACT,
target: 'interact devel-wizard:spell:devel_wizard_module',
)]
public function cmdModuleInteract(InputInterface $input): void {
$argName = 'machineName';
$machineName = $input->getArgument($argName);
if (!$machineName) {
$description = $this->getCommandInputDescription('', 'argument', $argName);
$machineName = $this->io()->ask(
"1/1 argument <comment>$argName</comment> - $description",
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);
}
}
/**
* @noinspection PhpUnusedParameterInspection
*/
#[CLI\Command(name: 'devel-wizard:spell:devel_wizard_module')]
#[CLI\Help(
description: 'Generates a minimal code base for a new module.',
)]
#[CLI\Bootstrap(level: \Drush\Boot\DrupalBootLevels::FULL)]
#[CLI\Argument(
name: 'machineName',
description: 'Machine-name of the new module.',
)]
public function cmdModuleExecute(string $machineName): void {
$this->execute();
}
protected function buildSpellConfigurationFromInput(): array {
$input = $this->input();
$spellId = $this->getSpellIdFromInput();
return match ($spellId) {
'devel_wizard_module' => [
'type' => 'sub_module',
'machine_name' => $input->getArgument('machineName'),
],
default => [],
};
}
}
