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