annotate core/lib/Drupal/Core/Extension/ModuleInstaller.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Extension;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\Cache;
Chris@0 6 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 7 use Drupal\Core\DrupalKernelInterface;
Chris@0 8 use Drupal\Core\Entity\EntityStorageException;
Chris@0 9 use Drupal\Core\Entity\FieldableEntityInterface;
Chris@0 10 use Drupal\Core\Serialization\Yaml;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Default implementation of the module installer.
Chris@0 14 *
Chris@0 15 * It registers the module in config, installs its own configuration,
Chris@0 16 * installs the schema, updates the Drupal kernel and more.
Chris@17 17 *
Chris@17 18 * We don't inject dependencies yet, as we would need to reload them after
Chris@17 19 * each installation or uninstallation of a module.
Chris@17 20 * https://www.drupal.org/project/drupal/issues/2350111 for example tries to
Chris@17 21 * solve this dilemma.
Chris@0 22 */
Chris@0 23 class ModuleInstaller implements ModuleInstallerInterface {
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The module handler.
Chris@0 27 *
Chris@0 28 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 29 */
Chris@0 30 protected $moduleHandler;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The drupal kernel.
Chris@0 34 *
Chris@0 35 * @var \Drupal\Core\DrupalKernelInterface
Chris@0 36 */
Chris@0 37 protected $kernel;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The app root.
Chris@0 41 *
Chris@0 42 * @var string
Chris@0 43 */
Chris@0 44 protected $root;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * The uninstall validators.
Chris@0 48 *
Chris@0 49 * @var \Drupal\Core\Extension\ModuleUninstallValidatorInterface[]
Chris@0 50 */
Chris@0 51 protected $uninstallValidators;
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Constructs a new ModuleInstaller instance.
Chris@0 55 *
Chris@0 56 * @param string $root
Chris@0 57 * The app root.
Chris@0 58 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 59 * The module handler.
Chris@0 60 * @param \Drupal\Core\DrupalKernelInterface $kernel
Chris@0 61 * The drupal kernel.
Chris@0 62 *
Chris@0 63 * @see \Drupal\Core\DrupalKernel
Chris@0 64 * @see \Drupal\Core\CoreServiceProvider
Chris@0 65 */
Chris@0 66 public function __construct($root, ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel) {
Chris@0 67 $this->root = $root;
Chris@0 68 $this->moduleHandler = $module_handler;
Chris@0 69 $this->kernel = $kernel;
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public function addUninstallValidator(ModuleUninstallValidatorInterface $uninstall_validator) {
Chris@0 76 $this->uninstallValidators[] = $uninstall_validator;
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * {@inheritdoc}
Chris@0 81 */
Chris@0 82 public function install(array $module_list, $enable_dependencies = TRUE) {
Chris@0 83 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
Chris@0 84 if ($enable_dependencies) {
Chris@0 85 // Get all module data so we can find dependencies and sort.
Chris@0 86 $module_data = system_rebuild_module_data();
Chris@0 87 $module_list = $module_list ? array_combine($module_list, $module_list) : [];
Chris@0 88 if ($missing_modules = array_diff_key($module_list, $module_data)) {
Chris@0 89 // One or more of the given modules doesn't exist.
Chris@0 90 throw new MissingDependencyException(sprintf('Unable to install modules %s due to missing modules %s.', implode(', ', $module_list), implode(', ', $missing_modules)));
Chris@0 91 }
Chris@0 92
Chris@0 93 // Only process currently uninstalled modules.
Chris@0 94 $installed_modules = $extension_config->get('module') ?: [];
Chris@0 95 if (!$module_list = array_diff_key($module_list, $installed_modules)) {
Chris@0 96 // Nothing to do. All modules already installed.
Chris@0 97 return TRUE;
Chris@0 98 }
Chris@0 99
Chris@0 100 // Add dependencies to the list. The new modules will be processed as
Chris@14 101 // the foreach loop continues.
Chris@14 102 foreach ($module_list as $module => $value) {
Chris@0 103 foreach (array_keys($module_data[$module]->requires) as $dependency) {
Chris@0 104 if (!isset($module_data[$dependency])) {
Chris@0 105 // The dependency does not exist.
Chris@0 106 throw new MissingDependencyException("Unable to install modules: module '$module' is missing its dependency module $dependency.");
Chris@0 107 }
Chris@0 108
Chris@0 109 // Skip already installed modules.
Chris@0 110 if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) {
Chris@0 111 $module_list[$dependency] = $dependency;
Chris@0 112 }
Chris@0 113 }
Chris@0 114 }
Chris@0 115
Chris@0 116 // Set the actual module weights.
Chris@0 117 $module_list = array_map(function ($module) use ($module_data) {
Chris@0 118 return $module_data[$module]->sort;
Chris@0 119 }, $module_list);
Chris@0 120
Chris@0 121 // Sort the module list by their weights (reverse).
Chris@0 122 arsort($module_list);
Chris@0 123 $module_list = array_keys($module_list);
Chris@0 124 }
Chris@0 125
Chris@0 126 // Required for module installation checks.
Chris@0 127 include_once $this->root . '/core/includes/install.inc';
Chris@0 128
Chris@0 129 /** @var \Drupal\Core\Config\ConfigInstaller $config_installer */
Chris@0 130 $config_installer = \Drupal::service('config.installer');
Chris@0 131 $sync_status = $config_installer->isSyncing();
Chris@0 132 if ($sync_status) {
Chris@0 133 $source_storage = $config_installer->getSourceStorage();
Chris@0 134 }
Chris@0 135 $modules_installed = [];
Chris@0 136 foreach ($module_list as $module) {
Chris@0 137 $enabled = $extension_config->get("module.$module") !== NULL;
Chris@0 138 if (!$enabled) {
Chris@0 139 // Throw an exception if the module name is too long.
Chris@0 140 if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
Chris@0 141 throw new ExtensionNameLengthException("Module name '$module' is over the maximum allowed length of " . DRUPAL_EXTENSION_NAME_MAX_LENGTH . ' characters');
Chris@0 142 }
Chris@0 143
Chris@0 144 // Load a new config object for each iteration, otherwise changes made
Chris@0 145 // in hook_install() are not reflected in $extension_config.
Chris@0 146 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
Chris@0 147
Chris@0 148 // Check the validity of the default configuration. This will throw
Chris@0 149 // exceptions if the configuration is not valid.
Chris@0 150 $config_installer->checkConfigurationToInstall('module', $module);
Chris@0 151
Chris@0 152 // Save this data without checking schema. This is a performance
Chris@0 153 // improvement for module installation.
Chris@0 154 $extension_config
Chris@0 155 ->set("module.$module", 0)
Chris@0 156 ->set('module', module_config_sort($extension_config->get('module')))
Chris@0 157 ->save(TRUE);
Chris@0 158
Chris@0 159 // Prepare the new module list, sorted by weight, including filenames.
Chris@0 160 // This list is used for both the ModuleHandler and DrupalKernel. It
Chris@0 161 // needs to be kept in sync between both. A DrupalKernel reboot or
Chris@0 162 // rebuild will automatically re-instantiate a new ModuleHandler that
Chris@0 163 // uses the new module list of the kernel. However, DrupalKernel does
Chris@0 164 // not cause any modules to be loaded.
Chris@0 165 // Furthermore, the currently active (fixed) module list can be
Chris@0 166 // different from the configured list of enabled modules. For all active
Chris@0 167 // modules not contained in the configured enabled modules, we assume a
Chris@0 168 // weight of 0.
Chris@0 169 $current_module_filenames = $this->moduleHandler->getModuleList();
Chris@0 170 $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
Chris@0 171 $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
Chris@0 172 $module_filenames = [];
Chris@0 173 foreach ($current_modules as $name => $weight) {
Chris@0 174 if (isset($current_module_filenames[$name])) {
Chris@0 175 $module_filenames[$name] = $current_module_filenames[$name];
Chris@0 176 }
Chris@0 177 else {
Chris@17 178 $module_path = \Drupal::service('extension.list.module')->getPath($name);
Chris@0 179 $pathname = "$module_path/$name.info.yml";
Chris@0 180 $filename = file_exists($module_path . "/$name.module") ? "$name.module" : NULL;
Chris@0 181 $module_filenames[$name] = new Extension($this->root, 'module', $pathname, $filename);
Chris@0 182 }
Chris@0 183 }
Chris@0 184
Chris@0 185 // Update the module handler in order to load the module's code.
Chris@0 186 // This allows the module to participate in hooks and its existence to
Chris@0 187 // be discovered by other modules.
Chris@0 188 // The current ModuleHandler instance is obsolete with the kernel
Chris@0 189 // rebuild below.
Chris@0 190 $this->moduleHandler->setModuleList($module_filenames);
Chris@0 191 $this->moduleHandler->load($module);
Chris@0 192 module_load_install($module);
Chris@0 193
Chris@17 194 // Clear the static cache of the "extension.list.module" service to pick
Chris@17 195 // up the new module, since it merges the installation status of modules
Chris@17 196 // into its statically cached list.
Chris@17 197 \Drupal::service('extension.list.module')->reset();
Chris@0 198
Chris@0 199 // Update the kernel to include it.
Chris@0 200 $this->updateKernel($module_filenames);
Chris@0 201
Chris@12 202 // Replace the route provider service with a version that will rebuild
Chris@12 203 // if routes used during installation. This ensures that a module's
Chris@12 204 // routes are available during installation. This has to occur before
Chris@12 205 // any services that depend on it are instantiated otherwise those
Chris@12 206 // services will have the old route provider injected. Note that, since
Chris@12 207 // the container is rebuilt by updating the kernel, the route provider
Chris@12 208 // service is the regular one even though we are in a loop and might
Chris@12 209 // have replaced it before.
Chris@12 210 \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
Chris@12 211 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
Chris@12 212
Chris@0 213 // Allow modules to react prior to the installation of a module.
Chris@0 214 $this->moduleHandler->invokeAll('module_preinstall', [$module]);
Chris@0 215
Chris@0 216 // Now install the module's schema if necessary.
Chris@0 217 drupal_install_schema($module);
Chris@0 218
Chris@0 219 // Clear plugin manager caches.
Chris@0 220 \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
Chris@0 221
Chris@0 222 // Set the schema version to the number of the last update provided by
Chris@0 223 // the module, or the minimum core schema version.
Chris@0 224 $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
Chris@0 225 $versions = drupal_get_schema_versions($module);
Chris@0 226 if ($versions) {
Chris@0 227 $version = max(max($versions), $version);
Chris@0 228 }
Chris@0 229
Chris@0 230 // Notify interested components that this module's entity types and
Chris@0 231 // field storage definitions are new. For example, a SQL-based storage
Chris@0 232 // handler can use this as an opportunity to create the necessary
Chris@0 233 // database tables.
Chris@0 234 // @todo Clean this up in https://www.drupal.org/node/2350111.
Chris@0 235 $entity_manager = \Drupal::entityManager();
Chris@0 236 $update_manager = \Drupal::entityDefinitionUpdateManager();
Chris@0 237 foreach ($entity_manager->getDefinitions() as $entity_type) {
Chris@0 238 if ($entity_type->getProvider() == $module) {
Chris@0 239 $update_manager->installEntityType($entity_type);
Chris@0 240 }
Chris@0 241 elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
Chris@0 242 // The module being installed may be adding new fields to existing
Chris@0 243 // entity types. Field definitions for any entity type defined by
Chris@0 244 // the module are handled in the if branch.
Chris@0 245 foreach ($entity_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
Chris@0 246 if ($storage_definition->getProvider() == $module) {
Chris@0 247 // If the module being installed is also defining a storage key
Chris@0 248 // for the entity type, the entity schema may not exist yet. It
Chris@0 249 // will be created later in that case.
Chris@0 250 try {
Chris@0 251 $update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition);
Chris@0 252 }
Chris@0 253 catch (EntityStorageException $e) {
Chris@0 254 watchdog_exception('system', $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()]);
Chris@0 255 }
Chris@0 256 }
Chris@0 257 }
Chris@0 258 }
Chris@0 259 }
Chris@0 260
Chris@0 261 // Install default configuration of the module.
Chris@0 262 $config_installer = \Drupal::service('config.installer');
Chris@0 263 if ($sync_status) {
Chris@0 264 $config_installer
Chris@0 265 ->setSyncing(TRUE)
Chris@0 266 ->setSourceStorage($source_storage);
Chris@0 267 }
Chris@0 268 \Drupal::service('config.installer')->installDefaultConfig('module', $module);
Chris@0 269
Chris@0 270 // If the module has no current updates, but has some that were
Chris@0 271 // previously removed, set the version to the value of
Chris@0 272 // hook_update_last_removed().
Chris@0 273 if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) {
Chris@0 274 $version = max($version, $last_removed);
Chris@0 275 }
Chris@0 276 drupal_set_installed_schema_version($module, $version);
Chris@0 277
Chris@0 278 // Ensure that all post_update functions are registered already.
Chris@0 279 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
Chris@0 280 $post_update_registry = \Drupal::service('update.post_update_registry');
Chris@0 281 $post_update_registry->registerInvokedUpdates($post_update_registry->getModuleUpdateFunctions($module));
Chris@0 282
Chris@0 283 // Record the fact that it was installed.
Chris@0 284 $modules_installed[] = $module;
Chris@0 285
Chris@0 286 // Drupal's stream wrappers needs to be re-registered in case a
Chris@0 287 // module-provided stream wrapper is used later in the same request. In
Chris@0 288 // particular, this happens when installing Drupal via Drush, as the
Chris@0 289 // 'translations' stream wrapper is provided by Interface Translation
Chris@0 290 // module and is later used to import translations.
Chris@0 291 \Drupal::service('stream_wrapper_manager')->register();
Chris@0 292
Chris@0 293 // Update the theme registry to include it.
Chris@0 294 drupal_theme_rebuild();
Chris@0 295
Chris@0 296 // Modules can alter theme info, so refresh theme data.
Chris@0 297 // @todo ThemeHandler cannot be injected into ModuleHandler, since that
Chris@0 298 // causes a circular service dependency.
Chris@0 299 // @see https://www.drupal.org/node/2208429
Chris@0 300 \Drupal::service('theme_handler')->refreshInfo();
Chris@0 301
Chris@0 302 // Allow the module to perform install tasks.
Chris@0 303 $this->moduleHandler->invoke($module, 'install');
Chris@0 304
Chris@0 305 // Record the fact that it was installed.
Chris@0 306 \Drupal::logger('system')->info('%module module installed.', ['%module' => $module]);
Chris@0 307 }
Chris@0 308 }
Chris@0 309
Chris@0 310 // If any modules were newly installed, invoke hook_modules_installed().
Chris@0 311 if (!empty($modules_installed)) {
Chris@14 312 // If the container was rebuilt during hook_install() it might not have
Chris@14 313 // the 'router.route_provider.old' service.
Chris@14 314 if (\Drupal::hasService('router.route_provider.old')) {
Chris@14 315 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.old'));
Chris@14 316 }
Chris@0 317 if (!\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) {
Chris@0 318 // Rebuild routes after installing module. This is done here on top of
Chris@0 319 // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
Chris@0 320 // fastCGI which executes ::destruct() after the module installation
Chris@0 321 // page was sent already.
Chris@0 322 \Drupal::service('router.builder')->rebuild();
Chris@0 323 }
Chris@0 324
Chris@0 325 $this->moduleHandler->invokeAll('modules_installed', [$modules_installed]);
Chris@0 326 }
Chris@0 327
Chris@0 328 return TRUE;
Chris@0 329 }
Chris@0 330
Chris@0 331 /**
Chris@0 332 * {@inheritdoc}
Chris@0 333 */
Chris@0 334 public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
Chris@0 335 // Get all module data so we can find dependencies and sort.
Chris@0 336 $module_data = system_rebuild_module_data();
Chris@0 337 $module_list = $module_list ? array_combine($module_list, $module_list) : [];
Chris@0 338 if (array_diff_key($module_list, $module_data)) {
Chris@0 339 // One or more of the given modules doesn't exist.
Chris@0 340 return FALSE;
Chris@0 341 }
Chris@0 342
Chris@0 343 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
Chris@0 344 $installed_modules = $extension_config->get('module') ?: [];
Chris@0 345 if (!$module_list = array_intersect_key($module_list, $installed_modules)) {
Chris@0 346 // Nothing to do. All modules already uninstalled.
Chris@0 347 return TRUE;
Chris@0 348 }
Chris@0 349
Chris@0 350 if ($uninstall_dependents) {
Chris@0 351 // Add dependent modules to the list. The new modules will be processed as
Chris@14 352 // the foreach loop continues.
Chris@14 353 foreach ($module_list as $module => $value) {
Chris@0 354 foreach (array_keys($module_data[$module]->required_by) as $dependent) {
Chris@0 355 if (!isset($module_data[$dependent])) {
Chris@0 356 // The dependent module does not exist.
Chris@0 357 return FALSE;
Chris@0 358 }
Chris@0 359
Chris@0 360 // Skip already uninstalled modules.
Chris@17 361 if (isset($installed_modules[$dependent]) && !isset($module_list[$dependent])) {
Chris@0 362 $module_list[$dependent] = $dependent;
Chris@0 363 }
Chris@0 364 }
Chris@0 365 }
Chris@0 366 }
Chris@0 367
Chris@0 368 // Use the validators and throw an exception with the reasons.
Chris@0 369 if ($reasons = $this->validateUninstall($module_list)) {
Chris@0 370 foreach ($reasons as $reason) {
Chris@0 371 $reason_message[] = implode(', ', $reason);
Chris@0 372 }
Chris@0 373 throw new ModuleUninstallValidatorException('The following reasons prevent the modules from being uninstalled: ' . implode('; ', $reason_message));
Chris@0 374 }
Chris@0 375 // Set the actual module weights.
Chris@0 376 $module_list = array_map(function ($module) use ($module_data) {
Chris@0 377 return $module_data[$module]->sort;
Chris@0 378 }, $module_list);
Chris@0 379
Chris@0 380 // Sort the module list by their weights.
Chris@0 381 asort($module_list);
Chris@0 382 $module_list = array_keys($module_list);
Chris@0 383
Chris@0 384 // Only process modules that are enabled. A module is only enabled if it is
Chris@0 385 // configured as enabled. Custom or overridden module handlers might contain
Chris@0 386 // the module already, which means that it might be loaded, but not
Chris@0 387 // necessarily installed.
Chris@0 388 foreach ($module_list as $module) {
Chris@0 389
Chris@0 390 // Clean up all entity bundles (including fields) of every entity type
Chris@0 391 // provided by the module that is being uninstalled.
Chris@0 392 // @todo Clean this up in https://www.drupal.org/node/2350111.
Chris@0 393 $entity_manager = \Drupal::entityManager();
Chris@18 394 $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
Chris@0 395 foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
Chris@0 396 if ($entity_type->getProvider() == $module) {
Chris@18 397 foreach (array_keys($entity_type_bundle_info->getBundleInfo($entity_type_id)) as $bundle) {
Chris@18 398 \Drupal::service('entity_bundle.listener')->onBundleDelete($bundle, $entity_type_id);
Chris@0 399 }
Chris@0 400 }
Chris@0 401 }
Chris@0 402
Chris@0 403 // Allow modules to react prior to the uninstallation of a module.
Chris@0 404 $this->moduleHandler->invokeAll('module_preuninstall', [$module]);
Chris@0 405
Chris@0 406 // Uninstall the module.
Chris@0 407 module_load_install($module);
Chris@0 408 $this->moduleHandler->invoke($module, 'uninstall');
Chris@0 409
Chris@0 410 // Remove all configuration belonging to the module.
Chris@0 411 \Drupal::service('config.manager')->uninstall('module', $module);
Chris@0 412
Chris@0 413 // In order to make uninstalling transactional if anything uses routes.
Chris@0 414 \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
Chris@0 415 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
Chris@0 416
Chris@0 417 // Notify interested components that this module's entity types are being
Chris@0 418 // deleted. For example, a SQL-based storage handler can use this as an
Chris@0 419 // opportunity to drop the corresponding database tables.
Chris@0 420 // @todo Clean this up in https://www.drupal.org/node/2350111.
Chris@0 421 $update_manager = \Drupal::entityDefinitionUpdateManager();
Chris@0 422 foreach ($entity_manager->getDefinitions() as $entity_type) {
Chris@0 423 if ($entity_type->getProvider() == $module) {
Chris@0 424 $update_manager->uninstallEntityType($entity_type);
Chris@0 425 }
Chris@0 426 elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
Chris@14 427 // The module being uninstalled might have added new fields to
Chris@14 428 // existing entity types. This will add them to the deleted fields
Chris@14 429 // repository so their data will be purged on cron.
Chris@14 430 foreach ($entity_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
Chris@14 431 if ($storage_definition->getProvider() == $module) {
Chris@0 432 $update_manager->uninstallFieldStorageDefinition($storage_definition);
Chris@0 433 }
Chris@0 434 }
Chris@0 435 }
Chris@0 436 }
Chris@0 437
Chris@0 438 // Remove the schema.
Chris@0 439 drupal_uninstall_schema($module);
Chris@0 440
Chris@0 441 // Remove the module's entry from the config. Don't check schema when
Chris@0 442 // uninstalling a module since we are only clearing a key.
Chris@0 443 \Drupal::configFactory()->getEditable('core.extension')->clear("module.$module")->save(TRUE);
Chris@0 444
Chris@0 445 // Update the module handler to remove the module.
Chris@0 446 // The current ModuleHandler instance is obsolete with the kernel rebuild
Chris@0 447 // below.
Chris@0 448 $module_filenames = $this->moduleHandler->getModuleList();
Chris@0 449 unset($module_filenames[$module]);
Chris@0 450 $this->moduleHandler->setModuleList($module_filenames);
Chris@0 451
Chris@0 452 // Remove any potential cache bins provided by the module.
Chris@0 453 $this->removeCacheBins($module);
Chris@0 454
Chris@17 455 // Clear the static cache of the "extension.list.module" service to pick
Chris@17 456 // up the new module, since it merges the installation status of modules
Chris@17 457 // into its statically cached list.
Chris@17 458 \Drupal::service('extension.list.module')->reset();
Chris@0 459
Chris@0 460 // Clear plugin manager caches.
Chris@0 461 \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
Chris@0 462
Chris@0 463 // Update the kernel to exclude the uninstalled modules.
Chris@0 464 $this->updateKernel($module_filenames);
Chris@0 465
Chris@0 466 // Update the theme registry to remove the newly uninstalled module.
Chris@0 467 drupal_theme_rebuild();
Chris@0 468
Chris@0 469 // Modules can alter theme info, so refresh theme data.
Chris@0 470 // @todo ThemeHandler cannot be injected into ModuleHandler, since that
Chris@0 471 // causes a circular service dependency.
Chris@0 472 // @see https://www.drupal.org/node/2208429
Chris@0 473 \Drupal::service('theme_handler')->refreshInfo();
Chris@0 474
Chris@0 475 \Drupal::logger('system')->info('%module module uninstalled.', ['%module' => $module]);
Chris@0 476
Chris@0 477 $schema_store = \Drupal::keyValue('system.schema');
Chris@0 478 $schema_store->delete($module);
Chris@0 479
Chris@0 480 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
Chris@0 481 $post_update_registry = \Drupal::service('update.post_update_registry');
Chris@0 482 $post_update_registry->filterOutInvokedUpdatesByModule($module);
Chris@0 483 }
Chris@0 484 // Rebuild routes after installing module. This is done here on top of
Chris@0 485 // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
Chris@0 486 // fastCGI which executes ::destruct() after the Module uninstallation page
Chris@0 487 // was sent already.
Chris@0 488 \Drupal::service('router.builder')->rebuild();
Chris@0 489 drupal_get_installed_schema_version(NULL, TRUE);
Chris@0 490
Chris@0 491 // Let other modules react.
Chris@0 492 $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list]);
Chris@0 493
Chris@0 494 // Flush all persistent caches.
Chris@0 495 // Any cache entry might implicitly depend on the uninstalled modules,
Chris@0 496 // so clear all of them explicitly.
Chris@0 497 $this->moduleHandler->invokeAll('cache_flush');
Chris@0 498 foreach (Cache::getBins() as $service_id => $cache_backend) {
Chris@0 499 $cache_backend->deleteAll();
Chris@0 500 }
Chris@0 501
Chris@0 502 return TRUE;
Chris@0 503 }
Chris@0 504
Chris@0 505 /**
Chris@0 506 * Helper method for removing all cache bins registered by a given module.
Chris@0 507 *
Chris@0 508 * @param string $module
Chris@0 509 * The name of the module for which to remove all registered cache bins.
Chris@0 510 */
Chris@0 511 protected function removeCacheBins($module) {
Chris@0 512 $service_yaml_file = drupal_get_path('module', $module) . "/$module.services.yml";
Chris@12 513 if (!file_exists($service_yaml_file)) {
Chris@12 514 return;
Chris@12 515 }
Chris@12 516
Chris@12 517 $definitions = Yaml::decode(file_get_contents($service_yaml_file));
Chris@12 518
Chris@12 519 $cache_bin_services = array_filter(
Chris@12 520 isset($definitions['services']) ? $definitions['services'] : [],
Chris@12 521 function ($definition) {
Chris@12 522 $tags = isset($definition['tags']) ? $definition['tags'] : [];
Chris@12 523 foreach ($tags as $tag) {
Chris@12 524 if (isset($tag['name']) && ($tag['name'] == 'cache.bin')) {
Chris@12 525 return TRUE;
Chris@0 526 }
Chris@0 527 }
Chris@12 528 return FALSE;
Chris@12 529 }
Chris@12 530 );
Chris@12 531
Chris@12 532 foreach (array_keys($cache_bin_services) as $service_id) {
Chris@12 533 $backend = $this->kernel->getContainer()->get($service_id);
Chris@12 534 if ($backend instanceof CacheBackendInterface) {
Chris@12 535 $backend->removeBin();
Chris@0 536 }
Chris@0 537 }
Chris@0 538 }
Chris@0 539
Chris@0 540 /**
Chris@0 541 * Updates the kernel module list.
Chris@0 542 *
Chris@0 543 * @param string $module_filenames
Chris@0 544 * The list of installed modules.
Chris@0 545 */
Chris@0 546 protected function updateKernel($module_filenames) {
Chris@0 547 // This reboots the kernel to register the module's bundle and its services
Chris@0 548 // in the service container. The $module_filenames argument is taken over as
Chris@0 549 // %container.modules% parameter, which is passed to a fresh ModuleHandler
Chris@0 550 // instance upon first retrieval.
Chris@0 551 $this->kernel->updateModules($module_filenames, $module_filenames);
Chris@0 552 // After rebuilding the container we need to update the injected
Chris@0 553 // dependencies.
Chris@0 554 $container = $this->kernel->getContainer();
Chris@0 555 $this->moduleHandler = $container->get('module_handler');
Chris@0 556 }
Chris@0 557
Chris@0 558 /**
Chris@0 559 * {@inheritdoc}
Chris@0 560 */
Chris@0 561 public function validateUninstall(array $module_list) {
Chris@0 562 $reasons = [];
Chris@0 563 foreach ($module_list as $module) {
Chris@0 564 foreach ($this->uninstallValidators as $validator) {
Chris@0 565 $validation_reasons = $validator->validate($module);
Chris@0 566 if (!empty($validation_reasons)) {
Chris@0 567 if (!isset($reasons[$module])) {
Chris@0 568 $reasons[$module] = [];
Chris@0 569 }
Chris@0 570 $reasons[$module] = array_merge($reasons[$module], $validation_reasons);
Chris@0 571 }
Chris@0 572 }
Chris@0 573 }
Chris@0 574 return $reasons;
Chris@0 575 }
Chris@0 576
Chris@0 577 }