Skip to content

Commit

Permalink
Run setPhpIniEntry('auto_prepend_file' before the first php.run()
Browse files Browse the repository at this point in the history
     file. However, the setPhpIniEntry() was called after the initial
     php.run() which made it noop. This broke the networking support
     merged in #724.

     This commit solves that by moving the setPhpIniEntry right after
     the PHP module is initialized. I am not super happy about exporting
     the virtualized file path from the blueprints module – let's
     revisit that in the future.
  • Loading branch information
adamziel committed Nov 15, 2023
1 parent d34a66b commit 126d4cb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/php-wasm/util/src/lib/index.ts
@@ -1,7 +1,7 @@
import Semaphore from './semaphore';
export { Semaphore };
export type { SemaphoreOptions } from './semaphore';
export { joinPaths, basename } from './paths';
export { joinPaths, basename, dirname } from './paths';
export { createSpawnHandler } from './create-spawn-handler';

export * from './php-vars';
14 changes: 13 additions & 1 deletion packages/php-wasm/util/src/lib/paths.spec.ts
@@ -1,4 +1,4 @@
import { basename, joinPaths, normalizePath } from './paths';
import { basename, dirname, joinPaths, normalizePath } from './paths';

describe('joinPaths', () => {
it('should join paths correctly', () => {
Expand Down Expand Up @@ -50,3 +50,15 @@ describe('basename', () => {
expect(basename('/path/to//file')).toEqual('file');
});
});

describe('dirname', () => {
it('should return the directory name of a path', () => {
expect(dirname('/path/to/file.txt')).toEqual('/path/to');
expect(dirname('/path/to/directory/')).toEqual('/path/to');
expect(dirname('/path/to//file')).toEqual('/path/to');
expect(dirname('/path/to/')).toEqual('/path');
expect(dirname('/path/to')).toEqual('/path');
expect(dirname('/')).toEqual('/');
expect(dirname('')).toEqual('');
});
});
27 changes: 25 additions & 2 deletions packages/php-wasm/util/src/lib/paths.ts
Expand Up @@ -38,7 +38,7 @@ export function joinPaths(...paths: string[]) {
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
return path;
}

/**
Expand All @@ -61,6 +61,29 @@ export function basename(path: string) {
return path.substr(lastSlash + 1);
}

/**
* Returns the directory name of a path.
*
* @param path - The path to extract the directory name from.
* @returns The directory name of the path.
*/
export function dirname(path: string) {
if (path === '/') {
return '/';
}

path = normalizePath(path);

const lastSlash = path.lastIndexOf('/');
if (lastSlash === -1) {
return '';
}
if (lastSlash === 0) {
return '/';
}
return path.substr(0, lastSlash);
}

/**
* Normalizes a path.
*
Expand All @@ -78,7 +101,7 @@ export function normalizePath(path: string) {
path.split('/').filter((p: any) => !!p),
!isAbsolute
).join('/');
return path.replace(/\/$/, '');
return (isAbsolute ? '/' : '') + path.replace(/\/$/, '');
}

/**
Expand Down
Expand Up @@ -2,6 +2,8 @@ import { StepHandler } from '.';
import { updateFile } from './common';

export const VFS_TMP_DIRECTORY = '/vfs-blueprints';
export const phpConstsFilePath = `${VFS_TMP_DIRECTORY}/wp-consts.php`;
export const phpConstsJsonPath = `${VFS_TMP_DIRECTORY}/playground-consts.json`;

/**
* @inheritDoc defineWpConfigConsts
Expand Down Expand Up @@ -46,12 +48,10 @@ export const defineWpConfigConsts: StepHandler<
DefineWpConfigConstsStep
> = async (playground, { consts }) => {
await playground.mkdir(VFS_TMP_DIRECTORY);
const phpConstsFilePath = `${VFS_TMP_DIRECTORY}/wp-consts.php`;
const jsonPath = `${VFS_TMP_DIRECTORY}/playground-consts.json`;
await updateFile(playground, phpConstsFilePath, (contents) => {
if (!contents.includes(jsonPath)) {
if (!contents.includes(phpConstsJsonPath)) {
return `<?php
$consts = json_decode(file_get_contents('${jsonPath}'), true);
$consts = json_decode(file_get_contents('${phpConstsJsonPath}'), true);
foreach ($consts as $const => $value) {
if (!defined($const)) {
define($const, $value);
Expand All @@ -61,13 +61,13 @@ export const defineWpConfigConsts: StepHandler<
}
return contents;
});
await updateFile(playground, jsonPath, (contents) =>
await updateFile(playground, phpConstsJsonPath, (contents) =>
JSON.stringify({
...JSON.parse(contents || '{}'),
...consts,
})
);

await playground.setPhpIniEntry('auto_prepend_file', phpConstsFilePath);
// await playground.setPhpIniEntry('auto_prepend_file', phpConstsFilePath);
return phpConstsFilePath;
};
1 change: 1 addition & 0 deletions packages/playground/blueprints/src/lib/steps/index.ts
Expand Up @@ -24,6 +24,7 @@ import { RunPHPWithOptionsStep } from './run-php-with-options';
import { RequestStep } from './request';
import { WriteFileStep } from './write-file';
import { DefineWpConfigConstsStep } from './define-wp-config-consts';
export { phpConstsFilePath } from './define-wp-config-consts';
import { ActivateThemeStep } from './activate-theme';

export type Step = GenericStep<FileReference>;
Expand Down
14 changes: 13 additions & 1 deletion packages/playground/remote/src/lib/worker-thread.ts
@@ -1,4 +1,5 @@
import { WebPHP, WebPHPEndpoint, exposeAPI } from '@php-wasm/web';
import { dirname } from '@php-wasm/util';
import { EmscriptenDownloadMonitor } from '@php-wasm/progress';
import { setURLScope } from '@php-wasm/scopes';
import { DOCROOT, wordPressSiteUrl } from './config';
Expand All @@ -23,7 +24,10 @@ import {
bindOpfs,
playgroundAvailableInOpfs,
} from './opfs/bind-opfs';
import { applyWordPressPatches } from '@wp-playground/blueprints';
import {
applyWordPressPatches,
phpConstsFilePath,
} from '@wp-playground/blueprints';

// post message to parent
self.postMessage('worker-script-started');
Expand Down Expand Up @@ -190,6 +194,14 @@ const [setApiReady, setAPIError] = exposeAPI(
try {
await phpReady;

// Set up the auto_prepend_file PHP.ini option that is necessary for the
// defineWpConfigConsts Blueprint step to work. We can't define it in that
// step because `setPhpIniEntry` can only be called before the first php.run()
// call. We do it now, because when the step runs it may be too late.
php.mkdir(dirname(phpConstsFilePath));
php.writeFile(phpConstsFilePath, '');
php.setPhpIniEntry('auto_prepend_file', phpConstsFilePath);

if (!wordPressAvailableInOPFS) {
/**
* Patch WordPress when it's not restored from OPFS.
Expand Down

0 comments on commit 126d4cb

Please sign in to comment.