Skip to content
Closed
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
15 changes: 13 additions & 2 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import {
improveWASMErrorReporting,
UnhandledRejectionsTarget,
} from './wasm-error-reporting';
import { Semaphore, createSpawnHandler, joinPaths } from '@php-wasm/util';
import {
Semaphore,
createSpawnHandler,
joinPaths,
phpVars,
} from '@php-wasm/util';

const STRING = 'string';
const NUMBER = 'number';
Expand Down Expand Up @@ -267,7 +272,13 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
}
}
if (typeof request.code === 'string') {
this.#setPHPCode(' ?>' + request.code);
const js = phpVars(request.variables || {});
const phpVariablesDeclarations = Object.entries(js)
.map(([key, value]) => `$${key} = ${value};`)
.join('\n');
this.#setPHPCode(
` ${phpVariablesDeclarations} ?>${request.code}`
);
}
this.#addServerGlobalEntriesInWasm();
const response = await this.#handleRequest();
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/universal/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type {
SupportedPHPExtensionBundle,
} from './supported-php-extensions';
export { BasePHP, __private__dont__use } from './base-php';
export { loadPHPRuntime } from './load-php-runtime';
export { currentJsRuntime, loadPHPRuntime } from './load-php-runtime';
export type {
DataModule,
EmscriptenOptions,
Expand Down
5 changes: 5 additions & 0 deletions packages/php-wasm/universal/src/lib/universal-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ export interface PHPRunOptions {
*/
code?: string;

/**
* The variables to pass to the PHP code.
*/
variables?: Record<string, any>;

/**
* Whether to throw an error if the PHP process exits with a non-zero code
* or outputs to stderr.
Expand Down
15 changes: 13 additions & 2 deletions packages/php-wasm/universal/src/lib/write-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,30 @@ export interface WriteFilesOptions {
export async function writeFiles(
php: UniversalPHP,
root: string,
newFiles: Record<string, Uint8Array | string>,
newFiles: Record<string, File | Uint8Array | string>,
{ rmRoot = false }: WriteFilesOptions = {}
) {
if (rmRoot) {
if (await php.isDir(root)) {
await php.rmdir(root, { recursive: true });
}
}
await php.mkdir(root);
for (const [relativePath, content] of Object.entries(newFiles)) {
const filePath = joinPaths(root, relativePath);
if (!(await php.fileExists(dirname(filePath)))) {
await php.mkdir(dirname(filePath));
}
await php.writeFile(filePath, content);
const finalContent =
content instanceof File
? new Uint8Array(await content.arrayBuffer())
: content;
await php.writeFile(filePath, finalContent);
}

const paths: Record<string, string> = {};
for (const [relativePath] of Object.entries(newFiles)) {
paths[relativePath] = joinPaths(root, relativePath);
}
return paths;
}
1 change: 1 addition & 0 deletions packages/playground/blueprints/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '@php-wasm/node-polyfills';

export * from './lib/steps';
export * from './lib/steps/handlers';
export { linkSnapshot, setSnapshot } from './lib/steps/import-wordpress-files';
export { runBlueprintSteps, compileBlueprint } from './lib/compile';
export type { Blueprint } from './lib/blueprint';
export type {
Expand Down
Loading