@php-wasm/node 0.1.46 → 0.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs +6 -2
  2. package/index.d.ts +101 -12
  3. package/package.json +4 -4
package/index.cjs CHANGED
@@ -68273,8 +68273,12 @@ var _NodePHP = class extends BasePHP2 {
68273
68273
  /**
68274
68274
  * Starts a PHP CLI session with given arguments.
68275
68275
  *
68276
- * Can only be used when PHP was compiled with the CLI SAPI.
68277
- * Cannot be used in conjunction with `run()`.
68276
+ * This method can only be used when PHP was compiled with the CLI SAPI
68277
+ * and it cannot be used in conjunction with `run()`.
68278
+ *
68279
+ * Once this method finishes running, the PHP instance is no
68280
+ * longer usable and should be discarded. This is because PHP
68281
+ * internally cleans up all the resources and calls exit().
68278
68282
  *
68279
68283
  * @param argv - The arguments to pass to the CLI.
68280
68284
  * @returns The exit code of the CLI session.
package/index.d.ts CHANGED
@@ -105,7 +105,33 @@ export interface RequestHandler {
105
105
  /**
106
106
  * Serves the request – either by serving a static file, or by
107
107
  * dispatching it to the PHP runtime.
108
- * Cannot be used in conjunction with `cli()`.
108
+ *
109
+ * The request() method mode behaves like a web server and only works if
110
+ * the PHP was initialized with a `requestHandler` option (which the online version
111
+ * of WordPress Playground does by default).
112
+ *
113
+ * In the request mode, you pass an object containing the request information
114
+ * (method, headers, body, etc.) and the path to the PHP file to run:
115
+ *
116
+ * ```ts
117
+ * const php = PHP.load('7.4', {
118
+ * requestHandler: {
119
+ * documentRoot: "/www"
120
+ * }
121
+ * })
122
+ * php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
123
+ * const result = await php.run({
124
+ * method: "GET",
125
+ * headers: {
126
+ * "Content-Type": "text/plain"
127
+ * },
128
+ * body: "Hello world!",
129
+ * path: "/www/index.php"
130
+ * });
131
+ * // result.text === "Hello world!"
132
+ * ```
133
+ *
134
+ * The `request()` method cannot be used in conjunction with `cli()`.
109
135
  *
110
136
  * @example
111
137
  * ```js
@@ -152,7 +178,18 @@ export interface RequestHandler {
152
178
  documentRoot: string;
153
179
  }
154
180
  export interface IsomorphicLocalPHP extends RequestHandler {
181
+ /**
182
+ * Sets the path to the php.ini file to use for the PHP instance.
183
+ *
184
+ * @param path - The path to the php.ini file.
185
+ */
155
186
  setPhpIniPath(path: string): void;
187
+ /**
188
+ * Sets a value for a specific key in the php.ini file for the PHP instance.
189
+ *
190
+ * @param key - The key to set the value for.
191
+ * @param value - The value to set for the key.
192
+ */
156
193
  setPhpIniEntry(key: string, value: string): void;
157
194
  /**
158
195
  * Recursively creates a directory with the given path in the PHP filesystem.
@@ -244,24 +281,72 @@ export interface IsomorphicLocalPHP extends RequestHandler {
244
281
  chdir(path: string): void;
245
282
  /**
246
283
  * Runs PHP code.
247
- * Cannot be used in conjunction with `cli()`.
248
284
  *
249
- * @example
250
- * ```js
251
- * const output = await php.run('<?php echo "Hello world!";');
252
- * console.log(output.stdout); // "Hello world!"
285
+ * This low-level method directly interacts with the WebAssembly
286
+ * PHP interpreter.
287
+ *
288
+ * Every time you call run(), it prepares the PHP
289
+ * environment and:
290
+ *
291
+ * * Resets the internal PHP state
292
+ * * Populates superglobals ($_SERVER, $_GET, etc.)
293
+ * * Handles file uploads
294
+ * * Populates input streams (stdin, argv, etc.)
295
+ * * Sets the current working directory
296
+ *
297
+ * You can use run() in two primary modes:
298
+ *
299
+ * ### Code snippet mode
300
+ *
301
+ * In this mode, you pass a string containing PHP code to run.
302
+ *
303
+ * ```ts
304
+ * const result = await php.run({
305
+ * code: `<?php echo "Hello world!";`
306
+ * });
307
+ * // result.text === "Hello world!"
253
308
  * ```
254
309
  *
310
+ * In this mode, information like __DIR__ or __FILE__ isn't very
311
+ * useful because the code is not associated with any file.
312
+ *
313
+ * Under the hood, the PHP snippet is passed to the `zend_eval_string`
314
+ * C function.
315
+ *
316
+ * ### File mode
317
+ *
318
+ * In the file mode, you pass a scriptPath and PHP executes a file
319
+ * found at a that path:
320
+ *
321
+ * ```ts
322
+ * php.writeFile(
323
+ * "/www/index.php",
324
+ * `<?php echo "Hello world!";"`
325
+ * );
326
+ * const result = await php.run({
327
+ * scriptPath: "/www/index.php"
328
+ * });
329
+ * // result.text === "Hello world!"
330
+ * ```
331
+ *
332
+ * In this mode, you can rely on path-related information like __DIR__
333
+ * or __FILE__.
334
+ *
335
+ * Under the hood, the PHP file is executed with the `php_execute_script`
336
+ * C function.
337
+ *
338
+ * The `run()` method cannot be used in conjunction with `cli()`.
339
+ *
255
340
  * @example
256
341
  * ```js
257
- * console.log(await php.run(`<?php
342
+ * const result = await php.run(`<?php
258
343
  * $fp = fopen('php://stderr', 'w');
259
344
  * fwrite($fp, "Hello, world!");
260
- * `));
261
- * // {"exitCode":0,"stdout":"","stderr":["Hello, world!"]}
345
+ * `);
346
+ * // result.errors === "Hello, world!"
262
347
  * ```
263
348
  *
264
- * @param options - PHP run options.
349
+ * @param options - PHP runtime options.
265
350
  */
266
351
  run(options: PHPRunOptions): Promise<PHPResponse>;
267
352
  }
