Skip to content

Commit 3b63f39

Browse files
committed
Move text parsing logic into TextParser class, delete procedural parse_text.php
Standalone build now inlines proper namespaced classes instead of global functions.
1 parent 92db2c8 commit 3b63f39

5 files changed

Lines changed: 373 additions & 403 deletions

File tree

build/build.php

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* 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.
55
*
66
* Run: php build/build.php
77
*/
@@ -12,30 +12,79 @@
1212
$css = file_get_contents($root . '/dist/styles.css');
1313
$js = file_get_contents($root . '/dist/app.js');
1414

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+
];
1826

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);
2045
$source = str_replace(
21-
"require_once __DIR__ . '/../src/Parsers/parse_text.php';",
22-
$parser,
46+
"require_once __DIR__ . '/../vendor/autoload.php';\n",
47+
'',
2348
$source
2449
);
2550

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(
2756
'<?php include(__DIR__ . "/../dist/styles.css"); ?>',
2857
$css,
29-
$source
58+
$output
3059
);
3160

32-
$source = str_replace(
61+
$output = str_replace(
3362
'<?php include(__DIR__ . "/../dist/app.js"); ?>',
3463
$js,
35-
$source
64+
$output
3665
);
3766

3867
$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+
}
4087

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+
}

build/go.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
phpinfo();
1818
$raw = ob_get_clean();
1919

20-
// ── Text parser (shared with package) ─────────────────────────────────
21-
require_once __DIR__ . '/../src/Parsers/parse_text.php';
20+
// ── Parse ────────────────────────────────────────────────────────────
21+
require_once __DIR__ . '/../vendor/autoload.php';
2222

23-
$info = pp_parse($raw);
23+
$info = (new \STS\Phpinfo\Parsers\TextParser($raw))->parse();
2424

2525
// ── Output complete HTML page ─────────────────────────────────────────
2626
ob_start();
@@ -45,8 +45,8 @@
4545
<header class="absolute top-0 h-14 lg:h-16 w-full flex items-center justify-between py-3 px-6 xl:px-8 z-30 bg-white/80 dark:bg-slate-950/80 backdrop-blur-sm border-b border-slate-200 dark:border-slate-800">
4646
<div class="flex-1 md:flex items-center gap-3">
4747
<img class="h-6 md:h-10 dark:invert" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 -1 100 50'%3E%3Cpath d='m7.579 10.123 14.204 0c4.169 0.035 7.19 1.237 9.063 3.604 1.873 2.367 2.491 5.6 1.855 9.699-0.247 1.873-0.795 3.71-1.643 5.512-0.813 1.802-1.943 3.427-3.392 4.876-1.767 1.837-3.657 3.003-5.671 3.498-2.014 0.495-4.099 0.742-6.254 0.742l-6.36 0-2.014 10.07-7.367 0 7.579-38.001 0 0m6.201 6.042-3.18 15.9c0.212 0.035 0.424 0.053 0.636 0.053 0.247 0 0.495 0 0.742 0 3.392 0.035 6.219-0.3 8.48-1.007 2.261-0.742 3.781-3.321 4.558-7.738 0.636-3.71 0-5.848-1.908-6.413-1.873-0.565-4.222-0.83-7.049-0.795-0.424 0.035-0.83 0.053-1.219 0.053-0.353 0-0.724 0-1.113 0l0.053-0.053'/%3E%3Cpath d='m41.093 0 7.314 0-2.067 10.123 6.572 0c3.604 0.071 6.289 0.813 8.056 2.226 1.802 1.413 2.332 4.099 1.59 8.056l-3.551 17.649-7.42 0 3.392-16.854c0.353-1.767 0.247-3.021-0.318-3.763-0.565-0.742-1.784-1.113-3.657-1.113l-5.883-0.053-4.346 21.783-7.314 0 7.632-38.054 0 0'/%3E%3Cpath d='m70.412 10.123 14.204 0c4.169 0.035 7.19 1.237 9.063 3.604 1.873 2.367 2.491 5.6 1.855 9.699-0.247 1.873-0.795 3.71-1.643 5.512-0.813 1.802-1.943 3.427-3.392 4.876-1.767 1.837-3.657 3.003-5.671 3.498-2.014 0.495-4.099 0.742-6.254 0.742l-6.36 0-2.014 10.07-7.367 0 7.579-38.001 0 0m6.201 6.042-3.18 15.9c0.212 0.035 0.424 0.053 0.636 0.053 0.247 0 0.495 0 0.742 0 3.392 0.035 6.219-0.3 8.48-1.007 2.261-0.742 3.781-3.321 4.558-7.738 0.636-3.71 0-5.848-1.908-6.413-1.873-0.565-4.222-0.83-7.049-0.795-0.424 0.035-0.83 0.053-1.219 0.053-0.353 0-0.724 0-1.113 0l0.053-0.053'/%3E%3C/svg%3E%0A"/>
48-
<span class="hidden md:inline-block text-sm font-mono text-slate-400 dark:text-slate-500"><?php echo $info['version'] ?></span>
49-
<span class="md:hidden text-xs font-mono text-slate-400"><?php echo $info['version'] ?></span>
48+
<span class="hidden md:inline-block text-sm font-mono text-slate-400 dark:text-slate-500"><?php echo $info->version() ?></span>
49+
<span class="md:hidden text-xs font-mono text-slate-400"><?php echo $info->version() ?></span>
5050
</div>
5151
<div class="flex-1 flex justify-center">
5252
<div class="relative group">
@@ -186,7 +186,7 @@ class="px-4 py-1 rounded block"
186186
hash: null,
187187
mobileNav: false,
188188
info: <?php echo json_encode($info) ?>,
189-
sections: <?php echo json_encode(array_column($info['modules'], 'key')) ?>,
189+
sections: <?php echo json_encode($info->modules()->map(fn($m) => $m->key())->all()) ?>,
190190
selected: null,
191191
selectedIndex: null,
192192
initialized: false,

0 commit comments

Comments
 (0)