View source
<?php
namespace Drupal\config_sync;
use Drupal\config_normalizer\Config\NormalizedReadOnlyStorage;
use Drupal\config_normalizer\Config\NormalizedReadOnlyStorageInterface;
use Drupal\config_normalizer\Config\NormalizedStorageComparerTrait;
use Drupal\config_normalizer\Plugin\ConfigNormalizerManager;
use Drupal\config_snapshot\ConfigSnapshotStorageTrait;
use Drupal\config_sync\Config\SettableStorageComparer;
use Drupal\config_sync\Plugin\SyncConfigCollectorInterface;
use Drupal\config_update\ConfigListInterface as ConfigUpdateListerInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\NullStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class ConfigSyncLister implements ConfigSyncListerInterface {
use ConfigSnapshotStorageTrait;
use ConfigSyncActiveStoragesTrait;
use ConfigSyncExtensionsTrait;
use NormalizedStorageComparerTrait;
use StringTranslationTrait;
protected $configCollector;
protected $configUpdateLister;
protected $providerStorage;
protected $normalizedActiveStorage;
protected $state;
protected $configTypes = [];
protected $activeStorageComparer;
public function __construct(SyncConfigCollectorInterface $config_collector, ConfigUpdateListerInterface $config_update_lister, ConfigNormalizerManager $normalizer_manager, StorageInterface $active_storage, StorageInterface $provider_storage, ConfigManagerInterface $config_manager, StateInterface $state) {
$this->configCollector = $config_collector;
$this->configUpdateLister = $config_update_lister;
$this
->setNormalizerManager($normalizer_manager);
$this->activeStorages[$active_storage
->getCollectionName()] = $active_storage;
$this->providerStorage = $provider_storage;
$this
->setConfigManager($config_manager);
$this->state = $state;
$this->normalizedActiveStorage = new NormalizedReadOnlyStorage($active_storage, $normalizer_manager);
$this->activeStorageComparer = new SettableStorageComparer(new NormalizedReadOnlyStorage(new NullStorage(), $normalizer_manager), $this->normalizedActiveStorage, $config_manager);
}
public function getExtensionChangelists(array $extension_names = []) {
$changelist = [];
if (empty($extension_names)) {
$extension_names = $this
->getSyncExtensions();
}
foreach ($extension_names as $type => $names) {
foreach ($names as $name) {
if ($extension_changelist = $this
->getExtensionChangelist($type, $name)) {
$changelist[$type][$name] = $extension_changelist;
}
}
}
return $changelist;
}
public function getExtensionChangelist($type, $name) {
$update_mode = $this->state
->get('config_sync.update_mode', ConfigSyncListerInterface::DEFAULT_UPDATE_MODE);
$pathname = $this
->drupalGetFilename($type, $name);
$extension = new Extension(\Drupal::root(), $type, $pathname);
$extensions = [
$name => $extension,
];
$this->configCollector
->addInstallableConfig($extensions);
$return = [];
if (empty($this->providerStorage
->listAll())) {
return $return;
}
if ($update_mode === ConfigSyncListerInterface::UPDATE_MODE_FULL_RESET) {
$normalized_provider_storage = new NormalizedReadOnlyStorage($this->providerStorage, $this->normalizerManager, [
'normalization_mode' => NormalizedReadOnlyStorageInterface::DEFAULT_NORMALIZATION_MODE,
'reference_storage_service' => $this
->getActiveStorages(),
]);
$this->activeStorageComparer
->setSourceStorage($normalized_provider_storage);
$this->normalizedActiveStorage
->setContext([
'normalization_mode' => NormalizedReadOnlyStorageInterface::DEFAULT_NORMALIZATION_MODE,
'reference_storage_service' => $this->providerStorage,
]);
$storage_comparer = $this->activeStorageComparer;
}
else {
$snapshot_storage = $this
->getConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, $type, $name);
$storage_comparer = $this
->createStorageComparer($this->providerStorage, $snapshot_storage);
}
if ($storage_comparer
->createChangelist()
->hasChanges()) {
foreach ($storage_comparer
->getAllCollectionNames() as $collection) {
$changelist = $storage_comparer
->getChangelist(NULL, $collection);
unset($changelist['delete']);
unset($changelist['rename']);
$changelist = array_filter($changelist);
foreach ($changelist as $change_type => $item_names) {
foreach ($item_names as $item_name) {
$adjusted_change_type = $change_type;
$target_exists = $this
->getActiveStorages($collection)
->exists($item_name);
if ($change_type === 'update' && !$target_exists) {
switch ($update_mode) {
case ConfigSyncListerInterface::UPDATE_MODE_MERGE:
continue 2;
case ConfigSyncListerInterface::UPDATE_MODE_PARTIAL_RESET:
$adjusted_change_type = 'create';
break;
}
}
$config_type = $this->configUpdateLister
->getTypeNameByConfigName($item_name);
if (!$config_type) {
$label = $item_name;
}
else {
$config = $this->providerStorage
->read($item_name);
$definition = $this->configUpdateLister
->getType($config_type);
$key = $definition
->getKey('label') ?: $definition
->getKey('id');
$label = $config[$key];
}
$return[$collection][$adjusted_change_type][$item_name] = $label;
}
}
}
}
return $return;
}
public function listConfigTypes() {
if (empty($this->configTypes)) {
$definitions = $this->configUpdateLister
->listTypes();
$config_types = array_map(function (EntityTypeInterface $definition) {
return $definition
->getLabel();
}, $definitions);
$config_types['system_simple'] = $this
->t('Simple configuration');
uasort($config_types, 'strnatcasecmp');
$this->configTypes = $config_types;
}
return $this->configTypes;
}
protected function drupalGetFilename($type, $name, $filename = NULL) {
return drupal_get_filename($type, $name, $filename);
}
}