@@ -567,8 +652,12 @@ export declare class NodePHP extends BasePHP {
567
652
  /**
568
653
  * Starts a PHP CLI session with given arguments.
569
654
  *
570
- * Can only be used when PHP was compiled with the CLI SAPI.
571
- * Cannot be used in conjunction with `run()`.
655
+ * This method can only be used when PHP was compiled with the CLI SAPI
656
+ * and it cannot be used in conjunction with `run()`.
657
+ *
658
+ * Once this method finishes running, the PHP instance is no
659
+ * longer usable and should be discarded. This is because PHP
660
+ * internally cleans up all the resources and calls exit().
572
661
  *
573
662
  * @param argv - The arguments to pass to the CLI.
574
663
  * @returns The exit code of the CLI session.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/node",
3
- "version": "0.1.46",
3
+ "version": "0.1.49",
4
4
  "description": "PHP.wasm for Node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,13 +28,13 @@
28
28
  "license": "GPL-2.0-or-later",
29
29
  "main": "index.cjs",
30
30
  "types": "index.d.ts",
31
- "gitHead": "b1e1b4b57b00fb52429f0490af4ccb943f496f74",
31
+ "gitHead": "93834fa77a89c21be2ade4c1d67430981ed92c83",
32
32
  "dependencies": {
33
33
  "comlink": "^4.4.1",
34
34
  "express": "4.18.2",
35
35
  "ws": "8.13.0",
36
36
  "yargs": "17.7.2",
37
- "@php-wasm/universal": "0.1.46",
38
- "@php-wasm/util": "0.1.46"
37
+ "@php-wasm/universal": "0.1.49",
38
+ "@php-wasm/util": "0.1.49"
39
39
  }
40
40
  }