Skip to content

Commit 0761dd8

Browse files
authored
Preserve strict_types for run-php code files
1 parent d373084 commit 0761dd8

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

packages/runtime-playground/src/php-bootstrap.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ export function bootstrapPhpCode(spec: RuntimeCreateSpec, code: string, args: st
2525
return code
2626
}
2727

28+
const command = splitLeadingStrictTypesDeclare(code)
29+
2830
return `<?php
29-
${phpFatalDiagnosticPhp()}
31+
${command.strictTypesDeclare ? `${command.strictTypesDeclare}\n` : ""}${phpFatalDiagnosticPhp()}
3032
${pluginRuntimeBootstrapPhp(spec)}
3133
${saveQueriesBootstrapPhp(args)}
3234
${runtimeEnvPhp(spec)}
@@ -37,7 +39,16 @@ ${recipeActivePluginBootstrapPhp(spec, args)}
3739
${wpCliBridge ? `putenv(${JSON.stringify(`WP_CODEBOX_TERMINAL_ACTION_URL=${wpCliBridge.url}`)});
3840
putenv(${JSON.stringify(`WP_CODEBOX_TERMINAL_ACTION_TOKEN=${wpCliBridge.token}`)});
3941
` : ""}
40-
${phpBody(code)}`
42+
${command.body}`
43+
}
44+
45+
export function splitLeadingStrictTypesDeclare(code: string): { strictTypesDeclare: string; body: string } {
46+
const normalized = normalizePhpCode(code)
47+
const match = normalized.match(/^<\?php\s*(declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;)\s*/i)
48+
49+
return match
50+
? { strictTypesDeclare: match[1], body: normalized.slice(match[0].length) }
51+
: { strictTypesDeclare: "", body: phpBody(normalized) }
4152
}
4253

4354
function saveQueriesBootstrapPhp(args: string[]): string {

packages/runtime-playground/src/wordpress-command-runners.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
themeCheckRunCode,
5050
themeSetupInputFromArgs,
5151
} from "./commands.js"
52-
import { bootstrapAbilityPhpCode, bootstrapPhpCode, phpCodeFromArgs } from "./php-bootstrap.js"
52+
import { bootstrapAbilityPhpCode, bootstrapPhpCode, phpCodeFromArgs, splitLeadingStrictTypesDeclare } from "./php-bootstrap.js"
5353
import { assertPlaygroundResponseOk, type PlaygroundRunResponse } from "./playground-command-errors.js"
5454
import type { PlaygroundCliServer } from "./preview-server.js"
5555
import { persistCorePhpunitResult, persistPluginPhpunitResult, persistVfsDiagnosticFileToHost, readCorePhpunitDiagnostic, readPluginPhpunitDiagnostic } from "./runtime-diagnostics.js"
@@ -150,12 +150,14 @@ interface RunPhpDiagnosticsPayload {
150150
}
151151

152152
function runPhpCommandDiagnosticsPhp(code: string, marker: string, maxItems: number, maxBytes: number): string {
153+
const command = splitLeadingStrictTypesDeclare(code)
154+
153155
return `
154-
$wp_codebox_command_observation_started_at = gmdate('Y-m-d\\TH:i:s.v\\Z');
156+
${command.strictTypesDeclare ? `${command.strictTypesDeclare}\n` : ""}$wp_codebox_command_observation_started_at = gmdate('Y-m-d\\TH:i:s.v\\Z');
155157
$wp_codebox_command_observation_start_time = microtime(true);
156158
$wp_codebox_command_observation_start_memory = memory_get_usage(true);
157159
$wp_codebox_command_diagnostics_start = isset($GLOBALS['wpdb']->queries) && is_array($GLOBALS['wpdb']->queries) ? count($GLOBALS['wpdb']->queries) : 0;
158-
${code.replace(/^<\?php\s*/, "")}
160+
${command.body}
159161
$wp_codebox_command_observation_finished_at = gmdate('Y-m-d\\TH:i:s.v\\Z');
160162
$wp_codebox_command_observation_duration_ms = round((microtime(true) - $wp_codebox_command_observation_start_time) * 1000, 3);
161163
$wp_codebox_command_observation_end_memory = memory_get_usage(true);

tests/runtime-php-snippets.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { tmpdir } from "node:os"
66
import { mkdtempSync } from "node:fs"
77
import { resolveSandboxTaskCode } from "../packages/cli/src/agent-code.js"
88
import { phpRuntimeComponentLifecycleActionReplayFunction, phpRuntimeComponentLifecycleReplayFunction } from "../packages/runtime-core/src/index.js"
9-
import { bootstrapPhpCode } from "../packages/runtime-playground/src/php-bootstrap.js"
9+
import { bootstrapPhpCode, phpCodeFromArgs } from "../packages/runtime-playground/src/php-bootstrap.js"
1010

1111
const lifecycleReplaySnippet = phpRuntimeComponentLifecycleReplayFunction("contained_runtime_test")
1212
assert.match(lifecycleReplaySnippet, /function contained_runtime_test_component_lifecycle_replay_prepare\(\): array/)
@@ -94,6 +94,15 @@ assert.match(bootstrappedRunPhp, /contained_runtime_run_php_component_lifecycle_
9494
assert.match(bootstrappedRunPhp, /CONTAINED_RUNTIME_COMPONENT_MANIFEST_JSON/)
9595
assert.doesNotMatch(bootstrappedRunPhp, /wp_codebox_run_php|wp_codebox_component_manifest|WP_CODEBOX_COMPONENT_MANIFEST_JSON/)
9696

97+
const strictTypesCodeFileRoot = mkdtempSync(join(tmpdir(), "wp-codebox-run-php-strict-types-"))
98+
const strictTypesCodeFile = join(strictTypesCodeFileRoot, "strict-types.php")
99+
writeFileSync(strictTypesCodeFile, "<?php declare(strict_types=1);\necho 'strict';")
100+
const strictTypesCode = await phpCodeFromArgs([`code-file=${strictTypesCodeFile}`])
101+
const bootstrappedStrictTypesCodeFile = bootstrapPhpCode({} as never, strictTypesCode, [])
102+
assert.match(bootstrappedStrictTypesCodeFile, /^<\?php\ndeclare\(strict_types=1\);\nregister_shutdown_function/)
103+
assert.equal((bootstrappedStrictTypesCodeFile.match(/declare\(strict_types=1\);/g) ?? []).length, 1)
104+
assert.match(bootstrappedStrictTypesCodeFile, /echo 'strict';/)
105+
97106
const sandboxAgentCode = await resolveSandboxTaskCode({
98107
task: "Say hello",
99108
agent: "wp-codebox-sandbox",

0 commit comments

Comments
 (0)