/
DeltaMiddleware.php
108 lines (97 loc) · 2.92 KB
/
DeltaMiddleware.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
<?php
namespace SilverStripe\Config\Middleware;
use InvalidArgumentException;
use SilverStripe\Config\Collections\DeltaConfigCollection;
use SilverStripe\Config\MergeStrategy\Priority;
/**
* Applies a set of user-customised modifications to config
*/
class DeltaMiddleware implements Middleware
{
use MiddlewareCommon;
/**
* Source for deltas
*
* @var DeltaConfigCollection
*/
protected $collection = null;
/**
* DeltaMiddleware constructor.
*
* @param DeltaConfigCollection $collection
* @param int $disableFlag
*/
public function __construct(DeltaConfigCollection $collection, $disableFlag = 0)
{
$this->setCollection($collection);
$this->setDisableFlag($disableFlag);
}
/**
* @return DeltaConfigCollection
*/
public function getCollection()
{
return $this->collection;
}
/**
* @param DeltaConfigCollection $collection
* @return $this
*/
public function setCollection(DeltaConfigCollection $collection)
{
$this->collection = $collection;
return $this;
}
/**
* Get config for a class
*
* @param string $class Name of class
* @param int|true $excludeMiddleware Middleware disable flags
* @param callable $next Callback to next middleware
* @return array Complete class config
*/
public function getClassConfig($class, $excludeMiddleware, $next)
{
// Check if enabled
$enabled = $this->enabled($excludeMiddleware);
if (!$enabled) {
return $next($class, $excludeMiddleware);
}
// Note: If config is reset, we don't need to call parent config
if ($this->getCollection()->isDeltaReset($class)) {
$config = [];
} else {
$config = $next($class, $excludeMiddleware);
}
// Apply all deltas
$deltas = $this->getCollection()->getDeltas($class);
foreach ($deltas as $delta) {
$config = $this->applyDelta($config, $delta);
}
return $config;
}
/**
* Apply a single delta to a class config
*
* @param array $config
* @param array $delta
* @return array
*/
protected function applyDelta($config, $delta)
{
switch ($delta['type']) {
case DeltaConfigCollection::SET:
return array_merge($config, $delta['config']);
case DeltaConfigCollection::MERGE:
return Priority::mergeArray($delta['config'], $config);
case DeltaConfigCollection::CLEAR:
return [];
case DeltaConfigCollection::REPLACE:
return $delta['config'];
case DeltaConfigCollection::REMOVE:
return array_diff_key($config ?? [], $delta['config']);
default:
throw new InvalidArgumentException("Invalid delta " . $delta['type']);
}
}
}