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

src/Plugin/DevelWizard/Spell/ModuleSpell.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;

use Drupal\Component\Serialization\YamlSymfony;
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\ShellProcessFactoryInterface;
use Drupal\devel_wizard\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

/**
 * @todo Common parent spell for project generator spells.
 * @todo Rename devel_wizard_module to devel_wizard_project_drupal_module.
 *
 * @see \Drupal\devel_wizard\Plugin\DevelWizard\Spell\ProjectDrupalDrushSpell
 */
#[DevelWizardSpell(
  id: 'devel_wizard_module',
  category: new TranslatableMarkup('Code'),
  label: new TranslatableMarkup('Module'),
  description: new TranslatableMarkup('Generates a minimal code base for a new module.'),
  tags: [
    'code' => new TranslatableMarkup('Code'),
    'project' => new TranslatableMarkup('Project'),
    'module' => new TranslatableMarkup('Module'),
  ],
)]
class ModuleSpell extends SpellBase implements
  PluginFormInterface,
  ContainerFactoryPluginInterface {

  protected ModuleExtensionList $moduleList;

  protected ShellProcessFactoryInterface $processFactory;

  protected Filesystem $fs;

  protected TwigEnvironment $twig;

  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'),
      $container->get('devel_wizard.shell_process_factory'),
      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,
    ShellProcessFactoryInterface $processFactory,
    Filesystem $fs,
  ) {
    $this->moduleList = $moduleList;
    $this->twig = $twig;
    $this->processFactory = $processFactory;
    $this->fs = $fs;
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $messenger,
      $logger,
      $stringTranslation,
      $utils,
      $configFactory,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $coreVersionParts = explode('.', \Drupal::VERSION);

    return [
      'type' => 'custom',
      'sub_module_of' => '',
      'machine_name' => '',
      'name' => '',
      'package' => '',
      'description' => '',
      'php_min' => '8.2',
      'core_compatibility' => [
        '10' => $coreVersionParts[0] === '10',
        '11' => $coreVersionParts[0] === '11',
        '12' => $coreVersionParts[0] === '12',
      ],
      'parent_dir' => '',
      'info.yml' => [],
      'initial_git_branch' => '1.x',
      'add_git_branch_as_dir_suffix' => TRUE,
    ];
  }

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

    if (empty($this->configuration['machine_name'])) {
      throw new \InvalidArgumentException('machine_name is required');
    }

    $this->configuration['machineName'] = $this->configuration['machine_name'];

    if ($this->configuration['type'] === 'sub_module'
      && empty($this->configuration['sub_module_of'])
    ) {
      throw new \InvalidArgumentException('sub_module_of is required');
    }

    $nameParts = explode('_', $this->configuration['machine_name'], 2);
    if (!$this->configuration['package']) {
      $this->configuration['package'] = ucwords($nameParts[0]);
    }

    if (!$this->configuration['name']) {
      $this->configuration['name'] = ucwords(implode(' - ', $nameParts));
    }

    if (!$this->configuration['description']) {
      $this->configuration['description'] = 'Useless description';
    }

    if (!$this->configuration['package']) {
      $this->configuration['package'] = $this->configuration['machine_name'];
    }

    if (!$this->configuration['parent_dir']) {
      $this->configuration['parent_dir'] = match($this->configuration['type']) {
        'custom' => $this->getDefaultParentDirCustom(),
        'standalone' => $this->getDefaultParentDirStandalone(),
        'sub_module' => $this->getDefaultParentDirSubModule(),
      };
    }

    $dirName = $this->configuration['machine_name'];
    if ($this->configuration['type'] === 'standalone'
      && $this->configuration['add_git_branch_as_dir_suffix']
      && !empty($this->configuration['initial_git_branch'])
    ) {
      $dirName .= "-{$this->configuration['initial_git_branch']}";
    }
    $this->configuration['dst_dir'] = Path::join($this->configuration['parent_dir'], $dirName);

    return $this;
  }

  public function getDefaultParentDirCustom(): string {
    return 'modules/custom';
  }

  public function getDefaultParentDirStandalone(): string {
    // @todo Configurable standalone "drupal/*" projects directory.
    return '../../../drupal';
  }

  public function getDefaultParentDirSubModule(): string {
    // @todo Not ideal to use this function for both example value and real value.
    if ($this->configuration['sub_module_of']) {
      return Path::join(
        $this->moduleList->getPath($this->configuration['sub_module_of']),
        'modules',
      );
    }

    return 'modules/?/<sub_module_of>/modules';
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['#attributes']['class'][] = 'devel-wizard-spell-subform-devel-wizard-module';
    $form['#attached']['library'][] = 'devel_wizard/spell.module';
    $parents = $form['#parents'];
    $configuration = $this->getConfiguration();

    $typeName = $this->utils->inputName($parents, 'type');
    $statesTypeStandalone = [
      'visible' => [
        ":input[name=\"$typeName\"]" => ['value' => 'standalone'],
      ],
    ];
    $statesTypeSubmodule = [
      'visible' => [
        ":input[name=\"$typeName\"]" => ['value' => 'sub_module'],
      ],
    ];

    $form['type'] = [
      '#type' => 'radios',
      '#required' => TRUE,
      '#title' => $this->t('Type'),
      '#options' => [
        'custom' => $this->t('Custom'),
        'standalone' => $this->t('Standalone'),
        'sub_module' => $this->t('Sub-module'),
      ],
      '#default_value' => $configuration['type'],
    ];

    $form['sub_module_of'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => [Utils::class, 'alwaysFalse'],
        'standalone' => TRUE,
      ],
      '#required' => FALSE,
      '#title' => $this->t('Sub-module of'),
      '#description' => $this->t('Machine readable name of the parent module'),
      '#default_value' => $configuration['sub_module_of'],
      '#field_suffix' => '<code>.info.yml</code>',
      '#size' => $this->identifierInputSize,
      '#maxlength' => $this->identifierMaxLength,
      '#autocomplete_route_name' => 'devel_wizard.autocomplete.module',
      '#states' => $statesTypeSubmodule,
    ];

    $form['machine_name'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => [Utils::class, 'alwaysFalse'],
        'standalone' => TRUE,
      ],
      '#required' => TRUE,
      '#title' => $this->t('Machine-name'),
      '#description' => $this->t('Machine readable name of the new module'),
      '#default_value' => $configuration['machine_name'],
      '#field_suffix' => '<code>.info.yml</code>',
      '#size' => $this->identifierInputSize,
      '#maxlength' => $this->identifierMaxLength,
      '#autocomplete_route_name' => 'devel_wizard.autocomplete.module',
    ];

    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#description' => $this->t(
        'Value for <code>@json_ref</code>',
        [
          '@json_ref' => '*.info.yml#/name',
        ],
      ),
      '#default_value' => $configuration['name'],
    ];

    $form['package'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Package'),
      '#description' => $this->t(
        'Value for <code>@json_ref</code>',
        [
          '@json_ref' => '*.info.yml#/package',
        ],
      ),
      '#default_value' => $configuration['package'],
    ];

    $form['description'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Description'),
      '#description' => $this->t(
        'Value for <code>@json_ref</code>',
        [
          '@json_ref' => '*.info.yml#/description',
        ],
      ),
      '#default_value' => $configuration['description'],
    ];

    $form['php_min'] = [
      '#type' => 'select',
      '#title' => $this->t('Minimum PHP version'),
      '#description' => $this->t(
        'Value for <code>@json_ref</code>',
        [
          '@json_ref' => 'composer.json#/require/php',
        ],
      ),
      '#options' => $this->utils->phpVersionChoices(),
      '#default_value' => $configuration['php_min'],
      '#states' => $statesTypeStandalone,
    ];

    $form['core_compatibility'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Core compatibility'),
      '#description' => $this->t(
        'Value for <code>@json_ref</code>',
        [
          '@json_ref' => '*.info.yml#/core_compatibility',
        ],
      ),
      '#options' => [
        '10' => '10',
        '11' => '11',
        '12' => '12',
      ],
      '#default_value' => array_keys($configuration['core_compatibility'], TRUE, TRUE),
      '#states' => $statesTypeStandalone,
    ];

    $form['parent_dir'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Parent directory'),
      '#field_suffix' => '<code>/&lt;machine_name&gt;/&lt;machine_name&gt;.info.yml</code>',
      '#default_value' => $configuration['parent_dir'],
      '#description' => $this->t('Relative from DRUPAL_ROOT'),
      '#size' => 40,
    ];

    return $form;
  }

  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    // @todo Implement validateConfigurationForm() method.
  }

  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValue($form['#parents'], []);
    $values['core_compatibility'] = array_fill_keys(
      array_keys($values['core_compatibility'], TRUE),
      TRUE,
    );
    $this->setConfiguration($values);
  }

  /**
   * @throws \Twig\Error\SyntaxError
   * @throws \Twig\Error\RuntimeError
   * @throws \Twig\Error\LoaderError
   */
  protected function doIt(): static {
    $this->generateInfoYml();
    if ($this->configuration['type'] === 'standalone') {
      $this
        ->generateComposerJson()
        ->generateReadmeMd()
        ->gitInit();
    }
    else {
      drupal_flush_all_caches();
    }

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

    return $this;
  }

  protected function generateInfoYml(): static {
    $fileName = Path::join(
      $this->configuration['dst_dir'],
      "{$this->configuration['machine_name']}.info.yml",
    );

    $this->fs->mkdir(Path::getDirectory($fileName));
    $this->fs->dumpFile(
      $fileName,
      YamlSymfony::encode($this->getInfoYmlData()),
    );
    $this->messageFilesystemEntryCreate($fileName);

    return $this;
  }

  protected function generateComposerJson(): static {
    $fileName = Path::join(
      $this->configuration['dst_dir'],
      'composer.json',
    );

    $this->fs->mkdir(Path::getDirectory($fileName));
    $this->fs->dumpFile(
      $fileName,
      json_encode($this->getComposerJsonData(), $this->utils->getJsonEncodeFlags()) . "\n",
    );
    $this->messageFilesystemEntryCreate($fileName);

    return $this;
  }

  /**
   * @throws \Twig\Error\RuntimeError
   * @throws \Twig\Error\SyntaxError
   * @throws \Twig\Error\LoaderError
   */
  protected function generateReadmeMd(): static {
    $fileName = Path::join(
      $this->configuration['dst_dir'],
      'README.md',
    );
    $this->fs->mkdir(Path::getDirectory($fileName));
    $this->fs->dumpFile($fileName, $this->getReadmeMdContent());
    $this->messageFilesystemEntryCreate($fileName);

    return $this;
  }

  protected function gitInit(): static {
    $process = $this->processFactory->createInstance(
      [
        'git',
        'init',
        "--initial-branch={$this->configuration['initial_git_branch']}",
      ],
      $this->configuration['dst_dir'],
    );
    $process->run();

    $exitCode = $process->getExitCode();

    $msgArgs = [
      '@spell' => $this->getPluginDefinition()->id(),
      '%dir' => $this->configuration['dst_dir'],
    ];
    $this->batchContext['sandbox']['messages'][] = [
      'type' => $exitCode ? MessengerInterface::TYPE_WARNING : MessengerInterface::TYPE_STATUS,
      'message' => ($exitCode ?
        $this->t('@spell Git repository could not be initialized in: %dir', $msgArgs)
        : $this->t('@spell Git repository has been initialized in: %dir', $msgArgs)
      ),
    ];

    return $this;
  }

  /**
   * @throws \Twig\Error\SyntaxError
   * @throws \Twig\Error\RuntimeError
   * @throws \Twig\Error\LoaderError
   */
  protected function getReadmeMdContent(): string {
    return $this->twig->render(
      '@devel_wizard/devel_wizard.extension.README.md.twig',
      $this->getConfiguration(),
    );
  }

  /**
   * @todo This should be part of the ::populateCalculatedConfigurationValues().
   */
  protected function getInfoYmlData(): array {
    $configuration = $this->getConfiguration();
    $data = $configuration['info.yml'] ?? [];

    if (empty($data['type'])) {
      $data['type'] = 'module';
    }

    if (empty($data['package'])) {
      $data['package'] = $this->configuration['package'];
    }

    if (empty($data['name'])) {
      $data['name'] = $this->configuration['name'];
    }

    if (empty($data['description'])) {
      $data['description'] = $this->configuration['description'];
    }

    if (empty($data['core_version_requirement'])) {
      $data['core_version_requirement'] = '^' . implode(' || ^', array_keys($this->configuration['core_compatibility'], TRUE, TRUE));
    }

    // @todo Short keys.
    return $data;
  }

  protected function getComposerJsonData(): array {
    $configuration = $this->getConfiguration();

    return [
      'type' => 'drupal-module',
      'name' => "drupal/{$configuration['machine_name']}",
      'description' => "{$configuration['description']}",
      'keywords' => [
        'drupal',
      ],
      'license' => 'GPL-2.0-or-later',
      'homepage' => "https://drupal.org/project/{$configuration['machine_name']}",
      'support' => [
        'issues' => "https://drupal.org/project/issues/{$configuration['machine_name']}",
        'source' => "https://git.drupalcode.org/project/{$configuration['machine_name']}",
      ],
      'require' => [
        'php' => ">={$configuration['php_min']}",
      ],
    ];
  }

}

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

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