StorageReplaceDataWrapper.php in Drupal 10
File
core/modules/config/src/StorageReplaceDataWrapper.php
View source
<?php
namespace Drupal\config;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class StorageReplaceDataWrapper implements StorageInterface {
use DependencySerializationTrait;
protected $storage;
protected $replacementData = [];
protected $collection;
public function __construct(StorageInterface $storage, $collection = StorageInterface::DEFAULT_COLLECTION) {
$this->storage = $storage;
$this->collection = $collection;
$this->replacementData[$collection] = [];
}
public function exists($name) {
return isset($this->replacementData[$this->collection][$name]) || $this->storage
->exists($name);
}
public function read($name) {
if (isset($this->replacementData[$this->collection][$name])) {
return $this->replacementData[$this->collection][$name];
}
return $this->storage
->read($name);
}
public function readMultiple(array $names) {
$data = $this->storage
->readMultiple($names);
foreach ($names as $name) {
if (isset($this->replacementData[$this->collection][$name])) {
$data[$name] = $this->replacementData[$this->collection][$name];
}
}
return $data;
}
public function write($name, array $data) {
if (isset($this->replacementData[$this->collection][$name])) {
unset($this->replacementData[$this->collection][$name]);
}
return $this->storage
->write($name, $data);
}
public function delete($name) {
if (isset($this->replacementData[$this->collection][$name])) {
unset($this->replacementData[$this->collection][$name]);
}
return $this->storage
->delete($name);
}
public function rename($name, $new_name) {
if (isset($this->replacementData[$this->collection][$name])) {
$this->replacementData[$this->collection][$new_name] = $this->replacementData[$this->collection][$name];
unset($this->replacementData[$this->collection][$name]);
}
return $this->storage
->rename($name, $new_name);
}
public function encode($data) {
return $this->storage
->encode($data);
}
public function decode($raw) {
return $this->storage
->decode($raw);
}
public function listAll($prefix = '') {
$names = $this->storage
->listAll($prefix);
$additional_names = [];
if ($prefix === '') {
$additional_names = array_keys($this->replacementData[$this->collection]);
}
else {
foreach (array_keys($this->replacementData[$this->collection]) as $name) {
if (strpos($name, $prefix) === 0) {
$additional_names[] = $name;
}
}
}
if (!empty($additional_names)) {
$names = array_unique(array_merge($names, $additional_names));
}
return $names;
}
public function deleteAll($prefix = '') {
if ($prefix === '') {
$this->replacementData[$this->collection] = [];
}
else {
foreach (array_keys($this->replacementData[$this->collection]) as $name) {
if (strpos($name, $prefix) === 0) {
unset($this->replacementData[$this->collection][$name]);
}
}
}
return $this->storage
->deleteAll($prefix);
}
public function createCollection($collection) {
return new static($this->storage
->createCollection($collection), $collection);
}
public function getAllCollectionNames() {
return $this->storage
->getAllCollectionNames();
}
public function getCollectionName() {
return $this->collection;
}
public function replaceData($name, array $data) {
$this->replacementData[$this->collection][$name] = $data;
return $this;
}
}