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

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

declare(strict_types=1);

namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;

use Drupal\Core\Asset\LibraryDiscoveryInterface;
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\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\Yaml\Yaml;

#[DevelWizardSpell(
  id: 'devel_wizard_library_extend',
  category: new TranslatableMarkup('Code'),
  label: new TranslatableMarkup('Library extend'),
  description: new TranslatableMarkup('Generates a new library which extends another one.'),
  tags: [
    'code' => new TranslatableMarkup('Code'),
    'library' => new TranslatableMarkup('Library'),
  ],
)]
class LibraryExtendSpell extends SpellBase implements
  PluginFormInterface,
  ContainerFactoryPluginInterface {

  protected LibraryDiscoveryInterface $libraryDiscovery;

  protected ModuleExtensionList $moduleList;

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

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    MessengerInterface $messenger,
    LoggerInterface $logger,
    TranslationInterface $stringTranslation,
    Utils $utils,
    ConfigFactoryInterface $configFactory,
    ModuleExtensionList $moduleList,
    ?Filesystem $fs = NULL,
  ) {
    $this->moduleList = $moduleList;
    $this->fs = $fs ?: new Filesystem();

    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $messenger,
      $logger,
      $stringTranslation,
      $utils,
      $configFactory,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'libraryToExtend' => [
        'id' => '',
      ],
      'extender' => [
        'module' => '',
      ],
    ];
  }

  protected function populateCalculatedConfigurationValues(): static {
    parent::populateCalculatedConfigurationValues();

    $this->configuration['extender']['moduleDir'] = $this->moduleList->getPath($this->configuration['extender']['module']);
    $this->configuration['extender']['moduleDash'] = str_replace('_', '-', $this->configuration['extender']['module']);

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $configuration = $this->getConfiguration();

    $form['libraryToExtend'] = [
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#title' => $this->t('Library to extend'),

      'id' => [
        '#type' => 'textfield',
        '#required' => TRUE,
        '#title' => $this->t('Library to extend'),
        '#default_value' => $configuration['libraryToExtend']['id'],
        '#autocomplete_route_name' => 'devel_wizard.autocomplete.library',
      ],
    ];

    $form['extender'] = [
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#title' => $this->t('Extender'),

      'module' => [
        '#title' => $this->t('Module'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#default_value' => $configuration['extender']['module'],
        '#description' => $this->t('The module to put the library extend into'),
        '#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);
  }

  protected function doIt(): static {
    $conf = $this->getConfiguration();
    // @todo Implement doIt() method.
    // - add *.libraries.yml entry
    // - Where to put a dummy CSS/JS file?
    // - add *.info.yml#libraries-extend entry.
    $filePath = Path::join($conf['extender']['moduleDir'], "{$conf['extender']['module']}.info.yml");
    $info = Yaml::parseFile($filePath);

    $lteId = $conf['libraryToExtend']['id'];
    $extenderLibraryName = str_replace('/', '-', $lteId);
    $extenderLibraryId = "{$conf['extender']['module']}/{$extenderLibraryName}";

    $info['libraries-extend'][$lteId][] = $extenderLibraryId;
    $info['libraries-extend'][$lteId] = array_unique($info['libraries-extend'][$lteId]);
    ksort($info['libraries-extend']);
    $this->fs->dumpFile($filePath, Yaml::dump($info, 99, 2));
    $this->messageFilesystemEntryUpdate($filePath);

    $filePath = Path::join($conf['extender']['moduleDir'], "{$conf['extender']['module']}.libraries.yml");
    $info = $this->fs->exists($filePath) ?
      Yaml::parseFile($filePath)
      : [];
    $info += [
      $extenderLibraryName => [
        'dependencies' => [],
        'css' => [
          'theme' => [],
        ],
        'js' => [],
      ],
    ];
    $this->fs->dumpFile($filePath, Yaml::dump($info, 99, 2));
    $this->messageFilesystemEntryUpdate($filePath);

    return $this;
  }

}

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

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