devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/BatchHandlerSpell.php
src/Plugin/DevelWizard/Spell/BatchHandlerSpell.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\devel_wizard\Attribute\DevelWizardSpell;
use Drupal\devel_wizard\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\String\UnicodeString;
#[DevelWizardSpell(
id: 'devel_wizard_batch_handler',
category: new TranslatableMarkup('Code'),
label: new TranslatableMarkup('Batch process handler'),
description: new TranslatableMarkup('Generates a handler for Core BatchAPI.'),
tags: [
'code' => new TranslatableMarkup('Code'),
'batch' => new TranslatableMarkup('Batch'),
]
)]
class BatchHandlerSpell extends SpellBase implements
PluginFormInterface,
ContainerFactoryPluginInterface,
ConfigurableInterface {
protected Filesystem $fs;
protected TwigEnvironment $twig;
protected ModuleExtensionList $moduleList;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('messenger'),
$container->get('logger.channel.devel_wizard_spell'),
$container->get('string_translation'),
$container->get('devel_wizard.utils'),
$container->get('config.factory'),
$container->get('twig'),
$container->get('extension.list.module'),
new Filesystem(),
);
}
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MessengerInterface $messenger,
LoggerInterface $logger,
TranslationInterface $stringTranslation,
Utils $utils,
ConfigFactoryInterface $configFactory,
TwigEnvironment $twig,
ModuleExtensionList $moduleList,
Filesystem $fs,
) {
$this->fs = $fs;
$this->twig = $twig;
$this->moduleList = $moduleList;
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$messenger,
$logger,
$stringTranslation,
$utils,
$configFactory,
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'module' => '',
'object' => '',
];
}
protected function populateCalculatedConfigurationValues(): static {
parent::populateCalculatedConfigurationValues();
if (empty($this->configuration['module'])) {
throw new \InvalidArgumentException('module is required');
}
if (empty($this->configuration['object'])) {
$this->configuration['object'] = $this->configuration['module'];
}
$this->configuration['moduleDir'] = $this->moduleList->getPath($this->configuration['module']);
$this->configuration['objectUpperCamel'] = (new UnicodeString("a_{$this->configuration['object']}"))
->camel()
->trimPrefix('a')
->toString();
$this->configuration += [
'handler' => [],
];
$this->configuration['handler'] += [
'className' => $this->configuration['objectUpperCamel'] . 'BatchHandler',
'classNamespace' => implode('\\', [
'Drupal',
$this->configuration['module'],
'BatchHandler',
]),
];
$this->configuration['handler'] += [
'filePath' => Path::join(
$this->configuration['moduleDir'],
'src',
'BatchHandler',
$this->configuration['handler']['className'] . '.php',
),
'classFqn' => $this->configuration['handler']['classNamespace'] . '\\' . $this->configuration['handler']['className'],
];
return $this;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$conf = $this->getConfiguration();
$form['module'] = [
'#type' => 'textfield',
'#required' => TRUE,
'#title' => $this->t('Module'),
'#default_value' => $conf['module'],
'#autocomplete_route_name' => 'devel_wizard.autocomplete.module',
];
$form['object'] = [
'#type' => 'textfield',
'#required' => TRUE,
'#title' => $this->t('Object'),
'#default_value' => $conf['object'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// @todo Implement validateConfigurationForm() method.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue($form['#parents'], []);
$this->setConfiguration($values);
}
/**
* @throws \Twig\Error\SyntaxError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\LoaderError
*/
protected function doIt(): static {
$this->doItHandler();
$this->batchContext['sandbox']['finished'] = 1.0;
return $this;
}
/**
* @throws \Twig\Error\SyntaxError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\LoaderError
*/
protected function doItHandler(): static {
$filePath = $this->configuration['handler']['filePath'];
$fileContent = $this->twig->render(
'@devel_wizard/spell/batch_handler/devel-wizard.batch-handler.handler.php.twig',
$this->configuration,
);
$this->fs->mkdir(Path::getDirectory($filePath));
$this->fs->dumpFile($filePath, $fileContent);
$this->messageFilesystemEntryCreate($filePath);
return $this;
}
}
