trait GenerateCommandTrait
Runs a DevelGenerate plugin from a console command.
Hierarchy
- trait \Drupal\devel_generate\Command\GenerateCommandTrait
File
-
devel_generate/
src/ Command/ GenerateCommandTrait.php, line 21
Namespace
Drupal\devel_generate\CommandView source
trait GenerateCommandTrait {
public function __construct(protected readonly DevelGeneratePluginManager $manager, protected readonly ModuleHandlerInterface $moduleHandler, protected readonly MessengerInterface $messenger, protected readonly RequestStack $requestStack) {
}
/**
* Generates with the given plugin, from the command's arguments and options.
*
* @param string $plugin_id
* The DevelGenerate plugin ID.
* @param \Symfony\Component\Console\Input\InputInterface $input
* The input.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* The output.
* @param string[] $modules
* The modules that must be installed.
*/
protected function generate(string $plugin_id, InputInterface $input, OutputInterface $output, array $modules = []) : int {
$missing = array_filter($modules, fn(string $module): bool => !$this->moduleHandler
->moduleExists($module));
if ($missing !== []) {
throw new \RuntimeException(sprintf('Install these modules first: %s', implode(', ', $missing)));
}
/** @var \Drupal\devel_generate\DevelGenerateBaseInterface $instance */
$instance = $this->manager
->createInstance($plugin_id, []);
try {
$parameters = $instance->validateDrushParams($input->getArguments(), $input->getOptions());
$instance->generate($parameters);
$this->processBatch();
} finally {
$this->writeMessages($input, $output);
}
return Command::SUCCESS;
}
/**
* Runs the batch that a plugin has set, if any, in this process.
*/
protected function processBatch() : void {
$batch =& batch_get();
if (empty($batch)) {
return;
}
// Run all the operations now, instead of redirecting to the batch page.
$batch['progressive'] = FALSE;
// batch_process() builds a URL from the current route. A command line
// request has none, so give it a placeholder.
$request = $this->requestStack
->getCurrentRequest();
if ($request && !$request->attributes
->has(RouteObjectInterface::ROUTE_OBJECT)) {
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, '<none>');
}
batch_process();
}
/**
* Writes the messages that the plugin has set, and clears them.
*
* They are progress reports rather than data, so they go to stderr.
*/
protected function writeMessages(InputInterface $input, OutputInterface $output) : void {
$io = (new SymfonyStyle($input, $output))->getErrorStyle();
foreach ($this->messenger
->deleteAll() as $type => $messages) {
foreach ($messages as $message) {
$message = trim(html_entity_decode(strip_tags((string) $message), ENT_QUOTES | ENT_HTML5));
match ($type) { MessengerInterface::TYPE_ERROR => $io->error($message),
MessengerInterface::TYPE_WARNING => $io->warning($message),
default => $io->writeln($message),
};
}
}
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary |
|---|---|---|---|
| GenerateCommandTrait::generate | protected | function | Generates with the given plugin, from the command's arguments and options. |
| GenerateCommandTrait::processBatch | protected | function | Runs the batch that a plugin has set, if any, in this process. |
| GenerateCommandTrait::writeMessages | protected | function | Writes the messages that the plugin has set, and clears them. |
| GenerateCommandTrait::__construct | public | function |