/
DeltaConfigCollection.php
287 lines (255 loc) · 7.19 KB
/
DeltaConfigCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace SilverStripe\Config\Collections;
use SilverStripe\Config\Middleware\DeltaMiddleware;
/**
* Applies config modifications as a set of deltas on top of the
* middleware, instead of as modifications to the underlying list.
*/
class DeltaConfigCollection extends MemoryConfigCollection
{
/**
* Remove delta
*/
const REMOVE = 'remove';
/**
* Merge delta
*/
const MERGE = 'merge';
/**
* Remove all config for this class
*/
const CLEAR = 'clear';
/**
* Replace all config for this class
*/
const REPLACE = 'replace';
/**
* Set delta
*/
const SET = 'set';
/**
* @var DeltaMiddleware
*/
protected $deltaMiddleware = null;
/**
* List of deltas keyed by class
*
* @var array
*/
protected $deltas = [];
/**
* True if removeAll() is applied
*
* @var bool
*/
protected $deltaReset = false;
/**
* Construct a delta collection
*
* @param bool $trackMetadata Set to true to track metadata
* @param int $middlewareFlag Flag to use to disable delta middleware (optional)
*/
public function __construct($trackMetadata = false, $middlewareFlag = 0)
{
parent::__construct($trackMetadata);
$this->deltaMiddleware = new DeltaMiddleware($this, $middlewareFlag);
$this->postInit();
}
/**
* Create a delta collection from a parent collection
*
* @param ConfigCollectionInterface $parent
* @param int $middlewareFlag Flag to use to disable delta middleware (optional)
* @return static
*/
public static function createFromCollection(ConfigCollectionInterface $parent, $middlewareFlag = 0)
{
// Copy properties to subclass
$collection = new static();
foreach (get_object_vars($parent) as $key => $value) {
$collection->$key = $value;
}
// Set middleware flag
$collection
->getDeltaMiddleware()
->setDisableFlag($middlewareFlag);
// Ensure back-links are re-created between middleware and collection
$collection->postInit();
return $collection;
}
/**
* Get middleware for handling deltas
*
* @return DeltaMiddleware
*/
public function getDeltaMiddleware()
{
return $this->deltaMiddleware;
}
public function getMiddlewares()
{
$middlewares = parent::getMiddlewares();
array_unshift($middlewares, $this->getDeltaMiddleware());
return $middlewares;
}
/**
* Get deltas for the given class
*
* @param string $class
* @return array
*/
public function getDeltas($class)
{
$classKey = strtolower($class ?? '');
return isset($this->deltas[$classKey])
? $this->deltas[$classKey]
: [];
}
/**
* Check if config should be completely reset before getting config
*
* @param string $class Class to check
* @return bool
*/
public function isDeltaReset($class = null)
{
// Global reset
if ($this->deltaReset) {
return true;
}
// Note: Deltas of the given type reset the prior config
$deltas = $this->getDeltas($class);
return isset($deltas[0]['type'])
&& in_array(
$deltas[0]['type'],
[self::REPLACE, self::CLEAR]
);
}
public function __unserialize(array $data): void
{
parent::__unserialize($data);
$this->postInit();
}
public function __clone()
{
$this->deltaMiddleware = clone $this->deltaMiddleware;
$this->postInit();
}
/**
* Restore back-links post-creation or unserialization
*/
protected function postInit()
{
// Ensure backlink to middleware
$this->getDeltaMiddleware()->setCollection($this);
}
public function set(string $class, ?string $name, mixed $data, array $metadata = []): static
{
// Check config to merge
$this->clearDeltas($class, $name);
if ($name) {
$this->addDelta($class, [
'type' => self::SET,
'config' => [$name => $data],
]);
} else {
$this->addDelta($class, [
'type' => self::REPLACE,
'config' => $data,
]);
}
return $this;
}
public function remove(string $class, ?string $name = null): static
{
// Check config to merge
$this->clearDeltas($class, $name);
if ($name) {
$this->addDelta($class, [
'type' => self::REMOVE,
'config' => [$name => true],
]);
} else {
$this->addDelta($class, [
'type' => self::CLEAR,
]);
}
return $this;
}
public function merge(string $class, string|null $name, array $value): static
{
// Check config to merge
if ($name) {
$config = [$name => $value];
} else {
$config = $value;
}
$this->addDelta($class, [
'type' => self::MERGE,
'config' => $config,
]);
return $this;
}
public function removeAll()
{
$this->deltaReset = true;
$this->clearDeltas();
}
/**
* Remove all deltas for the given class and/or key combination
*
* @param string $class Optional class to limit clear to
* @param string $key Optional field to limit clear to
*/
protected function clearDeltas($class = null, $key = null)
{
$this->callCache = [];
// Clear all classes
if (!$class) {
$this->deltas = [];
return;
}
// Clear just one class
$classKey = strtolower($class ?? '');
if (!$key) {
unset($this->deltas[$classKey]);
return;
}
// Filter deltas that would be ignored by this key.value replacement
if (!isset($this->deltas[$classKey])) {
return;
}
$this->deltas[$classKey] = array_filter(
$this->deltas[$classKey] ?? [],
function ($delta) use ($key) {
// Clear if an array with exactly one element, with the key
// being the affected config property
return !isset($delta['config'])
|| !is_array($delta['config'])
|| (count($delta['config'] ?? []) !== 1)
|| !isset($delta['config'][$key]);
}
);
}
/**
* Push new delta
*
* @param string $class
* @param array $delta
*/
protected function addDelta($class, $delta)
{
if (is_array($delta['config'] ?? null)) {
foreach (array_keys($delta['config']) as $configKey) {
$this->checkForDeprecatedConfig($class, $configKey);
}
}
$classKey = strtolower($class ?? '');
if (!isset($this->deltas[$classKey])) {
$this->deltas[$classKey] = [];
}
$this->deltas[$classKey][] = $delta;
// Flush call cache
$this->callCache = [];
}
}