class MemoryStorage

Same name in this branch
  1. 10 core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php \Drupal\Core\KeyValueStore\MemoryStorage
Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/MemoryStorage.php \Drupal\Core\Config\MemoryStorage
  2. 9 core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php \Drupal\Core\KeyValueStore\MemoryStorage
  3. 8.9.x core/lib/Drupal/Core/Config/MemoryStorage.php \Drupal\Core\Config\MemoryStorage
  4. 8.9.x core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php \Drupal\Core\KeyValueStore\MemoryStorage
  5. 11.x core/lib/Drupal/Core/Config/MemoryStorage.php \Drupal\Core\Config\MemoryStorage
  6. 11.x core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php \Drupal\Core\KeyValueStore\MemoryStorage

Provides an in memory configuration storage.

Hierarchy

Expanded class hierarchy of MemoryStorage

9 files declare their use of MemoryStorage
CheckpointStorageTest.php in core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php
ExtensionInstallStorageTest.php in core/tests/Drupal/Tests/Core/Config/ExtensionInstallStorageTest.php
ImportStorageTransformerTest.php in core/tests/Drupal/KernelTests/Core/Config/ImportStorageTransformerTest.php
ManagedStorageTest.php in core/tests/Drupal/KernelTests/Core/Config/Storage/ManagedStorageTest.php
MemoryStorageTest.php in core/tests/Drupal/KernelTests/Core/Config/Storage/MemoryStorageTest.php

... See full list

File

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

Namespace

Drupal\Core\Config
View source
class MemoryStorage implements StorageInterface {
    
    /**
     * The configuration, an object shared by reference across collections.
     *
     * @var \ArrayAccess
     */
    protected $config;
    
    /**
     * The storage collection.
     *
     * @var string
     */
    protected $collection;
    
    /**
     * Constructs a new MemoryStorage.
     *
     * @param string $collection
     *   (optional) The collection to store configuration in. Defaults to the
     *   default collection.
     */
    public function __construct($collection = StorageInterface::DEFAULT_COLLECTION) {
        $this->collection = $collection;
        $this->config = new \ArrayObject();
        $this->config[$collection] = [];
    }
    
    /**
     * {@inheritdoc}
     */
    public function exists($name) {
        return isset($this->config[$this->collection][$name]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function read($name) {
        if ($this->exists($name)) {
            return $this->config[$this->collection][$name];
        }
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function readMultiple(array $names) {
        return array_intersect_key($this->config[$this->collection], array_flip($names));
    }
    
    /**
     * {@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]);
            // Remove the collection if it is empty.
            if (empty($this->config[$this->collection])) {
                $this->config
                    ->offsetUnset($this->collection);
            }
            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 = '') {
        if (empty($this->config[$this->collection])) {
            // If the collection is empty no keys are set.
            return [];
        }
        $names = array_keys($this->config[$this->collection]);
        if ($prefix !== '') {
            $names = array_filter($names, function ($name) use ($prefix) {
                return str_starts_with($name, $prefix);
            });
        }
        return $names;
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteAll($prefix = '') {
        if (!$this->config
            ->offsetExists($this->collection)) {
            // There's nothing to delete.
            return FALSE;
        }
        if ($prefix === '') {
            $this->config
                ->offsetUnset($this->collection);
            return TRUE;
        }
        $success = FALSE;
        foreach (array_keys($this->config[$this->collection]) as $name) {
            if (str_starts_with($name, $prefix)) {
                $success = TRUE;
                unset($this->config[$this->collection][$name]);
            }
        }
        // Remove the collection if it is empty.
        if (empty($this->config[$this->collection])) {
            $this->config
                ->offsetUnset($this->collection);
        }
        return $success;
    }
    
    /**
     * {@inheritdoc}
     */
    public function createCollection($collection) {
        $collection = new static($collection);
        $collection->config = $this->config;
        return $collection;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAllCollectionNames() {
        $collection_names = [];
        foreach ($this->config as $collection_name => $data) {
            // Exclude the default collection and empty collections.
            if ($collection_name !== StorageInterface::DEFAULT_COLLECTION && !empty($data)) {
                $collection_names[] = $collection_name;
            }
        }
        sort($collection_names);
        return $collection_names;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCollectionName() {
        return $this->collection;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
MemoryStorage::$collection protected property The storage collection.
MemoryStorage::$config protected property The configuration, an object shared by reference across collections.
MemoryStorage::createCollection public function Overrides StorageInterface::createCollection
MemoryStorage::decode public function Overrides StorageInterface::decode
MemoryStorage::delete public function Overrides StorageInterface::delete
MemoryStorage::deleteAll public function Overrides StorageInterface::deleteAll
MemoryStorage::encode public function Overrides StorageInterface::encode
MemoryStorage::exists public function Overrides StorageInterface::exists
MemoryStorage::getAllCollectionNames public function Overrides StorageInterface::getAllCollectionNames
MemoryStorage::getCollectionName public function Overrides StorageInterface::getCollectionName
MemoryStorage::listAll public function Overrides StorageInterface::listAll
MemoryStorage::read public function Overrides StorageInterface::read
MemoryStorage::readMultiple public function Overrides StorageInterface::readMultiple
MemoryStorage::rename public function Overrides StorageInterface::rename
MemoryStorage::write public function Overrides StorageInterface::write
MemoryStorage::__construct public function Constructs a new MemoryStorage.
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.