-
-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathConfigValidator.php
More file actions
242 lines (233 loc) · 10.8 KB
/
ConfigValidator.php
File metadata and controls
242 lines (233 loc) · 10.8 KB
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
<?php
declare(strict_types=1);
namespace SPC\util;
use SPC\exception\ValidationException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
class ConfigValidator
{
/**
* 验证 source.json
*
* @param array $data source.json 加载后的数据
* @throws ValidationException
*/
public static function validateSource(array $data): void
{
foreach ($data as $name => $src) {
isset($src['type']) || throw new ValidationException("source {$name} must have prop: [type]");
is_string($src['type']) || throw new ValidationException("source {$name} type prop must be string");
in_array($src['type'], ['filelist', 'git', 'ghtagtar', 'ghtar', 'ghrel', 'url', 'custom']) || throw new ValidationException("source {$name} type [{$src['type']}] is invalid");
switch ($src['type']) {
case 'filelist':
isset($src['url'], $src['regex']) || throw new ValidationException("source {$name} needs [url] and [regex] props");
is_string($src['url']) && is_string($src['regex']) || throw new ValidationException("source {$name} [url] and [regex] must be string");
break;
case 'git':
isset($src['url'], $src['rev']) || throw new ValidationException("source {$name} needs [url] and [rev] props");
is_string($src['url']) && is_string($src['rev']) || throw new ValidationException("source {$name} [url] and [rev] must be string");
is_string($src['path'] ?? '') || throw new ValidationException("source {$name} [path] must be string");
break;
case 'ghtagtar':
case 'ghtar':
isset($src['repo']) || throw new ValidationException("source {$name} needs [repo] prop");
is_string($src['repo']) || throw new ValidationException("source {$name} [repo] must be string");
is_string($src['path'] ?? '') || throw new ValidationException("source {$name} [path] must be string");
break;
case 'ghrel':
isset($src['repo'], $src['match']) || throw new ValidationException("source {$name} needs [repo] and [match] props");
is_string($src['repo']) && is_string($src['match']) || throw new ValidationException("source {$name} [repo] and [match] must be string");
break;
case 'url':
isset($src['url']) || throw new ValidationException("source {$name} needs [url] prop");
is_string($src['url']) || throw new ValidationException("source {$name} [url] must be string");
break;
}
}
}
/**
* @throws ValidationException
*/
public static function validateLibs(mixed $data, array $source_data = []): void
{
// check if it is an array
if (!is_array($data)) {
throw new ValidationException('lib.json is broken');
}
// check each lib
foreach ($data as $name => $lib) {
// check if lib is an assoc array
if (!is_assoc_array($lib)) {
throw new ValidationException("lib {$name} is not an object");
}
// check if lib has valid type
if (!in_array($lib['type'] ?? 'lib', ['lib', 'package', 'target', 'root'])) {
throw new ValidationException("lib {$name} type is invalid");
}
// check if lib and package has source
if (in_array($lib['type'] ?? 'lib', ['lib', 'package']) && !isset($lib['source'])) {
throw new ValidationException("lib {$name} does not assign any source");
}
// check if source is valid
if (isset($lib['source']) && !empty($source_data) && !isset($source_data[$lib['source']])) {
throw new ValidationException("lib {$name} assigns an invalid source: {$lib['source']}");
}
// check if [lib-depends|lib-suggests|static-libs][-windows|-unix|-macos|-linux] are valid list array
$suffixes = ['', '-windows', '-unix', '-macos', '-linux'];
foreach ($suffixes as $suffix) {
if (isset($lib['lib-depends' . $suffix]) && !is_list_array($lib['lib-depends' . $suffix])) {
throw new ValidationException("lib {$name} lib-depends must be a list");
}
if (isset($lib['lib-suggests' . $suffix]) && !is_list_array($lib['lib-suggests' . $suffix])) {
throw new ValidationException("lib {$name} lib-suggests must be a list");
}
if (isset($lib['static-libs' . $suffix]) && !is_list_array($lib['static-libs' . $suffix])) {
throw new ValidationException("lib {$name} static-libs must be a list");
}
}
// check if frameworks is a list array
if (isset($lib['frameworks']) && !is_list_array($lib['frameworks'])) {
throw new ValidationException("lib {$name} frameworks must be a list");
}
}
}
/**
* @throws ValidationException
*/
public static function validateExts(mixed $data): void
{
is_array($data) || throw new ValidationException('ext.json is broken');
}
/**
* @throws ValidationException
*/
public static function validatePkgs(mixed $data): void
{
is_array($data) || throw new ValidationException('pkg.json is broken');
}
/**
* @param mixed $craft_file craft.yml path
* @param Command $command craft command instance
* @return array{
* php-version?: string,
* extensions: array<string>,
* shared-extensions?: array<string>,
* libs?: array<string>,
* sapi: array<string>,
* debug?: bool,
* clean-build?: bool,
* build-options?: array<string, mixed>,
* download-options?: array<string, mixed>,
* extra-env?: array<string, string>,
* craft-options?: array{
* doctor?: bool,
* download?: bool,
* build?: bool
* }
* }
* @throws ValidationException
*/
public static function validateAndParseCraftFile(mixed $craft_file, Command $command): array
{
$build_options = $command->getApplication()->find('build')->getDefinition()->getOptions();
$download_options = $command->getApplication()->find('download')->getDefinition()->getOptions();
try {
$craft = Yaml::parse(file_get_contents($craft_file));
} catch (ParseException $e) {
throw new ValidationException('Craft file is broken: ' . $e->getMessage());
}
if (!is_assoc_array($craft)) {
throw new ValidationException('Craft file is broken');
}
// check php-version
if (isset($craft['php-version'])) {
// validdate version, accept 8.x, 7.x, 8.x.x, 7.x.x, 8, 7
$version = strval($craft['php-version']);
if (!preg_match('/^(\d+)(\.\d+)?(\.\d+)?$/', $version, $matches)) {
throw new ValidationException('Craft file php-version is invalid');
}
}
// check extensions
if (!isset($craft['extensions'])) {
throw new ValidationException('Craft file must have extensions');
}
if (is_string($craft['extensions'])) {
$craft['extensions'] = array_filter(array_map(fn ($x) => trim($x), explode(',', $craft['extensions'])));
}
if (!isset($craft['shared-extensions'])) {
$craft['shared-extensions'] = [];
}
if (is_string($craft['shared-extensions'] ?? [])) {
$craft['shared-extensions'] = array_filter(array_map(fn ($x) => trim($x), explode(',', $craft['shared-extensions'])));
}
// check libs
if (isset($craft['libs']) && is_string($craft['libs'])) {
$craft['libs'] = array_filter(array_map(fn ($x) => trim($x), explode(',', $craft['libs'])));
} elseif (!isset($craft['libs'])) {
$craft['libs'] = [];
}
// check sapi
if (!isset($craft['sapi'])) {
throw new ValidationException('Craft file must have sapi');
}
if (is_string($craft['sapi'])) {
$craft['sapi'] = array_filter(array_map(fn ($x) => trim($x), explode(',', $craft['sapi'])));
}
// debug as boolean
if (isset($craft['debug'])) {
$craft['debug'] = filter_var($craft['debug'], FILTER_VALIDATE_BOOLEAN);
} else {
$craft['debug'] = false;
}
// check clean-build
$craft['clean-build'] ??= false;
// check build-options
if (isset($craft['build-options'])) {
if (!is_assoc_array($craft['build-options'])) {
throw new ValidationException('Craft file build-options must be an object');
}
foreach ($craft['build-options'] as $key => $value) {
if (!isset($build_options[$key])) {
throw new ValidationException("Craft file build-options {$key} is invalid");
}
// check an array
if ($build_options[$key]->isArray() && !is_array($value)) {
throw new ValidationException("Craft file build-options {$key} must be an array");
}
}
} else {
$craft['build-options'] = [];
}
// check download options
if (isset($craft['download-options'])) {
if (!is_assoc_array($craft['download-options'])) {
throw new ValidationException('Craft file download-options must be an object');
}
foreach ($craft['download-options'] as $key => $value) {
if (!isset($download_options[$key])) {
throw new ValidationException("Craft file download-options {$key} is invalid");
}
// check an array
if ($download_options[$key]->isArray() && !is_array($value)) {
throw new ValidationException("Craft file download-options {$key} must be an array");
}
}
} else {
$craft['download-options'] = [];
}
// check extra-env
if (isset($craft['extra-env'])) {
if (!is_assoc_array($craft['extra-env'])) {
throw new ValidationException('Craft file extra-env must be an object');
}
} else {
$craft['extra-env'] = [];
}
// check craft-options
$craft['craft-options']['doctor'] ??= true;
$craft['craft-options']['download'] ??= true;
$craft['craft-options']['build'] ??= true;
return $craft;
}
}