@li880103/php-8.2
v1.0.0
Published
PHP 8.2 WASM 引擎 — 纯 Node.js 运行,无需系统 PHP,npm install 后直接 require 使用
Readme
@li880103/php-8.2
在 Node.js 中直接运行 PHP 8.2,无需安装系统 PHP。底层为 WordPress Playground 的 php-wasm(GPL-2.0-or-later)打包的 PHP 8.2,WASM 文件随依赖自动安装。
安装
npm install @li880103/php-8.2要求 Node.js >= 20.10。
用法
const php82 = require('@li880103/php-8.2');
// 1. 执行 PHP 代码字符串
const a = await php82.evalPhp('<?php echo "你好 ", PHP_VERSION; ?>');
console.log(a); // "你好 8.2.x"
// 2. 执行项目里的 PHP 文件(自动把文件所在目录挂载进 WASM 虚拟文件系统)
const b = await php82.execFile('./views/index.php');
console.log(b);
// 3. 显式挂载目录,之后 execFile 走该虚拟路径
await php82.mount('./public', '/app');
const c = await php82.execFile('/app/hello.php');
// 4. 更多信息
const { text, exitCode, errors } = await php82.execFile('./a.php', { full: true });
// 5. 底层实例(高级用法,可用 php.runStream / php.request 等)
const php = await php82.getInstance();API
init(): Promise<PHP>初始化引擎(懒加载,幂等)evalPhp(code: string): Promise<string>执行 PHP 代码,返回 stdoutexecFile(filePath, { full? }): Promise<string | { text, exitCode, errors, phpVersion }>执行宿主文件mount(localDir, targetPath = '/app'): Promise<Unmount>把宿主目录挂载为虚拟路径getInstance(): Promise<PHP>获取底层 @php-wasm/universal 的 PHP 实例
注意
- 首次加载需编译 20MB+ WASM,耗时数秒,之后实例复用
- 同一进程可同时安装多个 @li880103/php-* 版本包并行使用,互不干扰
- 本包装配代码为 MIT;PHP 引擎本体来自 GPL-2.0-or-later 的 php-wasm
