forked from ircmaxell/php-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectManifest.php
More file actions
272 lines (228 loc) · 7.79 KB
/
ProjectManifest.php
File metadata and controls
272 lines (228 loc) · 7.79 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;
/**
* Minimal phpc.json reader (issue #106 subset for serve --aot and phpc build --project).
*/
final class ProjectManifest
{
/**
* Directory containing phpc.json (walks up from $startDir).
*/
public static function resolveProjectDir(string $startDir): ?string
{
$dir = realpath($startDir);
if (false === $dir) {
return null;
}
for ($i = 0; $i < 8; ++$i) {
if (is_file($dir.'/phpc.json')) {
return $dir;
}
$parent = dirname($dir);
if ($parent === $dir) {
break;
}
$dir = $parent;
}
return null;
}
/**
* Decoded phpc.json from project dir.
*
* @return array<string, mixed>|null
*/
public static function loadManifest(string $startDir): ?array
{
$projectDir = self::resolveProjectDir($startDir);
if (null === $projectDir) {
return null;
}
$raw = file_get_contents($projectDir.'/phpc.json');
if (false === $raw) {
return null;
}
$data = json_decode($raw, true);
return is_array($data) ? $data : null;
}
/**
* Entry script path for phpc build --project (issue #106).
*/
public static function resolveEntryPath(string $startDir, ?array $manifest = null): ?string
{
$projectDir = self::resolveProjectDir($startDir);
if (null === $projectDir) {
return null;
}
$manifest ??= self::loadManifest($projectDir);
if (null === $manifest || !isset($manifest['entry']) || !is_string($manifest['entry']) || '' === $manifest['entry']) {
return null;
}
$path = self::resolveRelativePath($projectDir, $manifest['entry']);
return is_file($path) ? $path : null;
}
/**
* Output path for AOT binary from manifest "binary" key (file may not exist yet).
*/
/**
* Extra PHP paths from manifest "includes" (issue #452).
*
* @return list<string> absolute paths relative to project dir
*/
public static function resolveIncludePaths(string $startDir, ?array $manifest = null): array
{
$projectDir = self::resolveProjectDir($startDir);
if (null === $projectDir) {
return [];
}
$manifest ??= self::loadManifest($projectDir);
if (null === $manifest || !isset($manifest['includes']) || !is_array($manifest['includes'])) {
return [];
}
$paths = [];
foreach ($manifest['includes'] as $item) {
if (!is_string($item) || '' === $item) {
continue;
}
$paths[] = self::resolveRelativePath($projectDir, $item);
}
return $paths;
}
/**
* Ordered compile units for phpc build --project: manifest includes[] then entry (issue #729).
*
* Absolute paths; null when project dir or entry is missing.
*
* @return list<string>|null
*/
public static function resolveCompileUnitPaths(string $startDir, ?array $manifest = null): ?array
{
$projectDir = self::resolveProjectDir($startDir);
if (null === $projectDir) {
return null;
}
$entry = self::resolveEntryPath($projectDir, $manifest);
if (null === $entry) {
return null;
}
$paths = self::resolveIncludePaths($projectDir, $manifest);
$paths[] = $entry;
return $paths;
}
public static function resolveBinaryOutputPath(string $startDir, ?array $manifest = null): ?string
{
$projectDir = self::resolveProjectDir($startDir);
if (null === $projectDir) {
return null;
}
$manifest ??= self::loadManifest($projectDir);
if (null === $manifest || !isset($manifest['binary']) || !is_string($manifest['binary']) || '' === $manifest['binary']) {
return null;
}
return self::resolveRelativePath($projectDir, $manifest['binary']);
}
/**
* Resolve AOT binary path from phpc.json, explicit flag, or default layout.
*/
public static function resolveBinaryPath(string $startDir, ?string $explicit = null): ?string
{
if (null !== $explicit && '' !== $explicit) {
$path = self::resolveRelativePath($startDir, $explicit);
return is_file($path) ? $path : null;
}
$dir = realpath($startDir);
if (false === $dir) {
return null;
}
for ($i = 0; $i < 8; ++$i) {
$manifest = $dir.'/phpc.json';
if (is_file($manifest)) {
$data = json_decode((string) file_get_contents($manifest), true);
if (is_array($data) && isset($data['binary']) && is_string($data['binary'])) {
$candidate = self::resolveRelativePath($dir, $data['binary']);
if (is_file($candidate)) {
return $candidate;
}
}
}
$parent = dirname($dir);
if ($parent === $dir) {
break;
}
$dir = $parent;
}
foreach (['.phpc/bin/app', 'bin/app', 'app'] as $relative) {
$candidate = self::resolveRelativePath($startDir, $relative);
if (is_file($candidate) && is_executable($candidate)) {
return $candidate;
}
}
return null;
}
/**
* HTTP document root for phpc serve when phpc.json is present (issue #443).
*
* Uses manifest "public" when set; otherwise keeps the resolved start directory
* (e.g. examples/001-SimpleWeb without a public/ subtree).
*/
public static function resolvePublicDir(string $startDir): string
{
$dir = realpath($startDir);
if (false === $dir) {
return $startDir;
}
$projectDir = self::resolveProjectDir($dir);
if (null === $projectDir) {
return $dir;
}
$manifest = self::loadManifest($projectDir);
if (null === $manifest) {
return $dir;
}
if (!isset($manifest['public']) || !is_string($manifest['public']) || '' === $manifest['public']) {
return $dir;
}
$publicDir = self::resolveRelativePath($projectDir, $manifest['public']);
$publicReal = realpath($publicDir);
if (false !== $publicReal && is_dir($publicReal)) {
return $publicReal;
}
return $publicDir;
}
/**
* Static assets directory beside public docroot (issue #594).
*
* Absolute directory when manifest "assets" is set and exists.
*
* @return string|null
*/
public static function resolveAssetsDir(string $startDir, ?array $manifest = null): ?string
{
$dir = realpath($startDir);
if (false === $dir) {
return null;
}
$projectDir = self::resolveProjectDir($dir);
if (null === $projectDir) {
return null;
}
$manifest ??= self::loadManifest($projectDir);
if (null === $manifest || !isset($manifest['assets']) || !is_string($manifest['assets']) || '' === $manifest['assets']) {
return null;
}
$assetsDir = self::resolveRelativePath($projectDir, $manifest['assets']);
$assetsReal = realpath($assetsDir);
return false !== $assetsReal && is_dir($assetsReal) ? $assetsReal : null;
}
public static function resolveRelativePath(string $baseDir, string $path): string
{
if ('/' === $path[0]) {
return $path;
}
$base = realpath($baseDir);
if (false === $base) {
$base = $baseDir;
}
return $base.'/'.$path;
}
}