Verified Commit daf0e8e3 authored by Dave Long's avatar Dave Long
Browse files

task: #3595652 Deprecate module.inc contents

By: nicxvan
By: claudiu.cristea
By: catch
parent 5d978bc3
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1509,7 +1509,7 @@ services:
    arguments: ['@config.manager', '@config.storage', '@config.storage.snapshot']
  config_exclude_modules_subscriber:
    class: Drupal\Core\EventSubscriber\ExcludedModulesEventSubscriber
    arguments: ['@config.storage', '@settings', '@config.manager']
    arguments: ['@config.storage', '@settings', '@config.manager', '@Drupal\Core\Extension\ModuleWeight']
  exception.needs_installer:
    class: Drupal\Core\EventSubscriber\ExceptionDetectNeedsInstallSubscriber
    arguments: ['@database']
@@ -2090,3 +2090,5 @@ services:
      - { name: needs_destruction }
  Drupal\Core\Extension\ThemeSettingsProvider:
    autowire: true
  Drupal\Core\Extension\ModuleWeight:
    autowire: true
+2 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
use Drupal\Core\StringTranslation\Translator\FileTranslation;
use Drupal\Core\StackMiddleware\ReverseProxyMiddleware;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleWeight;
use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Entity\ConfigurableLanguage;
@@ -1815,7 +1816,7 @@ function install_finished(&$install_state): void {
  // If the profile has asked to be automatically uninstalled, do that.
  if ($install_state['profile_info']['keep_profile'] ?? TRUE) {
    // Installation profiles are always loaded last.
    module_set_weight($profile, 1000);
    \Drupal::service(ModuleWeight::class)->set($profile, 1000);
  }
  else {
    \Drupal::service('module_installer')->uninstall([$profile]);
+16 −41
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * API for loading and interacting with Drupal modules.
 */

use Drupal\Core\Extension\ModuleWeight;

/**
 * Sets weight of a particular module.
 *
@@ -14,31 +16,15 @@
 *   The name of the module (without the .module extension).
 * @param int $weight
 *   An integer representing the weight of the module.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(ModuleWeight::class)->set() instead.
 *
 * @see https://www.drupal.org/node/3595653
 */
function module_set_weight($module, $weight): void {
  $extension_config = \Drupal::configFactory()->getEditable('core.extension');
  if ($extension_config->get("module.$module") !== NULL) {
    // Pre-cast the $weight to an integer so that we can save this without using
    // schema. This is a performance improvement for module installation.
    $extension_config
      ->set("module.$module", (int) $weight)
      ->set('module', module_config_sort($extension_config->get('module')))
      ->save();

    // Prepare the new module list, sorted by weight, including filenames.
    // @see \Drupal\Core\Extension\ModuleInstaller::install()
    $module_handler = \Drupal::moduleHandler();
    $current_module_filenames = $module_handler->getModuleList();
    $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
    $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
    $module_filenames = [];
    foreach ($current_modules as $name => $weight) {
      $module_filenames[$name] = $current_module_filenames[$name];
    }
    // Update the module list in the extension handler.
    $module_handler->setModuleList($module_filenames);
    return;
  }
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal::service(ModuleWeight::class)->set() instead. See https://www.drupal.org/node/3595653', E_USER_DEPRECATED);
  \Drupal::service(ModuleWeight::class)->set($module, $weight);
}

/**
@@ -52,24 +38,13 @@ function module_set_weight($module, $weight): void {
 *
 * @return array
 *   An array of module configuration data sorted by weight and name.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(ModuleWeight::class)->sort() instead.
 *
 * @see https://www.drupal.org/node/3595653
 */
function module_config_sort($data) {
  // PHP array sorting functions such as uasort() do not work with both keys and
  // values at the same time, so we achieve weight and name sorting by computing
  // strings with both information concatenated (weight first, name second) and
  // use that as a regular string sort reference list via array_multisort(),
  // compound of "[sign-as-integer][padded-integer-weight][name]"; e.g., given
  // two modules and weights (spaces added for clarity):
  // - Block with weight -5: 0 0000000000000000005 block
  // - Node  with weight  0: 1 0000000000000000000 node
  $sort = [];
  foreach ($data as $name => $weight) {
    // Prefix negative weights with 0, positive weights with 1.
    // +/- signs cannot be used, since + (ASCII 43) is before - (ASCII 45).
    $prefix = (int) ($weight >= 0);
    // The maximum weight is PHP_INT_MAX, so pad all weights to 19 digits.
    $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name;
  }
  array_multisort($sort, SORT_STRING, $data);
  return $data;
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal::service(ModuleWeight::class)->sort() instead. See https://www.drupal.org/node/3595653', E_USER_DEPRECATED);
  return \Drupal::service(ModuleWeight::class)->sort($data);
}
+8 −31
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageTransformEvent;
use Drupal\Core\Extension\ModuleWeight;
use Drupal\Core\Site\Settings;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

@@ -20,36 +21,12 @@ final class ExcludedModulesEventSubscriber implements EventSubscriberInterface {
   */
  const EXCLUDED_MODULES_KEY = "config_exclude_modules";

  /**
   * @var \Drupal\Core\Config\StorageInterface
   */
  private $activeStorage;

  /**
   * @var \Drupal\Core\Site\Settings
   */
  private $settings;

  /**
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  private $manager;

  /**
   * EnvironmentModulesEventSubscriber constructor.
   *
   * @param \Drupal\Core\Config\StorageInterface $active_storage
   *   The active config storage.
   * @param \Drupal\Core\Site\Settings $settings
   *   The Drupal settings.
   * @param \Drupal\Core\Config\ConfigManagerInterface $manager
   *   The config manager.
   */
  public function __construct(StorageInterface $active_storage, Settings $settings, ConfigManagerInterface $manager) {
    $this->activeStorage = $active_storage;
    $this->settings = $settings;
    $this->manager = $manager;
  }
  public function __construct(
    private StorageInterface $activeStorage,
    private Settings $settings,
    private ConfigManagerInterface $manager,
    private ModuleWeight $moduleWeight,
  ) {}

  /**
   * {@inheritdoc}
@@ -102,7 +79,7 @@ public function onConfigTransformImport(StorageTransformEvent $event) {
    }

    // Sort the extensions.
    $extension['module'] = module_config_sort($modules);
    $extension['module'] = $this->moduleWeight->sort($modules);
    // Set the modified extension.
    $storage->write('core.extension', $extension);
  }
+8 −23
Original line number Diff line number Diff line
@@ -66,27 +66,6 @@ class ModuleInstaller implements ModuleInstallerInterface {
   */
  protected $updateRegistry;

  /**
   * Constructs a new ModuleInstaller instance.
   *
   * @param string $root
   *   The app root.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\DrupalKernelInterface $kernel
   *   The drupal kernel.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Update\UpdateHookRegistry $update_registry
   *   The update registry service.
   * @param \Psr\Log\LoggerInterface|null $logger
   *   The logger.
   * @param \Traversable|null $uninstallValidators
   *   The uninstall validator services.
   *
   * @see \Drupal\Core\DrupalKernel
   * @see \Drupal\Core\CoreServiceProvider
   */
  public function __construct(
    #[Autowire(param: 'app.root')]
    string $root,
@@ -98,6 +77,7 @@ public function __construct(
    protected LoggerInterface $logger,
    #[AutowireIterator(tag: 'module_install.uninstall_validator')]
    protected ?\Traversable $uninstallValidators = NULL,
    protected ?ModuleWeight $moduleWeight = NULL,
  ) {
    $this->root = $root;
    $this->moduleHandler = $module_handler;
@@ -107,6 +87,11 @@ public function __construct(
    if ($this->uninstallValidators === NULL) {
      $this->uninstallValidators = \Drupal::service('module_installer.uninstall_validators');
    }

    if (!$this->moduleWeight instanceof ModuleWeight) {
      @trigger_error('Calling ' . __METHOD__ . '() without the $moduleWeight argument is deprecated in drupal:11.5.0 and the argument will be required in drupal:12.0.0. See https://www.drupal.org/project/drupal/issues/3595653', E_USER_DEPRECATED);
      $this->moduleWeight = \Drupal::service(ModuleWeight::class);
    }
  }

  /**
@@ -274,7 +259,7 @@ private function doInstall(array $module_list, array $installed_modules, bool $s
    // Save this data without checking schema. This is a performance
    // improvement for module installation.
    $extension_config
      ->set('module', module_config_sort(array_merge(
      ->set('module', $this->moduleWeight->sort(array_merge(
        array_fill_keys($module_list, 0),
        $installed_modules
      )))
@@ -292,7 +277,7 @@ private function doInstall(array $module_list, array $installed_modules, bool $s
    // weight of 0.
    $current_module_filenames = $this->moduleHandler->getModuleList();
    $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
    $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
    $current_modules = $this->moduleWeight->sort(array_merge($current_modules, $extension_config->get('module')));
    $module_filenames = [];
    foreach ($current_modules as $name => $weight) {
      if (isset($current_module_filenames[$name])) {
Loading