Skip to content

Commit

Permalink
Compat with the new PHPProcessManager API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Apr 22, 2024
1 parent 703634c commit 6136e41
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 72 deletions.
62 changes: 12 additions & 50 deletions packages/playground/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ import express, { Request } from 'express';
// import compression from 'compression';
// import compressible from 'compressible';
import { addTrailingSlash } from './middleware/add-trailing-slash';
import {
MaxPhpInstancesError,
PHPBrowser,
PHPRequestHandler,
PHPResponse,
PhpProcessManager,
SpawnedPHP,
} from '@php-wasm/universal';
import { PHPRequestHandler } from '@php-wasm/universal';
import { IncomingMessage, Server, ServerResponse } from 'http';
import { AddressInfo } from 'net';
import { createPhp } from './setup-php';
import { NodePHP } from '@php-wasm/node';
import { defineSiteUrl } from '@wp-playground/blueprints';

export interface Mount {
Expand Down Expand Up @@ -56,19 +48,13 @@ export async function startServer(options: ServerOptions) {
console.log(`Server is running on ${absoluteUrl}`);
// open(absoluteUrl); // Open the URL in the default browser

const procManager = new PhpProcessManager<NodePHP>();
const requestHandler = new PHPBrowser(
new PHPRequestHandler({
documentRoot: '/wordpress',
absoluteUrl,
})
);
const phpFactory = () =>
createPhp(procManager, requestHandler, options.mounts || []);
procManager.setPhpFactory(phpFactory);

const primaryPhp = await phpFactory();
procManager.setPrimaryPhp(primaryPhp);
const requestHandler = new PHPRequestHandler({
phpFactory: async () => await createPhp(options.mounts || []),
documentRoot: '/wordpress',
absoluteUrl,
});
const primaryPhp = await requestHandler.getPrimaryPhp();
console.log({ primaryPhp });

await defineSiteUrl(primaryPhp, {
siteUrl: absoluteUrl,
Expand All @@ -77,37 +63,13 @@ export async function startServer(options: ServerOptions) {
console.log('PHP started', { absoluteUrl });

app.use('/', async (req, res) => {
console.log('Request received');
let spawnedPHP: SpawnedPHP<NodePHP> | undefined = undefined;
try {
spawnedPHP = await procManager.spawn();
} catch (e) {
if (e instanceof MaxPhpInstancesError) {
res.statusCode = 502;
res.end('Max PHP instances reached');
} else {
res.statusCode = 500;
res.end('Internal server error');
}
return;
}

console.log({
const phpResponse = await requestHandler.request({
url: req.url,
headers: parseHeaders(req),
method: req.method as any,
body: await bufferRequestBody(req),
});

let phpResponse: PHPResponse | undefined = undefined;
try {
phpResponse = await requestHandler.request(spawnedPHP!.php, {
url: req.url,
headers: parseHeaders(req),
method: req.method as any,
body: await bufferRequestBody(req),
});
} finally {
spawnedPHP!.reap();
}

res.statusCode = phpResponse.httpStatusCode;
for (const key in phpResponse.headers) {
res.setHeader(key, phpResponse.headers[key]);
Expand Down
14 changes: 3 additions & 11 deletions packages/playground/cli/src/setup-php.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { NodePHP } from '@php-wasm/node';
import {
type RequestHandler,
PhpProcessManager,
rotatePHPRuntime,
} from '@php-wasm/universal';
import { rotatePHPRuntime } from '@php-wasm/universal';
import { Mount } from './server';
import { rootCertificates } from 'tls';

export async function createPhp(
processManager: PhpProcessManager<NodePHP>,
requestHandler: RequestHandler,
mounts: Mount[]
) {
export async function createPhp(mounts: Mount[]) {
const php = new NodePHP();
php.requestHandler = requestHandler as any;
php.initializeRuntime(await createPhpRuntime());
php.setPhpIniEntry('disable_functions', '');
php.setPhpIniEntry('allow_url_fopen', '1');
Expand All @@ -32,6 +23,7 @@ export async function createPhp(
// @see https://github.com/WordPress/wordpress-playground/pull/990 for more context
rotatePHPRuntime({
php,
cwd: '/wordpress',
recreateRuntime: createPhpRuntime,
maxRequests: 400,
});
Expand Down
23 changes: 12 additions & 11 deletions packages/playground/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
runBlueprintSteps,
} from '@wp-playground/blueprints';
import { NodePHP } from '@php-wasm/node';
import { UniversalPHP } from '@php-wasm/universal';
import { PHPRequestHandler, UniversalPHP } from '@php-wasm/universal';
import { collectPhpLogs, logger } from '@php-wasm/logger';

export interface NodePlaygroundOptions {
Expand All @@ -37,23 +37,24 @@ export async function startPlaygroundNode(
* @TODO use this workflow to compile WordPress for the web
*/
const compiled = compileBlueprint(options.blueprint || {});
const playground = await NodePHP.load(compiled.versions.php, {
requestHandler: {
documentRoot: options.wordpressPathOnHost,
absoluteUrl: options.serverUrl,
},

const handler = new PHPRequestHandler({
phpFactory: async () => await NodePHP.load(compiled.versions.php),
documentRoot: options.wordpressPathOnHost,
absoluteUrl: options.serverUrl,
});
const php = await handler.getPrimaryPhp();

collectPhpLogs(logger, playground);
collectPhpLogs(logger, php);

await defineSiteUrl(playground, {
await defineSiteUrl(php, {
siteUrl: options.serverUrl,
});

await allowWpOrgHosts(playground, options.wordpressPathOnHost);
await allowWpOrgHosts(php, options.wordpressPathOnHost);

await runBlueprintSteps(compiled, playground);
return playground;
await runBlueprintSteps(compiled, php);
return php;
}

export async function allowWpOrgHosts(
Expand Down

0 comments on commit 6136e41

Please sign in to comment.