|
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * Build script that compiles build/go.php into a fully self-contained file |
4 | | - * with CSS, JS, and the shared text parser baked in. |
| 4 | + * with CSS, JS, and source classes baked in. |
5 | 5 | * |
6 | 6 | * Run: php build/build.php |
7 | 7 | */ |
|
12 | 12 | $css = file_get_contents($root . '/dist/styles.css'); |
13 | 13 | $js = file_get_contents($root . '/dist/app.js'); |
14 | 14 |
|
15 | | -// Read the shared parser, stripping its opening <?php tag |
16 | | -$parser = file_get_contents($root . '/src/Parsers/parse_text.php'); |
17 | | -$parser = preg_replace('/^<\?php\s*/s', '', $parser); |
| 15 | +// Source files to inline (in dependency order) |
| 16 | +$classFiles = [ |
| 17 | + 'src/Support/Str.php', |
| 18 | + 'src/Support/Items.php', |
| 19 | + 'src/Models/Config.php', |
| 20 | + 'src/Models/Group.php', |
| 21 | + 'src/Models/Module.php', |
| 22 | + 'src/PhpInfo.php', |
| 23 | + 'src/Parsers/Parser.php', |
| 24 | + 'src/Parsers/TextParser.php', |
| 25 | +]; |
18 | 26 |
|
19 | | -// Replace the include() calls with the actual file contents |
| 27 | +// Convert each source file to a namespace block and concatenate |
| 28 | +$inlined = ''; |
| 29 | +foreach ($classFiles as $file) { |
| 30 | + $inlined .= toNamespaceBlock(file_get_contents($root . '/' . $file)) . "\n"; |
| 31 | +} |
| 32 | + |
| 33 | +// Add the items() helper in the global namespace |
| 34 | +$inlined .= "namespace {\n"; |
| 35 | +$inlined .= " if (!function_exists('items')) {\n"; |
| 36 | +$inlined .= " function items(iterable \$items = []): \\STS\\Phpinfo\\Support\\Items {\n"; |
| 37 | +$inlined .= " return new \\STS\\Phpinfo\\Support\\Items(\$items);\n"; |
| 38 | +$inlined .= " }\n"; |
| 39 | +$inlined .= " }\n"; |
| 40 | +$inlined .= "}\n\n"; |
| 41 | + |
| 42 | +// Strip opening <?php and the autoload require from go.php, |
| 43 | +// then wrap the main script in a global namespace block. |
| 44 | +$source = preg_replace('/^<\?php\s*/s', '', $source); |
20 | 45 | $source = str_replace( |
21 | | - "require_once __DIR__ . '/../src/Parsers/parse_text.php';", |
22 | | - $parser, |
| 46 | + "require_once __DIR__ . '/../vendor/autoload.php';\n", |
| 47 | + '', |
23 | 48 | $source |
24 | 49 | ); |
25 | 50 |
|
26 | | -$source = str_replace( |
| 51 | +// Build the final output: <?php + class blocks + main script in namespace {} |
| 52 | +$output = "<?php\n" . $inlined . "namespace {\n\n" . $source . "\n}\n"; |
| 53 | + |
| 54 | +// Replace the include() calls with the actual file contents |
| 55 | +$output = str_replace( |
27 | 56 | '<?php include(__DIR__ . "/../dist/styles.css"); ?>', |
28 | 57 | $css, |
29 | | - $source |
| 58 | + $output |
30 | 59 | ); |
31 | 60 |
|
32 | | -$source = str_replace( |
| 61 | +$output = str_replace( |
33 | 62 | '<?php include(__DIR__ . "/../dist/app.js"); ?>', |
34 | 63 | $js, |
35 | | - $source |
| 64 | + $output |
36 | 65 | ); |
37 | 66 |
|
38 | 67 | $out = $root . '/dist/go-standalone.php'; |
39 | | -file_put_contents($out, $source); |
| 68 | +file_put_contents($out, $output); |
| 69 | + |
| 70 | +echo "Built dist/go-standalone.php (" . number_format(strlen($output)) . " bytes)\n"; |
| 71 | + |
| 72 | +/** |
| 73 | + * Convert a PHP source file from "namespace X;" declaration syntax |
| 74 | + * to "namespace X { ... }" block syntax. |
| 75 | + */ |
| 76 | +function toNamespaceBlock(string $content): string |
| 77 | +{ |
| 78 | + // Strip opening <?php tag |
| 79 | + $content = preg_replace('/^<\?php\s*/s', '', $content); |
| 80 | + |
| 81 | + // Extract the namespace declaration |
| 82 | + if (preg_match('/^namespace\s+([^;]+);\s*/m', $content, $matches)) { |
| 83 | + $namespace = $matches[1]; |
| 84 | + $content = preg_replace('/^namespace\s+[^;]+;\s*/m', '', $content); |
| 85 | + return "namespace {$namespace} {\n{$content}}\n"; |
| 86 | + } |
40 | 87 |
|
41 | | -echo "Built dist/go-standalone.php (" . number_format(strlen($source)) . " bytes)\n"; |
| 88 | + // No namespace — wrap in global namespace block |
| 89 | + return "namespace {\n{$content}}\n"; |
| 90 | +} |
0 commit comments