You are here

trait StorageCopyTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/StorageCopyTrait.php \Drupal\Core\Config\StorageCopyTrait

Utility trait to copy configuration from one storage to another.

Hierarchy

3 files declare their use of StorageCopyTrait
ConfigTestTrait.php in core/tests/Drupal/Tests/ConfigTestTrait.php
ReadOnlyStorageTest.php in core/tests/Drupal/Tests/Core/Config/ReadOnlyStorageTest.php
StorageCopyTraitTest.php in core/tests/Drupal/Tests/Core/Config/StorageCopyTraitTest.php

File

core/lib/Drupal/Core/Config/StorageCopyTrait.php, line 8

Namespace

Drupal\Core\Config
View source
trait StorageCopyTrait {

  /**
   * Copy the configuration from one storage to another and remove stale items.
   *
   * This method empties target storage and copies all collections from source.
   * Configuration is only copied and not imported, should not be used
   * with the active storage as the target.
   *
   * @param \Drupal\Core\Config\StorageInterface $source
   *   The configuration storage to copy from.
   * @param \Drupal\Core\Config\StorageInterface $target
   *   The configuration storage to copy to.
   */
  protected static function replaceStorageContents(StorageInterface $source, StorageInterface &$target) {

    // Make sure there is no stale configuration in the target storage.
    foreach (array_merge([
      StorageInterface::DEFAULT_COLLECTION,
    ], $target
      ->getAllCollectionNames()) as $collection) {
      $target
        ->createCollection($collection)
        ->deleteAll();
    }

    // Copy all the configuration from all the collections.
    foreach (array_merge([
      StorageInterface::DEFAULT_COLLECTION,
    ], $source
      ->getAllCollectionNames()) as $collection) {
      $source_collection = $source
        ->createCollection($collection);
      $target_collection = $target
        ->createCollection($collection);
      foreach ($source_collection
        ->listAll() as $name) {
        $data = $source_collection
          ->read($name);
        if ($data !== FALSE) {
          $target_collection
            ->write($name, $data);
        }
        else {
          \Drupal::logger('config')
            ->notice('Missing required data for configuration: %config', [
            '%config' => $name,
          ]);
        }
      }
    }

    // Make sure that the target is set to the same collection as the source.
    $target = $target
      ->createCollection($source
      ->getCollectionName());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.