Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP: Use auto_prepend_file to preload mu-plugins (instead of creating them in wp-content/mu-plugins) #1366

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Preload all the PHP files from /internal/preload
  • Loading branch information
adamziel committed May 3, 2024
commit 2bb1393c67412d9ed8a8274fa7b34e81757e0667
3 changes: 3 additions & 0 deletions packages/php-wasm/compile/php/phpwasm-emscripten-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const LibraryExample = {
// stdout, stderr, and headers information are written for the JavaScript
// code to read later on.
FS.mkdir("/internal");
// The files from the preload directory are preloaded using the
// auto_prepend_file php.ini directive.
FS.mkdir("/internal/preload");

PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE
? require('events').EventEmitter
Expand Down
42 changes: 25 additions & 17 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,32 +358,40 @@ export abstract class BasePHP implements IsomorphicLocalPHP, Disposable {
}

#initWebRuntime() {
this.writeFile(
'/internal/auto_prepend_file.php',
new TextEncoder().encode(`<?php
foreach (glob('/internal/preload/*.php') as $file) {
require_once $file;
}
`)
);
this.setPhpIniEntry(
'auto_prepend_file',
'/internal/auto_prepend_file.php'
);
adamziel marked this conversation as resolved.
Show resolved Hide resolved
/**
* This creates a consts.php file in an in-memory
* /internal directory and sets the auto_prepend_file PHP option
* /internal/preload directory and sets the auto_prepend_file PHP option
* to always load that file.
* @see https://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
*
* Technically, this is a workaround. In the future, let's implement a
* WASM SAPI method to pass consts directly.
* @see https://github.com/WordPress/wordpress-playground/issues/750
*/
// this.setPhpIniEntry('auto_prepend_file', '/internal/consts.php');
if (!this.fileExists('/internal/consts.php')) {
this.writeFile(
'/internal/consts.php',
`<?php
if(file_exists('/internal/consts.json')) {
$consts = json_decode(file_get_contents('/internal/consts.json'), true);
foreach ($consts as $const => $value) {
if (!defined($const) && is_scalar($value)) {
define($const, $value);
}
}
}`
);
}

this.writeFile(
'/internal/preload/consts.php',
new TextEncoder().encode(`<?php
if(file_exists('/internal/consts.json')) {
$consts = json_decode(file_get_contents('/internal/consts.json'), true);
foreach ($consts as $const => $value) {
if (!defined($const) && is_scalar($value)) {
define($const, $value);
}
}
}`)
);
if (this.#phpIniOverrides.length > 0) {
const overridesAsIni =
this.#phpIniOverrides
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/web/public/kitchen-sink/php_8_0.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions packages/playground/remote/src/lib/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ const requestHandler = new PHPRequestHandler({
requestHandler.documentRoot
);
}
php.setPhpIniEntry('auto_prepend_file', '/internal/env.php');
php.writeFile(
'/internal/env.php',
'/internal/preload/env.php',
`<?php

// Allow adding filters/actions prior to loading WordPress.
Expand Down Expand Up @@ -257,7 +256,6 @@ try {
// * The mu-plugin is always up to date.
await writeFiles(primaryPhp, joinPaths('/internal/mu-plugins'), {
'0-playground.php': playgroundMuPlugin,
'load-consts.php': `<?php require "/internal/consts.php"; `,
'playground-includes/wp_http_dummy.php': transportDummy,
'playground-includes/wp_http_fetch.php': transportFetch,
});
Expand Down