devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/TokenHooksSpell.php

src/Plugin/DevelWizard/Spell/TokenHooksSpell.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\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

#[DevelWizardSpell(
  id: 'devel_wizard_token_hooks',
  category: new TranslatableMarkup('Code'),
  label: new TranslatableMarkup('Token hooks'),
  description: new TranslatableMarkup('Generates hook_token_info() and hook_tokens() implementations.'),
  tags: [
    'code' => new TranslatableMarkup('Code'),
    'token' => new TranslatableMarkup('Token'),
    'hook' => new TranslatableMarkup('Hook'),
  ],
)]
class TokenHooksSpell extends SpellBase implements
  PluginFormInterface,
  ContainerFactoryPluginInterface {

  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' => '',
    ];
  }

  public function populateCalculatedConfigurationValues(): static {
    parent::populateCalculatedConfigurationValues();
    if (empty($this->configuration['module'])) {
      throw new \InvalidArgumentException('module is required');
    }

    $this->configuration['moduleDir'] = $this->moduleList->getPath($this->configuration['module']);

    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',
    ];

    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\LoaderError
   * @throws \Twig\Error\SyntaxError
   * @throws \Twig\Error\RuntimeError
   */
  protected function doIt(): static {
    $conf = $this->getConfiguration();
    $filePath = Path::join(
      $conf['moduleDir'],
      "{$conf['module']}.tokens.inc",
    );

    if ($this->fs->exists($filePath)) {
      $this->batchContext['sandbox']['finished'] = 1.0;

      return $this;
    }

    $this->fs->mkdir(Path::getDirectory($filePath));
    $this->fs->dumpFile(
      $filePath,
      $this->twig->render(
        '@devel_wizard/token/devel_wizard.token.hooks.php.twig',
        $conf,
      ),
    );
    $this->messageFilesystemEntryCreate($filePath);

    drupal_flush_all_caches();

    $this->batchContext['sandbox']['finished'] = 1.0;

    return $this;
  }

}

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

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