InMemoryStorage.php in Configuration Provider 8
File
src/InMemoryStorage.php
View source
<?php
namespace Drupal\config_provider;
use Drupal\Core\Config\StorageInterface;
class InMemoryStorage implements StorageInterface {
protected $config;
protected $collection;
public function __construct($collection = StorageInterface::DEFAULT_COLLECTION, array $config = []) {
$this->collection = $collection;
$this->config = $config;
if (!isset($this->config[$collection])) {
$this->config[$collection] = [];
}
}
public function exists($name) {
return isset($this->config[$this->collection][$name]);
}
public function read($name) {
if (isset($this->config[$this->collection][$name])) {
return $this->config[$this->collection][$name];
}
return FALSE;
}
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;
}
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]);
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 = '') {
$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;
}
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;
}
public function createCollection($collection) {
return new static($collection, $this->config);
}
public function getAllCollectionNames() {
$collections = array_keys($this->config);
unset($collections[array_search(StorageInterface::DEFAULT_COLLECTION, $collections)]);
return $collections;
}
public function getCollectionName() {
return $this->collection;
}
public function writeToCollection($name, array $data, $collection) {
if (!isset($this->config[$collection])) {
$this->config[$collection] = [];
}
$this->config[$collection][$name] = $data;
return TRUE;
}
}