1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Config\Middleware; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Config\Collections\DeltaConfigCollection; |
7
|
|
|
use SilverStripe\Config\MergeStrategy\Priority; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Applies a set of user-customised modifications to config |
11
|
|
|
*/ |
12
|
|
|
class DeltaMiddleware implements Middleware |
13
|
|
|
{ |
14
|
|
|
use MiddlewareCommon; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Source for deltas |
18
|
|
|
* |
19
|
|
|
* @var DeltaConfigCollection |
20
|
|
|
*/ |
21
|
|
|
protected $collection = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* DeltaMiddleware constructor. |
25
|
|
|
* |
26
|
|
|
* @param DeltaConfigCollection $collection |
27
|
|
|
* @param int $disableFlag |
28
|
|
|
*/ |
29
|
4 |
|
public function __construct(DeltaConfigCollection $collection, $disableFlag = 0) |
30
|
|
|
{ |
31
|
4 |
|
$this->setCollection($collection); |
32
|
4 |
|
$this->setDisableFlag($disableFlag); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return DeltaConfigCollection |
37
|
|
|
*/ |
38
|
4 |
|
public function getCollection() |
39
|
|
|
{ |
40
|
4 |
|
return $this->collection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param DeltaConfigCollection $collection |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
4 |
|
public function setCollection(DeltaConfigCollection $collection) |
48
|
|
|
{ |
49
|
4 |
|
$this->collection = $collection; |
50
|
4 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get config for a class |
55
|
|
|
* |
56
|
|
|
* @param string $class Name of class |
57
|
|
|
* @param int|true $excludeMiddleware Middleware disable flags |
58
|
|
|
* @param callable $next Callback to next middleware |
59
|
|
|
* @return array Complete class config |
60
|
|
|
*/ |
61
|
4 |
|
public function getClassConfig($class, $excludeMiddleware, $next) |
62
|
|
|
{ |
63
|
|
|
// Check if enabled |
64
|
4 |
|
$enabled = $this->enabled($excludeMiddleware); |
65
|
4 |
|
if (!$enabled) { |
66
|
|
|
return $next($class, $excludeMiddleware); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Note: If config is reset, we don't need to call parent config |
70
|
4 |
|
if ($this->getCollection()->isDeltaReset($class)) { |
71
|
3 |
|
$config = []; |
72
|
3 |
|
} else { |
73
|
3 |
|
$config = $next($class, $excludeMiddleware); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Apply all deltas |
77
|
4 |
|
$deltas = $this->getCollection()->getDeltas($class); |
78
|
4 |
|
foreach ($deltas as $delta) { |
79
|
3 |
|
$config = $this->applyDelta($config, $delta); |
80
|
4 |
|
} |
81
|
4 |
|
return $config; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Apply a single delta to a class config |
86
|
|
|
* |
87
|
|
|
* @param array $config |
88
|
|
|
* @param array $delta |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
3 |
|
protected function applyDelta($config, $delta) |
92
|
|
|
{ |
93
|
3 |
|
switch ($delta['type']) { |
94
|
3 |
|
case DeltaConfigCollection::SET: |
95
|
1 |
|
return array_merge($config, $delta['config']); |
96
|
3 |
|
case DeltaConfigCollection::MERGE: |
97
|
2 |
|
return Priority::mergeArray($delta['config'], $config); |
98
|
2 |
|
case DeltaConfigCollection::CLEAR: |
99
|
1 |
|
return []; |
100
|
2 |
|
case DeltaConfigCollection::REPLACE: |
101
|
1 |
|
return $delta['config']; |
102
|
1 |
|
case DeltaConfigCollection::REMOVE: |
103
|
1 |
|
return array_diff_key($config, $delta['config']); |
104
|
|
|
default: |
105
|
|
|
throw new InvalidArgumentException("Invalid delta " . $delta['type']); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|