annotate core/lib/Drupal/Core/Extension/ModuleInstaller.php @ 14:1fec387a4317

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