ExcludedModulesEventSubscriber.php in Drupal 10
File
core/lib/Drupal/Core/EventSubscriber/ExcludedModulesEventSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageTransformEvent;
use Drupal\Core\Site\Settings;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class ExcludedModulesEventSubscriber implements EventSubscriberInterface {
const EXCLUDED_MODULES_KEY = "config_exclude_modules";
private $activeStorage;
private $settings;
private $manager;
public function __construct(StorageInterface $active_storage, Settings $settings, ConfigManagerInterface $manager) {
$this->activeStorage = $active_storage;
$this->settings = $settings;
$this->manager = $manager;
}
public static function getSubscribedEvents() : array {
return [
'config.transform.import' => [
'onConfigTransformImport',
-500,
],
'config.transform.export' => [
'onConfigTransformExport',
500,
],
];
}
public function onConfigTransformImport(StorageTransformEvent $event) {
$storage = $event
->getStorage();
if (!$storage
->exists('core.extension')) {
return;
}
foreach (array_merge([
StorageInterface::DEFAULT_COLLECTION,
], $this->activeStorage
->getAllCollectionNames()) as $collectionName) {
$collection = $storage
->createCollection($collectionName);
$activeCollection = $this->activeStorage
->createCollection($collectionName);
foreach ($this
->getDependentConfigNames() as $configName) {
if (!$collection
->exists($configName) && $activeCollection
->exists($configName)) {
$collection
->write($configName, $activeCollection
->read($configName));
}
}
}
$extension = $storage
->read('core.extension');
$existing = $this->activeStorage
->read('core.extension');
$modules = $extension['module'];
foreach ($this
->getExcludedModules() as $module) {
if (array_key_exists($module, $existing['module'])) {
$modules[$module] = $existing['module'][$module];
}
}
$extension['module'] = module_config_sort($modules);
$storage
->write('core.extension', $extension);
}
public function onConfigTransformExport(StorageTransformEvent $event) {
$storage = $event
->getStorage();
if (!$storage
->exists('core.extension')) {
return;
}
foreach (array_merge([
StorageInterface::DEFAULT_COLLECTION,
], $storage
->getAllCollectionNames()) as $collectionName) {
$collection = $storage
->createCollection($collectionName);
foreach ($this
->getDependentConfigNames() as $configName) {
$collection
->delete($configName);
}
}
$extension = $storage
->read('core.extension');
$extension['module'] = array_diff_key($extension['module'], array_flip($this
->getExcludedModules()));
$storage
->write('core.extension', $extension);
}
private function getExcludedModules() {
return $this->settings
->get(self::EXCLUDED_MODULES_KEY, []);
}
private function getDependentConfigNames() {
$modules = $this
->getExcludedModules();
$dependencyManager = $this->manager
->getConfigDependencyManager();
$config = [];
foreach ($modules as $module) {
foreach ($dependencyManager
->getDependentEntities('module', $module) as $dependent) {
$config[] = $dependent
->getConfigDependencyName();
}
$config = array_merge($config, $this->activeStorage
->listAll($module . '.'));
}
foreach ($this->manager
->findConfigEntityDependencies('config', array_unique($config)) as $dependent) {
$config[] = $dependent
->getConfigDependencyName();
}
return array_unique($config);
}
}