You are here

protected static function SyncFilter::initSyncSourceStorage in Configuration Synchronizer 8.2

Initializes the sync source storage.

Parameters

array $configuration: The plugin configuration.

string $root: The app root.

\Drupal\config_sync\Plugin\SyncConfigCollectorInterface $config_collector: The config collector.

\Drupal\config_sync\ConfigSyncListerInterface $config_sync_lister: The config sync lister.

\Drupal\config_normalizer\Plugin\ConfigNormalizerManager $normalizer_manager: The normalizer plugin manager.

\Drupal\Core\Config\StorageInterface $provider_storage: The configuration provider storage.

\Drupal\Core\Config\StorageInterface $active_storage: The active configuration storage.

\Drupal\Core\Config\ConfigManagerInterface $config_manager: The configuration manager.

\Drupal\Core\State\StateInterface $state: The state.

\Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher: The event dispatcher.

Return value

\Drupal\Core\Config\StorageInterface The initialised sync source storage

Throws

\Exception

1 call to SyncFilter::initSyncSourceStorage()
SyncFilter::create in src/Plugin/ConfigFilter/SyncFilter.php
Creates an instance of the plugin.

File

src/Plugin/ConfigFilter/SyncFilter.php, line 115

Class

SyncFilter
Provides a sync filter that brings in updates from installed extensions.

Namespace

Drupal\config_sync\Plugin\ConfigFilter

Code

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);

  // For a full reset, write all provided configuration to the sync source
  // storage.
  if ($update_mode === ConfigSyncListerInterface::UPDATE_MODE_FULL_RESET) {

    // Create a storage with configuration installable from this extension.
    $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 {

    // Can't use Drupal\config_snapshot\ConfigSnapshotStorageTrait because
    // we're in a static method.
    $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) {

      // Ensure storages are using the specified collection.
      foreach ([
        'snapshot',
        'provider',
        'active',
        'sync_source',
      ] as $storage_prefix) {
        if ($collection !== ${$storage_prefix . '_storage'}
          ->getCollectionName()) {
          ${$storage_prefix . '_storage'} = ${$storage_prefix . '_storage'}
            ->createCollection($collection);
        }
      }

      // Process changes.
      if (!empty($changelist['create'])) {

        // To create, we simply save the new item to the merge storage.
        foreach (array_keys($changelist['create']) as $item_name) {
          $sync_source_storage
            ->write($item_name, $provider_storage
            ->read($item_name));
        }
      }

      // Process update changes.
      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) {

            // Merge the value into that of the active storage.
            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;

            // Reset to the currently provided value.
            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);
        }
      }
    }
  }

  // Normalize the sync source storage to get changes matching those used
  // in comparison.
  $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;
}