You are here

class SplitCollectionStorage in Configuration Split 2.0.x

A config storage that lives in a collection of another config storage.

Hierarchy

Expanded class hierarchy of SplitCollectionStorage

4 files declare their use of SplitCollectionStorage
ConfigSplitManager.php in src/ConfigSplitManager.php
SplitCollectionStorageTest.php in tests/src/Kernel/SplitCollectionStorageTest.php
SplitMergeTest.php in tests/src/Kernel/SplitMergeTest.php
SplitTestTrait.php in tests/src/Kernel/SplitTestTrait.php

File

src/Config/SplitCollectionStorage.php, line 11

Namespace

Drupal\config_split\Config
View source
class SplitCollectionStorage implements StorageInterface {
  const PREFIX = 'split.';

  /**
   * The storage into which the config is saved.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $storage;

  /**
   * The name of the storage.
   *
   * This corresponds to the prefixed name of the collection in the storage.
   *
   * @var string
   */
  protected $name;

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

  /**
   * SplitCollectionStorage constructor.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   The storage onto which the collection is grafted.
   * @param string $name
   *   The name of storage, must be a valid collection name.
   * @param string $collection
   *   The collection name.
   */
  public function __construct(StorageInterface $storage, string $name, string $collection = StorageInterface::DEFAULT_COLLECTION) {
    $this->name = $name;
    $this->collection = $collection;
    $this->storage = $storage
      ->createCollection(static::PREFIX . $name . ($collection !== StorageInterface::DEFAULT_COLLECTION ? '.' . $collection : ''));
  }

  /**
   * {@inheritdoc}
   */
  public function exists($name) {
    return $this
      ->getStorage()
      ->exists($name);
  }

  /**
   * {@inheritdoc}
   */
  public function read($name) {
    return $this
      ->getStorage()
      ->read($name);
  }

  /**
   * {@inheritdoc}
   */
  public function readMultiple(array $names) {
    return $this
      ->getStorage()
      ->readMultiple($names);
  }

  /**
   * {@inheritdoc}
   */
  public function write($name, array $data) {
    return $this
      ->getStorage()
      ->write($name, $data);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($name) {
    return $this
      ->getStorage()
      ->delete($name);
  }

  /**
   * {@inheritdoc}
   */
  public function rename($name, $new_name) {
    return $this
      ->getStorage()
      ->rename($name, $new_name);
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function listAll($prefix = '') {
    return $this
      ->getStorage()
      ->listAll($prefix);
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll($prefix = '') {
    return $this
      ->getStorage()
      ->deleteAll($prefix);
  }

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

  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() {
    return array_values(array_filter(array_map(function ($c) {
      $prefix = static::PREFIX . $this->name . '.';
      if (strpos($c, $prefix) !== 0) {

        // If the underlying storage has collections that don't concern us,
        // they will be filtered out, including the default collection.
        return FALSE;
      }
      return substr($c, strlen($prefix));
    }, $this->storage
      ->getAllCollectionNames())));
  }

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

  /**
   * Get the storage to interact with.
   *
   * @return \Drupal\Core\Config\StorageInterface
   *   The config storage.
   */
  protected function getStorage() {
    if (strpos($this->collection, static::PREFIX) !== FALSE) {

      // Avoid recursion.
      // This means this storage can not live inside another one of this type.
      return new NullStorage();
    }
    return $this->storage;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SplitCollectionStorage::$collection protected property The storage collection.
SplitCollectionStorage::$name protected property The name of the storage.
SplitCollectionStorage::$storage protected property The storage into which the config is saved.
SplitCollectionStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
SplitCollectionStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
SplitCollectionStorage::delete public function Deletes a configuration object from the storage. Overrides StorageInterface::delete
SplitCollectionStorage::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
SplitCollectionStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
SplitCollectionStorage::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
SplitCollectionStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
SplitCollectionStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
SplitCollectionStorage::getStorage protected function Get the storage to interact with.
SplitCollectionStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
SplitCollectionStorage::PREFIX constant 1
SplitCollectionStorage::read public function Reads configuration data from the storage. Overrides StorageInterface::read
SplitCollectionStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
SplitCollectionStorage::rename public function Renames a configuration object in the storage. Overrides StorageInterface::rename
SplitCollectionStorage::write public function Writes configuration data to the storage. Overrides StorageInterface::write
SplitCollectionStorage::__construct public function SplitCollectionStorage constructor.
StorageInterface::DEFAULT_COLLECTION constant The default collection name.