ExcludedModulesEventSubscriber.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Config\ConfigManagerInterface;
  4. use Drupal\Core\Config\StorageInterface;
  5. use Drupal\Core\Config\StorageTransformEvent;
  6. use Drupal\Core\Site\Settings;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9. * The event subscriber preventing excluded modules to be exported.
  10. */
  11. final class ExcludedModulesEventSubscriber implements EventSubscriberInterface {
  12. /**
  13. * The key in settings and state for listing excluded modules.
  14. *
  15. * @var string
  16. */
  17. const EXCLUDED_MODULES_KEY = "config_exclude_modules";
  18. /**
  19. * @var \Drupal\Core\Config\StorageInterface
  20. */
  21. private $activeStorage;
  22. /**
  23. * @var \Drupal\Core\Site\Settings
  24. */
  25. private $settings;
  26. /**
  27. * @var \Drupal\Core\Config\ConfigManagerInterface
  28. */
  29. private $manager;
  30. /**
  31. * EnvironmentModulesEventSubscriber constructor.
  32. *
  33. * @param \Drupal\Core\Config\StorageInterface $active_storage
  34. * The active config storage.
  35. * @param \Drupal\Core\Site\Settings $settings
  36. * The Drupal settings.
  37. * @param \Drupal\Core\Config\ConfigManagerInterface $manager
  38. * The config manager.
  39. */
  40. public function __construct(StorageInterface $active_storage, Settings $settings, ConfigManagerInterface $manager) {
  41. $this->activeStorage = $active_storage;
  42. $this->settings = $settings;
  43. $this->manager = $manager;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function getSubscribedEvents() {
  49. // React early on export and late on import.
  50. return [
  51. 'config.transform.import' => ['onConfigTransformImport', -500],
  52. 'config.transform.export' => ['onConfigTransformExport', 500],
  53. ];
  54. }
  55. /**
  56. * Transform the storage which is used to import the configuration.
  57. *
  58. * Make sure excluded modules are not uninstalled by adding them and their
  59. * config to the storage when importing configuration.
  60. *
  61. * @param \Drupal\Core\Config\StorageTransformEvent $event
  62. * The transformation event.
  63. */
  64. public function onConfigTransformImport(StorageTransformEvent $event) {
  65. $storage = $event->getStorage();
  66. if (!$storage->exists('core.extension')) {
  67. // If the core.extension config is not present there is nothing to do.
  68. // This means that probably the storage is empty or non-functional.
  69. return;
  70. }
  71. foreach (array_merge([StorageInterface::DEFAULT_COLLECTION], $this->activeStorage->getAllCollectionNames()) as $collectionName) {
  72. $collection = $storage->createCollection($collectionName);
  73. $activeCollection = $this->activeStorage->createCollection($collectionName);
  74. foreach ($this->getDependentConfigNames() as $configName) {
  75. if (!$collection->exists($configName) && $activeCollection->exists($configName)) {
  76. // Make sure the config is not removed if it exists.
  77. $collection->write($configName, $activeCollection->read($configName));
  78. }
  79. }
  80. }
  81. $extension = $storage->read('core.extension');
  82. $existing = $this->activeStorage->read('core.extension');
  83. $modules = $extension['module'];
  84. foreach ($this->getExcludedModules() as $module) {
  85. if (array_key_exists($module, $existing['module'])) {
  86. // Set the modules weight from the active store.
  87. $modules[$module] = $existing['module'][$module];
  88. }
  89. }
  90. // Sort the extensions.
  91. $extension['module'] = module_config_sort($modules);
  92. // Set the modified extension.
  93. $storage->write('core.extension', $extension);
  94. }
  95. /**
  96. * Transform the storage which is used to export the configuration.
  97. *
  98. * Make sure excluded modules are not exported by removing all the config
  99. * which depends on them from the storage that is exported.
  100. *
  101. * @param \Drupal\Core\Config\StorageTransformEvent $event
  102. * The transformation event.
  103. */
  104. public function onConfigTransformExport(StorageTransformEvent $event) {
  105. $storage = $event->getStorage();
  106. if (!$storage->exists('core.extension')) {
  107. // If the core.extension config is not present there is nothing to do.
  108. // This means some other process has rendered it non-functional already.
  109. return;
  110. }
  111. foreach (array_merge([StorageInterface::DEFAULT_COLLECTION], $storage->getAllCollectionNames()) as $collectionName) {
  112. $collection = $storage->createCollection($collectionName);
  113. foreach ($this->getDependentConfigNames() as $configName) {
  114. $collection->delete($configName);
  115. }
  116. }
  117. $extension = $storage->read('core.extension');
  118. // Remove all the excluded modules from the extensions list.
  119. $extension['module'] = array_diff_key($extension['module'], array_flip($this->getExcludedModules()));
  120. $storage->write('core.extension', $extension);
  121. }
  122. /**
  123. * Get the modules set as excluded in the Drupal settings.
  124. *
  125. * @return string[]
  126. * An array of module names.
  127. */
  128. private function getExcludedModules() {
  129. return $this->settings->get(self::EXCLUDED_MODULES_KEY, []);
  130. }
  131. /**
  132. * Get all the configuration which depends on one of the excluded modules.
  133. *
  134. * @return string[]
  135. * An array of configuration names.
  136. */
  137. private function getDependentConfigNames() {
  138. $modules = $this->getExcludedModules();
  139. $dependencyManager = $this->manager->getConfigDependencyManager();
  140. $config = [];
  141. // Find all the configuration depending on the excluded modules.
  142. foreach ($modules as $module) {
  143. foreach ($dependencyManager->getDependentEntities('module', $module) as $dependent) {
  144. $config[] = $dependent->getConfigDependencyName();
  145. }
  146. $config = array_merge($config, $this->activeStorage->listAll($module . '.'));
  147. }
  148. // Find all configuration that depends on the configuration found above.
  149. foreach ($this->manager->findConfigEntityDependents('config', array_unique($config)) as $dependent) {
  150. $config[] = $dependent->getConfigDependencyName();
  151. }
  152. return array_unique($config);
  153. }
  154. }