Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/001-SimpleWeb/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<!DOCTYPE html><html><head><link rel="stylesheet" href="/style.css"></head><body>';
Expand Down
69 changes: 55 additions & 14 deletions test/aot/ExampleWebAotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<h1>Hello Example</h1>', $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('<h1>Hello Alice</h1>', $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('<h1>Hello Bob</h1>', $outBob);

@unlink($binary);
}

public function testStaticWebExampleFile(): void
Expand All @@ -46,17 +64,14 @@ public function testStaticWebExampleFile(): void
}

/**
* @param list<string> $compileExtraArgs e.g. ['-q', 'name=Example']
* @param list<string> $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(),
Expand All @@ -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<string, string> $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<string> $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<string, string>
*/
Expand Down