Skip to content

Commit 0356d50

Browse files
authored
[docs] Removing deprecated run method reference (#2778)
This pull request updates the documentation and usage examples for the PHP WASM library to use the new `runStream` method instead of the deprecated `run` method, reflecting recent API changes. These updates ensure that users and developers are guided to use the latest recommended approach for executing PHP code and handling output streams. **Documentation and Example Updates:** * Updated the JavaScript API Playground documentation to use `php.runStream` in both English and Portuguese versions, replacing the previous `php.run` method. * Revised the Node.js README example to use `php.runStream` and updated the output handling to use `output.stdoutText` instead of `response.text`. * Modified the Web README example to use `php.runStream` for running scripts, ensuring consistency with the new API.
1 parent 8809931 commit 0356d50

File tree

6 files changed

+454
-22
lines changed

6 files changed

+454
-22
lines changed

packages/docs/site/docs/developers/06-apis/javascript-api/03-playground-api-client.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ You can pass messages from PHP to JavaScript using the `post_message_to_js()` fu
7777

7878
For example, here's how you would send a message with a JSON-encoded post ID and title:
7979

80-
```ts
81-
const php = await PHP.load('8.0');
80+
```TypeScript
81+
import { PHP } from '@php-wasm/universal';
82+
import { loadNodeRuntime } from '@php-wasm/node';
83+
84+
const php = new PHP(await loadNodeRuntime('8.3'));
8285

8386
php.onMessage(
8487
// The data is always passed as a string
@@ -90,14 +93,14 @@ php.onMessage(
9093

9194
// Now that we have a listener in place, let's
9295
// dispatch a message:
93-
await php.run({
96+
await php.runStream({
9497
code: `<?php
9598
post_message_to_js(
9699
json_encode([
97100
'post_id' => '15',
98101
'post_title' => 'This is a blog post!'
99102
])
100-
));
103+
);
101104
`,
102105
});
103106

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
---
2+
slug: /developers/apis/javascript-api/playground-api-client
3+
---
4+
5+
<!--
6+
# Playground API Client
7+
-->
8+
9+
# Cliente API de Playground
10+
11+
<!--
12+
The `PlaygroundClient` object implements the `UniversalPHP` interface. All the methods from that interface are also available in Node.js and same-process PHP instances (Playground runs PHP in a web worker).
13+
-->
14+
15+
El objeto `PlaygroundClient` implementa la interfaz `UniversalPHP`. Todos los métodos de esa interfaz también están disponibles en Node.js e instancias PHP del mismo proceso (Playground ejecuta PHP en un web worker).
16+
17+
<!--
18+
Broadly speaking, you can use the client to perform three types of operations:
19+
20+
- Running PHP code
21+
- Customizing `PHP.ini`
22+
- Managing files and directories
23+
-->
24+
25+
En términos generales, puedes usar el cliente para realizar tres tipos de operaciones:
26+
27+
- Ejecutar código PHP
28+
- Personalizar `PHP.ini`
29+
- Gestionar archivos y directorios
30+
31+
<!--
32+
## Running PHP code
33+
-->
34+
35+
## Ejecutar código PHP
36+
37+
<!--
38+
The two methods you can use to run PHP code are:
39+
40+
- [`run()`](#the-run-method) - runs PHP code and returns the output
41+
- [`request()`](#the-request-method) - makes an HTTP request to the website
42+
-->
43+
44+
Los dos métodos que puedes usar para ejecutar código PHP son:
45+
46+
- [`run()`](#the-run-method) - ejecuta código PHP y devuelve la salida
47+
- [`request()`](#the-request-method) - realiza una solicitud HTTP al sitio web
48+
49+
<!--
50+
In Node.js, you can also use the [`cli()`](#the-cli-method) method to run PHP in a CLI mode.
51+
-->
52+
53+
En Node.js, también puedes usar el método [`cli()`](#the-cli-method) para ejecutar PHP en modo CLI.
54+
55+
<!--
56+
### The `run()` method
57+
-->
58+
59+
### El método `run()`
60+
61+
import TSDocstring from '@site/src/components/TSDocstring';
62+
63+
<TSDocstring path={[ "@wp-playground/client", "PlaygroundClient", "run" ]} />
64+
65+
<!--
66+
### The `request()` method
67+
-->
68+
69+
### El método `request()`
70+
71+
<TSDocstring path={[ "@wp-playground/client", "PlaygroundClient", "request" ]} />
72+
73+
<!--
74+
## Customizing `PHP.ini`
75+
-->
76+
77+
## Personalizar `PHP.ini`
78+
79+
<!--
80+
The API client also allows you to change the `php.ini` file:
81+
-->
82+
83+
El cliente API también te permite cambiar el archivo `php.ini`:
84+
85+
```ts
86+
await setPhpIniEntries(client, {
87+
display_errors: 'On',
88+
error_reporting: 'E_ALL',
89+
});
90+
```
91+
92+
<!--
93+
## Managing files and directories
94+
-->
95+
96+
## Gestionar archivos y directorios
97+
98+
<!--
99+
The `client` object provides you with a low-level API for managing files and directories in the PHP filesystem:
100+
-->
101+
102+
El objeto `client` te proporciona una API de bajo nivel para gestionar archivos y directorios en el sistema de archivos PHP:
103+
104+
```ts
105+
await client.mkdirTree('/wordpress/test');
106+
// Create a new PHP file
107+
await client.writeFile(
108+
'/wordpress/test/index.php',
109+
`<?php
110+
echo "Hello, world!<br/>";
111+
// List all the files in current directory
112+
print_r(glob(__DIR__ . '/*'));
113+
`
114+
);
115+
// Create files named 1, 2, and 3
116+
await client.writeFile('/wordpress/test/1', '');
117+
await client.writeFile('/wordpress/test/2', '');
118+
await client.writeFile('/wordpress/test/3', '');
119+
// Remove the file named 1
120+
await client.unlink('/wordpress/test/1');
121+
// Navigate to our PHP file
122+
await client.goTo('/test/index.php');
123+
```
124+
125+
<!--
126+
For a complete list of these methods, refer to the `PlaygroundClient` interface.
127+
-->
128+
129+
Para obtener una lista completa de estos métodos, consulta la interfaz `PlaygroundClient`.
130+
131+
<!--
132+
## Sending messages to JavaScript
133+
-->
134+
135+
## Enviar mensajes a JavaScript
136+
137+
<!--
138+
You can pass messages from PHP to JavaScript using the `post_message_to_js()` function. It accepts one argument:
139+
140+
- `$data` (string) – Data to pass to JavaScript.
141+
-->
142+
143+
Puedes pasar mensajes de PHP a JavaScript usando la función `post_message_to_js()`. Acepta un argumento:
144+
145+
- `$data` (string) – Datos para pasar a JavaScript.
146+
147+
<!--
148+
For example, here's how you would send a message with a JSON-encoded post ID and title:
149+
-->
150+
151+
Por ejemplo, así es como enviarías un mensaje con un ID de publicación y un título codificado en JSON:
152+
153+
```TypeScript
154+
import { PHP } from '@php-wasm/universal';
155+
import { loadNodeRuntime } from '@php-wasm/node';
156+
157+
const php = new PHP(await loadNodeRuntime('8.3'));
158+
159+
php.onMessage(
160+
// The data is always passed as a string
161+
function (data: string) {
162+
// Let's decode and log the data:
163+
console.log(JSON.parse(data));
164+
}
165+
);
166+
167+
// Now that we have a listener in place, let's
168+
// dispatch a message:
169+
const output = await php.runStream({
170+
code: `<?php
171+
post_message_to_js(
172+
json_encode([
173+
'post_id' => '15',
174+
'post_title' => 'This is a blog post!'
175+
])
176+
);
177+
`,
178+
});
179+
180+
console.log(await output.stdoutText);
181+
// You will see the following output in the console:
182+
// { post_id: '15', post_title: 'This is a blog post!' }
183+
```
184+
185+
<!--
186+
## The `cli()` method
187+
-->
188+
189+
## El método `cli()`
190+
191+
<!--
192+
In Node.js, you also have access to the `cli()` method that runs PHP in a CLI mode:
193+
-->
194+
195+
En Node.js, también tienes acceso al método `cli()` que ejecuta PHP en modo CLI:
196+
197+
```ts
198+
// Run PHP in a CLI mode
199+
client.cli(['-r', 'echo "Hello, world!";']);
200+
// Outputs "Hello, world!"
201+
```
202+
203+
<!--
204+
Once `cli()` method finishes running, the PHP instance is no longer usable and should be discarded. This is because PHP internally cleans up all the resources and calls `exit()`.
205+
-->
206+
207+
Una vez que el método `cli()` termina de ejecutarse, la instancia de PHP ya no es utilizable y debe descartarse. Esto se debe a que PHP internamente limpia todos los recursos y llama a `exit()`.

0 commit comments

Comments
 (0)