forked from ircmaxell/php-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManifestValidator.php
More file actions
272 lines (232 loc) · 8.61 KB
/
ManifestValidator.php
File metadata and controls
272 lines (232 loc) · 8.61 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
<?php
declare(strict_types=1);
namespace PHPCompiler\Web;
/**
* Validates phpc.json structure and on-disk paths (issue #263).
*/
final class ManifestValidator
{
/** @var list<string> */
private const TOP_LEVEL_KEYS = [
'binary',
'public',
'assets',
'entry',
'index',
'includes',
'autoload',
];
/**
* Pre-build validation: phpc.json + entry exist; binary path is declared but need not exist yet.
*
* Actionable error messages (empty when valid).
*
* @return list<string>
*/
public static function validateForBuild(string $projectDir): array
{
$dir = realpath($projectDir);
if (false === $dir || !is_dir($dir)) {
return ['project directory not found: '.$projectDir];
}
$manifestPath = $dir.'/phpc.json';
if (!is_file($manifestPath)) {
return ['phpc.json not found in '.$dir];
}
$raw = file_get_contents($manifestPath);
if (false === $raw) {
return ['cannot read phpc.json'];
}
$data = json_decode($raw, true);
if (JSON_ERROR_NONE !== json_last_error()) {
return ['phpc.json is not valid JSON: '.json_last_error_msg()];
}
if (!is_array($data)) {
return ['phpc.json root must be a JSON object'];
}
$errors = [];
foreach (array_keys($data) as $key) {
if (!is_string($key) || !in_array($key, self::TOP_LEVEL_KEYS, true)) {
$errors[] = 'unknown key in phpc.json: '.$key;
}
}
if (!isset($data['entry']) || !is_string($data['entry']) || '' === $data['entry']) {
$errors[] = 'missing required key: entry';
} else {
$entryPath = ProjectManifest::resolveRelativePath($dir, $data['entry']);
if (!is_file($entryPath)) {
$errors[] = 'entry path not found: '.$data['entry'];
}
}
if (!isset($data['binary']) || !is_string($data['binary']) || '' === $data['binary']) {
$errors[] = 'missing required key: binary';
}
if (isset($data['includes'])) {
$errors = array_merge($errors, self::validateIncludesOnDisk($dir, $data['includes']));
}
if (isset($data['assets'])) {
$errors = array_merge($errors, self::validateAssetsOnDisk($dir, $data['assets']));
}
if (isset($data['autoload'])) {
$errors = array_merge($errors, self::validateAutoload($data['autoload']));
$errors = array_merge($errors, ProjectAutoload::validatePsr4PathsOnDisk($dir, $data['autoload']));
}
return $errors;
}
/**
* Actionable error messages (empty when valid).
*
* @return list<string>
*/
public static function validate(string $projectDir): array
{
$dir = realpath($projectDir);
if (false === $dir || !is_dir($dir)) {
return ['project directory not found: '.$projectDir];
}
$manifestPath = $dir.'/phpc.json';
if (!is_file($manifestPath)) {
return ['phpc.json not found in '.$dir];
}
$raw = file_get_contents($manifestPath);
if (false === $raw) {
return ['cannot read phpc.json'];
}
$data = json_decode($raw, true);
if (JSON_ERROR_NONE !== json_last_error()) {
return ['phpc.json is not valid JSON: '.json_last_error_msg()];
}
if (!is_array($data)) {
return ['phpc.json root must be a JSON object'];
}
$errors = [];
foreach (array_keys($data) as $key) {
if (!is_string($key) || !in_array($key, self::TOP_LEVEL_KEYS, true)) {
$errors[] = 'unknown key in phpc.json: '.$key;
}
}
if (!isset($data['binary'])) {
$errors[] = 'missing required key: binary';
} elseif (!is_string($data['binary']) || '' === $data['binary']) {
$errors[] = 'binary must be a non-empty string';
} else {
$binaryPath = ProjectManifest::resolveRelativePath($dir, $data['binary']);
if (!is_file($binaryPath)) {
$errors[] = 'binary path not found: '.$data['binary'];
}
}
if (isset($data['public'])) {
if (!is_string($data['public']) || '' === $data['public']) {
$errors[] = 'public must be a non-empty string';
} else {
$publicDir = ProjectManifest::resolveRelativePath($dir, $data['public']);
if (!is_dir($publicDir)) {
$errors[] = 'public directory not found: '.$data['public'];
} else {
$index = $publicDir.'/index.php';
if (!is_file($index)) {
$errors[] = 'missing public/index.php under '.$data['public'];
}
}
}
}
if (isset($data['entry'])) {
if (!is_string($data['entry']) || '' === $data['entry']) {
$errors[] = 'entry must be a non-empty string';
} else {
$entryPath = ProjectManifest::resolveRelativePath($dir, $data['entry']);
if (!is_file($entryPath)) {
$errors[] = 'entry path not found: '.$data['entry'];
}
}
}
if (isset($data['index'])) {
if (!is_string($data['index']) || '' === $data['index']) {
$errors[] = 'index must be a non-empty string';
} else {
$indexPath = ProjectManifest::resolveRelativePath($dir, $data['index']);
if (!is_file($indexPath)) {
$errors[] = 'index path not found: '.$data['index'];
}
}
}
if (isset($data['includes'])) {
$errors = array_merge($errors, self::validateIncludesOnDisk($dir, $data['includes']));
}
if (isset($data['assets'])) {
$errors = array_merge($errors, self::validateAssetsOnDisk($dir, $data['assets']));
}
if (isset($data['autoload'])) {
$errors = array_merge($errors, self::validateAutoload($data['autoload']));
$errors = array_merge($errors, ProjectAutoload::validatePsr4PathsOnDisk($dir, $data['autoload']));
}
return $errors;
}
/**
* @return list<string>
*/
private static function validateAssetsOnDisk(string $projectDir, mixed $assets): array
{
if (!is_string($assets) || '' === $assets) {
return ['assets must be a non-empty string'];
}
$assetsDir = ProjectManifest::resolveRelativePath($projectDir, $assets);
if (!is_dir($assetsDir)) {
return ['assets directory not found: '.$assets];
}
return [];
}
/**
* @return list<string>
*/
private static function validateIncludesOnDisk(string $projectDir, mixed $includes): array
{
if (!is_array($includes)) {
return ['includes must be an array of strings'];
}
$errors = [];
foreach ($includes as $i => $item) {
if (!is_string($item) || '' === $item) {
$errors[] = 'includes['.$i.'] must be a non-empty string';
continue;
}
$includePath = ProjectManifest::resolveRelativePath($projectDir, $item);
if (!is_file($includePath)) {
$errors[] = 'includes path not found: '.$item;
}
}
return $errors;
}
/**
* @return list<string>
*/
private static function validateAutoload(mixed $autoload): array
{
if (!is_array($autoload)) {
return ['autoload must be an object'];
}
$errors = [];
foreach (array_keys($autoload) as $key) {
if (!is_string($key) || 'psr-4' !== $key) {
$errors[] = 'unknown key in autoload: '.(is_string($key) ? $key : '(invalid)');
}
}
if (!isset($autoload['psr-4'])) {
return $errors;
}
$psr4 = $autoload['psr-4'];
if (!is_array($psr4)) {
$errors[] = 'autoload.psr-4 must be an object';
return $errors;
}
foreach ($psr4 as $prefix => $path) {
if (!is_string($prefix) || '' === $prefix) {
$errors[] = 'autoload.psr-4 keys must be non-empty namespace prefixes';
}
if (!is_string($path) || '' === $path) {
$errors[] = 'autoload.psr-4 values must be non-empty paths';
}
}
return $errors;
}
}