Skip to content

Commit

Permalink
Support previewing WordPress Pull Requests
Browse files Browse the repository at this point in the history
This PR, paired with WordPress/wordpress-develop#5481, enables
previewing WordPress Pull Requests.

Changes:

* Adds wordpress.html – an easy entrypoint to previewing PRs
* Allows pulling GH artifacts from wordpress-develop repo
* Patches the controlled iframe in JavaScript, not in Docker, to enable
  re-applying the patch in wordpress-develop branch #668
* Serves static files using the PHP instance when they exist, not based
  on heuristics like path.startsWith('/wp-content')
  • Loading branch information
adamziel committed Oct 13, 2023
1 parent 772bdd4 commit e12988d
Show file tree
Hide file tree
Showing 15 changed files with 1,707 additions and 1,406 deletions.
3 changes: 0 additions & 3 deletions packages/php-wasm/node/src/test/php-request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe.each(SupportedPHPVersions)(
php = new NodePHP(runtimeId);
handler = new PHPRequestHandler(php, {
documentRoot: '/',
isStaticFilePath: (path) => !path.endsWith('.php'),
});
});

Expand Down Expand Up @@ -171,8 +170,6 @@ describe.each(SupportedPHPVersions)(
php.mkdirTree('/var/www');
handler = new PHPRequestHandler(php, {
documentRoot: '/var/www',
// Treat all files as dynamic
isStaticFilePath: () => false,
});
});

Expand Down
26 changes: 11 additions & 15 deletions packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
PHPRunOptions,
RequestHandler,
} from './universal-php';
import { seemsLikeAPHPRequestHandlerPath } from '@php-wasm/web-service-worker';

export interface PHPRequestHandlerConfiguration {
/**
Expand All @@ -24,11 +25,6 @@ export interface PHPRequestHandlerConfiguration {
* Request Handler URL. Used to populate $_SERVER details like HTTP_HOST.
*/
absoluteUrl?: string;
/**
* Callback used by the PHPRequestHandler to decide whether
* the requested path refers to a PHP file or a static file.
*/
isStaticFilePath?: (path: string) => boolean;
}

/** @inheritDoc */
Expand All @@ -46,7 +42,6 @@ export class PHPRequestHandler implements RequestHandler {
* The PHP instance
*/
php: BasePHP;
#isStaticFilePath: (path: string) => boolean;

/**
* @param php - The PHP instance.
Expand All @@ -57,11 +52,9 @@ export class PHPRequestHandler implements RequestHandler {
const {
documentRoot = '/www/',
absoluteUrl = typeof location === 'object' ? location?.href : '',
isStaticFilePath = () => false,
} = config;
this.php = php;
this.#DOCROOT = documentRoot;
this.#isStaticFilePath = isStaticFilePath;

const url = new URL(absoluteUrl);
this.#HOSTNAME = url.hostname;
Expand Down Expand Up @@ -122,25 +115,28 @@ export class PHPRequestHandler implements RequestHandler {
isAbsolute ? undefined : DEFAULT_BASE_URL
);

const normalizedRelativeUrl = removePathPrefix(
const normalizedRequestedPath = removePathPrefix(
requestedUrl.pathname,
this.#PATHNAME
);
if (this.#isStaticFilePath(normalizedRelativeUrl)) {
return this.#serveStaticFile(normalizedRelativeUrl);
const fsPath = `${this.#DOCROOT}${normalizedRequestedPath}`;
if (
this.php.fileExists(fsPath) &&
!this.php.isDir(fsPath) &&
!seemsLikeAPHPRequestHandlerPath(fsPath)
) {
return this.#serveStaticFile(fsPath);
}
return await this.#dispatchToPHP(request, requestedUrl);
}

/**
* Serves a static file from the PHP filesystem.
*
* @param path - The requested static file path.
* @param fsPath - Absolute path of the static file to serve.
* @returns The response.
*/
#serveStaticFile(path: string): PHPResponse {
const fsPath = `${this.#DOCROOT}${path}`;

#serveStaticFile(fsPath: string): PHPResponse {
if (!this.php.fileExists(fsPath)) {
return new PHPResponse(
404,
Expand Down
Loading

0 comments on commit e12988d

Please sign in to comment.