annotate core/lib/Drupal/Core/Extension/ModuleInstaller.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
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@0 96 // the while loop continues.
Chris@0 97 while (list($module) = each($module_list)) {
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@0 197 // Allow modules to react prior to the installation of a module.
Chris@0 198 $this->moduleHandler->invokeAll('module_preinstall', [$module]);
Chris@0 199
Chris@0 200 // Now install the module's schema if necessary.
Chris@0 201 drupal_install_schema($module);
Chris@0 202
Chris@0 203 // Clear plugin manager caches.
Chris@0 204 \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
Chris@0 205
Chris@0 206 // Set the schema version to the number of the last update provided by
Chris@0 207 // the module, or the minimum core schema version.
Chris@0 208 $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
Chris@0 209 $versions = drupal_get_schema_versions($module);
Chris@0 210 if ($versions) {
Chris@0 211 $version = max(max($versions), $version);
Chris@0 212 }
Chris@0 213
Chris@0 214 // Notify interested components that this module's entity types and
Chris@0 215 // field storage definitions are new. For example, a SQL-based storage
Chris@0 216 // handler can use this as an opportunity to create the necessary
Chris@0 217 // database tables.
Chris@0 218 // @todo Clean this up in https://www.drupal.org/node/2350111.
Chris@0 219 $entity_manager = \Drupal::entityManager();
Chris@0 220 $update_manager = \Drupal::entityDefinitionUpdateManager();
Chris@0 221 foreach ($entity_manager->getDefinitions() as $entity_type) {
Chris@0 222 if ($entity_type->getProvider() == $module) {
Chris@0 223 $update_manager->installEntityType($entity_type);
Chris@0 224 }
Chris@0 225 elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
Chris@0 226 // The module being installed may be adding new fields to existing
Chris@0 227 // entity types. Field definitions for any entity type defined by
Chris@0 228 // the module are handled in the if branch.
Chris@0 229 foreach ($entity_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
Chris@0 230 if ($storage_definition->getProvider() == $module) {
Chris@0 231 // If the module being installed is also defining a storage key
Chris@0 232 // for the entity type, the entity schema may not exist yet. It
Chris@0 233 // will be created later in that case.
Chris@0 234 try {
Chris@0 235 $update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition);
Chris@0 236 }
Chris@0 237 catch (EntityStorageException $e) {
Chris@0 238 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 239 }
Chris@0 240 }
Chris@0 241 }
Chris@0 242 }
Chris@0 243 }
Chris@0 244
Chris@0 245 // Install default configuration of the module.
Chris@0 246 $config_installer = \Drupal::service('config.installer');
Chris@0 247 if ($sync_status) {
Chris@0 248 $config_installer
Chris@0 249 ->setSyncing(TRUE)
Chris@0 250 ->setSourceStorage($source_storage);
Chris@0 251 }
Chris@0 252 \Drupal::service('config.installer')->installDefaultConfig('module', $module);
Chris@0 253
Chris@0 254 // If the module has no current updates, but has some that were
Chris@0 255 // previously removed, set the version to the value of
Chris@0 256 // hook_update_last_removed().
Chris@0 257 if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) {
Chris@0 258 $version = max($version, $last_removed);
Chris@0 259 }
Chris@0 260 drupal_set_installed_schema_version($module, $version);
Chris@0 261
Chris@0 262 // Ensure that all post_update functions are registered already.
Chris@0 263 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
Chris@0 264 $post_update_registry = \Drupal::service('update.post_update_registry');
Chris@0 265 $post_update_registry->registerInvokedUpdates($post_update_registry->getModuleUpdateFunctions($module));
Chris@0 266
Chris@0 267 // Record the fact that it was installed.
Chris@0 268 $modules_installed[] = $module;
Chris@0 269
Chris@0 270 // Drupal's stream wrappers needs to be re-registered in case a
Chris@0 271 // module-provided stream wrapper is used later in the same request. In
Chris@0 272 // particular, this happens when installing Drupal via Drush, as the
Chris@0 273 // 'translations' stream wrapper is provided by Interface Translation
Chris@0 274 // module and is later used to import translations.
Chris@0 275 \Drupal::service('stream_wrapper_manager')->register();
Chris@0 276
Chris@0 277 // Update the theme registry to include it.
Chris@0 278 drupal_theme_rebuild();
Chris@0 279
Chris@0 280 // Modules can alter theme info, so refresh theme data.
Chris@0 281 // @todo ThemeHandler cannot be injected into ModuleHandler, since that
Chris@0 282 // causes a circular service dependency.
Chris@0 283 // @see https://www.drupal.org/node/2208429
Chris@0 284 \Drupal::service('theme_handler')->refreshInfo();
Chris@0 285
Chris@0 286 // In order to make uninstalling transactional if anything uses routes.
Chris@0 287 \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
Chris@0 288 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
Chris@0 289
Chris@0 290 // Allow the module to perform install tasks.
Chris@0 291 $this->moduleHandler->invoke($module, 'install');
Chris@0 292
Chris@0 293 // Record the fact that it was installed.
Chris@0 294 \Drupal::logger('system')->info('%module module installed.', ['%module' => $module]);
Chris@0 295 }
Chris@0 296 }
Chris@0 297
Chris@0 298 // If any modules were newly installed, invoke hook_modules_installed().
Chris@0 299 if (!empty($modules_installed)) {
Chris@0 300 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.old'));
Chris@0 301 if (!\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) {
Chris@0 302 // Rebuild routes after installing module. This is done here on top of
Chris@0 303 // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
Chris@0 304 // fastCGI which executes ::destruct() after the module installation
Chris@0 305 // page was sent already.
Chris@0 306 \Drupal::service('router.builder')->rebuild();
Chris@0 307 }
Chris@0 308
Chris@0 309 $this->moduleHandler->invokeAll('modules_installed', [$modules_installed]);
Chris@0 310 }
Chris@0 311
Chris@0 312 return TRUE;
Chris@0 313 }
Chris@0 314
Chris@0 315 /**
Chris@0 316 * {@inheritdoc}
Chris@0 317 */
Chris@0 318 public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
Chris@0 319 // Get all module data so we can find dependencies and sort.
Chris@0 320 $module_data = system_rebuild_module_data();
Chris@0 321 $module_list = $module_list ? array_combine($module_list, $module_list) : [];
Chris@0 322 if (array_diff_key($module_list, $module_data)) {
Chris@0 323 // One or more of the given modules doesn't exist.
Chris@0 324 return FALSE;
Chris@0 325 }
Chris@0 326
Chris@0 327 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
Chris@0 328 $installed_modules = $extension_config->get('module') ?: [];
Chris@0 329 if (!$module_list = array_intersect_key($module_list, $installed_modules)) {
Chris@0 330 // Nothing to do. All modules already uninstalled.
Chris@0 331 return TRUE;
Chris@0 332 }
Chris@0 333
Chris@0 334 if ($uninstall_dependents) {
Chris@0 335 // Add dependent modules to the list. The new modules will be processed as
Chris@0 336 // the while loop continues.
Chris@0 337 $profile = drupal_get_profile();
Chris@0 338 while (list($module) = each($module_list)) {
Chris@0 339 foreach (array_keys($module_data[$module]->required_by) as $dependent) {
Chris@0 340 if (!isset($module_data[$dependent])) {
Chris@0 341 // The dependent module does not exist.
Chris@0 342 return FALSE;
Chris@0 343 }
Chris@0 344
Chris@0 345 // Skip already uninstalled modules.
Chris@0 346 if (isset($installed_modules[$dependent]) && !isset($module_list[$dependent]) && $dependent != $profile) {
Chris@0 347 $module_list[$dependent] = $dependent;
Chris@0 348 }
Chris@0 349 }
Chris@0 350 }
Chris@0 351 }
Chris@0 352
Chris@0 353 // Use the validators and throw an exception with the reasons.
Chris@0 354 if ($reasons = $this->validateUninstall($module_list)) {
Chris@0 355 foreach ($reasons as $reason) {
Chris@0 356 $reason_message[] = implode(', ', $reason);
Chris@0 357 }
Chris@0 358 throw new ModuleUninstallValidatorException('The following reasons prevent the modules from being uninstalled: ' . implode('; ', $reason_message));
Chris@0 359 }
Chris@0 360 // Set the actual module weights.
Chris@0 361 $module_list = array_map(function ($module) use ($module_data) {
Chris@0 362 return $module_data[$module]->sort;
Chris@0 363 }, $module_list);
Chris@0 364
Chris@0 365 // Sort the module list by their weights.
Chris@0 366 asort($module_list);
Chris@0 367 $module_list = array_keys($module_list);
Chris@0 368
Chris@0 369 // Only process modules that are enabled. A module is only enabled if it is
Chris@0 370 // configured as enabled. Custom or overridden module handlers might contain
Chris@0 371 // the module already, which means that it might be loaded, but not
Chris@0 372 // necessarily installed.
Chris@0 373 foreach ($module_list as $module) {
Chris@0 374
Chris@0 375 // Clean up all entity bundles (including fields) of every entity type
Chris@0 376 // provided by the module that is being uninstalled.
Chris@0 377 // @todo Clean this up in https://www.drupal.org/node/2350111.
Chris@0 378 $entity_manager = \Drupal::entityManager();
Chris@0 379 foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
Chris@0 380 if ($entity_type->getProvider() == $module) {
Chris@0 381 foreach (array_keys($entity_manager->getBundleInfo($entity_type_id)) as $bundle) {
Chris@0 382 $entity_manager->onBundleDelete($bundle, $entity_type_id);
Chris@0 383 }
Chris@0 384 }
Chris@0 385 }
Chris@0 386
Chris@0 387 // Allow modules to react prior to the uninstallation of a module.
Chris@0 388 $this->moduleHandler->invokeAll('module_preuninstall', [$module]);
Chris@0 389
Chris@0 390 // Uninstall the module.
Chris@0 391 module_load_install($module);
Chris@0 392 $this->moduleHandler->invoke($module, 'uninstall');
Chris@0 393
Chris@0 394 // Remove all configuration belonging to the module.
Chris@0 395 \Drupal::service('config.manager')->uninstall('module', $module);
Chris@0 396
Chris@0 397 // In order to make uninstalling transactional if anything uses routes.
Chris@0 398 \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
Chris@0 399 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
Chris@0 400
Chris@0 401 // Notify interested components that this module's entity types are being
Chris@0 402 // deleted. For example, a SQL-based storage handler can use this as an
Chris@0 403 // opportunity to drop the corresponding database tables.
Chris@0 404 // @todo Clean this up in https://www.drupal.org/node/2350111.
Chris@0 405 $update_manager = \Drupal::entityDefinitionUpdateManager();
Chris@0 406 foreach ($entity_manager->getDefinitions() as $entity_type) {
Chris@0 407 if ($entity_type->getProvider() == $module) {
Chris@0 408 $update_manager->uninstallEntityType($entity_type);
Chris@0 409 }
Chris@0 410 elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
Chris@0 411 // The module being installed may be adding new fields to existing
Chris@0 412 // entity types. Field definitions for any entity type defined by
Chris@0 413 // the module are handled in the if branch.
Chris@0 414 $entity_type_id = $entity_type->id();
Chris@0 415 /** @var \Drupal\Core\Entity\FieldableEntityStorageInterface $storage */
Chris@0 416 $storage = $entity_manager->getStorage($entity_type_id);
Chris@0 417 foreach ($entity_manager->getFieldStorageDefinitions($entity_type_id) as $storage_definition) {
Chris@0 418 // @todo We need to trigger field purging here.
Chris@0 419 // See https://www.drupal.org/node/2282119.
Chris@0 420 if ($storage_definition->getProvider() == $module && !$storage->countFieldData($storage_definition, TRUE)) {
Chris@0 421 $update_manager->uninstallFieldStorageDefinition($storage_definition);
Chris@0 422 }
Chris@0 423 }
Chris@0 424 }
Chris@0 425 }
Chris@0 426
Chris@0 427 // Remove the schema.
Chris@0 428 drupal_uninstall_schema($module);
Chris@0 429
Chris@0 430 // Remove the module's entry from the config. Don't check schema when
Chris@0 431 // uninstalling a module since we are only clearing a key.
Chris@0 432 \Drupal::configFactory()->getEditable('core.extension')->clear("module.$module")->save(TRUE);
Chris@0 433
Chris@0 434 // Update the module handler to remove the module.
Chris@0 435 // The current ModuleHandler instance is obsolete with the kernel rebuild
Chris@0 436 // below.
Chris@0 437 $module_filenames = $this->moduleHandler->getModuleList();
Chris@0 438 unset($module_filenames[$module]);
Chris@0 439 $this->moduleHandler->setModuleList($module_filenames);
Chris@0 440
Chris@0 441 // Remove any potential cache bins provided by the module.
Chris@0 442 $this->removeCacheBins($module);
Chris@0 443
Chris@0 444 // Clear the static cache of system_rebuild_module_data() to pick up the
Chris@0 445 // new module, since it merges the installation status of modules into
Chris@0 446 // its statically cached list.
Chris@0 447 drupal_static_reset('system_rebuild_module_data');
Chris@0 448
Chris@0 449 // Clear plugin manager caches.
Chris@0 450 \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
Chris@0 451
Chris@0 452 // Update the kernel to exclude the uninstalled modules.
Chris@0 453 $this->updateKernel($module_filenames);
Chris@0 454
Chris@0 455 // Update the theme registry to remove the newly uninstalled module.
Chris@0 456 drupal_theme_rebuild();
Chris@0 457
Chris@0 458 // Modules can alter theme info, so refresh theme data.
Chris@0 459 // @todo ThemeHandler cannot be injected into ModuleHandler, since that
Chris@0 460 // causes a circular service dependency.
Chris@0 461 // @see https://www.drupal.org/node/2208429
Chris@0 462 \Drupal::service('theme_handler')->refreshInfo();
Chris@0 463
Chris@0 464 \Drupal::logger('system')->info('%module module uninstalled.', ['%module' => $module]);
Chris@0 465
Chris@0 466 $schema_store = \Drupal::keyValue('system.schema');
Chris@0 467 $schema_store->delete($module);
Chris@0 468
Chris@0 469 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
Chris@0 470 $post_update_registry = \Drupal::service('update.post_update_registry');
Chris@0 471 $post_update_registry->filterOutInvokedUpdatesByModule($module);
Chris@0 472 }
Chris@0 473 // Rebuild routes after installing module. This is done here on top of
Chris@0 474 // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
Chris@0 475 // fastCGI which executes ::destruct() after the Module uninstallation page
Chris@0 476 // was sent already.
Chris@0 477 \Drupal::service('router.builder')->rebuild();
Chris@0 478 drupal_get_installed_schema_version(NULL, TRUE);
Chris@0 479
Chris@0 480 // Let other modules react.
Chris@0 481 $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list]);
Chris@0 482
Chris@0 483 // Flush all persistent caches.
Chris@0 484 // Any cache entry might implicitly depend on the uninstalled modules,
Chris@0 485 // so clear all of them explicitly.
Chris@0 486 $this->moduleHandler->invokeAll('cache_flush');
Chris@0 487 foreach (Cache::getBins() as $service_id => $cache_backend) {
Chris@0 488 $cache_backend->deleteAll();
Chris@0 489 }
Chris@0 490
Chris@0 491 return TRUE;
Chris@0 492 }
Chris@0 493
Chris@0 494 /**
Chris@0 495 * Helper method for removing all cache bins registered by a given module.
Chris@0 496 *
Chris@0 497 * @param string $module
Chris@0 498 * The name of the module for which to remove all registered cache bins.
Chris@0 499 */
Chris@0 500 protected function removeCacheBins($module) {
Chris@0 501 // Remove any cache bins defined by a module.
Chris@0 502 $service_yaml_file = drupal_get_path('module', $module) . "/$module.services.yml";
Chris@0 503 if (file_exists($service_yaml_file)) {
Chris@0 504 $definitions = Yaml::decode(file_get_contents($service_yaml_file));
Chris@0 505 if (isset($definitions['services'])) {
Chris@0 506 foreach ($definitions['services'] as $id => $definition) {
Chris@0 507 if (isset($definition['tags'])) {
Chris@0 508 foreach ($definition['tags'] as $tag) {
Chris@0 509 // This works for the default cache registration and even in some
Chris@0 510 // cases when a non-default "super" factory is used. That should
Chris@0 511 // be extremely rare.
Chris@0 512 if ($tag['name'] == 'cache.bin' && isset($definition['factory_service']) && isset($definition['factory_method']) && !empty($definition['arguments'])) {
Chris@0 513 try {
Chris@0 514 $factory = \Drupal::service($definition['factory_service']);
Chris@0 515 if (method_exists($factory, $definition['factory_method'])) {
Chris@0 516 $backend = call_user_func_array([$factory, $definition['factory_method']], $definition['arguments']);
Chris@0 517 if ($backend instanceof CacheBackendInterface) {
Chris@0 518 $backend->removeBin();
Chris@0 519 }
Chris@0 520 }
Chris@0 521 }
Chris@0 522 catch (\Exception $e) {
Chris@0 523 watchdog_exception('system', $e, 'Failed to remove cache bin defined by the service %id.', ['%id' => $id]);
Chris@0 524 }
Chris@0 525 }
Chris@0 526 }
Chris@0 527 }
Chris@0 528 }
Chris@0 529 }
Chris@0 530 }
Chris@0 531 }
Chris@0 532
Chris@0 533 /**
Chris@0 534 * Updates the kernel module list.
Chris@0 535 *
Chris@0 536 * @param string $module_filenames
Chris@0 537 * The list of installed modules.
Chris@0 538 */
Chris@0 539 protected function updateKernel($module_filenames) {
Chris@0 540 // This reboots the kernel to register the module's bundle and its services
Chris@0 541 // in the service container. The $module_filenames argument is taken over as
Chris@0 542 // %container.modules% parameter, which is passed to a fresh ModuleHandler
Chris@0 543 // instance upon first retrieval.
Chris@0 544 $this->kernel->updateModules($module_filenames, $module_filenames);
Chris@0 545 // After rebuilding the container we need to update the injected
Chris@0 546 // dependencies.
Chris@0 547 $container = $this->kernel->getContainer();
Chris@0 548 $this->moduleHandler = $container->get('module_handler');
Chris@0 549 }
Chris@0 550
Chris@0 551 /**
Chris@0 552 * {@inheritdoc}
Chris@0 553 */
Chris@0 554 public function validateUninstall(array $module_list) {
Chris@0 555 $reasons = [];
Chris@0 556 foreach ($module_list as $module) {
Chris@0 557 foreach ($this->uninstallValidators as $validator) {
Chris@0 558 $validation_reasons = $validator->validate($module);
Chris@0 559 if (!empty($validation_reasons)) {
Chris@0 560 if (!isset($reasons[$module])) {
Chris@0 561 $reasons[$module] = [];
Chris@0 562 }
Chris@0 563 $reasons[$module] = array_merge($reasons[$module], $validation_reasons);
Chris@0 564 }
Chris@0 565 }
Chris@0 566 }
Chris@0 567 return $reasons;
Chris@0 568 }
Chris@0 569
Chris@0 570 }