diff --git a/examples/001-SimpleWeb/example.php b/examples/001-SimpleWeb/example.php
index d2893169..b7aef423 100644
--- a/examples/001-SimpleWeb/example.php
+++ b/examples/001-SimpleWeb/example.php
@@ -11,11 +11,10 @@
* QUERY_STRING='name=World' ./simpleweb
* Or: phpc build -o .phpc/bin/app example.php && phpc serve --aot examples/001-SimpleWeb
* $_GET is read from QUERY_STRING at runtime (see lib/AOT/runtime/superglobals_refresh.c).
- * Pass -q at compile time to bake $_GET for static pages instead.
+ * Optional: pass -q at compile time to bake $_GET for static-only builds.
* $_SERVER['REQUEST_METHOD'] and $_REQUEST are populated automatically (see lib/Web/Superglobals.php).
*/
-// For AOT, pass the query at compile time: -q 'name=World' or QUERY_STRING=name=World
-// $_GET is populated from that string during compilation (see SuperglobalInit).
+// AOT: set QUERY_STRING (or use phpc serve --aot); compile-time -q is optional.
$name = $_GET['name'];
header('Content-Type: text/html; charset=UTF-8');
echo '
';
diff --git a/test/aot/ExampleWebAotTest.php b/test/aot/ExampleWebAotTest.php
index 1399c20a..eb191bbd 100644
--- a/test/aot/ExampleWebAotTest.php
+++ b/test/aot/ExampleWebAotTest.php
@@ -31,9 +31,27 @@ public function testSimpleWebExampleFile(): void
{
$source = realpath(__DIR__ . '/../../examples/001-SimpleWeb/example.php');
$this->assertNotFalse($source);
- $result = $this->compileAndRun($source, ['-q', 'name=Example']);
- $this->assertStringContainsString('Content-Type: text/html; charset=UTF-8', $result);
- $this->assertStringContainsString('Hello Example ', $result);
+
+ $repoRoot = dirname(__DIR__, 2);
+ $env = $this->llvmProcessEnv($repoRoot);
+ $binary = $this->compileToBinary($source, [], $repoRoot, $env);
+
+ $envAlice = $env;
+ $envAlice['QUERY_STRING'] = 'name=Alice';
+ $envAlice['SCRIPT_NAME'] = '/example.php';
+ $envAlice['REQUEST_URI'] = '/example.php?name=Alice';
+ $outAlice = $this->runBinary($binary, $envAlice);
+ $this->assertStringContainsString('Content-Type: text/html; charset=UTF-8', $outAlice);
+ $this->assertStringContainsString('Hello Alice ', $outAlice);
+
+ $envBob = $env;
+ $envBob['QUERY_STRING'] = 'name=Bob';
+ $envBob['SCRIPT_NAME'] = '/example.php';
+ $envBob['REQUEST_URI'] = '/example.php?name=Bob';
+ $outBob = $this->runBinary($binary, $envBob);
+ $this->assertStringContainsString('Hello Bob ', $outBob);
+
+ @unlink($binary);
}
public function testStaticWebExampleFile(): void
@@ -46,17 +64,14 @@ public function testStaticWebExampleFile(): void
}
/**
- * @param list $compileExtraArgs e.g. ['-q', 'name=Example']
+ * @param list $compileExtraArgs
*/
- private function compileAndRun(string $source, array $compileExtraArgs): string
+ private function compileToBinary(string $source, array $compileExtraArgs, string $repoRoot, array $env): string
{
$outfile = tempnam(sys_get_temp_dir(), 'phpc_web_');
$this->assertNotFalse($outfile);
unlink($outfile);
- $repoRoot = dirname(__DIR__, 2);
- $env = $this->llvmProcessEnv($repoRoot);
-
$compileArgv = array_merge(
self::llvmEnvPrefix(),
self::phpCommand(),
@@ -77,18 +92,44 @@ private function compileAndRun(string $source, array $compileExtraArgs): string
$this->assertFileExists($outfile, trim($compileErr !== false ? $compileErr : ''));
$this->assertTrue(is_executable($outfile));
- $run = proc_open([$outfile], $descriptorSpec, $runPipes, $repoRoot, $env);
- $result = stream_get_contents($runPipes[1]);
- fclose($runPipes[0]);
- fclose($runPipes[1]);
- fclose($runPipes[2]);
+ return $outfile;
+ }
+
+ /**
+ * @param array $env
+ */
+ private function runBinary(string $binary, array $env): string
+ {
+ $descriptorSpec = [
+ 0 => ['pipe', 'r'],
+ 1 => ['pipe', 'w'],
+ 2 => ['pipe', 'w'],
+ ];
+ $run = proc_open([$binary], $descriptorSpec, $pipes, null, $env);
+ $result = stream_get_contents($pipes[1]);
+ fclose($pipes[0]);
+ fclose($pipes[1]);
+ fclose($pipes[2]);
$exitCode = proc_close($run);
$this->assertSame(0, $exitCode, 'AOT binary should exit with status 0');
- @unlink($outfile);
return $result !== false ? $result : '';
}
+ /**
+ * @param list $compileExtraArgs
+ */
+ private function compileAndRun(string $source, array $compileExtraArgs): string
+ {
+ $repoRoot = dirname(__DIR__, 2);
+ $env = $this->llvmProcessEnv($repoRoot);
+ $binary = $this->compileToBinary($source, $compileExtraArgs, $repoRoot, $env);
+ $result = $this->runBinary($binary, $env);
+ @unlink($binary);
+
+ return $result;
+ }
+
/**
* @return array
*/