You are here

public function ConfigSplitManager::mergeSplit in Configuration Split 2.0.x

Merge the config of a split to the transformation storage.

Parameters

\Drupal\Core\Config\ImmutableConfig $config: The split config.

\Drupal\Core\Config\StorageInterface $transforming: The transforming storage.

\Drupal\Core\Config\StorageInterface $splitStorage: The split storage.

2 calls to ConfigSplitManager::mergeSplit()
ConfigSplitManager::importTransform in src/ConfigSplitManager.php
Process the import of a split.
ConfigSplitManager::singleImportOrActivate in src/ConfigSplitManager.php
Importing and activating are almost the same.

File

src/ConfigSplitManager.php, line 388

Class

ConfigSplitManager
The manager to split and merge.

Namespace

Drupal\config_split

Code

public function mergeSplit(ImmutableConfig $config, StorageInterface $transforming, StorageInterface $splitStorage) : void {
  $transforming = $transforming
    ->createCollection(StorageInterface::DEFAULT_COLLECTION);
  $splitStorage = $splitStorage
    ->createCollection(StorageInterface::DEFAULT_COLLECTION);

  // Merge all the configuration from all collections.
  foreach (array_merge([
    StorageInterface::DEFAULT_COLLECTION,
  ], $splitStorage
    ->getAllCollectionNames()) as $collection) {
    $split = $splitStorage
      ->createCollection($collection);
    $storage = $transforming
      ->createCollection($collection);
    foreach ($split
      ->listAll() as $name) {
      $data = $split
        ->read($name);
      if ($data !== FALSE) {
        if (strpos($name, self::SPLIT_PARTIAL_PREFIX) === 0) {
          $name = substr($name, strlen(self::SPLIT_PARTIAL_PREFIX));
          $diff = ConfigPatch::fromArray($data);
          if ($storage
            ->exists($name)) {

            // Skip patches for config that doesn't exist in the storage.
            $data = $storage
              ->read($name);
            $data = $this->patchMerge
              ->mergePatch($data, $diff
              ->invert(), $name);
            $storage
              ->write($name, $data);
          }
        }
        else {
          $storage
            ->write($name, $data);
        }
      }
    }
  }

  // When merging a split with the collection storage we delete all in it.
  if ($config
    ->get('storage') === 'collection') {

    // We can not assume $splitStorage is grafted onto $transforming.
    $collectionStorage = new SplitCollectionStorage($transforming, $config
      ->get('id'));
    foreach (array_merge([
      StorageInterface::DEFAULT_COLLECTION,
    ], $collectionStorage
      ->getAllCollectionNames()) as $collection) {
      $collectionStorage
        ->createCollection($collection)
        ->deleteAll();
    }
  }

  // Now special case the extensions.
  $extensions = $transforming
    ->read('core.extension');
  if ($extensions === FALSE) {
    return;
  }
  $updated = $transforming
    ->read($config
    ->getName());
  if ($updated === FALSE) {
    return;
  }
  $extensions['module'] = array_merge($extensions['module'], $updated['module'] ?? []);
  $extensions['theme'] = array_merge($extensions['theme'], $updated['theme'] ?? []);

  // Sort the modules.
  $sorted = $extensions['module'];
  uksort($sorted, function ($a, $b) use ($sorted) {

    // Sort by module weight, this assumes the schema of core.extensions.
    if ($sorted[$a] != $sorted[$b]) {
      return $sorted[$a] > $sorted[$b] ? 1 : -1;
    }

    // Or sort by module name.
    return $a > $b ? 1 : -1;
  });
  $extensions['module'] = $sorted;
  $transforming
    ->write('core.extension', $extensions);
}