3 namespace Drupal\config;
5 use Drupal\Core\Config\StorageInterface;
6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
9 * Wraps a configuration storage to allow replacing specific configuration data.
11 class StorageReplaceDataWrapper implements StorageInterface {
12 use DependencySerializationTrait;
15 * The configuration storage to be wrapped.
17 * @var \Drupal\Core\Config\StorageInterface
22 * The configuration replacement data, keyed by configuration object name.
26 protected $replacementData = [];
29 * The storage collection.
33 protected $collection;
36 * Constructs a new StorageReplaceDataWrapper.
38 * @param \Drupal\Core\Config\StorageInterface $storage
39 * A configuration storage to be used to read and write configuration.
40 * @param string $collection
41 * (optional) The collection to store configuration in. Defaults to the
44 public function __construct(StorageInterface $storage, $collection = StorageInterface::DEFAULT_COLLECTION) {
45 $this->storage = $storage;
46 $this->collection = $collection;
47 $this->replacementData[$collection] = [];
53 public function exists($name) {
54 return isset($this->replacementData[$this->collection][$name]) || $this->storage->exists($name);
60 public function read($name) {
61 if (isset($this->replacementData[$this->collection][$name])) {
62 return $this->replacementData[$this->collection][$name];
64 return $this->storage->read($name);
70 public function readMultiple(array $names) {
71 $data = $this->storage->readMultiple(($names));
72 foreach ($names as $name) {
73 if (isset($this->replacementData[$this->collection][$name])) {
74 $data[$name] = $this->replacementData[$this->collection][$name];
83 public function write($name, array $data) {
84 if (isset($this->replacementData[$this->collection][$name])) {
85 unset($this->replacementData[$this->collection][$name]);
87 return $this->storage->write($name, $data);
93 public function delete($name) {
94 if (isset($this->replacementData[$this->collection][$name])) {
95 unset($this->replacementData[$this->collection][$name]);
97 return $this->storage->delete($name);
103 public function rename($name, $new_name) {
104 if (isset($this->replacementData[$this->collection][$name])) {
105 $this->replacementData[$this->collection][$new_name] = $this->replacementData[$this->collection][$name];
106 unset($this->replacementData[$this->collection][$name]);
108 return $this->storage->rename($name, $new_name);
114 public function encode($data) {
115 return $this->storage->encode($data);
121 public function decode($raw) {
122 return $this->storage->decode($raw);
128 public function listAll($prefix = '') {
129 $names = $this->storage->listAll($prefix);
130 $additional_names = [];
131 if ($prefix === '') {
132 $additional_names = array_keys($this->replacementData[$this->collection]);
135 foreach (array_keys($this->replacementData[$this->collection]) as $name) {
136 if (strpos($name, $prefix) === 0) {
137 $additional_names[] = $name;
141 if (!empty($additional_names)) {
142 $names = array_unique(array_merge($names, $additional_names));
150 public function deleteAll($prefix = '') {
151 if ($prefix === '') {
152 $this->replacementData[$this->collection] = [];
155 foreach (array_keys($this->replacementData[$this->collection]) as $name) {
156 if (strpos($name, $prefix) === 0) {
157 unset($this->replacementData[$this->collection][$name]);
161 return $this->storage->deleteAll($prefix);
167 public function createCollection($collection) {
169 $this->storage->createCollection($collection),
177 public function getAllCollectionNames() {
178 return $this->storage->getAllCollectionNames();
184 public function getCollectionName() {
185 return $this->collection;
189 * Replaces the configuration object data with the supplied data.
192 * The configuration object name whose data to replace.
194 * The configuration data.
198 public function replaceData($name, array $data) {
199 $this->replacementData[$this->collection][$name] = $data;