View source
<?php
namespace Drupal\config_sync\Plugin\ConfigFilter;
use Drupal\config_filter\Plugin\ConfigFilterBase;
use Drupal\config_merge\ConfigMerger;
use Drupal\config_merge\Event\ConfigMergeEvent;
use Drupal\config_merge\Event\ConfigMergeEvents;
use Drupal\config_normalizer\Config\NormalizedReadOnlyStorage;
use Drupal\config_normalizer\Config\NormalizedReadOnlyStorageInterface;
use Drupal\config_normalizer\Plugin\ConfigNormalizerManager;
use Drupal\config_provider\Config\MemoryStorage;
use Drupal\config_snapshot\ConfigSnapshotStorage;
use Drupal\config_sync\ConfigSyncListerInterface;
use Drupal\config_sync\ConfigSyncSnapshotterInterface;
use Drupal\config_sync\Plugin\SyncConfigCollectorInterface;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SyncFilter extends ConfigFilterBase implements ContainerFactoryPluginInterface {
protected $syncSourceStorage;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, StorageInterface $sync_source_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->syncSourceStorage = $sync_source_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$sync_source_storage = self::initSyncSourceStorage($configuration, $container
->get('app.root'), $container
->get('config_sync.collector'), $container
->get('config_sync.lister'), $container
->get('plugin.manager.config_normalizer'), $container
->get('config_provider.storage'), $container
->get('config.storage'), $container
->get('config.manager'), $container
->get('state'), $container
->get('event_dispatcher'));
return new static($configuration, $plugin_id, $plugin_definition, $sync_source_storage);
}
protected static function initSyncSourceStorage(array $configuration, $root, SyncConfigCollectorInterface $config_collector, ConfigSyncListerInterface $config_sync_lister, ConfigNormalizerManager $normalizer_manager, StorageInterface $provider_storage, StorageInterface $active_storage, ConfigManagerInterface $config_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher) {
$sync_source_storage = new MemoryStorage();
$update_mode = $state
->get('config_sync.update_mode', ConfigSyncListerInterface::DEFAULT_UPDATE_MODE);
if ($update_mode === ConfigSyncListerInterface::UPDATE_MODE_FULL_RESET) {
$pathname = drupal_get_filename($configuration['extension_type'], $configuration['extension_name']);
$extension = new Extension($root, $configuration['extension_type'], $pathname);
$extensions = [
$configuration['extension_name'] => $extension,
];
$config_collector
->addInstallableConfig($extensions);
$config_manager
->createSnapshot($provider_storage, $sync_source_storage);
}
else {
$service_id = "config_snapshot.{ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET}.{$configuration['extension_type']}.{$configuration['extension_name']}";
if (\Drupal::getContainer() && \Drupal::hasService($service_id)) {
$snapshot_storage = \Drupal::service($service_id);
}
else {
$snapshot_storage = new ConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, $configuration['extension_type'], $configuration['extension_name']);
}
$changelists = $config_sync_lister
->getExtensionChangelist($configuration['extension_type'], $configuration['extension_name']);
foreach ($changelists as $collection => $changelist) {
foreach ([
'snapshot',
'provider',
'active',
'sync_source',
] as $storage_prefix) {
if ($collection !== ${$storage_prefix . '_storage'}
->getCollectionName()) {
${$storage_prefix . '_storage'} = ${$storage_prefix . '_storage'}
->createCollection($collection);
}
}
if (!empty($changelist['create'])) {
foreach (array_keys($changelist['create']) as $item_name) {
$sync_source_storage
->write($item_name, $provider_storage
->read($item_name));
}
}
if (!empty($changelist['update'])) {
$config_merger = new ConfigMerger();
foreach (array_keys($changelist['update']) as $item_name) {
$current = $provider_storage
->read($item_name);
switch ($update_mode) {
case ConfigSyncListerInterface::UPDATE_MODE_MERGE:
$previous = $snapshot_storage
->read($item_name);
$active = $active_storage
->read($item_name);
$updated = $config_merger
->mergeConfigItemStates($previous, $current, $active);
$logs = $config_merger
->getLogs();
$event = new ConfigMergeEvent($item_name, $logs, $configuration['extension_type'], $configuration['extension_name']);
$event_dispatcher
->dispatch(ConfigMergeEvents::POST_MERGE, $event);
break;
case ConfigSyncListerInterface::UPDATE_MODE_PARTIAL_RESET:
$updated = $current;
break;
default:
throw new \Exception('Invalid state value for config_sync.update_mode.');
}
$sync_source_storage
->write($item_name, $updated);
}
}
}
}
$context = [
'normalization_mode' => NormalizedReadOnlyStorageInterface::NORMALIZATION_MODE_PROVIDE,
'reference_storage_service' => $active_storage,
];
$normalized_storage = new NormalizedReadOnlyStorage($sync_source_storage, $normalizer_manager, $context);
return $normalized_storage;
}
protected function syncSourceStorageRead($name, $data) {
if ($sync = $this->syncSourceStorage
->read($name)) {
return $sync;
}
return $data;
}
protected function syncSourceStorageReadMultiple(array $names, array $data) {
$filtered_data = [];
foreach ($names as $name) {
$filtered_data[$name] = $this
->syncSourceStorageRead($name, isset($data[$name]) ? $data[$name] : []);
}
return $filtered_data;
}
public function filterRead($name, $data) {
return $this
->syncSourceStorageRead($name, $data);
}
public function filterExists($name, $exists) {
return $exists || $this->syncSourceStorage
->exists($name);
}
public function filterReadMultiple(array $names, array $data) {
$sync_data = $this
->syncSourceStorageReadMultiple($names, $data);
return array_merge($data, $sync_data);
}
public function filterListAll($prefix, array $data) {
$sync_names = $this->syncSourceStorage
->listAll($prefix);
return array_unique(array_merge($data, $sync_names));
}
public function filterCreateCollection($collection) {
return new static($this->configuration, $this->pluginId, $this->pluginDefinition, $this->syncSourceStorage
->createCollection($collection));
}
public function filterGetAllCollectionNames(array $collections) {
return array_merge($collections, $this->syncSourceStorage
->getAllCollectionNames());
}
}