MemoryStorage.php in Drupal 10
File
core/lib/Drupal/Core/Config/MemoryStorage.php
View source
<?php
namespace Drupal\Core\Config;
class MemoryStorage implements StorageInterface {
protected $config;
protected $collection;
public function __construct($collection = StorageInterface::DEFAULT_COLLECTION) {
$this->collection = $collection;
$this->config = new \ArrayObject();
}
public function exists($name) {
return isset($this->config[$this->collection][$name]);
}
public function read($name) {
if ($this
->exists($name)) {
return $this->config[$this->collection][$name];
}
return FALSE;
}
public function readMultiple(array $names) {
return array_intersect_key($this->config[$this->collection], array_flip($names));
}
public function write($name, array $data) {
$this->config[$this->collection][$name] = $data;
return TRUE;
}
public function delete($name) {
if (isset($this->config[$this->collection][$name])) {
unset($this->config[$this->collection][$name]);
if (empty($this->config[$this->collection])) {
$this->config
->offsetUnset($this->collection);
}
return TRUE;
}
return FALSE;
}
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;
}
public function encode($data) {
return $data;
}
public function decode($raw) {
return $raw;
}
public function listAll($prefix = '') {
if (empty($this->config[$this->collection])) {
return [];
}
$names = array_keys($this->config[$this->collection]);
if ($prefix !== '') {
$names = array_filter($names, function ($name) use ($prefix) {
return strpos($name, $prefix) === 0;
});
}
return $names;
}
public function deleteAll($prefix = '') {
if (!$this->config
->offsetExists($this->collection)) {
return FALSE;
}
if ($prefix === '') {
$this->config
->offsetUnset($this->collection);
return TRUE;
}
$success = FALSE;
foreach (array_keys($this->config[$this->collection]) as $name) {
if (strpos($name, $prefix) === 0) {
$success = TRUE;
unset($this->config[$this->collection][$name]);
}
}
if (empty($this->config[$this->collection])) {
$this->config
->offsetUnset($this->collection);
}
return $success;
}
public function createCollection($collection) {
$collection = new static($collection);
$collection->config = $this->config;
return $collection;
}
public function getAllCollectionNames() {
$collection_names = [];
foreach ($this->config as $collection_name => $data) {
if ($collection_name !== StorageInterface::DEFAULT_COLLECTION && !empty($data)) {
$collection_names[] = $collection_name;
}
}
sort($collection_names);
return $collection_names;
}
public function getCollectionName() {
return $this->collection;
}
}