class RecipeConfigStorageWrapper

Same name in other branches
  1. 10 core/lib/Drupal/Core/Recipe/RecipeConfigStorageWrapper.php \Drupal\Core\Recipe\RecipeConfigStorageWrapper

Merges two storages together.

@internal This API is experimental.

Hierarchy

Expanded class hierarchy of RecipeConfigStorageWrapper

1 file declares its use of RecipeConfigStorageWrapper
RecipeConfigStorageWrapperTest.php in core/tests/Drupal/Tests/Core/Recipe/RecipeConfigStorageWrapperTest.php

File

core/lib/Drupal/Core/Recipe/RecipeConfigStorageWrapper.php, line 16

Namespace

Drupal\Core\Recipe
View source
final class RecipeConfigStorageWrapper implements StorageInterface {
    
    /**
     * @param \Drupal\Core\Config\StorageInterface $storageA
     *   First config storage to wrap.
     * @param \Drupal\Core\Config\StorageInterface $storageB
     *   Second config storage to wrap.
     * @param string $collection
     *   (optional) The collection to store configuration in. Defaults to the
     *   default collection.
     */
    public function __construct(StorageInterface $storageA, StorageInterface $storageB, string $collection = StorageInterface::DEFAULT_COLLECTION) {
    }
    
    /**
     * Creates a single config storage for an array of storages.
     *
     * If the same configuration is contained in multiple storages then the
     * version returned is from the first storage supplied in the $storages array.
     *
     * @param \Drupal\Core\Config\StorageInterface[] $storages
     *   An array of storages to merge into a single storage.
     *
     * @return \Drupal\Core\Config\StorageInterface
     *   A config storage that represents a merge of all the provided storages.
     */
    public static function createStorageFromArray(array $storages) : StorageInterface {
        // If storages is empty use the NullStorage to represent an empty storage.
        if (empty($storages)) {
            return new NullStorage();
        }
        // When there is only one storage there is no point wrapping it.
        if (count($storages) === 1) {
            return reset($storages);
        }
        // Reduce all the storages to a single RecipeConfigStorageWrapper object.
        // The storages are prioritized in the order they are added to $storages.
        return array_reduce($storages, fn(StorageInterface $carry, StorageInterface $storage) => new static($carry, $storage), new static(array_shift($storages), array_shift($storages)));
    }
    
    /**
     * {@inheritdoc}
     */
    public function exists($name) : bool {
        return $this->storageA
            ->exists($name) || $this->storageB
            ->exists($name);
    }
    
    /**
     * {@inheritdoc}
     */
    public function read($name) : array|bool {
        return $this->storageA
            ->read($name) ?: $this->storageB
            ->read($name);
    }
    
    /**
     * {@inheritdoc}
     */
    public function readMultiple(array $names) : array {
        // If both storageA and storageB contain the same configuration, the value
        // for storageA takes precedence.
        return array_merge($this->storageB
            ->readMultiple($names), $this->storageA
            ->readMultiple($names));
    }
    
    /**
     * {@inheritdoc}
     */
    public function write($name, array $data) : bool {
        throw new \BadMethodCallException();
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete($name) : bool {
        throw new \BadMethodCallException();
    }
    
    /**
     * {@inheritdoc}
     */
    public function rename($name, $new_name) : bool {
        throw new \BadMethodCallException();
    }
    
    /**
     * {@inheritdoc}
     */
    public function encode($data) : string {
        throw new \BadMethodCallException();
    }
    
    /**
     * {@inheritdoc}
     */
    public function decode($raw) : array {
        throw new \BadMethodCallException();
    }
    
    /**
     * {@inheritdoc}
     */
    public function listAll($prefix = '') : array {
        return array_unique(array_merge($this->storageA
            ->listAll($prefix), $this->storageB
            ->listAll($prefix)));
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteAll($prefix = '') : bool {
        throw new \BadMethodCallException();
    }
    
    /**
     * {@inheritdoc}
     */
    public function createCollection($collection) : static {
        return new static($this->storageA
            ->createCollection($collection), $this->storageB
            ->createCollection($collection), $collection);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAllCollectionNames() : array {
        return array_unique(array_merge($this->storageA
            ->getAllCollectionNames(), $this->storageB
            ->getAllCollectionNames()));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCollectionName() : string {
        return $this->collection;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RecipeConfigStorageWrapper::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
RecipeConfigStorageWrapper::createStorageFromArray public static function Creates a single config storage for an array of storages.
RecipeConfigStorageWrapper::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
RecipeConfigStorageWrapper::delete public function Deletes a configuration object from the storage. Overrides StorageInterface::delete
RecipeConfigStorageWrapper::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
RecipeConfigStorageWrapper::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
RecipeConfigStorageWrapper::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
RecipeConfigStorageWrapper::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
RecipeConfigStorageWrapper::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
RecipeConfigStorageWrapper::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
RecipeConfigStorageWrapper::read public function Reads configuration data from the storage. Overrides StorageInterface::read
RecipeConfigStorageWrapper::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
RecipeConfigStorageWrapper::rename public function Renames a configuration object in the storage. Overrides StorageInterface::rename
RecipeConfigStorageWrapper::write public function Writes configuration data to the storage. Overrides StorageInterface::write
RecipeConfigStorageWrapper::__construct public function
StorageInterface::DEFAULT_COLLECTION constant The default collection name.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.