summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Extension/ModuleInstaller.php
blob: b1b794bca580f1ffc498fcd1aa5e57e262d0475b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
<?php

namespace Drupal\Core\Extension;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\DefaultConfigMode;
use Drupal\Core\Database\Connection;
use Drupal\Core\DrupalKernelInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\Exception\ObsoleteExtensionException;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Update\UpdateHookRegistry;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;

/**
 * Default implementation of the module installer.
 *
 * It registers the module in config, installs its own configuration,
 * installs the schema, updates the Drupal kernel and more.
 *
 * We don't inject dependencies yet, as we would need to reload them after
 * each installation or uninstallation of a module.
 * https://www.drupal.org/project/drupal/issues/2350111 for example tries to
 * solve this dilemma.
 */
class ModuleInstaller implements ModuleInstallerInterface {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The drupal kernel.
   *
   * @var \Drupal\Core\DrupalKernelInterface
   */
  protected $kernel;

  /**
   * The app root.
   *
   * @var string
   */
  protected $root;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The update registry service.
   *
   * @var \Drupal\Core\Update\UpdateHookRegistry
   */
  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,
    ModuleHandlerInterface $module_handler,
    DrupalKernelInterface $kernel,
    Connection $connection,
    UpdateHookRegistry $update_registry,
    #[Autowire(service: 'logger.channel.default')]
    protected LoggerInterface $logger,
    #[AutowireIterator(tag: 'module_install.uninstall_validator')]
    protected ?\Traversable $uninstallValidators = NULL,
  ) {
    $this->root = $root;
    $this->moduleHandler = $module_handler;
    $this->kernel = $kernel;
    $this->connection = $connection;
    $this->updateRegistry = $update_registry;
    if ($this->uninstallValidators === NULL) {
      $this->uninstallValidators = \Drupal::service('module_installer.uninstall_validators');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function addUninstallValidator(ModuleUninstallValidatorInterface $uninstall_validator) {
    @trigger_error(__METHOD__ . ' is deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Inject the uninstall validators into the constructor instead. See https://www.drupal.org/node/3432595', E_USER_DEPRECATED);
  }

  /**
   * {@inheritdoc}
   */
  public function install(array $module_list, $enable_dependencies = TRUE) {
    $extension_config = \Drupal::configFactory()->getEditable('core.extension');

    // Remove any modules that are already installed.
    $installed_modules = $extension_config->get('module') ?: [];
    // Only process currently uninstalled modules.
    $module_list = array_diff($module_list, array_keys($installed_modules));

    if (empty($module_list)) {
      // Nothing to do. All modules already installed.
      return TRUE;
    }

    // Get all module data so we can find dependencies and sort and find the
    // core requirements. The module list needs to be reset so that it can
    // re-scan and include any new modules that may have been added directly
    // into the filesystem.
    $module_data = \Drupal::service('extension.list.module')->reset()->getList();
    foreach ($module_list as $module) {
      if (!empty($module_data[$module]->info['core_incompatible'])) {
        throw new MissingDependencyException("Unable to install modules: module '$module' is incompatible with this version of Drupal core.");
      }
      if ($module_data[$module]->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::OBSOLETE) {
        throw new ObsoleteExtensionException("Unable to install modules: module '$module' is obsolete.");
      }
      if ($module_data[$module]->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::DEPRECATED) {
        // phpcs:ignore Drupal.Semantics.FunctionTriggerError
        @trigger_error("The module '$module' is deprecated. See " . $module_data[$module]->info['lifecycle_link'], E_USER_DEPRECATED);
      }
    }
    if ($enable_dependencies) {
      $module_list = $module_list ? array_combine($module_list, $module_list) : [];
      if ($missing_modules = array_diff_key($module_list, $module_data)) {
        // One or more of the given modules doesn't exist.
        throw new MissingDependencyException(sprintf('Unable to install modules %s due to missing modules %s.', implode(', ', $module_list), implode(', ', $missing_modules)));
      }

      // Add dependencies to the list. The new modules will be processed as
      // the foreach loop continues.
      foreach ($module_list as $module => $value) {
        foreach (array_keys($module_data[$module]->requires) as $dependency) {
          if (!isset($module_data[$dependency])) {
            // The dependency does not exist.
            throw new MissingDependencyException("Unable to install modules: module '$module' is missing its dependency module $dependency.");
          }

          // Skip already installed modules.
          if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) {
            if ($module_data[$dependency]->info['core_incompatible']) {
              throw new MissingDependencyException("Unable to install modules: module '$module'. Its dependency module '$dependency' is incompatible with this version of Drupal core.");
            }
            $module_list[$dependency] = $dependency;
          }
        }
      }

      // Set the actual module weights.
      $module_list = array_map(function ($module) use ($module_data) {
        return $module_data[$module]->sort;
      }, $module_list);

      // Sort the module list by their weights (reverse).
      arsort($module_list);
      $module_list = array_keys($module_list);
    }

    // Required for module installation checks.
    include_once $this->root . '/core/includes/install.inc';

    /** @var \Drupal\Core\Config\ConfigInstaller $config_installer */
    $config_installer = \Drupal::service('config.installer');
    $sync_status = $config_installer->isSyncing();
    foreach ($module_list as $module) {
      // Throw an exception if the module name is too long.
      if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
        throw new ExtensionNameLengthException("Module name '$module' is over the maximum allowed length of " . DRUPAL_EXTENSION_NAME_MAX_LENGTH . ' characters');
      }

      // Throw an exception if a theme with the same name is enabled.
      $installed_themes = $extension_config->get('theme') ?: [];
      if (isset($installed_themes[$module])) {
        throw new ExtensionNameReservedException("Module name $module is already in use by an installed theme.");
      }
    }

    // Check the validity of the default configuration. This will throw
    // exceptions if the configuration is not valid.
    $config_installer->checkConfigurationToInstall('module', $module_list);

    // Some modules require a container rebuild before and after install.
    // Group modules such that as many are installed together as possible until
    // one needs a container rebuild.
    $module_groups = [];
    $index = 0;
    foreach ($module_list as $module) {
      // Ensure the container is rebuilt both before and after a module that
      // requires a container rebuild is installed.
      $container_rebuild_required = !empty($module_data[$module]->info['container_rebuild_required']);
      if ($container_rebuild_required && !empty($module_groups[$index])) {
        $index++;
      }
      $module_groups[$index][] = $module;
      if ($container_rebuild_required) {
        $index++;
      }
    }
    foreach ($module_groups as $modules) {
      $this->doInstall($modules, $installed_modules, $sync_status);
      // Refresh the installed modules list from configuration to preserve
      // module weight.
      $extension_config = \Drupal::configFactory()->getEditable('core.extension');
      $installed_modules = $extension_config->get('module') ?: [];
    }
    if (!InstallerKernel::installationAttempted()) {
      // If the container was rebuilt during hook_install() it might not have
      // the 'router.route_provider.old' service.
      if (\Drupal::hasService('router.route_provider.old')) {
        \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.old'));
      }
      if (!\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) {
        // Rebuild routes after installing module. This is done here on top of
        // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
        // fastCGI which executes ::destruct() after the module installation
        // page was sent already.
        \Drupal::service('router.builder')->rebuild();
      }
      else {
        // Rebuild the router immediately if it is marked as needing a rebuild.
        // @todo Work this through a bit more. This fixes
        //   \Drupal\Tests\standard\Functional\StandardTest::testStandard()
        //   after separately out the optional configuration install.
        \Drupal::service('router.builder')->rebuildIfNeeded();
      }
    }

    $this->moduleHandler->invokeAll('modules_installed', [$module_list, $sync_status]);
    return TRUE;
  }

  /**
   * Installs a set of modules.
   *
   * @param array $module_list
   *   The list of modules to install.
   * @param array $installed_modules
   *   An array of the already installed modules.
   * @param bool $sync_status
   *   The config sync status.
   */
  private function doInstall(array $module_list, array $installed_modules, bool $sync_status): void {
    $extension_config = \Drupal::configFactory()->getEditable('core.extension');

    // Save this data without checking schema. This is a performance
    // improvement for module installation.
    $extension_config
      ->set('module', module_config_sort(array_merge(
        array_fill_keys($module_list, 0),
        $installed_modules
      )))
      ->save(TRUE);

    // Prepare the new module list, sorted by weight, including filenames.
    // This list is used for both the ModuleHandler and DrupalKernel. It
    // needs to be kept in sync between both. A DrupalKernel reboot or
    // rebuild will automatically re-instantiate a new ModuleHandler that
    // uses the new module list of the kernel. However, DrupalKernel does
    // not cause any modules to be loaded.
    // Furthermore, the currently active (fixed) module list can be
    // different from the configured list of enabled modules. For all active
    // modules not contained in the configured enabled modules, we assume a
    // 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')));
    $module_filenames = [];
    foreach ($current_modules as $name => $weight) {
      if (isset($current_module_filenames[$name])) {
        $module_filenames[$name] = $current_module_filenames[$name];
      }
      else {
        $module_path = \Drupal::service('extension.list.module')
          ->getPath($name);
        $pathname = "$module_path/$name.info.yml";
        $filename = file_exists($module_path . "/$name.module") ? "$name.module" : NULL;
        $module_filenames[$name] = new Extension($this->root, 'module', $pathname, $filename);
      }
    }

    // Update the module handler in order to have the correct module list
    // for the kernel update.
    $this->moduleHandler->setModuleList($module_filenames);

    // Clear the static cache of the "extension.list.module" service to pick
    // up the new module, since it merges the installation status of modules
    // into its statically cached list.
    \Drupal::service('extension.list.module')->reset();

    // Update the kernel to include it.
    $this->updateKernel($module_filenames);

    if (!InstallerKernel::installationAttempted()) {
      // Replace the route provider service with a version that will rebuild
      // if routes are used during installation. This ensures that a module's
      // routes are available during installation. This has to occur before
      // any services that depend on it are instantiated otherwise those
      // services will have the old route provider injected. Note that, since
      // the container is rebuilt by updating the kernel, the route provider
      // service is the regular one even though we are in a loop and might
      // have replaced it before.
      \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
      \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
    }

    foreach ($module_list as $module) {
      // Load the module's .module and .install files. Do this for all modules
      // prior to calling hook_module_preinstall() in order to not pollute the
      // cache.
      $this->moduleHandler->load($module);
      $this->moduleHandler->loadInclude($module, 'install');
    }

    foreach ($module_list as $module) {
      // Allow modules to react prior to the installation of a module.
      $this->moduleHandler->invokeAll('module_preinstall', [$module, $sync_status]);

      // Now install the module's schema if necessary.
      $this->installSchema($module);
    }

    // Clear plugin manager caches.
    // @todo should this be in the loop?
    \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();

    $entity_type_providers_to_install = $module_list;
    foreach ($module_list as $module) {
      // Remove the module from the list of possible entity type providers to
      // install.
      array_shift($entity_type_providers_to_install);

      // Notify interested components that this module's entity types and
      // field storage definitions are new. For example, a SQL-based storage
      // handler can use this as an opportunity to create the necessary
      // database tables.
      // @todo Clean this up in https://www.drupal.org/node/2350111.
      $entity_type_manager = \Drupal::entityTypeManager();
      $update_manager = \Drupal::entityDefinitionUpdateManager();
      /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
      $entity_field_manager = \Drupal::service('entity_field.manager');
      foreach ($entity_type_manager->getDefinitions() as $entity_type) {
        $is_fieldable_entity_type = $entity_type->entityClassImplements(FieldableEntityInterface::class);

        if ($entity_type->getProvider() == $module) {
          if ($is_fieldable_entity_type) {
            $update_manager->installFieldableEntityType($entity_type, $entity_field_manager->getFieldStorageDefinitions($entity_type->id()));
          }
          else {
            $update_manager->installEntityType($entity_type);
          }
        }
        elseif ($is_fieldable_entity_type && !in_array($entity_type->getProvider(), $entity_type_providers_to_install, TRUE)) {
          // The module being installed may be adding new fields to existing
          // entity types. Field definitions for any entity type defined by
          // modules being installed are handled in the if branch.
          foreach ($entity_field_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
            if ($storage_definition->getProvider() == $module) {
              // If the module being installed is also defining a storage key
              // for the entity type, the entity schema may not exist yet. It
              // will be created later in that case.
              try {
                $update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition);
              }
              catch (EntityStorageException $e) {
                Error::logException($this->logger, $e, 'An error occurred while notifying the creation of the @name field storage definition: "@message" in %function (line %line of %file).', ['@name' => $storage_definition->getName()]);
              }
            }
          }
        }
      }
    }

    foreach ($module_list as $module) {
      // Install default configuration of the module.
      $config_installer = \Drupal::service('config.installer');
      $config_installer->installDefaultConfig('module', $module, DefaultConfigMode::InstallSimple);

      // Set the schema version to the number of the last update provided by
      // the module, or the minimum core schema version.
      $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
      $versions = $this->updateRegistry->getAvailableUpdates($module);
      if ($versions) {
        $version = max(max($versions), $version);
      }

      // If the module has no current updates, but has some that were
      // previously removed, set the version to the value of
      // hook_update_last_removed().
      if ($last_removed = $this->invoke($module, 'update_last_removed')) {
        $version = max($version, $last_removed);
      }
      $this->updateRegistry->setInstalledVersion($module, $version);
    }

    // Drupal's stream wrappers needs to be re-registered in case a
    // module-provided stream wrapper is used later in the same request. In
    // particular, this happens when installing Drupal via Drush, as the
    // 'translations' stream wrapper is provided by Interface Translation
    // module and is later used to import translations.
    \Drupal::service('stream_wrapper_manager')->register();

    // Update the theme registry to include it.
    \Drupal::service('theme.registry')->reset();

    // Modules can alter theme info, so refresh theme data.
    // @todo ThemeHandler cannot be injected into ModuleHandler, since that
    //   causes a circular service dependency.
    // @see https://www.drupal.org/node/2208429
    \Drupal::service('theme_handler')->refreshInfo();

    // Modules may provide single directory components which are added to
    // the core library definitions rather than the module itself, this
    // requires the library discovery cache to be rebuilt.
    \Drupal::service('library.discovery')->clear();

    $config_installer = \Drupal::service('config.installer');
    foreach ($module_list as $module) {
      // Create config entities a module has in the /install directory.
      $config_installer->installDefaultConfig('module', $module, DefaultConfigMode::InstallEntities);

      // Allow the module to perform install tasks.
      $this->invoke($module, 'install', [$sync_status]);

      // Record the fact that it was installed.
      \Drupal::logger('system')->info('%module module installed.', ['%module' => $module]);
    }

    // Install optional configuration from modules once all the modules have
    // been properly installed. This is often where soft dependencies lie.
    // @todo This code fixes \Drupal\Tests\help\Functional\HelpTest::testHelp().
    foreach ($module_list as $module) {
      $config_installer->installDefaultConfig('module', $module, DefaultConfigMode::Optional);
    }
    // Install optional configuration from other modules once all the modules
    // have been properly installed. This is often where soft dependencies lie.
    // @todo This code fixes
    //   \Drupal\Tests\forum\Functional\Module\DependencyTest::testUninstallDependents().
    foreach ($module_list as $module) {
      $config_installer->installDefaultConfig('module', $module, DefaultConfigMode::SiteOptional);
    }

    if (count($module_list) > 1) {
      // Reset the container so static caches are rebuilt. This prevents static
      // caches like those in \Drupal\views\ViewsData() from having stale data.
      // @todo Adding this code fixed
      //   \Drupal\KernelTests\Config\DefaultConfigTest::testModuleConfig().
      //   \Drupal\Component\DependencyInjection\Container::reset() seems to
      //   offer a way to do this but was broken for the following reasons:
      //   1. Needs to set itself to 'service_container' like the constructor.
      //   2. Needs to persist services, user and session like
      //      DrupalKernel::initializeContainer()
      //   3. Needs to work out how to work with things like
      //      KernelTestBase::register() which set synthetic like services.
      $this->updateKernel([]);

      // Refresh anything cached with core.extension. This prevents caches in
      // things like \Drupal\views\ViewsData() from having stale data.
      // @todo This fixes \Drupal\Tests\views\Functional\ViewsFormAlterTest().
      Cache::invalidateTags(['config:core.extension']);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
    // Get all module data so we can find dependencies and sort.
    $module_data = \Drupal::service('extension.list.module')->getList();
    $sync_status = \Drupal::service('config.installer')->isSyncing();
    $module_list = $module_list ? array_combine($module_list, $module_list) : [];
    if (array_diff_key($module_list, $module_data)) {
      // One or more of the given modules doesn't exist.
      return FALSE;
    }

    $extension_config = \Drupal::configFactory()->getEditable('core.extension');
    $installed_modules = $extension_config->get('module') ?: [];
    if (!$module_list = array_intersect_key($module_list, $installed_modules)) {
      // Nothing to do. All modules already uninstalled.
      return TRUE;
    }

    if ($uninstall_dependents) {
      $theme_list = \Drupal::service('extension.list.theme')->getList();

      // Add dependent modules to the list. The new modules will be processed as
      // the foreach loop continues.
      foreach ($module_list as $module => $value) {
        foreach (array_keys($module_data[$module]->required_by) as $dependent) {
          if (!isset($module_data[$dependent]) && !isset($theme_list[$dependent])) {
            // The dependent module or theme does not exist.
            return FALSE;
          }

          // Skip already uninstalled modules.
          if (isset($installed_modules[$dependent]) && !isset($module_list[$dependent])) {
            $module_list[$dependent] = $dependent;
          }
        }
      }
    }

    // Use the validators and throw an exception with the reasons.
    if ($reasons = $this->validateUninstall($module_list)) {
      foreach ($reasons as $reason) {
        $reason_message[] = implode(', ', $reason);
      }
      throw new ModuleUninstallValidatorException('The following reasons prevent the modules from being uninstalled: ' . implode('; ', $reason_message));
    }
    // Set the actual module weights.
    $module_list = array_map(function ($module) use ($module_data) {
      return $module_data[$module]->sort;
    }, $module_list);

    // Sort the module list by their weights.
    asort($module_list);
    $module_list = array_keys($module_list);

    // Only process modules that are enabled. A module is only enabled if it is
    // configured as enabled. Custom or overridden module handlers might contain
    // the module already, which means that it might be loaded, but not
    // necessarily installed.
    foreach ($module_list as $module) {

      // Clean up all entity bundles (including fields) of every entity type
      // provided by the module that is being uninstalled.
      // @todo Clean this up in https://www.drupal.org/node/2350111.
      $entity_type_manager = \Drupal::entityTypeManager();
      $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
      foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $entity_type) {
        if ($entity_type->getProvider() == $module) {
          foreach (array_keys($entity_type_bundle_info->getBundleInfo($entity_type_id)) as $bundle) {
            \Drupal::service('entity_bundle.listener')->onBundleDelete($bundle, $entity_type_id);
          }
        }
      }

      // Allow modules to react prior to the uninstallation of a module.
      $this->moduleHandler->invokeAll('module_preuninstall', [$module, $sync_status]);

      // Uninstall the module.
      $this->moduleHandler->loadInclude($module, 'install');
      $this->invoke($module, 'uninstall', [$sync_status]);

      // Remove all configuration belonging to the module.
      \Drupal::service('config.manager')->uninstall('module', $module);

      // In order to make uninstalling transactional if anything uses routes.
      \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
      \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));

      // Notify interested components that this module's entity types are being
      // deleted. For example, a SQL-based storage handler can use this as an
      // opportunity to drop the corresponding database tables.
      // @todo Clean this up in https://www.drupal.org/node/2350111.
      $update_manager = \Drupal::entityDefinitionUpdateManager();
      /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
      $entity_field_manager = \Drupal::service('entity_field.manager');
      foreach ($entity_type_manager->getDefinitions() as $entity_type) {
        if ($entity_type->getProvider() == $module) {
          $update_manager->uninstallEntityType($entity_type);
        }
        elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
          // The module being uninstalled might have added new fields to
          // existing entity types. This will add them to the deleted fields
          // repository so their data will be purged on cron.
          foreach ($entity_field_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
            if ($storage_definition->getProvider() == $module) {
              $update_manager->uninstallFieldStorageDefinition($storage_definition);
            }
          }
        }
      }

      // Remove the schema.
      $this->uninstallSchema($module);

      // Remove the module's entry from the config. Don't check schema when
      // uninstalling a module since we are only clearing a key.
      $core_extension = \Drupal::configFactory()->getEditable('core.extension');
      $core_extension->clear("module.$module");
      // If the install profile is being uninstalled then remove the site's
      // profile key to indicate that the site no longer has an installation
      // profile.
      if ($core_extension->get('profile') === $module) {
        $core_extension->clear('profile');
      }
      $core_extension->save(TRUE);

      // Update the module handler to remove the module.
      // The current ModuleHandler instance is obsolete with the kernel rebuild
      // below.
      $module_filenames = $this->moduleHandler->getModuleList();
      unset($module_filenames[$module]);
      $this->moduleHandler->setModuleList($module_filenames);

      // Remove any potential cache bins provided by the module.
      $this->removeCacheBins($module);

      // Clear the static cache of the "extension.list.module" service to pick
      // up the new module, since it merges the installation status of modules
      // into its statically cached list.
      \Drupal::service('extension.list.module')->reset();

      // Update the kernel to exclude the uninstalled modules.
      $this->updateKernel($module_filenames);

      // Clear plugin manager caches.
      \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();

      // Update the theme registry to remove the newly uninstalled module.
      \Drupal::service('theme.registry')->reset();

      // Modules can alter theme info, so refresh theme data.
      // @todo ThemeHandler cannot be injected into ModuleHandler, since that
      //   causes a circular service dependency.
      // @see https://www.drupal.org/node/2208429
      \Drupal::service('theme_handler')->refreshInfo();

      \Drupal::logger('system')->info('%module module uninstalled.', ['%module' => $module]);

      /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
      $update_registry = \Drupal::service('update.update_hook_registry');
      $update_registry->deleteInstalledVersion($module);
    }
    // Rebuild routes after installing module. This is done here on top of
    // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
    // fastCGI which executes ::destruct() after the Module uninstallation page
    // was sent already.
    \Drupal::service('router.builder')->rebuild();

    // Let other modules react.
    $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list, $sync_status]);

    // Flush all persistent caches.
    // Any cache entry might implicitly depend on the uninstalled modules,
    // so clear all of them explicitly.
    $this->moduleHandler->invokeAll('cache_flush');
    foreach (Cache::getBins() as $cache_backend) {
      $cache_backend->deleteAll();
    }

    return TRUE;
  }

  /**
   * Helper method for removing all cache bins registered by a given module.
   *
   * @param string $module
   *   The name of the module for which to remove all registered cache bins.
   */
  protected function removeCacheBins($module) {
    $service_yaml_file = \Drupal::service('extension.list.module')->getPath($module) . "/$module.services.yml";
    if (!file_exists($service_yaml_file)) {
      return;
    }

    $definitions = Yaml::decode(file_get_contents($service_yaml_file));

    $cache_bin_services = array_filter(
      $definitions['services'] ?? [],
      function ($definition) {
        $tags = $definition['tags'] ?? [];
        foreach ($tags as $tag) {
          if (isset($tag['name']) && ($tag['name'] == 'cache.bin')) {
            return TRUE;
          }
        }
        return FALSE;
      }
    );

    foreach (array_keys($cache_bin_services) as $service_id) {
      $backend = $this->kernel->getContainer()->get($service_id);
      if ($backend instanceof CacheBackendInterface) {
        $backend->removeBin();
      }
    }
  }

  /**
   * Updates the kernel module list.
   *
   * @param \Drupal\Core\Extension\Extension[] $module_filenames
   *   The list of installed modules.
   */
  protected function updateKernel($module_filenames) {
    // Save current state of config installer, so it can be restored after the
    // container is rebuilt.
    /** @var \Drupal\Core\Config\ConfigInstallerInterface $config_installer */
    $config_installer = $this->kernel->getContainer()->get('config.installer');
    $sync_status = $config_installer->isSyncing();
    $source_storage = $config_installer->getSourceStorage();

    if (!empty($module_filenames)) {
      // This reboots the kernel to register the module's bundle and its
      // services in the service container. The $module_filenames argument is
      // taken over as %container.modules% parameter, which is passed to a fresh
      // ModuleHandler instance upon first retrieval.
      $this->kernel->updateModules($module_filenames, $module_filenames);
      $container = $this->kernel->getContainer();
    }
    else {
      $container = $this->kernel->resetContainer();
    }

    // After rebuilding the container we need to update the injected
    // dependencies.
    $this->moduleHandler = $container->get('module_handler');
    $this->connection = $container->get('database');
    $this->updateRegistry = $container->get('update.update_hook_registry');

    // Restore state of config installer.
    if ($sync_status) {
      $container->get('config.installer')
        ->setSyncing(TRUE)
        ->setSourceStorage($source_storage);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateUninstall(array $module_list) {
    $reasons = [];
    foreach ($module_list as $module) {
      foreach ($this->uninstallValidators as $validator) {
        $validation_reasons = $validator->validate($module);
        if (!empty($validation_reasons)) {
          if (!isset($reasons[$module])) {
            $reasons[$module] = [];
          }
          $reasons[$module] = array_merge($reasons[$module], $validation_reasons);
        }
      }
    }
    return $reasons;
  }

  /**
   * Creates all tables defined in a module's hook_schema().
   *
   * @param string $module
   *   The module for which the tables will be created.
   *
   * @internal
   */
  protected function installSchema(string $module): void {
    $tables = $this->invoke($module, 'schema') ?? [];
    $schema = $this->connection->schema();
    foreach ($tables as $name => $table) {
      $schema->createTable($name, $table);
    }
  }

  /**
   * Removes all tables defined in a module's hook_schema().
   *
   * @param string $module
   *   The module for which the tables will be removed.
   *
   * @internal
   */
  protected function uninstallSchema(string $module): void {
    $tables = $this->invoke($module, 'schema') ?? [];
    $schema = $this->connection->schema();
    foreach (array_keys($tables) as $table) {
      if ($schema->tableExists($table)) {
        $schema->dropTable($table);
      }
    }
  }

  /**
   * Call a procedural hook in an installed module during installation.
   *
   * Hooks called during install will remain procedural.
   * - hook_install()
   * - hook_install_tasks()
   * - hook_install_tasks_alter()
   * - hook_post_update_NAME()
   * - hook_schema()
   * - hook_uninstall()
   * - hook_update_last_removed()
   * - hook_update_N()
   *
   * @param string $module
   *   The module (it can be a profile, too).
   * @param string $hook
   *   The name of the hook to invoke.
   * @param array $args
   *   Arguments to pass to the hook.
   *
   * @return mixed
   *   The return value of the procedural hook. Defaults to NULL if a hook
   *   function does not exist.
   */
  protected function invoke(string $module, string $hook, array $args = []): mixed {
    $function = $module . '_' . $hook;
    return function_exists($function) ? $function(... $args) : NULL;
  }

}