devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/RouteParamConverterSpell.php
src/Plugin/DevelWizard/Spell/RouteParamConverterSpell.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;
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\Spell\SpellTraitPackageManager;
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_route_param_converter',
category: new TranslatableMarkup('Code'),
label: new TranslatableMarkup('Route - parameter converter'),
description: new TranslatableMarkup('Generates a minimal code for a new route parameter converter.'),
tags: [
'code' => new TranslatableMarkup('Code'),
'route' => new TranslatableMarkup('Route'),
'param_converter' => new TranslatableMarkup('Parameter converter'),
],
)]
class RouteParamConverterSpell extends SpellBase implements
PluginFormInterface,
ContainerFactoryPluginInterface {
use SpellTraitPackageManager;
protected ModuleExtensionList $moduleList;
protected TwigEnvironment $twig;
protected Filesystem $fs;
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('extension.list.module'),
$container->get('twig'),
new Filesystem(),
);
}
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MessengerInterface $messenger,
LoggerInterface $logger,
TranslationInterface $stringTranslation,
Utils $utils,
ConfigFactoryInterface $configFactory,
ModuleExtensionList $moduleList,
TwigEnvironment $twig,
Filesystem $fs,
) {
$this->moduleList = $moduleList;
$this->twig = $twig;
$this->fs = $fs;
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['paramConverter'] = [
'filePath' => Path::join(
$this->configuration['moduleDir'],
'src',
'Route',
$this->configuration['objectUpperCamel'] . 'ParamConverter.php',
),
'className' => $this->configuration['objectUpperCamel'] . 'ParamConverter',
'classFqn' => "Drupal\\{$this->configuration['module']}\\Route\\{$this->configuration['objectUpperCamel']}ParamConverter",
];
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) {
$configuration = $form_state->getValue($form['#parents'], []);
$this->setConfiguration($configuration);
}
/**
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \Twig\Error\LoaderError
*/
protected function doIt(): static {
$this
->doItParamConverter()
->doItService();
$this->batchContext['sandbox']['finished'] = 1.0;
return $this;
}
/**
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \Twig\Error\LoaderError
*/
protected function doItParamConverter(): static {
$filePath = Path::join(
$this->configuration['moduleDir'],
'Route',
"{$this->configuration['objectUpperCamel']}ParamConverter.php",
);
$fileContent = $this->twig->render(
'@devel_wizard/route/devel-wizard.route.param-converter.php.twig',
$this->configuration,
);
$this->fs->dumpFile($filePath, $fileContent);
$this->messageFilesystemEntryCreate($filePath);
return $this;
}
protected function doItService(): static {
$conf = $this->getConfiguration();
$filePath = Path::join(
$this->configuration['moduleDir'],
"{$this->configuration['module']}.services.yml",
);
$this->utils->ymlFileReplace(
$filePath,
[
"{$conf['module']}.{$conf['object']}.param_converter" => [
'class' => $conf['paramConverter']['classFqn'],
'tags' => [
[
'name' => 'paramconverter',
],
],
],
],
['services'],
);
return $this;
}
}
