-
-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathConfigValidator.php
More file actions
376 lines (346 loc) · 15.5 KB
/
ConfigValidator.php
File metadata and controls
376 lines (346 loc) · 15.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
declare(strict_types=1);
namespace StaticPHP\Config;
use StaticPHP\Exception\ValidationException;
class ConfigValidator
{
/**
* Global field type definitions
* Maps field names to their expected types and validation rules
* Note: This only includes fields used in config files (source.json, lib.json, ext.json, pkg.json, pre-built.json)
*/
public const array PACKAGE_FIELD_TYPES = [
// package fields
'type' => ConfigType::STRING,
'depends' => ConfigType::LIST_ARRAY, // @
'suggests' => ConfigType::LIST_ARRAY, // @
'artifact' => ConfigType::STRING,
'license' => [ConfigType::class, 'validateLicenseField'],
'lang' => ConfigType::STRING,
'frameworks' => ConfigType::LIST_ARRAY, // @
// php-extension type fields
'php-extension' => ConfigType::ASSOC_ARRAY,
'zend-extension' => ConfigType::BOOL,
'support' => ConfigType::ASSOC_ARRAY,
'arg-type' => ConfigType::STRING,
'build-shared' => ConfigType::BOOL,
'build-static' => ConfigType::BOOL,
'build-with-php' => ConfigType::BOOL,
'notes' => ConfigType::BOOL,
// library and target fields
'headers' => ConfigType::LIST_ARRAY, // @
'static-libs' => ConfigType::LIST_ARRAY, // @
'pkg-configs' => ConfigType::LIST_ARRAY,
'static-bins' => ConfigType::LIST_ARRAY, // @
];
public const array PACKAGE_FIELDS = [
'type' => true,
'depends' => false, // @
'suggests' => false, // @
'artifact' => false,
'license' => false,
'lang' => false,
'frameworks' => false, // @
// php-extension type fields
'php-extension' => false,
// library and target fields
'headers' => false, // @
'static-libs' => false, // @
'pkg-configs' => false,
'static-bins' => false, // @
];
public const array SUFFIX_ALLOWED_FIELDS = [
'depends',
'suggests',
'headers',
'static-libs',
'static-bins',
];
public const array PHP_EXTENSION_FIELDS = [
'zend-extension' => false,
'support' => false,
'arg-type' => false, // @
'build-shared' => false,
'build-static' => false,
'build-with-php' => false,
'notes' => false,
];
public const array ARTIFACT_TYPE_FIELDS = [ // [required_fields, optional_fields]
'filelist' => [['url', 'regex'], ['extract']],
'git' => [['url', 'rev'], ['extract', 'submodules']],
'ghtagtar' => [['repo'], ['extract', 'prefer-stable', 'match']],
'ghtar' => [['repo'], ['extract', 'prefer-stable', 'match']],
'ghrel' => [['repo', 'match'], ['extract', 'prefer-stable']],
'url' => [['url'], ['filename', 'extract', 'version']],
'bitbuckettag' => [['repo'], ['extract']],
'local' => [['dirname'], ['extract']],
'pie' => [['repo'], ['extract']],
'php-release' => [[], ['extract']],
'custom' => [[], ['func']],
];
/**
* Validate and standardize artifacts configuration data.
*
* @param string $config_file_name Name of the configuration file (for error messages)
* @param mixed $data The configuration data to validate
*/
public static function validateAndLintArtifacts(string $config_file_name, mixed &$data): void
{
if (!is_array($data)) {
throw new ValidationException("{$config_file_name} is broken");
}
foreach ($data as $name => $artifact) {
foreach ($artifact as $k => $v) {
// check source field
if ($k === 'source' || $k === 'source-mirror') {
// source === custom is allowed
if ($v === 'custom') {
continue;
}
// expand string to url type (start with http:// or https://)
if (is_string($v) && (str_starts_with($v, 'http://') || str_starts_with($v, 'https://'))) {
$data[$name][$k] = [
'type' => 'url',
'url' => $v,
];
continue;
}
// source: object with type field
if (is_assoc_array($v)) {
self::validateArtifactObjectField($name, $v);
}
continue;
}
// check binary field
if ($k === 'binary') {
// binary === custom is allowed
if ($v === 'custom') {
$data[$name][$k] = [
'linux-x86_64' => ['type' => 'custom'],
'linux-aarch64' => ['type' => 'custom'],
'windows-x86_64' => ['type' => 'custom'],
'macos-x86_64' => ['type' => 'custom'],
'macos-aarch64' => ['type' => 'custom'],
];
continue;
}
if ($v === 'hosted') {
$data[$name][$k] = [
'linux-x86_64' => ['type' => 'hosted'],
'linux-aarch64' => ['type' => 'hosted'],
'windows-x86_64' => ['type' => 'hosted'],
'macos-x86_64' => ['type' => 'hosted'],
'macos-aarch64' => ['type' => 'hosted'],
];
continue;
}
if (is_assoc_array($v)) {
foreach ($v as $platform => $v_obj) {
self::validatePlatformString($platform);
// expand string to url type (start with http:// or https://)
if (is_string($v_obj) && (str_starts_with($v_obj, 'http://') || str_starts_with($v_obj, 'https://'))) {
$data[$name][$k][$platform] = [
'type' => 'url',
'url' => $v_obj,
];
continue;
}
// binary: object with type field
if (is_assoc_array($v_obj)) {
self::validateArtifactObjectField("{$name}::{$platform}", $v_obj);
}
}
}
}
}
}
}
/**
* Validate packages configuration data.
*
* @param string $config_file_name Name of the configuration file (for error messages)
* @param mixed $data The configuration data to validate
*/
public static function validateAndLintPackages(string $config_file_name, mixed &$data): void
{
if (!is_array($data)) {
throw new ValidationException("{$config_file_name} is broken");
}
foreach ($data as $name => $pkg) {
if (!is_assoc_array($pkg)) {
throw new ValidationException("Package [{$name}] in {$config_file_name} is not a valid associative array");
}
// check if package has valid type
if (!isset($pkg['type']) || !in_array($pkg['type'], ConfigType::PACKAGE_TYPES)) {
throw new ValidationException("Package [{$name}] in {$config_file_name} has invalid or missing 'type' field");
}
// validate basic fields using unified method
self::validatePackageFields($name, $pkg);
// validate list of suffix-allowed fields
$suffixes = ['', '@windows', '@unix', '@macos', '@linux'];
$fields = self::SUFFIX_ALLOWED_FIELDS;
self::validateSuffixAllowedFields($name, $pkg, $fields, $suffixes);
// check if "library|target" package has artifact field for target and library types
if (in_array($pkg['type'], ['target', 'library']) && !isset($pkg['artifact'])) {
throw new ValidationException("Package [{$name}] in {$config_file_name} of type '{$pkg['type']}' must have an 'artifact' field");
}
// check if "php-extension" package has php-extension specific fields and validate inside
if ($pkg['type'] === 'php-extension') {
self::validatePhpExtensionFields($name, $pkg);
}
// check for unknown fields
self::validateNoInvalidFields('package', $name, $pkg, array_keys(self::PACKAGE_FIELD_TYPES));
}
}
/**
* Validate platform string format.
*
* @param string $platform Platform string, like windows-x86_64
*/
public static function validatePlatformString(string $platform): void
{
$valid_platforms = ['windows', 'linux', 'macos'];
$valid_arch = ['x86_64', 'aarch64'];
$parts = explode('-', $platform);
if (count($parts) !== 2) {
throw new ValidationException("Invalid platform format '{$platform}', expected format 'os-arch'");
}
[$os, $arch] = $parts;
if (!in_array($os, $valid_platforms)) {
throw new ValidationException("Invalid platform OS '{$os}' in platform '{$platform}'");
}
if (!in_array($arch, $valid_arch)) {
throw new ValidationException("Invalid platform architecture '{$arch}' in platform '{$platform}'");
}
}
/**
* Validate an artifact download object field.
*
* @param string $item_name Artifact name (for error messages)
* @param array $data Artifact source object data
*/
private static function validateArtifactObjectField(string $item_name, array $data): void
{
if (!isset($data['type']) || !is_string($data['type'])) {
throw new ValidationException("Artifact source object must have a valid 'type' field");
}
$type = $data['type'];
if (!isset(self::ARTIFACT_TYPE_FIELDS[$type])) {
throw new ValidationException("Artifact source object has unknown type '{$type}'");
}
[$required_fields, $optional_fields] = self::ARTIFACT_TYPE_FIELDS[$type];
// check required fields
foreach ($required_fields as $field) {
if (!isset($data[$field])) {
throw new ValidationException("Artifact source object of type '{$type}' must have required field '{$field}'");
}
}
// check for unknown fields
$allowed_fields = array_merge(['type'], $required_fields, $optional_fields);
self::validateNoInvalidFields('artifact object', $item_name, $data, $allowed_fields);
}
/**
* Unified method to validate config fields based on field definitions
*
* @param string $package_name Package name
* @param mixed $pkg The package configuration array
*/
private static function validatePackageFields(string $package_name, mixed $pkg): void
{
foreach (self::PACKAGE_FIELDS as $field => $required) {
if ($required && !isset($pkg[$field])) {
throw new ValidationException("Package {$package_name} must have [{$field}] field");
}
if (isset($pkg[$field])) {
self::validatePackageFieldType($field, $pkg[$field], $package_name);
}
}
}
/**
* Validate a field based on its global type definition
*
* @param string $field Field name
* @param mixed $value Field value
* @param string $package_name Package name (for error messages)
*/
private static function validatePackageFieldType(string $field, mixed $value, string $package_name): void
{
// Check if field exists in FIELD_TYPES
if (!isset(self::PACKAGE_FIELD_TYPES[$field])) {
// Try to strip suffix and check base field name
$suffixes = ['@windows', '@unix', '@macos', '@linux'];
$base_field = $field;
foreach ($suffixes as $suffix) {
if (str_ends_with($field, $suffix)) {
$base_field = substr($field, 0, -strlen($suffix));
break;
}
}
if (!isset(self::PACKAGE_FIELD_TYPES[$base_field])) {
// Unknown field is not allowed - strict validation
throw new ValidationException("Package {$package_name} has unknown field [{$field}]");
}
// Use base field type for validation
$expected_type = self::PACKAGE_FIELD_TYPES[$base_field];
} else {
$expected_type = self::PACKAGE_FIELD_TYPES[$field];
}
match ($expected_type) {
ConfigType::STRING => is_string($value) ?: throw new ValidationException("Package {$package_name} [{$field}] must be string"),
ConfigType::BOOL => is_bool($value) ?: throw new ValidationException("Package {$package_name} [{$field}] must be boolean"),
ConfigType::LIST_ARRAY => is_list_array($value) ?: throw new ValidationException("Package {$package_name} [{$field}] must be a list"),
ConfigType::ASSOC_ARRAY => is_assoc_array($value) ?: throw new ValidationException("Package {$package_name} [{$field}] must be an object"),
default => $expected_type($value) ?: throw new ValidationException("Package {$package_name} [{$field}] has invalid type specification"),
};
}
/**
* Validate that fields with suffixes are list arrays
*/
private static function validateSuffixAllowedFields(int|string $name, mixed $item, array $fields, array $suffixes): void
{
foreach ($fields as $field) {
foreach ($suffixes as $suffix) {
$key = $field . $suffix;
if (isset($item[$key])) {
self::validatePackageFieldType($key, $item[$key], $name);
}
}
}
}
/**
* Validate php-extension specific fields for php-extension package
*/
private static function validatePhpExtensionFields(int|string $name, mixed $pkg): void
{
if (!isset($pkg['php-extension'])) {
return;
}
if (!is_assoc_array($pkg['php-extension'])) {
throw new ValidationException("Package {$name} [php-extension] must be an object");
}
foreach (self::PHP_EXTENSION_FIELDS as $field => $required) {
if (isset($pkg['php-extension'][$field])) {
self::validatePackageFieldType($field, $pkg['php-extension'][$field], $name);
}
}
// check for unknown fields in php-extension
self::validateNoInvalidFields('php-extension', $name, $pkg['php-extension'], array_keys(self::PHP_EXTENSION_FIELDS));
}
private static function validateNoInvalidFields(string $config_type, int|string $item_name, mixed $item_content, array $allowed_fields): void
{
foreach ($item_content as $k => $v) {
// remove suffixes for checking
$base_k = $k;
$suffixes = ['@windows', '@unix', '@macos', '@linux'];
foreach ($suffixes as $suffix) {
if (str_ends_with($k, $suffix)) {
$base_k = substr($k, 0, -strlen($suffix));
break;
}
}
if (!in_array($base_k, $allowed_fields)) {
throw new ValidationException("{$config_type} [{$item_name}] has invalid field [{$base_k}]");
}
}
}
}