You are here

class InMemoryStorage in Configuration Provider 8

Provides an in memory confituration storage.

Hierarchy

Expanded class hierarchy of InMemoryStorage

4 files declare their use of InMemoryStorage
ConfigCollector.php in src/Plugin/ConfigCollector.php
ConfigProviderInstall.php in src/Plugin/ConfigProvider/ConfigProviderInstall.php
ConfigProviderInterface.php in src/Plugin/ConfigProviderInterface.php
ConfigProviderOptional.php in src/Plugin/ConfigProvider/ConfigProviderOptional.php
1 string reference to 'InMemoryStorage'
config_provider.services.yml in ./config_provider.services.yml
config_provider.services.yml
1 service uses InMemoryStorage
config_provider.storage in ./config_provider.services.yml
Drupal\config_provider\InMemoryStorage

File

src/InMemoryStorage.php, line 10

Namespace

Drupal\config_provider
View source
class InMemoryStorage implements StorageInterface {

  /**
   * The configuration.
   *
   * @var array
   */
  protected $config;

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

  /**
   * Constructs a new InMemoryStorage.
   *
   * @param string $collection
   *   (optional) The collection to store configuration in. Defaults to the
   *   default collection.
   * @param array $config
   *   (optional) The configuration in the storage.
   */
  public function __construct($collection = StorageInterface::DEFAULT_COLLECTION, array $config = []) {
    $this->collection = $collection;
    $this->config = $config;
    if (!isset($this->config[$collection])) {
      $this->config[$collection] = [];
    }
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function write($name, array $data) {
    $this->config[$this->collection][$name] = $data;
    return TRUE;
  }

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

  /**
   * {@inheritdoc}
   */
  public function rename($name, $new_name) {
    if (!$this
      ->exists($name)) {
      return FALSE;
    }
    $this->config[$this->collection][$new_name] = $this->config[$this->collection][$name];
    unset($this->config[$this->collection][$name]);
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data) {
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function decode($raw) {
    return $raw;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() {
    $collections = array_keys($this->config);

    // The default collection is not included here.
    unset($collections[array_search(StorageInterface::DEFAULT_COLLECTION, $collections)]);
    return $collections;
  }

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

  /**
   * Writes configuration data to the storage for a collection.
   *
   * @param string $name
   *   The name of a configuration object to save.
   * @param array $data
   *   The configuration data to write.
   * @param string $collection
   *   The collection to store configuration in.
   *
   * @return bool
   *   TRUE on success, FALSE in case of an error.
   */
  public function writeToCollection($name, array $data, $collection) {
    if (!isset($this->config[$collection])) {
      $this->config[$collection] = [];
    }
    $this->config[$collection][$name] = $data;
    return TRUE;
  }

}

Members

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