|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Config\MergeStrategy; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Config\Collections\MutableConfigCollectionInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Priority |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Merges an array of values into a collection |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $mine Map of key to array with value and metadata sub-keys |
|
13
|
|
|
* @param MutableConfigCollectionInterface $theirs |
|
14
|
|
|
* @return MutableConfigCollectionInterface |
|
15
|
|
|
*/ |
|
16
|
18 |
|
public static function merge(array $mine, MutableConfigCollectionInterface $theirs) |
|
17
|
|
|
{ |
|
18
|
18 |
|
foreach ($mine as $class => $item) { |
|
19
|
|
|
// Ensure we have value/metadata keys |
|
20
|
18 |
|
$item = static::normaliseItem($item); |
|
21
|
18 |
|
$value = $item['value']; |
|
22
|
18 |
|
$metadata = $item['metadata']; |
|
23
|
|
|
|
|
24
|
|
|
// If the item doesn't exist in theirs, we can just set it and continue. |
|
25
|
18 |
|
$theirValue = $theirs->get($class, null, true); |
|
|
|
|
|
|
26
|
18 |
|
if (!isset($theirValue)) { |
|
27
|
|
|
$theirs->set($class, null, $value, $metadata); |
|
28
|
|
|
continue; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// If its an array and the key already esists, we can use array_merge |
|
32
|
18 |
|
if (is_array($value) && is_array($theirValue)) { |
|
33
|
7 |
|
$value = static::mergeArray($value, $theirValue); |
|
34
|
7 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Preserve metadata |
|
37
|
18 |
|
if (!$metadata) { |
|
38
|
6 |
|
$theirMetadata = $theirs->getMetadata(); |
|
39
|
6 |
|
if (isset($theirMetadata[$class])) { |
|
40
|
|
|
$metadata = $theirMetadata[$class]; |
|
41
|
|
|
} |
|
42
|
6 |
|
} |
|
43
|
18 |
|
$theirs->set($class, null, $value, $metadata); |
|
44
|
18 |
|
} |
|
45
|
|
|
|
|
46
|
18 |
|
return $theirs; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Deep merges a high priorty array into a lower priority array, overwriting duplicate |
|
51
|
|
|
* keys. If the keys are integers, then the merges acts like array_merge() and adds a new |
|
52
|
|
|
* item. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $highPriority |
|
55
|
|
|
* @param array $lowPriority |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
9 |
|
public static function mergeArray(array $highPriority, array $lowPriority) |
|
60
|
|
|
{ |
|
61
|
9 |
|
foreach ($highPriority as $key => $value) { |
|
62
|
|
|
// If value isn't an array, we can overwrite whatever was before it |
|
63
|
9 |
|
if (!is_array($value)) { |
|
64
|
8 |
|
if (is_int($key)) { |
|
65
|
1 |
|
$lowPriority[] = $value; |
|
66
|
1 |
|
} else { |
|
67
|
7 |
|
$lowPriority[$key] = $value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// If not set, or we're changing type we can set low priority |
|
74
|
6 |
|
if (is_int($key) || !array_key_exists($key, $lowPriority) || !is_array($lowPriority[$key])) { |
|
75
|
2 |
|
if (is_int($key)) { |
|
76
|
|
|
$lowPriority[] = $value; |
|
77
|
|
|
} else { |
|
78
|
2 |
|
$lowPriority[$key] = $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// We have two arrays, so we merge |
|
85
|
4 |
|
$lowPriority[$key] = static::mergeArray($value, $lowPriority[$key]); |
|
86
|
9 |
|
} |
|
87
|
|
|
|
|
88
|
9 |
|
return $lowPriority; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns a normalised array with value/metadata keys |
|
93
|
|
|
* |
|
94
|
|
|
* @param array |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
18 |
|
protected static function normaliseItem(array $item) |
|
99
|
|
|
{ |
|
100
|
18 |
|
if (!isset($item['value'])) { |
|
101
|
|
|
$item['value'] = ''; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
18 |
|
if (!isset($item['metadata'])) { |
|
105
|
6 |
|
$item['metadata'] = []; |
|
106
|
6 |
|
} |
|
107
|
|
|
|
|
108
|
18 |
|
return ['value' => $item['value'], 'metadata' => $item['metadata']]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: