You are here

public function ConfigSyncInitializer::initialize in Configuration Synchronizer 8

Initializes the merge storage with available configuration updates.

Parameters

bool $retain_active_overrides: Whether to retain configuration customizations in the active configuration storage. Defaults to TRUE.

array $extension_names: Array with keys of extension types ('module', 'theme') and values arrays of extension names.

Overrides ConfigSyncInitializerInterface::initialize

File

src/ConfigSyncInitializer.php, line 86

Class

ConfigSyncInitializer
Returns responses for config module routes.

Namespace

Drupal\config_sync

Code

public function initialize($retain_active_overrides = TRUE, array $extension_names = []) {
  $config_factory = $this->configManager
    ->getConfigFactory();

  // Convert incoming extension names into Extension objects.
  $extensions = [];
  foreach ($extension_names as $type => $names) {
    foreach ($names as $name) {
      $pathname = $this
        ->drupalGetFilename($type, $name);
      $extension = new Extension(\Drupal::root(), $type, $pathname);
      $extensions[$name] = $extension;
    }
  }
  $this
    ->seedMergeStorage();
  $active_config_items = $config_factory
    ->listAll();

  /* @var \Drupal\config_provider\InMemoryStorage $installable_config */
  $installable_config = $this->configCollector
    ->getInstallableConfig($extensions);

  // Set up a storage comparer.
  $storage_comparer = new StorageComparer($installable_config, $this->snapshotExtensionStorage, $this->configManager);
  $storage_comparer
    ->createChangelist();
  $changelist = $storage_comparer
    ->getChangelist();

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

    // Don't attempt to create items that already exist.
    $config_to_create = array_diff($changelist['create'], $active_config_items);

    // To create, we simply save the new item to the merge storage.
    foreach ($config_to_create as $item_name) {
      $this->mergedStorage
        ->write($item_name, $installable_config
        ->read($item_name));
    }
  }

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

    // Don't attempt to update items that don't exist.
    $config_to_update = array_intersect($changelist['update'], $active_config_items);
    $config_sync_merger = new ConfigSyncMerger();

    // To update, we merge the value into that of the active storage.
    foreach ($config_to_update as $item_name) {
      $current = $installable_config
        ->read($item_name);
      if ($retain_active_overrides) {
        $previous = $this->snapshotExtensionStorage
          ->read($item_name);
        $active = $config_factory
          ->get($item_name)
          ->getRawData();
        $merged_value = $config_sync_merger
          ->mergeConfigItemStates($previous, $current, $active);
      }
      else {
        $merged_value = $current;
      }

      // If we do not have a site UUID this means we are merging updates from
      // an install profile. Make sure the site UUID is set in this case.
      // Otherwise, the storage comparer will think we are installing config
      // from a different website and will reject the changes.
      if ($item_name === 'system.site' && empty($merged_value['uuid'])) {
        $merged_value['uuid'] = $config_factory
          ->get('system.site')
          ->get('uuid');
      }
      $this->mergedStorage
        ->write($item_name, $merged_value);
    }
  }
}