You are here

class StorageReplaceDataWrapper in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/config/src/StorageReplaceDataWrapper.php \Drupal\config\StorageReplaceDataWrapper

Wraps a configuration storage to allow replacing specific configuration data.

Hierarchy

Expanded class hierarchy of StorageReplaceDataWrapper

2 files declare their use of StorageReplaceDataWrapper
ConfigSingleImportForm.php in core/modules/config/src/Form/ConfigSingleImportForm.php
StorageReplaceDataWrapperTest.php in core/tests/Drupal/KernelTests/Core/Config/Storage/StorageReplaceDataWrapperTest.php

File

core/modules/config/src/StorageReplaceDataWrapper.php, line 11

Namespace

Drupal\config
View source
class StorageReplaceDataWrapper implements StorageInterface {
  use DependencySerializationTrait;

  /**
   * The configuration storage to be wrapped.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $storage;

  /**
   * The configuration replacement data, keyed by configuration object name.
   *
   * @var array
   */
  protected $replacementData = [];

  /**
   * The storage collection.
   *
   * @var string
   */
  protected $collection;

  /**
   * Constructs a new StorageReplaceDataWrapper.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   A configuration storage to be used to read and write configuration.
   * @param string $collection
   *   (optional) The collection to store configuration in. Defaults to the
   *   default collection.
   */
  public function __construct(StorageInterface $storage, $collection = StorageInterface::DEFAULT_COLLECTION) {
    $this->storage = $storage;
    $this->collection = $collection;
    $this->replacementData[$collection] = [];
  }

  /**
   * {@inheritdoc}
   */
  public function exists($name) {
    return isset($this->replacementData[$this->collection][$name]) || $this->storage
      ->exists($name);
  }

  /**
   * {@inheritdoc}
   */
  public function read($name) {
    if (isset($this->replacementData[$this->collection][$name])) {
      return $this->replacementData[$this->collection][$name];
    }
    return $this->storage
      ->read($name);
  }

  /**
   * {@inheritdoc}
   */
  public function readMultiple(array $names) {
    $data = $this->storage
      ->readMultiple($names);
    foreach ($names as $name) {
      if (isset($this->replacementData[$this->collection][$name])) {
        $data[$name] = $this->replacementData[$this->collection][$name];
      }
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function write($name, array $data) {
    if (isset($this->replacementData[$this->collection][$name])) {
      unset($this->replacementData[$this->collection][$name]);
    }
    return $this->storage
      ->write($name, $data);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($name) {
    if (isset($this->replacementData[$this->collection][$name])) {
      unset($this->replacementData[$this->collection][$name]);
    }
    return $this->storage
      ->delete($name);
  }

  /**
   * {@inheritdoc}
   */
  public function rename($name, $new_name) {
    if (isset($this->replacementData[$this->collection][$name])) {
      $this->replacementData[$this->collection][$new_name] = $this->replacementData[$this->collection][$name];
      unset($this->replacementData[$this->collection][$name]);
    }
    return $this->storage
      ->rename($name, $new_name);
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data) {
    return $this->storage
      ->encode($data);
  }

  /**
   * {@inheritdoc}
   */
  public function decode($raw) {
    return $this->storage
      ->decode($raw);
  }

  /**
   * {@inheritdoc}
   */
  public function listAll($prefix = '') {
    $names = $this->storage
      ->listAll($prefix);
    $additional_names = [];
    if ($prefix === '') {
      $additional_names = array_keys($this->replacementData[$this->collection]);
    }
    else {
      foreach (array_keys($this->replacementData[$this->collection]) as $name) {
        if (strpos($name, $prefix) === 0) {
          $additional_names[] = $name;
        }
      }
    }
    if (!empty($additional_names)) {
      $names = array_unique(array_merge($names, $additional_names));
    }
    return $names;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll($prefix = '') {
    if ($prefix === '') {
      $this->replacementData[$this->collection] = [];
    }
    else {
      foreach (array_keys($this->replacementData[$this->collection]) as $name) {
        if (strpos($name, $prefix) === 0) {
          unset($this->replacementData[$this->collection][$name]);
        }
      }
    }
    return $this->storage
      ->deleteAll($prefix);
  }

  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) {
    return new static($this->storage
      ->createCollection($collection), $collection);
  }

  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() {
    return $this->storage
      ->getAllCollectionNames();
  }

  /**
   * {@inheritdoc}
   */
  public function getCollectionName() {
    return $this->collection;
  }

  /**
   * Replaces the configuration object data with the supplied data.
   *
   * @param $name
   *   The configuration object name whose data to replace.
   * @param array $data
   *   The configuration data.
   *
   * @return $this
   */
  public function replaceData($name, array $data) {
    $this->replacementData[$this->collection][$name] = $data;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
StorageInterface::DEFAULT_COLLECTION constant The default collection name.
StorageReplaceDataWrapper::$collection protected property The storage collection.
StorageReplaceDataWrapper::$replacementData protected property The configuration replacement data, keyed by configuration object name.
StorageReplaceDataWrapper::$storage protected property The configuration storage to be wrapped.
StorageReplaceDataWrapper::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
StorageReplaceDataWrapper::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
StorageReplaceDataWrapper::delete public function Deletes a configuration object from the storage. Overrides StorageInterface::delete
StorageReplaceDataWrapper::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
StorageReplaceDataWrapper::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
StorageReplaceDataWrapper::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
StorageReplaceDataWrapper::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
StorageReplaceDataWrapper::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
StorageReplaceDataWrapper::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
StorageReplaceDataWrapper::read public function Reads configuration data from the storage. Overrides StorageInterface::read
StorageReplaceDataWrapper::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
StorageReplaceDataWrapper::rename public function Renames a configuration object in the storage. Overrides StorageInterface::rename
StorageReplaceDataWrapper::replaceData public function Replaces the configuration object data with the supplied data.
StorageReplaceDataWrapper::write public function Writes configuration data to the storage. Overrides StorageInterface::write
StorageReplaceDataWrapper::__construct public function Constructs a new StorageReplaceDataWrapper.