From f038a9a850ba9c059b46e92dc2fe20820328a2cb Mon Sep 17 00:00:00 2001 From: shimotmk Date: Sun, 2 Nov 2025 14:30:39 +0900 Subject: [PATCH] [i18n] Add Japanese translations to Playground API Client --- .../03-playground-api-client.md | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 packages/docs/site/i18n/ja/docusaurus-plugin-content-docs/current/developers/06-apis/javascript-api/03-playground-api-client.md diff --git a/packages/docs/site/i18n/ja/docusaurus-plugin-content-docs/current/developers/06-apis/javascript-api/03-playground-api-client.md b/packages/docs/site/i18n/ja/docusaurus-plugin-content-docs/current/developers/06-apis/javascript-api/03-playground-api-client.md new file mode 100644 index 0000000000..6fbfa4c230 --- /dev/null +++ b/packages/docs/site/i18n/ja/docusaurus-plugin-content-docs/current/developers/06-apis/javascript-api/03-playground-api-client.md @@ -0,0 +1,212 @@ +--- +slug: /developers/apis/javascript-api/playground-api-client +--- + +# Playground API クライアント + + + +`PlaygroundClient` オブジェクトは `UniversalPHP` インターフェースを実装しています。このインターフェースのすべてのメソッドは、Node.js および同一プロセス内の PHP インスタンスでも利用可能です( Playground は Web Worker 内で PHP を実行します)。 + + + +大まかに言うと、クライアントを使用して 3 種類の操作を実行できます。 + + + +- PHP コードの実行 +- `PHP.ini`のカスタマイズ +- ファイルとディレクトリの管理 + + + +## PHP コードの実行 + + + +PHP コードを実行するには、以下の 2 つの方法があります。 + + + +- [`run()`](#the-run-method) - PHP コードを実行し、その出力を返します。 +- [`request()`](#the-request-method) - ウェブサイトに HTTP リクエストを送信します。 + + + +Node.js では、[`cli()`](#the-cli-method)メソッドを使用して PHP を CLI モードで実行することもできます。 + + + +### `run()` メソッド + + + +import TSDocstring from '@site/src/components/TSDocstring'; + + + +### `request()` メソッド + + + + + +## `PHP.ini`のカスタマイズ + + + +この API クライアントを使用すると、`p​​hp.ini`ファイルを変更することもできます。 + + + +```ts +await setPhpIniEntries(client, { + display_errors: 'On', + error_reporting: 'E_ALL', +}); +``` + +## ファイルとディレクトリの管理 + + + +`client` オブジェクトは、PHP ファイルシステム内のファイルとディレクトリを管理するための低レベル API を提供します。 + + + +```ts +await client.mkdirTree('/wordpress/test'); +// Create a new PHP file +await client.writeFile( + '/wordpress/test/index.php', + `"; + // List all the files in current directory + print_r(glob(__DIR__ . '/*')); + ` +); +// Create files named 1, 2, and 3 +await client.writeFile('/wordpress/test/1', ''); +await client.writeFile('/wordpress/test/2', ''); +await client.writeFile('/wordpress/test/3', ''); +// Remove the file named 1 +await client.unlink('/wordpress/test/1'); +// Navigate to our PHP file +await client.goTo('/test/index.php'); +``` + +これらのメソッドの完全なリストについては、`PlaygroundClient` インターフェースを参照してください。 + + + +## JavaScript にメッセージを送信する + + + +PHP から JavaScript にメッセージを渡すには、`post_message_to_js()` 関数を使用します。この関数は引数を 1 つ取ります。 + + + +- `$data` (文字列) – JavaScript に渡すデータ。 + + + +例えば、JSON 形式でエンコードされた投稿 ID とタイトルを含むメッセージを送信するには、次のようにします。 + + + +```TypeScript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +php.onMessage( + // The data is always passed as a string + function (data: string) { + // Let's decode and log the data: + console.log(JSON.parse(data)); + } +); + +// Now that we have a listener in place, let's +// dispatch a message: +await php.runStream({ + code: ` '15', + 'post_title' => 'This is a blog post!' + ]) + ); + `, +}); + +// You will see the following output in the console: +// { post_id: '15', post_title: 'This is a blog post!' } +``` + +## `cli()` メソッド + + + +Node.js では、PHP を CLI モードで実行する `cli()` メソッドにもアクセスできます。 + + + +```ts +// Run PHP in a CLI mode +client.cli(['-r', 'echo "Hello, world!";']); +// Outputs "Hello, world!" +``` + +`cli()` メソッドの実行が完了すると、PHP インスタンスは使用できなくなるため、破棄する必要があります。これは、PHP が内部的にすべてのリソースを解放し、`exit()`を呼び出すためです。 + +