Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\config;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\StorageInterface;
|
Chris@0
|
6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Wraps a configuration storage to allow replacing specific configuration data.
|
Chris@0
|
10 */
|
Chris@0
|
11 class StorageReplaceDataWrapper implements StorageInterface {
|
Chris@0
|
12 use DependencySerializationTrait;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * The configuration storage to be wrapped.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var \Drupal\Core\Config\StorageInterface
|
Chris@0
|
18 */
|
Chris@0
|
19 protected $storage;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The configuration replacement data, keyed by configuration object name.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $replacementData = [];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * The storage collection.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var string
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $collection;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Constructs a new StorageReplaceDataWrapper.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @param \Drupal\Core\Config\StorageInterface $storage
|
Chris@0
|
39 * A configuration storage to be used to read and write configuration.
|
Chris@0
|
40 * @param string $collection
|
Chris@0
|
41 * (optional) The collection to store configuration in. Defaults to the
|
Chris@0
|
42 * default collection.
|
Chris@0
|
43 */
|
Chris@0
|
44 public function __construct(StorageInterface $storage, $collection = StorageInterface::DEFAULT_COLLECTION) {
|
Chris@0
|
45 $this->storage = $storage;
|
Chris@0
|
46 $this->collection = $collection;
|
Chris@0
|
47 $this->replacementData[$collection] = [];
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 public function exists($name) {
|
Chris@0
|
54 return isset($this->replacementData[$this->collection][$name]) || $this->storage->exists($name);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public function read($name) {
|
Chris@0
|
61 if (isset($this->replacementData[$this->collection][$name])) {
|
Chris@0
|
62 return $this->replacementData[$this->collection][$name];
|
Chris@0
|
63 }
|
Chris@0
|
64 return $this->storage->read($name);
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * {@inheritdoc}
|
Chris@0
|
69 */
|
Chris@0
|
70 public function readMultiple(array $names) {
|
Chris@0
|
71 $data = $this->storage->readMultiple(($names));
|
Chris@0
|
72 foreach ($names as $name) {
|
Chris@0
|
73 if (isset($this->replacementData[$this->collection][$name])) {
|
Chris@0
|
74 $data[$name] = $this->replacementData[$this->collection][$name];
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77 return $data;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * {@inheritdoc}
|
Chris@0
|
82 */
|
Chris@0
|
83 public function write($name, array $data) {
|
Chris@0
|
84 if (isset($this->replacementData[$this->collection][$name])) {
|
Chris@0
|
85 unset($this->replacementData[$this->collection][$name]);
|
Chris@0
|
86 }
|
Chris@0
|
87 return $this->storage->write($name, $data);
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * {@inheritdoc}
|
Chris@0
|
92 */
|
Chris@0
|
93 public function delete($name) {
|
Chris@0
|
94 if (isset($this->replacementData[$this->collection][$name])) {
|
Chris@0
|
95 unset($this->replacementData[$this->collection][$name]);
|
Chris@0
|
96 }
|
Chris@0
|
97 return $this->storage->delete($name);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * {@inheritdoc}
|
Chris@0
|
102 */
|
Chris@0
|
103 public function rename($name, $new_name) {
|
Chris@0
|
104 if (isset($this->replacementData[$this->collection][$name])) {
|
Chris@0
|
105 $this->replacementData[$this->collection][$new_name] = $this->replacementData[$this->collection][$name];
|
Chris@0
|
106 unset($this->replacementData[$this->collection][$name]);
|
Chris@0
|
107 }
|
Chris@0
|
108 return $this->storage->rename($name, $new_name);
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * {@inheritdoc}
|
Chris@0
|
113 */
|
Chris@0
|
114 public function encode($data) {
|
Chris@0
|
115 return $this->storage->encode($data);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function decode($raw) {
|
Chris@0
|
122 return $this->storage->decode($raw);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function listAll($prefix = '') {
|
Chris@0
|
129 $names = $this->storage->listAll($prefix);
|
Chris@0
|
130 $additional_names = [];
|
Chris@0
|
131 if ($prefix === '') {
|
Chris@0
|
132 $additional_names = array_keys($this->replacementData[$this->collection]);
|
Chris@0
|
133 }
|
Chris@0
|
134 else {
|
Chris@0
|
135 foreach (array_keys($this->replacementData[$this->collection]) as $name) {
|
Chris@0
|
136 if (strpos($name, $prefix) === 0) {
|
Chris@0
|
137 $additional_names[] = $name;
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|
Chris@0
|
140 }
|
Chris@0
|
141 if (!empty($additional_names)) {
|
Chris@0
|
142 $names = array_unique(array_merge($names, $additional_names));
|
Chris@0
|
143 }
|
Chris@0
|
144 return $names;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * {@inheritdoc}
|
Chris@0
|
149 */
|
Chris@0
|
150 public function deleteAll($prefix = '') {
|
Chris@0
|
151 if ($prefix === '') {
|
Chris@0
|
152 $this->replacementData[$this->collection] = [];
|
Chris@0
|
153 }
|
Chris@0
|
154 else {
|
Chris@0
|
155 foreach (array_keys($this->replacementData[$this->collection]) as $name) {
|
Chris@0
|
156 if (strpos($name, $prefix) === 0) {
|
Chris@0
|
157 unset($this->replacementData[$this->collection][$name]);
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161 return $this->storage->deleteAll($prefix);
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * {@inheritdoc}
|
Chris@0
|
166 */
|
Chris@0
|
167 public function createCollection($collection) {
|
Chris@0
|
168 return new static(
|
Chris@0
|
169 $this->storage->createCollection($collection),
|
Chris@0
|
170 $collection
|
Chris@0
|
171 );
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * {@inheritdoc}
|
Chris@0
|
176 */
|
Chris@0
|
177 public function getAllCollectionNames() {
|
Chris@0
|
178 return $this->storage->getAllCollectionNames();
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * {@inheritdoc}
|
Chris@0
|
183 */
|
Chris@0
|
184 public function getCollectionName() {
|
Chris@0
|
185 return $this->collection;
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Replaces the configuration object data with the supplied data.
|
Chris@0
|
190 *
|
Chris@0
|
191 * @param $name
|
Chris@0
|
192 * The configuration object name whose data to replace.
|
Chris@0
|
193 * @param array $data
|
Chris@0
|
194 * The configuration data.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @return $this
|
Chris@0
|
197 */
|
Chris@0
|
198 public function replaceData($name, array $data) {
|
Chris@0
|
199 $this->replacementData[$this->collection][$name] = $data;
|
Chris@0
|
200 return $this;
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 }
|