|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
|
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Vaimo\ComposerPatches\Utils; |
|
7
|
|
|
|
|
8
|
|
|
use Vaimo\ComposerPatches\Config; |
|
9
|
|
|
|
|
10
|
|
|
class ConfigUtils |
|
11
|
|
|
{ |
|
12
|
|
|
public function mergeApplierConfig(array $config, array $updates) |
|
13
|
|
|
{ |
|
14
|
|
|
$config = $this->mergeAppliers($config, $updates); |
|
15
|
|
|
$config = $this->mergeSources($config, $updates); |
|
16
|
|
|
$config = $this->mergeCustomFailures($config, $updates); |
|
17
|
|
|
$config = $this->mergeSequence($config, $updates); |
|
18
|
|
|
|
|
19
|
|
|
$this->overrideValue($config, $updates, Config::PATCHER_LEVELS); |
|
20
|
|
|
$this->overrideValue($config, $updates, Config::PATCHER_SECURE_HTTP); |
|
21
|
|
|
$this->overrideValue($config, $updates, Config::PATCHER_FORCE_RESET); |
|
22
|
|
|
$this->overrideValue($config, $updates, Config::PATCHER_GRACEFUL); |
|
23
|
|
|
$this->mergeArrayValue($config, $updates, Config::PATCHER_OPERATIONS); |
|
24
|
|
|
|
|
25
|
|
|
return $config; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function mergeSequence(array $config, array $updates) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->mergeArrayValue($config, $updates, Config::PATCHER_SEQUENCE); |
|
31
|
|
|
|
|
32
|
|
|
foreach (array_keys($config[Config::PATCHER_SEQUENCE]) as $sequenceName) { |
|
33
|
|
|
$origin = strtok($sequenceName, ':'); |
|
34
|
|
|
|
|
35
|
|
|
if ($origin === $sequenceName) { |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$config[$sequenceName] = $config[$origin]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $config; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function mergeSources(array $config, array $updates) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!isset($updates[Config::PATCHER_SOURCES])) { |
|
48
|
|
|
return $config; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$config[Config::PATCHER_SOURCES] = $updates[Config::PATCHER_SOURCES] |
|
52
|
|
|
? array_replace($config[Config::PATCHER_SOURCES], $updates[Config::PATCHER_SOURCES]) |
|
53
|
|
|
: array(); |
|
54
|
|
|
|
|
55
|
|
|
return $config; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function mergeCustomFailures(array $config, array $updates) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!isset($updates[Config::PATCHER_FAILURES])) { |
|
61
|
|
|
return $config; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
foreach ($updates[Config::PATCHER_FAILURES] as $code => $patterns) { |
|
65
|
|
|
$config[Config::PATCHER_FAILURES][$code] = array_replace( |
|
66
|
|
|
$config[Config::PATCHER_FAILURES][$code], |
|
67
|
|
|
$patterns |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $config; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function mergeAppliers(array $config, array $updates) |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($config[Config::PATCHER_APPLIERS] as $code => $operations) { |
|
77
|
|
|
if (!isset($updates[Config::PATCHER_APPLIERS][$code])) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
foreach ($updates[Config::PATCHER_APPLIERS][$code] as $operationCode => $operationConfig) { |
|
82
|
|
|
if (!is_array($operations[$operationCode])) { |
|
83
|
|
|
$operations[$operationCode] = array( |
|
84
|
|
|
'default' => $operations[$operationCode] |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!is_array($operationConfig)) { |
|
89
|
|
|
$operationConfig = array( |
|
90
|
|
|
'default' => $operationConfig |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$operations[$operationCode] = array_filter( |
|
95
|
|
|
array_replace($operations[$operationCode], $operationConfig) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$config[Config::PATCHER_APPLIERS][$code] = $operations; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $config; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function overrideValue(&$config, $update, $key) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!isset($update[$key])) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$config[$key] = $update[$key]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function mergeArrayValue(&$config, $update, $key) |
|
115
|
|
|
{ |
|
116
|
|
|
$config[$key] = array_replace( |
|
117
|
|
|
$config[$key], |
|
118
|
|
|
isset($update[$key]) ? $update[$key] : array() |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function sortApplierConfig(array $config) |
|
123
|
|
|
{ |
|
124
|
|
|
$sequences = $config[Config::PATCHER_SEQUENCE]; |
|
125
|
|
|
|
|
126
|
|
|
$sequencedConfigItems = array_keys($sequences); |
|
127
|
|
|
|
|
128
|
|
|
foreach ($sequencedConfigItems as $item) { |
|
129
|
|
|
if (!isset($config[$item])) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$config[$item] = array_replace( |
|
134
|
|
|
array_flip($sequences[$item]), |
|
135
|
|
|
array_intersect_key( |
|
136
|
|
|
$config[$item], |
|
137
|
|
|
array_flip($sequences[$item]) |
|
138
|
|
|
) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $config; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function validateConfig(array $config) |
|
146
|
|
|
{ |
|
147
|
|
|
$patchers = $this->extractArrayValue($config, Config::PATCHER_APPLIERS); |
|
148
|
|
|
|
|
149
|
|
|
$sequenceConfig = $config[Config::PATCHER_SEQUENCE]; |
|
150
|
|
|
|
|
151
|
|
|
$patcherSequence = array_filter( |
|
152
|
|
|
$this->extractArrayValue($sequenceConfig, Config::PATCHER_APPLIERS) |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
if ((empty($patcherSequence) || array_intersect_key($patchers, array_flip($patcherSequence))) |
|
156
|
|
|
&& is_array($patchers) && array_filter($patchers) |
|
157
|
|
|
) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$message = 'No valid patchers defined'; |
|
162
|
|
|
|
|
163
|
|
|
if (!empty($patcherSequence)) { |
|
164
|
|
|
$message = sprintf('No valid patchers found for sequence: %s', implode(',', $patcherSequence)); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
throw new \Vaimo\ComposerPatches\Exceptions\ConfigValidationException($message); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
private function extractArrayValue($data, $key) |
|
171
|
|
|
{ |
|
172
|
|
|
return isset($data[$key]) ? $data[$key] : array(); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|