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

src/Plugin/DevelWizard/Spell/ConfigObjectSpell.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\Menu\MenuLinkManagerInterface;
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\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\String\UnicodeString;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;

#[DevelWizardSpell(
  id: 'devel_wizard_config_object',
  category: new TranslatableMarkup('Code'),
  label: new TranslatableMarkup('Config object'),
  description: new TranslatableMarkup('Generates a new config_object with schema, edit form and menu link.'),
  tags: [
    'code' => new TranslatableMarkup('Code'),
    'config_object' => new TranslatableMarkup('Config object'),
  ],
)]
class ConfigObjectSpell extends SpellBase implements
  PluginFormInterface,
  ConfigurableInterface,
  ContainerFactoryPluginInterface {

  use SpellTraitPackageManager;

  protected ModuleExtensionList $moduleList;

  protected TwigEnvironment $twig;

  protected UrlMatcherInterface $router;

  protected MenuLinkManagerInterface $menuLinkManager;

  protected Filesystem $fs;

  /**
   * {@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('extension.list.module'),
      $container->get('twig'),
      $container->get('router.no_access_checks'),
      $container->get('plugin.manager.menu.link'),
      new Filesystem(),
    );
  }

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    MessengerInterface $messenger,
    LoggerInterface $logger,
    TranslationInterface $stringTranslation,
    Utils $utils,
    ConfigFactoryInterface $configFactory,
    ModuleExtensionList $moduleList,
    TwigEnvironment $twig,
    UrlMatcherInterface $router,
    MenuLinkManagerInterface $menuLinkManager,
    Filesystem $fs,
  ) {
    $this->moduleList = $moduleList;
    $this->twig = $twig;
    $this->router = $router;
    $this->menuLinkManager = $menuLinkManager;
    $this->fs = $fs;

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

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'module' => '',
      'parentPath' => '/admin/config/system',
      'object' => 'settings',
      'addRouteToInfoConfigure' => TRUE,
    ];
  }

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

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

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

    $this->configuration['objectUpperCamel'] = (new UnicodeString("a_{$this->configuration['object']}"))
      ->camel()
      ->trimPrefix('a')
      ->toString();

    $this->configuration += [
      'configSchema' => [],
      'configInstall' => [],
      'form' => [],
      'permissions' => [],
      'routing' => [],
      'linksMenu' => [],
    ];

    $this->configuration['configSchema'] += [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        'config',
        'schema',
        "{$this->configuration['module']}.schema.yml",
      ),
      'id' => sprintf(
        '%s.%s',
        $this->configuration['module'],
        $this->configuration['object'],
      ),
      'definition' => [
        'type' => 'config_object',
        'label' => "{$this->configuration['module']} {$this->configuration['object']}",
        'mapping' => [
          'foo' => [
            'type' => 'string',
            'label' => 'Foo',
          ],
        ],
      ],
    ];

    $this->configuration['configInstall'] += [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        'config',
        'install',
        "{$this->configuration['configSchema']['id']}.yml",
      ),
      'definition' => [
        'foo' => 'bar',
      ],
    ];

    $this->configuration['form'] += [
      'id' => "{$this->configuration['module']}_{$this->configuration['object']}_form",
      'namespace' => "Drupal\\{$this->configuration['module']}\\ConfigObject",
      'class' => "{$this->configuration['objectUpperCamel']}Form",
    ];
    $this->configuration['form']['classFqn'] = sprintf(
      '%s\\%s',
      $this->configuration['form']['namespace'],
      $this->configuration['form']['class'],
    );
    $this->configuration['form']['filePath'] = Path::join(
      $this->configuration['moduleDir'],
      $this->utils->classFqnToFilePath($this->configuration['form']['classFqn']),
    );

    $this->configuration['permissions'] += [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        "{$this->configuration['module']}.permissions.yml",
      ),
      'id' => "{$this->configuration['module']}.{$this->configuration['object']}.admin",
      'definition' => [],
    ];
    $this->configuration['permissions']['definition'] += [
      'title' => "Access to manage {$this->configuration['module']} {$this->configuration['object']}",
      'restrict access' => TRUE,
    ];

    $match = $this->router->match($this->configuration['parentPath']);
    $links = $this->menuLinkManager->loadLinksByRoute($match['_route']);
    /** @var \Drupal\Core\Menu\MenuLinkDefault $parentMenuLink */
    $parentMenuLink = reset($links) ?: NULL;

    $this->configuration['routing'] += [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        "{$this->configuration['module']}.routing.yml",
      ),
      'id' => "{$this->configuration['module']}.{$this->configuration['object']}.edit",
      'definition' => [],
    ];
    $this->configuration['routing']['definition'] += [
      'path' => sprintf(
        '%s/%s-%s',
        $this->configuration['parentPath'],
        $this->configuration['module'],
        strtr($this->configuration['object'], ['.' => '-']),
      ),
      'defaults' => [
        '_title' => $this->configuration['configSchema']['definition']['label'],
        '_form' => '\\' . $this->configuration['form']['classFqn'],
      ],
      'requirements' => [
        '_permission' => $this->configuration['permissions']['id'],
      ],
    ];

    $this->configuration['linksMenu'] += [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        "{$this->configuration['module']}.links.menu.yml",
      ),
      'id' => $this->configuration['routing']['id'],
      'definition' => [],
    ];
    $this->configuration['linksMenu']['definition'] = array_replace(
      [
        'route_name' => $this->configuration['routing']['id'],
        'parent' => $parentMenuLink->getPluginId(),
        'title' => $this->configuration['configSchema']['definition']['label'],
        'description' => $this->configuration['configSchema']['definition']['label'],
        'weight' => 10,
      ],
      $this->configuration['linksMenu']['definition'],
    );

    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['parentPath'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Parent path'),
      '#default_value' => $conf['parentPath'],
      '#description' => $this->t('For example: <code>/admin/config/system</code>, <code>/admin/config/content</code>, <code>/admin/config/development</code>'),
    ];

    $form['object'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Object'),
      '#default_value' => $conf['object'],
      '#description' => $this->t('My custom machine-name'),
    ];

    $form['addRouteToInfoConfigure'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Add route to *.info.yml#/configure'),
      '#default_value' => $conf['addRouteToInfoConfigure'],
      '#description' => $this->t('Only if it is still empty'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValue($form['#parents'], []);
    $parentPathInputName = implode(
      '][',
      array_merge($form['#parents'], ['parentPath']),
    );
    try {
      $match = $this->router->match($values['parentPath']);
      $links = $this->menuLinkManager->loadLinksByRoute($match['_route']);
      if (!$links) {
        $form_state->setErrorByName($parentPathInputName, $this->t('There is no menu link which points to the given parent path'));
      }
    }
    catch (ResourceNotFoundException) {
      $form_state->setErrorByName($parentPathInputName, $this->t('The given parent path is not exists'));
    }
  }

  /**
   * {@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();

    $this->utils->ymlFileReplace(
      $conf['configSchema']['filePath'],
      [
        $conf['configSchema']['id'] => $conf['configSchema']['definition'],
      ],
    );

    $this->utils->ymlFileReplace(
      $conf['configInstall']['filePath'],
      $conf['configInstall']['definition'],
    );

    $this->fs->mkdir(Path::getDirectory($conf['form']['filePath']));
    $this->fs->dumpFile(
      $conf['form']['filePath'],
      $this->twig->render(
        '@devel_wizard/spell/config_object/form.php.twig',
        $conf,
      ),
    );

    $this->utils->ymlFileReplace(
      $conf['permissions']['filePath'],
      [
        $conf['permissions']['id'] => $conf['permissions']['definition'],
      ],
    );

    $this->utils->ymlFileReplace(
      $conf['routing']['filePath'],
      [
        $conf['routing']['id'] => $conf['routing']['definition'],
      ],
    );

    $this->utils->ymlFileReplace(
      $conf['linksMenu']['filePath'],
      [
        $conf['linksMenu']['id'] => $conf['linksMenu']['definition'],
      ],
    );

    if ($conf['addRouteToInfoConfigure']) {
      $filePath = Path::join(
        $conf['moduleDir'],
        $conf['module'] . '.info.yml',
      );
      if ($this->fs->exists($filePath)) {
        $info = SymfonyYaml::parseFile($filePath ?: '{}');
        if (empty($info['configure'])) {
          $info['configure'] = $conf['routing']['id'];
          $this->fs->dumpFile($filePath, SymfonyYaml::dump($info, 99, 2));
        }
      }
    }

    drupal_flush_all_caches();

    // @todo If module is enabled
    $this->batchContext['sandbox']['finished'] = 1.0;

    return $this;
  }

}

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

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