From bfafb5cace489842ed6003233193809a3b9e0ca6 Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Tue, 14 Oct 2025 22:20:44 +0100 Subject: [PATCH 1/8] adding examples on the php wasm page --- .../05-local-development/03-php-wasm-node.md | 641 +++++++++++++++++- 1 file changed, 631 insertions(+), 10 deletions(-) diff --git a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md index cc75499dc2..a98621ffe5 100644 --- a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md @@ -1,20 +1,641 @@ +# Using WordPress Playground in Node.js + +As a WebAssembly project, you can also use WordPress Playground in Node.js. + +If you need low-level control over the underlying WebAssembly PHP build, take a look at the [@php-wasm/node package](https://npmjs.org/@php-wasm/node) which ships the PHP WebAssembly runtime. This package is at the core of all WordPress Playground tools for Node.js. + +Consult the [complete list](/api/node) of Classes, Functions, Interfaces, and Type Aliases. + --- -title: php-wasm/node -slug: /developers/local-development/php-wasm-node + +## WebAssembly PHP for Node.js + +This package ships WebAssembly PHP binaries and the JavaScript API optimized for Node.js. It uses the host filesystem directly and can access the network if you plug in a custom WS proxy. + +### Basic Usage + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); +const output = await php.runStream({ + code: '', +}); +console.log(await output.stdoutText); +``` + +## Use Cases + +### 1. **Server-Side PHP Execution** + +Run PHP scripts in Node.js environments without installing PHP natively. Perfect for: + +- CI/CD pipelines that need PHP execution +- Testing PHP code in JavaScript-based test suites +- Microservices that occasionally need PHP functionality +- Development tools and build scripts + +### 2. **Data Processing & Transformation** + +Process and manipulate data using PHP's rich ecosystem: + +- CSV, JSON, and XML data transformation +- SQLite database operations without external dependencies +- Archive creation and extraction (ZIP, TAR) +- Image processing and manipulation + +### 3. **Template Rendering & Content Generation** + +Generate dynamic content using PHP templates: + +- Email template rendering +- HTML report generation +- Documentation generation +- Dynamic configuration file creation + +### 4. **API Development & Testing** + +Build and test API endpoints: + +- Mock API servers for testing +- Request/response simulation +- API endpoint prototyping +- Integration testing for PHP APIs + +### 5. **PHP Code Analysis & Testing** + +Build tools that analyze, lint, or test PHP code: + +- Static analysis tools +- Code formatters and validators +- Unit test runners +- Documentation generators + +### 6. **Legacy Code Integration** + +Bridge PHP and JavaScript ecosystems: + +- Migrate PHP applications incrementally +- Use PHP libraries in JavaScript projects +- Run PHP alongside modern JavaScript frameworks +- Provide backward compatibility + +### 7. **Educational Platforms** + +Create interactive PHP learning environments: + +- Online coding tutorials +- Interactive documentation +- Code playgrounds and sandboxes +- Programming challenges + +### 8. **WordPress Development Tools** + +Build WordPress development utilities: + +- Plugin/theme validators +- WordPress CLI alternatives +- Development environment bootstrapping +- Automated testing tools + +## Practical Demos + +### Demo 1: File System Operations + +Execute PHP scripts that interact with the filesystem: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create directory structure +php.mkdirTree('/app/data'); + +// Write configuration file +await php.writeFile( + '/app/config.json', + JSON.stringify({ + app: 'MyApp', + version: '1.0.0', + debug: true, + }) +); + +// Create and run PHP script that reads the config +await php.writeFile( + '/app/index.php', + `` +); + +const result = await php.runStream({ scriptPath: '/app/index.php' }); +console.log(await result.stdoutText); +``` + +### Demo 2: SQLite Database Operations + +Use PHP's SQLite extension for data storage: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create directory for database +php.mkdirTree('/data'); + +// Create database, insert data, and query +const result = await php.runStream({ + code: `exec('CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + email TEXT UNIQUE NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP +)'); + +// Insert sample data +$stmt = $db->prepare('INSERT INTO users (name, email) VALUES (?, ?)'); +$users = [ + ['Alice Johnson', 'alice@example.com'], + ['Bob Smith', 'bob@example.com'], + ['Charlie Davis', 'charlie@example.com'] +]; + +foreach ($users as $user) { + $stmt->bindValue(1, $user[0]); + $stmt->bindValue(2, $user[1]); + $stmt->execute(); +} + +// Query data +echo "All Users:\\n"; +echo str_repeat('-', 50) . "\\n"; +$results = $db->query('SELECT * FROM users ORDER BY name'); +while ($row = $results->fetchArray(SQLITE3_ASSOC)) { + echo "ID: {$row['id']} | {$row['name']} ({$row['email']})\\n"; +} + +$db->close(); +?>`, +}); + +console.log(await result.stdoutText); + +// Database file persists in the virtual filesystem +const dbExists = await php.fileExists('/data/app.db'); +console.log('\nDatabase persisted:', dbExists); +``` + +### Demo 3: Processing Uploaded Files (ZIP Archives) + +Process ZIP files using PHP's Libzip extension: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create sample files +php.mkdirTree('/uploads'); +await php.writeFile('/uploads/readme.txt', 'This is a sample text file'); +await php.writeFile('/uploads/data.json', JSON.stringify({ name: 'Test', version: '1.0' })); + +// Create, process, and extract ZIP archive +const result = await php.runStream({ + code: `open('/uploads/archive.zip', ZipArchive::CREATE); +$zip->addFromString('readme.txt', file_get_contents('/uploads/readme.txt')); +$zip->addFromString('data.json', file_get_contents('/uploads/data.json')); +$zip->addFromString('info.txt', 'Created with PHP WASM'); +$zip->close(); + +echo "ZIP archive created successfully\\n\\n"; + +// Read and display archive contents +$zip->open('/uploads/archive.zip'); +echo "Archive Contents:\\n"; +echo str_repeat('=', 50) . "\\n"; + +for ($i = 0; $i < $zip->numFiles; $i++) { + $stat = $zip->statIndex($i); + $size = round($stat['size'] / 1024, 2); + echo sprintf("%-40s %10s KB\\n", $stat['name'], $size); +} + +// Extract files +$zip->extractTo('/uploads/extracted/'); +$zip->close(); + +echo "\\nExtracted successfully to /uploads/extracted/\\n"; + +// List extracted files +echo "\\nExtracted Files:\\n"; +$files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator('/uploads/extracted/') +); +foreach ($files as $file) { + if ($file->isFile()) { + echo " " . $file->getPathname() . "\\n"; + } +} +?>`, +}); + +console.log(await result.stdoutText); +``` + +### Demo 4: HTTP Request/Response Pattern + +Simulate web server behavior with request handlers: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Set up a simple API endpoint +await php.mkdirTree('/www/api'); +await php.writeFile( + '/www/api/users.php', + ` [ + ['id' => 1, 'name' => 'John Doe'], + ['id' => 2, 'name' => 'Jane Smith'] + ] + ]); + break; + + case 'POST': + $name = $input['name'] ?? 'Unknown'; + echo json_encode([ + 'success' => true, + 'user' => [ + 'id' => 3, + 'name' => $name + ], + 'message' => "User $name created" + ]); + break; + + default: + http_response_code(405); + echo json_encode(['error' => 'Method not allowed']); +} +?>` +); + +// Simulate GET request +const getResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'GET', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, +}); +console.log('GET Response:', await getResponse.stdoutText); + +// Simulate POST request +const postResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'POST', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, + body: JSON.stringify({ name: 'Alice Wonder' }), +}); +console.log('\\nPOST Response:', await postResponse.stdoutText); +``` + +### Demo 5: Template Rendering Engine + +Use PHP as a templating engine for dynamic content: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create templates directory +php.mkdirTree('/templates'); + +// Create template +await php.writeFile( + '/templates/email.php', + ` + + + + + +
+

Welcome, !

+
+
+

Thank you for registering with .

+

Your account details:

+ +

You now have access to the following features:

+ +
+ + +` +); + +// Render template with data +const templateData = { + name: 'John Developer', + email: 'john@example.com', + appName: 'MyAwesomeApp', + timestamp: Math.floor(Date.now() / 1000), + features: ['Dashboard Access', 'API Integration', 'Premium Support', 'Custom Branding'], +}; + +// Pass data to template via environment variables or files +await php.writeFile('/template-data.json', JSON.stringify(templateData)); + +const result = await php.runStream({ + code: ``, +}); + +console.log(await result.stdoutText); +// Now you have rendered HTML that can be sent via email or saved +``` + +### Demo 6: Real-time Code Execution & Streaming + +Process PHP output as it's generated: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +await php.writeFile( + '/stream-demo.php', + `` +); + +// Run PHP script +const streamedResponse = await php.runStream({ + scriptPath: '/stream-demo.php', +}); + +console.log('Processing PHP output:\n'); +console.log(await streamedResponse.stdoutText); +console.log(`\nExit code: ${streamedResponse.exitCode}`); +``` + --- -# Using WordPress Playground in Node.js +## Integration Patterns -As a WebAssembly project, you can also use WordPress Playground in Node.js. +### Pattern 1: Express.js Middleware -If you need low-level control over the underlying WebAssembly PHP build, take a look at the [`@php-wasm/node` package](https://npmjs.org/@php-wasm/node) which ships the PHP WebAssembly runtime. This package is at the core of all WordPress Playground tools for Node.js. +Integrate PHP processing into an Express.js application: -:::info **API reference** +```TypeScript +import express from 'express'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; -Consult the [complete list](/api/node) of Classes, Functions, Interfaces, and Type Aliases. +const app = express(); +const php = new PHP(await loadNodeRuntime('8.3')); + +// PHP execution middleware +app.use('/php', async (req, res, next) => { + try { + const phpScript = req.query.script || 'index.php'; + const result = await php.runStream({ + scriptPath: `/www/${phpScript}`, + env: { + REQUEST_METHOD: req.method, + QUERY_STRING: new URLSearchParams(req.query as Record).toString(), + REQUEST_URI: req.url, + }, + }); + + res.send(await result.stdoutText); + } catch (error) { + next(error); + } +}); + +app.listen(3000, () => { + console.log('Server with PHP support running on port 3000'); +}); +``` -::: +### Pattern 2: Automated Testing + +Create automated tests for PHP code: + +```TypeScript +import { describe, it, expect, beforeAll } from '@jest/globals'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +describe('PHP Functions', () => { + let php: PHP; + + beforeAll(async () => { + php = new PHP(await loadNodeRuntime('8.3')); + }); + + it('should calculate sum correctly', async () => { + const result = await php.run({ + code: ``, + }); + + expect(result.text).toBe('8'); + }); + + it('should handle JSON operations', async () => { + const input = { name: 'Test', value: 42 }; + const result = await php.run({ + code: ` $input, + 'doubled' => $input['value'] * 2 + ]; + echo json_encode($output); + ?>`, + }); + + const output = JSON.parse(result.text); + expect(output.doubled).toBe(84); + }); +}); +``` + +### Pattern 3: Build Tool Integration + +Use in build scripts with other Node.js tools: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; +import fs from 'fs/promises'; + +async function generateDocumentation() { + const php = new PHP(await loadNodeRuntime('8.3')); + + // Create output directory + php.mkdirTree('/output'); + + // Generate documentation + const result = await php.runStream({ + code: ``, + }); + + console.log(await result.stdoutText); + + // Extract generated docs back to Node.js filesystem + await fs.mkdir('./docs', { recursive: true }); + const summaryContent = await php.readFileAsText('/output/summary.md'); + await fs.writeFile('./docs/summary.md', summaryContent); + + console.log('Documentation saved to ./docs/summary.md'); +} + +generateDocumentation().catch(console.error); +``` + +## Advanced Features + +### Working with Environment Variables + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +const result = await php.runStream({ + code: '', + env: { + CUSTOM_VAR: 'Hello from Node.js!', + }, +}); + +console.log(await result.stdoutText); +``` + +### Error Handling + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +try { + const result = await php.runStream({ + code: '', + }); + + const stdout = await result.stdoutText; + const stderr = await result.stderrText; + + console.log('stdout:', stdout); + console.log('stderr:', stderr); + + if (stderr) { + console.error('PHP produced errors:', stderr); + } +} catch (error: any) { + console.error('JavaScript Error:', error.message); +} +``` + +--- -import PHPWASMNode from '@php-wasm/node/\README.md'; +## Performance Considerations - +- **Reuse PHP instances**: Creating a new PHP instance is expensive. Reuse the same instance when possible. +- **Batch operations**: Group multiple file operations together rather than running separate scripts. +- **Memory management**: Large files may impact performance. Consider streaming for big datasets. +- **Caching**: Cache compiled PHP scripts and frequently accessed data. From ed3eea1726b89b1e8de6592fb8e5c08403c31716 Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Wed, 15 Oct 2025 14:13:04 +0100 Subject: [PATCH 2/8] add description and removing deprecated function --- .../05-local-development/03-php-wasm-node.md | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md index a98621ffe5..b442026c3c 100644 --- a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md @@ -1,3 +1,9 @@ +--- +title: php-wasm/node +slug: /developers/local-development/php-wasm-node +description: WordPress Playground brings WebAssembly-powered PHP to Node.js for server-side execution, data processing, and testing without a native install. +--- + # Using WordPress Playground in Node.js As a WebAssembly project, you can also use WordPress Playground in Node.js. @@ -6,8 +12,6 @@ If you need low-level control over the underlying WebAssembly PHP build, take a Consult the [complete list](/api/node) of Classes, Functions, Interfaces, and Type Aliases. ---- - ## WebAssembly PHP for Node.js This package ships WebAssembly PHP binaries and the JavaScript API optimized for Node.js. It uses the host filesystem directly and can access the network if you plug in a custom WS proxy. @@ -112,7 +116,7 @@ import { loadNodeRuntime } from '@php-wasm/node'; const php = new PHP(await loadNodeRuntime('8.3')); // Create directory structure -php.mkdirTree('/app/data'); +php.mkdir('/app/data'); // Write configuration file await php.writeFile( @@ -158,7 +162,7 @@ import { loadNodeRuntime } from '@php-wasm/node'; const php = new PHP(await loadNodeRuntime('8.3')); // Create directory for database -php.mkdirTree('/data'); +php.mkdir('/data'); // Create database, insert data, and query const result = await php.runStream({ @@ -218,7 +222,7 @@ import { loadNodeRuntime } from '@php-wasm/node'; const php = new PHP(await loadNodeRuntime('8.3')); // Create sample files -php.mkdirTree('/uploads'); +php.mkdir('/uploads'); await php.writeFile('/uploads/readme.txt', 'This is a sample text file'); await php.writeFile('/uploads/data.json', JSON.stringify({ name: 'Test', version: '1.0' })); @@ -279,7 +283,7 @@ import { loadNodeRuntime } from '@php-wasm/node'; const php = new PHP(await loadNodeRuntime('8.3')); // Set up a simple API endpoint -await php.mkdirTree('/www/api'); +await php.mkdir('/www/api'); await php.writeFile( '/www/api/users.php', ` Date: Tue, 28 Oct 2025 09:21:25 +0000 Subject: [PATCH 3/8] Applying documentation standards --- .../05-local-development/03-php-wasm-node.md | 90 +++++++++---------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md index b442026c3c..b12b4e835a 100644 --- a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md @@ -14,9 +14,9 @@ Consult the [complete list](/api/node) of Classes, Functions, Interfaces, and Ty ## WebAssembly PHP for Node.js -This package ships WebAssembly PHP binaries and the JavaScript API optimized for Node.js. It uses the host filesystem directly and can access the network if you plug in a custom WS proxy. +This package ships WebAssembly PHP binaries and the JavaScript API optimized for Node.js. It uses the host file system directly and can access the network if you plug in a custom WS proxy. -### Basic Usage +### Basic usage ```javascript import { PHP } from '@php-wasm/universal'; @@ -29,85 +29,85 @@ const output = await php.runStream({ console.log(await output.stdoutText); ``` -## Use Cases +## Use cases -### 1. **Server-Side PHP Execution** +### Server-side PHP execution -Run PHP scripts in Node.js environments without installing PHP natively. Perfect for: +You can run PHP scripts in Node.js environments without installing PHP natively. Perfect for: - CI/CD pipelines that need PHP execution - Testing PHP code in JavaScript-based test suites - Microservices that occasionally need PHP functionality - Development tools and build scripts -### 2. **Data Processing & Transformation** +### Data processing and transformation -Process and manipulate data using PHP's rich ecosystem: +You can process and manipulate data using PHP's rich ecosystem: - CSV, JSON, and XML data transformation - SQLite database operations without external dependencies - Archive creation and extraction (ZIP, TAR) - Image processing and manipulation -### 3. **Template Rendering & Content Generation** +### Template rendering and content generation -Generate dynamic content using PHP templates: +You can generate dynamic content using PHP templates: - Email template rendering - HTML report generation - Documentation generation - Dynamic configuration file creation -### 4. **API Development & Testing** +### API development and testing -Build and test API endpoints: +You can build and test API endpoints: - Mock API servers for testing - Request/response simulation - API endpoint prototyping - Integration testing for PHP APIs -### 5. **PHP Code Analysis & Testing** +### PHP code analysis and testing -Build tools that analyze, lint, or test PHP code: +You can build tools that analyze, lint, or test PHP code: - Static analysis tools - Code formatters and validators - Unit test runners - Documentation generators -### 6. **Legacy Code Integration** +### Legacy code integration -Bridge PHP and JavaScript ecosystems: +You can bridge PHP and JavaScript ecosystems: - Migrate PHP applications incrementally - Use PHP libraries in JavaScript projects - Run PHP alongside modern JavaScript frameworks - Provide backward compatibility -### 7. **Educational Platforms** +### Educational platforms -Create interactive PHP learning environments: +You can create interactive PHP learning environments: - Online coding tutorials - Interactive documentation - Code playgrounds and sandboxes - Programming challenges -### 8. **WordPress Development Tools** +### WordPress development tools -Build WordPress development utilities: +You can build WordPress development utilities: - Plugin/theme validators - WordPress CLI alternatives - Development environment bootstrapping - Automated testing tools -## Practical Demos +## Practical demos -### Demo 1: File System Operations +### Demo 1: File system operations -Execute PHP scripts that interact with the filesystem: +Execute PHP scripts that interact with the file system: ```javascript import { PHP } from '@php-wasm/universal'; @@ -151,7 +151,7 @@ const result = await php.runStream({ scriptPath: '/app/index.php' }); console.log(await result.stdoutText); ``` -### Demo 2: SQLite Database Operations +### Demo 2: SQLite database operations Use PHP's SQLite extension for data storage: @@ -206,12 +206,12 @@ $db->close(); console.log(await result.stdoutText); -// Database file persists in the virtual filesystem +// Database file persists in the virtual file system const dbExists = await php.fileExists('/data/app.db'); console.log('\nDatabase persisted:', dbExists); ``` -### Demo 3: Processing Uploaded Files (ZIP Archives) +### Demo 3: Processing uploaded files (ZIP archives) Process ZIP files using PHP's Libzip extension: @@ -272,7 +272,7 @@ foreach ($files as $file) { console.log(await result.stdoutText); ``` -### Demo 4: HTTP Request/Response Pattern +### Demo 4: HTTP request/response pattern Simulate web server behavior with request handlers: @@ -323,7 +323,7 @@ switch ($method) { ?>` ); -// Simulate GET request +// Make GET request const getResponse = await php.runStream({ scriptPath: '/www/api/users.php', env: { @@ -334,7 +334,7 @@ const getResponse = await php.runStream({ }); console.log('GET Response:', await getResponse.stdoutText); -// Simulate POST request +// Make POST request const postResponse = await php.runStream({ scriptPath: '/www/api/users.php', env: { @@ -347,7 +347,7 @@ const postResponse = await php.runStream({ console.log('\\nPOST Response:', await postResponse.stdoutText); ``` -### Demo 5: Template Rendering Engine +### Demo 5: Template rendering engine Use PHP as a templating engine for dynamic content: @@ -400,8 +400,8 @@ await php.writeFile( // Render template with data const templateData = { - name: 'John Developer', - email: 'john@example.com', + name: 'Priya Sharma', + email: 'priya@example.com', appName: 'MyAwesomeApp', timestamp: Math.floor(Date.now() / 1000), features: ['Dashboard Access', 'API Integration', 'Premium Support', 'Custom Branding'], @@ -422,7 +422,7 @@ console.log(await result.stdoutText); // Now you have rendered HTML that can be sent via email or saved ``` -### Demo 6: Real-time Code Execution & Streaming +### Demo 6: Real-time code execution and streaming Process PHP output as it's generated: @@ -459,11 +459,9 @@ console.log(await streamedResponse.stdoutText); console.log(`\nExit code: ${streamedResponse.exitCode}`); ``` ---- - -## Integration Patterns +## Integration patterns -### Pattern 1: Express.js Middleware +### Pattern 1: Express.js middleware Integrate PHP processing into an Express.js application: @@ -483,7 +481,9 @@ app.use('/php', async (req, res, next) => { scriptPath: `/www/${phpScript}`, env: { REQUEST_METHOD: req.method, - QUERY_STRING: new URLSearchParams(req.query as Record).toString(), + QUERY_STRING: new URLSearchParams( + req.query as Record + ).toString(), REQUEST_URI: req.url, }, }); @@ -499,7 +499,7 @@ app.listen(3000, () => { }); ``` -### Pattern 2: Automated Testing +### Pattern 2: Automated testing Create automated tests for PHP code: @@ -547,7 +547,7 @@ describe('PHP Functions', () => { }); ``` -### Pattern 3: Build Tool Integration +### Pattern 3: Build tool integration Use in build scripts with other Node.js tools: @@ -577,7 +577,7 @@ echo "Documentation generated successfully!\\n"; console.log(await result.stdoutText); - // Extract generated docs back to Node.js filesystem + // Extract generated docs back to Node.js file system await fs.mkdir('./docs', { recursive: true }); const summaryContent = await php.readFileAsText('/output/summary.md'); await fs.writeFile('./docs/summary.md', summaryContent); @@ -588,9 +588,9 @@ echo "Documentation generated successfully!\\n"; generateDocumentation().catch(console.error); ``` -## Advanced Features +## Advanced features -### Working with Environment Variables +### Working with environment variables ```javascript import { PHP } from '@php-wasm/universal'; @@ -608,7 +608,7 @@ const result = await php.runStream({ console.log(await result.stdoutText); ``` -### Error Handling +### Error handling ```javascript import { PHP } from '@php-wasm/universal'; @@ -635,9 +635,7 @@ try { } ``` ---- - -## Performance Considerations +## Performance considerations - **Reuse PHP instances**: Creating a new PHP instance is expensive. Reuse the same instance when possible. - **Batch operations**: Group multiple file operations together rather than running separate scripts. From ce5806ddad7e9245a8c42fc3627e486788e58a16 Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Tue, 28 Oct 2025 09:28:45 +0000 Subject: [PATCH 4/8] Adding spanish version --- .../05-local-development/03-php-wasm-node.md | 643 ++++++++++++++++++ 1 file changed, 643 insertions(+) create mode 100644 packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md diff --git a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md new file mode 100644 index 0000000000..569d40a00a --- /dev/null +++ b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -0,0 +1,643 @@ +--- +title: php-wasm/node +slug: /developers/local-development/php-wasm-node +description: WordPress Playground trae PHP con WebAssembly a Node.js para ejecución del lado del servidor, procesamiento de datos y pruebas sin instalación nativa. +--- + +# Usando WordPress Playground en Node.js + +Como un proyecto WebAssembly, también puedes usar WordPress Playground en Node.js. + +Si necesitas control de bajo nivel sobre la compilación WebAssembly de PHP subyacente, echa un vistazo al [paquete @php-wasm/node](https://npmjs.org/@php-wasm/node) que incluye el runtime WebAssembly de PHP. Este paquete está en el núcleo de todas las herramientas de WordPress Playground para Node.js. + +Consulta la [lista completa](/api/node) de Clases, Funciones, Interfaces y Alias de Tipos. + +## WebAssembly PHP para Node.js + +Este paquete incluye binarios WebAssembly de PHP y la API JavaScript optimizada para Node.js. Utiliza el sistema de archivos del host directamente y puede acceder a la red si conectas un proxy WS personalizado. + +### Uso básico + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); +const output = await php.runStream({ + code: '', +}); +console.log(await output.stdoutText); +``` + +## Casos de uso + +### Ejecución PHP del lado del servidor + +Puedes ejecutar scripts PHP en entornos Node.js sin instalar PHP nativamente. Perfecto para: + +- Pipelines CI/CD que necesitan ejecución PHP +- Probar código PHP en suites de pruebas basadas en JavaScript +- Microservicios que ocasionalmente necesitan funcionalidad PHP +- Herramientas de desarrollo y scripts de construcción + +### Procesamiento y transformación de datos + +Puedes procesar y manipular datos usando el rico ecosistema de PHP: + +- Transformación de datos CSV, JSON y XML +- Operaciones de base de datos SQLite sin dependencias externas +- Creación y extracción de archivos (ZIP, TAR) +- Procesamiento y manipulación de imágenes + +### Renderización de plantillas y generación de contenido + +Puedes generar contenido dinámico usando plantillas PHP: + +- Renderización de plantillas de email +- Generación de informes HTML +- Generación de documentación +- Creación dinámica de archivos de configuración + +### Desarrollo y pruebas de APIs + +Puedes construir y probar endpoints de API: + +- Servidores de API simulados para pruebas +- Simulación de petición/respuesta +- Prototipado de endpoints de API +- Pruebas de integración para APIs PHP + +### Análisis y prueba de código PHP + +Puedes construir herramientas que analizan, hacen lint o prueban código PHP: + +- Herramientas de análisis estático +- Formateadores y validadores de código +- Ejecutores de pruebas unitarias +- Generadores de documentación + +### Integración de código heredado + +Puedes hacer puente entre los ecosistemas PHP y JavaScript: + +- Migrar aplicaciones PHP incrementalmente +- Usar bibliotecas PHP en proyectos JavaScript +- Ejecutar PHP junto con frameworks JavaScript modernos +- Proporcionar compatibilidad hacia atrás + +### Plataformas educativas + +Puedes crear entornos de aprendizaje PHP interactivos: + +- Tutoriales de codificación en línea +- Documentación interactiva +- Playgrounds y sandboxes de código +- Desafíos de programación + +### Herramientas de desarrollo WordPress + +Puedes construir utilidades de desarrollo WordPress: + +- Validadores de plugin/tema +- Alternativas al WordPress CLI +- Inicialización de entorno de desarrollo +- Herramientas de prueba automatizada + +## Demos prácticas + +### Demo 1: Operaciones en el sistema de archivos + +Ejecuta scripts PHP que interactúan con el sistema de archivos: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Crear estructura de directorios +php.mkdir('/app/data'); + +// Escribir archivo de configuración +await php.writeFile( + '/app/config.json', + JSON.stringify({ + app: 'MyApp', + version: '1.0.0', + debug: true, + }) +); + +// Crear y ejecutar script PHP que lee la configuración +await php.writeFile( + '/app/index.php', + `` +); + +const result = await php.runStream({ scriptPath: '/app/index.php' }); +console.log(await result.stdoutText); +``` + +### Demo 2: Operaciones de base de datos SQLite + +Usa la extensión SQLite de PHP para almacenamiento de datos: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Crear directorio para la base de datos +php.mkdir('/data'); + +// Crear base de datos, insertar datos y consultar +const result = await php.runStream({ + code: `exec('CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + email TEXT UNIQUE NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP +)'); + +// Insertar datos de ejemplo +$stmt = $db->prepare('INSERT INTO users (name, email) VALUES (?, ?)'); +$users = [ + ['Alice Johnson', 'alice@example.com'], + ['Bob Smith', 'bob@example.com'], + ['Charlie Davis', 'charlie@example.com'] +]; + +foreach ($users as $user) { + $stmt->bindValue(1, $user[0]); + $stmt->bindValue(2, $user[1]); + $stmt->execute(); +} + +// Consultar datos +echo "All Users:\\n"; +echo str_repeat('-', 50) . "\\n"; +$results = $db->query('SELECT * FROM users ORDER BY name'); +while ($row = $results->fetchArray(SQLITE3_ASSOC)) { + echo "ID: {$row['id']} | {$row['name']} ({$row['email']})\\n"; +} + +$db->close(); +?>`, +}); + +console.log(await result.stdoutText); + +// El archivo de base de datos persiste en el sistema de archivos virtual +const dbExists = await php.fileExists('/data/app.db'); +console.log('\nDatabase persisted:', dbExists); +``` + +### Demo 3: Procesamiento de archivos subidos (archivos ZIP) + +Procesa archivos ZIP usando la extensión Libzip de PHP: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Crear archivos de ejemplo +php.mkdir('/uploads'); +await php.writeFile('/uploads/readme.txt', 'This is a sample text file'); +await php.writeFile('/uploads/data.json', JSON.stringify({ name: 'Test', version: '1.0' })); + +// Crear, procesar y extraer archivo ZIP +const result = await php.runStream({ + code: `open('/uploads/archive.zip', ZipArchive::CREATE); +$zip->addFromString('readme.txt', file_get_contents('/uploads/readme.txt')); +$zip->addFromString('data.json', file_get_contents('/uploads/data.json')); +$zip->addFromString('info.txt', 'Created with PHP WASM'); +$zip->close(); + +echo "ZIP archive created successfully\\n\\n"; + +// Leer y mostrar contenido del archivo +$zip->open('/uploads/archive.zip'); +echo "Archive Contents:\\n"; +echo str_repeat('=', 50) . "\\n"; + +for ($i = 0; $i < $zip->numFiles; $i++) { + $stat = $zip->statIndex($i); + $size = round($stat['size'] / 1024, 2); + echo sprintf("%-40s %10s KB\\n", $stat['name'], $size); +} + +// Extraer archivos +$zip->extractTo('/uploads/extracted/'); +$zip->close(); + +echo "\\nExtracted successfully to /uploads/extracted/\\n"; + +// Listar archivos extraídos +echo "\\nExtracted Files:\\n"; +$files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator('/uploads/extracted/') +); +foreach ($files as $file) { + if ($file->isFile()) { + echo " " . $file->getPathname() . "\\n"; + } +} +?>`, +}); + +console.log(await result.stdoutText); +``` + +### Demo 4: Patrón de petición/respuesta HTTP + +Simula comportamiento de servidor web con manejadores de peticiones: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Configurar un endpoint de API simple +await php.mkdir('/www/api'); +await php.writeFile( + '/www/api/users.php', + ` [ + ['id' => 1, 'name' => 'John Doe'], + ['id' => 2, 'name' => 'Jane Smith'] + ] + ]); + break; + + case 'POST': + $name = $input['name'] ?? 'Unknown'; + echo json_encode([ + 'success' => true, + 'user' => [ + 'id' => 3, + 'name' => $name + ], + 'message' => "User $name created" + ]); + break; + + default: + http_response_code(405); + echo json_encode(['error' => 'Method not allowed']); +} +?>` +); + +// Hacer petición GET +const getResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'GET', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, +}); +console.log('GET Response:', await getResponse.stdoutText); + +// Hacer petición POST +const postResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'POST', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, + body: JSON.stringify({ name: 'Alice Wonder' }), +}); +console.log('\\nPOST Response:', await postResponse.stdoutText); +``` + +### Demo 5: Motor de renderización de plantillas + +Usa PHP como motor de plantillas para contenido dinámico: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Crear directorio de plantillas +php.mkdir('/templates'); + +// Crear plantilla +await php.writeFile( + '/templates/email.php', + ` + + + + + +
+

Welcome, !

+
+
+

Thank you for registering with .

+

Your account details:

+
    +
  • Email:
  • +
  • Member Since:
  • +
+

You now have access to the following features:

+
    + +
  • + +
+
+ + +` +); + +// Renderizar plantilla con datos +const templateData = { + name: 'Priya Sharma', + email: 'priya@example.com', + appName: 'MyAwesomeApp', + timestamp: Math.floor(Date.now() / 1000), + features: ['Dashboard Access', 'API Integration', 'Premium Support', 'Custom Branding'], +}; + +// Pasar datos a la plantilla vía variables de entorno o archivos +await php.writeFile('/template-data.json', JSON.stringify(templateData)); + +const result = await php.runStream({ + code: ``, +}); + +console.log(await result.stdoutText); +// Ahora tienes HTML renderizado que puede enviarse por email o guardarse +``` + +### Demo 6: Ejecución de código en tiempo real y streaming + +Procesa la salida de PHP conforme se genera: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +await php.writeFile( + '/stream-demo.php', + `` +); + +// Ejecutar script PHP +const streamedResponse = await php.runStream({ + scriptPath: '/stream-demo.php', +}); + +console.log('Processing PHP output:\n'); +console.log(await streamedResponse.stdoutText); +console.log(`\nExit code: ${streamedResponse.exitCode}`); +``` + +## Patrones de integración + +### Patrón 1: Middleware Express.js + +Integra procesamiento PHP en una aplicación Express.js: + +```TypeScript +import express from 'express'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const app = express(); +const php = new PHP(await loadNodeRuntime('8.3')); + +// Middleware de ejecución PHP +app.use('/php', async (req, res, next) => { + try { + const phpScript = req.query.script || 'index.php'; + const result = await php.runStream({ + scriptPath: `/www/${phpScript}`, + env: { + REQUEST_METHOD: req.method, + QUERY_STRING: new URLSearchParams( + req.query as Record + ).toString(), + REQUEST_URI: req.url, + }, + }); + + res.send(await result.stdoutText); + } catch (error) { + next(error); + } +}); + +app.listen(3000, () => { + console.log('Server with PHP support running on port 3000'); +}); +``` + +### Patrón 2: Pruebas automatizadas + +Crea pruebas automatizadas para código PHP: + +```TypeScript +import { describe, it, expect, beforeAll } from '@jest/globals'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +describe('PHP Functions', () => { + let php: PHP; + + beforeAll(async () => { + php = new PHP(await loadNodeRuntime('8.3')); + }); + + it('should calculate sum correctly', async () => { + const result = await php.run({ + code: ``, + }); + + expect(result.text).toBe('8'); + }); + + it('should handle JSON operations', async () => { + const input = { name: 'Test', value: 42 }; + const result = await php.run({ + code: ` $input, + 'doubled' => $input['value'] * 2 + ]; + echo json_encode($output); + ?>`, + }); + + const output = JSON.parse(result.text); + expect(output.doubled).toBe(84); + }); +}); +``` + +### Patrón 3: Integración con herramientas de construcción + +Usa en scripts de construcción con otras herramientas Node.js: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; +import fs from 'fs/promises'; + +async function generateDocumentation() { + const php = new PHP(await loadNodeRuntime('8.3')); + + // Crear directorio de salida + php.mkdir('/output'); + + // Generar documentación + const result = await php.runStream({ + code: ``, + }); + + console.log(await result.stdoutText); + + // Extraer documentación generada de vuelta al sistema de archivos Node.js + await fs.mkdir('./docs', { recursive: true }); + const summaryContent = await php.readFileAsText('/output/summary.md'); + await fs.writeFile('./docs/summary.md', summaryContent); + + console.log('Documentation saved to ./docs/summary.md'); +} + +generateDocumentation().catch(console.error); +``` + +## Características avanzadas + +### Trabajando con variables de entorno + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +const result = await php.runStream({ + code: '', + env: { + CUSTOM_VAR: 'Hello from Node.js!', + }, +}); + +console.log(await result.stdoutText); +``` + +### Manejo de errores + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +try { + const result = await php.runStream({ + code: '', + }); + + const stdout = await result.stdoutText; + const stderr = await result.stderrText; + + console.log('stdout:', stdout); + console.log('stderr:', stderr); + + if (stderr) { + console.error('PHP produced errors:', stderr); + } +} catch (error: any) { + console.error('JavaScript Error:', error.message); +} +``` + +## Consideraciones de rendimiento + +- **Reutiliza instancias PHP**: Crear una nueva instancia PHP es costoso. Reutiliza la misma instancia cuando sea posible. +- **Operaciones por lotes**: Agrupa múltiples operaciones de archivos juntas en lugar de ejecutar scripts separados. +- **Gestión de memoria**: Los archivos grandes pueden impactar el rendimiento. Considera streaming para grandes conjuntos de datos. +- **Caché**: Almacena en caché scripts PHP compilados y datos accedidos frecuentemente. From 462fbeeed73dd44a255711ed5eda386dc69ff3fd Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Tue, 28 Oct 2025 09:28:56 +0000 Subject: [PATCH 5/8] updating the portuguese version --- .../05-local-development/03-php-wasm-node.md | 639 +++++++++++++++++- 1 file changed, 626 insertions(+), 13 deletions(-) diff --git a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 74280541d4..987345381f 100644 --- a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -1,30 +1,643 @@ --- title: php-wasm/node slug: /developers/local-development/php-wasm-node +description: WordPress Playground traz PHP com WebAssembly para Node.js para execução do lado do servidor, processamento de dados e testes sem instalação nativa. --- - - # Usando WordPress Playground no Node.js - - Como um projeto WebAssembly, você também pode usar o WordPress Playground no Node.js. - +Se você precisa de controle de baixo nível sobre a compilação WebAssembly do PHP subjacente, dê uma olhada no [pacote @php-wasm/node](https://npmjs.org/@php-wasm/node) que inclui o runtime WebAssembly do PHP. Este pacote está no centro de todas as ferramentas WordPress Playground para Node.js. -Se você precisa de controle de baixo nível sobre a compilação WebAssembly do PHP subjacente, dê uma olhada no pacote [`@php-wasm/node`](https://npmjs.org/@php-wasm/node) que inclui o runtime WebAssembly do PHP. Este pacote está no centro de todas as ferramentas WordPress Playground para Node.js. +Consulte a [lista completa](/api/node) de Classes, Funções, Interfaces e Aliases de Tipo. - +## WebAssembly PHP para Node.js -:::info **Referência da API** +Este pacote inclui binários WebAssembly do PHP e a API JavaScript otimizada para Node.js. Ele usa o sistema de arquivos do host diretamente e pode acessar a rede se você conectar um proxy WS customizado. - +### Uso básico -Consulte a [lista completa](/api/node) de Classes, Funções, Interfaces e Aliases de Tipo. +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); +const output = await php.runStream({ + code: '', +}); +console.log(await output.stdoutText); +``` + +## Casos de uso + +### Execução PHP do lado do servidor + +Você pode executar scripts PHP em ambientes Node.js sem instalar PHP nativamente. Perfeito para: + +- Pipelines CI/CD que precisam de execução PHP +- Testar código PHP em suítes de teste baseadas em JavaScript +- Microserviços que ocasionalmente precisam de funcionalidade PHP +- Ferramentas de desenvolvimento e scripts de build + +### Processamento e transformação de dados + +Você pode processar e manipular dados usando o rico ecossistema do PHP: + +- Transformação de dados CSV, JSON e XML +- Operações de banco de dados SQLite sem dependências externas +- Criação e extração de arquivos (ZIP, TAR) +- Processamento e manipulação de imagens + +### Renderização de templates e geração de conteúdo + +Você pode gerar conteúdo dinâmico usando templates PHP: + +- Renderização de templates de email +- Geração de relatórios HTML +- Geração de documentação +- Criação dinâmica de arquivos de configuração + +### Desenvolvimento e teste de APIs + +Você pode construir e testar endpoints de API: + +- Servidores de API mock para testes +- Simulação de requisição/resposta +- Prototipagem de endpoints de API +- Testes de integração para APIs PHP + +### Análise e teste de código PHP + +Você pode construir ferramentas que analisam, fazem lint ou testam código PHP: + +- Ferramentas de análise estática +- Formatadores e validadores de código +- Executores de testes unitários +- Geradores de documentação + +### Integração de código legado + +Você pode fazer a ponte entre os ecossistemas PHP e JavaScript: + +- Migrar aplicações PHP incrementalmente +- Usar bibliotecas PHP em projetos JavaScript +- Executar PHP junto com frameworks JavaScript modernos +- Fornecer compatibilidade retroativa + +### Plataformas educacionais + +Você pode criar ambientes de aprendizado PHP interativos: + +- Tutoriais de codificação online +- Documentação interativa +- Playgrounds e sandboxes de código +- Desafios de programação + +### Ferramentas de desenvolvimento WordPress + +Você pode construir utilitários de desenvolvimento WordPress: + +- Validadores de plugin/tema +- Alternativas ao WordPress CLI +- Inicialização de ambiente de desenvolvimento +- Ferramentas de teste automatizado + +## Demos práticas + +### Demo 1: Operações no sistema de arquivos + +Execute scripts PHP que interagem com o sistema de arquivos: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Criar estrutura de diretórios +php.mkdir('/app/data'); + +// Escrever arquivo de configuração +await php.writeFile( + '/app/config.json', + JSON.stringify({ + app: 'MyApp', + version: '1.0.0', + debug: true, + }) +); + +// Criar e executar script PHP que lê a configuração +await php.writeFile( + '/app/index.php', + `` +); + +const result = await php.runStream({ scriptPath: '/app/index.php' }); +console.log(await result.stdoutText); +``` + +### Demo 2: Operações de banco de dados SQLite + +Use a extensão SQLite do PHP para armazenamento de dados: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Criar diretório para o banco de dados +php.mkdir('/data'); + +// Criar banco de dados, inserir dados e consultar +const result = await php.runStream({ + code: `exec('CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + email TEXT UNIQUE NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP +)'); + +// Inserir dados de exemplo +$stmt = $db->prepare('INSERT INTO users (name, email) VALUES (?, ?)'); +$users = [ + ['Alice Johnson', 'alice@example.com'], + ['Bob Smith', 'bob@example.com'], + ['Charlie Davis', 'charlie@example.com'] +]; + +foreach ($users as $user) { + $stmt->bindValue(1, $user[0]); + $stmt->bindValue(2, $user[1]); + $stmt->execute(); +} + +// Consultar dados +echo "All Users:\\n"; +echo str_repeat('-', 50) . "\\n"; +$results = $db->query('SELECT * FROM users ORDER BY name'); +while ($row = $results->fetchArray(SQLITE3_ASSOC)) { + echo "ID: {$row['id']} | {$row['name']} ({$row['email']})\\n"; +} + +$db->close(); +?>`, +}); + +console.log(await result.stdoutText); + +// Arquivo do banco de dados persiste no sistema de arquivos virtual +const dbExists = await php.fileExists('/data/app.db'); +console.log('\nDatabase persisted:', dbExists); +``` + +### Demo 3: Processamento de arquivos enviados (arquivos ZIP) + +Processe arquivos ZIP usando a extensão Libzip do PHP: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Criar arquivos de exemplo +php.mkdir('/uploads'); +await php.writeFile('/uploads/readme.txt', 'This is a sample text file'); +await php.writeFile('/uploads/data.json', JSON.stringify({ name: 'Test', version: '1.0' })); + +// Criar, processar e extrair arquivo ZIP +const result = await php.runStream({ + code: `open('/uploads/archive.zip', ZipArchive::CREATE); +$zip->addFromString('readme.txt', file_get_contents('/uploads/readme.txt')); +$zip->addFromString('data.json', file_get_contents('/uploads/data.json')); +$zip->addFromString('info.txt', 'Created with PHP WASM'); +$zip->close(); + +echo "ZIP archive created successfully\\n\\n"; + +// Ler e exibir conteúdo do arquivo +$zip->open('/uploads/archive.zip'); +echo "Archive Contents:\\n"; +echo str_repeat('=', 50) . "\\n"; + +for ($i = 0; $i < $zip->numFiles; $i++) { + $stat = $zip->statIndex($i); + $size = round($stat['size'] / 1024, 2); + echo sprintf("%-40s %10s KB\\n", $stat['name'], $size); +} + +// Extrair arquivos +$zip->extractTo('/uploads/extracted/'); +$zip->close(); + +echo "\\nExtracted successfully to /uploads/extracted/\\n"; + +// Listar arquivos extraídos +echo "\\nExtracted Files:\\n"; +$files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator('/uploads/extracted/') +); +foreach ($files as $file) { + if ($file->isFile()) { + echo " " . $file->getPathname() . "\\n"; + } +} +?>`, +}); + +console.log(await result.stdoutText); +``` + +### Demo 4: Padrão de requisição/resposta HTTP + +Simule comportamento de servidor web com manipuladores de requisição: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Configurar um endpoint de API simples +await php.mkdir('/www/api'); +await php.writeFile( + '/www/api/users.php', + ` [ + ['id' => 1, 'name' => 'John Doe'], + ['id' => 2, 'name' => 'Jane Smith'] + ] + ]); + break; + + case 'POST': + $name = $input['name'] ?? 'Unknown'; + echo json_encode([ + 'success' => true, + 'user' => [ + 'id' => 3, + 'name' => $name + ], + 'message' => "User $name created" + ]); + break; + + default: + http_response_code(405); + echo json_encode(['error' => 'Method not allowed']); +} +?>` +); + +// Fazer requisição GET +const getResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'GET', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, +}); +console.log('GET Response:', await getResponse.stdoutText); + +// Fazer requisição POST +const postResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'POST', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, + body: JSON.stringify({ name: 'Alice Wonder' }), +}); +console.log('\\nPOST Response:', await postResponse.stdoutText); +``` + +### Demo 5: Motor de renderização de templates + +Use PHP como um motor de templates para conteúdo dinâmico: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Criar diretório de templates +php.mkdir('/templates'); + +// Criar template +await php.writeFile( + '/templates/email.php', + ` + + + + + +
+

Welcome, !

+
+
+

Thank you for registering with .

+

Your account details:

+
    +
  • Email:
  • +
  • Member Since:
  • +
+

You now have access to the following features:

+
    + +
  • + +
+
+ + +` +); + +// Renderizar template com dados +const templateData = { + name: 'Priya Sharma', + email: 'priya@example.com', + appName: 'MyAwesomeApp', + timestamp: Math.floor(Date.now() / 1000), + features: ['Dashboard Access', 'API Integration', 'Premium Support', 'Custom Branding'], +}; + +// Passar dados para o template via variáveis de ambiente ou arquivos +await php.writeFile('/template-data.json', JSON.stringify(templateData)); + +const result = await php.runStream({ + code: ``, +}); + +console.log(await result.stdoutText); +// Agora você tem HTML renderizado que pode ser enviado por email ou salvo +``` + +### Demo 6: Execução de código em tempo real e streaming + +Processe a saída do PHP conforme ela é gerada: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +await php.writeFile( + '/stream-demo.php', + `` +); + +// Executar script PHP +const streamedResponse = await php.runStream({ + scriptPath: '/stream-demo.php', +}); + +console.log('Processing PHP output:\n'); +console.log(await streamedResponse.stdoutText); +console.log(`\nExit code: ${streamedResponse.exitCode}`); +``` + +## Padrões de integração + +### Padrão 1: Middleware Express.js + +Integre processamento PHP em uma aplicação Express.js: + +```TypeScript +import express from 'express'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const app = express(); +const php = new PHP(await loadNodeRuntime('8.3')); + +// Middleware de execução PHP +app.use('/php', async (req, res, next) => { + try { + const phpScript = req.query.script || 'index.php'; + const result = await php.runStream({ + scriptPath: `/www/${phpScript}`, + env: { + REQUEST_METHOD: req.method, + QUERY_STRING: new URLSearchParams( + req.query as Record + ).toString(), + REQUEST_URI: req.url, + }, + }); + + res.send(await result.stdoutText); + } catch (error) { + next(error); + } +}); + +app.listen(3000, () => { + console.log('Server with PHP support running on port 3000'); +}); +``` + +### Padrão 2: Testes automatizados + +Crie testes automatizados para código PHP: + +```TypeScript +import { describe, it, expect, beforeAll } from '@jest/globals'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +describe('PHP Functions', () => { + let php: PHP; + + beforeAll(async () => { + php = new PHP(await loadNodeRuntime('8.3')); + }); + + it('should calculate sum correctly', async () => { + const result = await php.run({ + code: ``, + }); + + expect(result.text).toBe('8'); + }); + + it('should handle JSON operations', async () => { + const input = { name: 'Test', value: 42 }; + const result = await php.run({ + code: ` $input, + 'doubled' => $input['value'] * 2 + ]; + echo json_encode($output); + ?>`, + }); + + const output = JSON.parse(result.text); + expect(output.doubled).toBe(84); + }); +}); +``` + +### Padrão 3: Integração com ferramentas de build + +Use em scripts de build com outras ferramentas Node.js: + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; +import fs from 'fs/promises'; + +async function generateDocumentation() { + const php = new PHP(await loadNodeRuntime('8.3')); + + // Criar diretório de saída + php.mkdir('/output'); + + // Gerar documentação + const result = await php.runStream({ + code: ``, + }); + + console.log(await result.stdoutText); + + // Extrair documentação gerada de volta para o sistema de arquivos Node.js + await fs.mkdir('./docs', { recursive: true }); + const summaryContent = await php.readFileAsText('/output/summary.md'); + await fs.writeFile('./docs/summary.md', summaryContent); + + console.log('Documentation saved to ./docs/summary.md'); +} + +generateDocumentation().catch(console.error); +``` + +## Recursos avançados + +### Trabalhando com variáveis de ambiente + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +const result = await php.runStream({ + code: '', + env: { + CUSTOM_VAR: 'Hello from Node.js!', + }, +}); + +console.log(await result.stdoutText); +``` + +### Tratamento de erros + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +try { + const result = await php.runStream({ + code: '', + }); + + const stdout = await result.stdoutText; + const stderr = await result.stderrText; + + console.log('stdout:', stdout); + console.log('stderr:', stderr); -::: + if (stderr) { + console.error('PHP produced errors:', stderr); + } +} catch (error: any) { + console.error('JavaScript Error:', error.message); +} +``` -import PHPWASMNode from '@php-wasm/node/\README.md'; +## Considerações de desempenho - +- **Reutilize instâncias PHP**: Criar uma nova instância PHP é custoso. Reutilize a mesma instância quando possível. +- **Operações em lote**: Agrupe múltiplas operações de arquivo juntas em vez de executar scripts separados. +- **Gerenciamento de memória**: Arquivos grandes podem impactar o desempenho. Considere streaming para grandes conjuntos de dados. +- **Cache**: Faça cache de scripts PHP compilados e dados acessados frequentemente. From 86ba15fc8a3099dc45b88f70f85998ff1682cd1c Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Tue, 28 Oct 2025 17:21:33 +0000 Subject: [PATCH 6/8] removing to close examples --- .../docs/developers/05-local-development/03-php-wasm-node.md | 1 - .../current/developers/05-local-development/03-php-wasm-node.md | 1 - .../current/developers/05-local-development/03-php-wasm-node.md | 1 - 3 files changed, 3 deletions(-) diff --git a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md index b12b4e835a..fde2dca660 100644 --- a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md @@ -36,7 +36,6 @@ console.log(await output.stdoutText); You can run PHP scripts in Node.js environments without installing PHP natively. Perfect for: - CI/CD pipelines that need PHP execution -- Testing PHP code in JavaScript-based test suites - Microservices that occasionally need PHP functionality - Development tools and build scripts diff --git a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 569d40a00a..724770e5fe 100644 --- a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -36,7 +36,6 @@ console.log(await output.stdoutText); Puedes ejecutar scripts PHP en entornos Node.js sin instalar PHP nativamente. Perfecto para: - Pipelines CI/CD que necesitan ejecución PHP -- Probar código PHP en suites de pruebas basadas en JavaScript - Microservicios que ocasionalmente necesitan funcionalidad PHP - Herramientas de desarrollo y scripts de construcción diff --git a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 987345381f..52891a028e 100644 --- a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -36,7 +36,6 @@ console.log(await output.stdoutText); Você pode executar scripts PHP em ambientes Node.js sem instalar PHP nativamente. Perfeito para: - Pipelines CI/CD que precisam de execução PHP -- Testar código PHP em suítes de teste baseadas em JavaScript - Microserviços que ocasionalmente precisam de funcionalidade PHP - Ferramentas de desenvolvimento e scripts de build From 4e5514d0abc8b72a280c1261f70bee1e6196b403 Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Mon, 10 Nov 2025 18:41:44 +0000 Subject: [PATCH 7/8] updating php wasm page --- .../05-local-development/03-php-wasm-node.md | 76 +- .../05-local-development/03-php-wasm-node.md | 145 ++-- .../05-local-development/03-php-wasm-node.md | 657 ++++++++++++++++++ .../05-local-development/03-php-wasm-node.md | 145 ++-- 4 files changed, 824 insertions(+), 199 deletions(-) create mode 100644 packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md diff --git a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md index fde2dca660..d14f89623a 100644 --- a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md @@ -31,79 +31,17 @@ console.log(await output.stdoutText); ## Use cases -### Server-side PHP execution +Run PHP inside Node.js without a native PHP install. Allow developer to produce the following solutions: -You can run PHP scripts in Node.js environments without installing PHP natively. Perfect for: - -- CI/CD pipelines that need PHP execution -- Microservices that occasionally need PHP functionality -- Development tools and build scripts - -### Data processing and transformation - -You can process and manipulate data using PHP's rich ecosystem: - -- CSV, JSON, and XML data transformation -- SQLite database operations without external dependencies -- Archive creation and extraction (ZIP, TAR) -- Image processing and manipulation - -### Template rendering and content generation - -You can generate dynamic content using PHP templates: - -- Email template rendering -- HTML report generation -- Documentation generation -- Dynamic configuration file creation - -### API development and testing - -You can build and test API endpoints: - -- Mock API servers for testing -- Request/response simulation -- API endpoint prototyping -- Integration testing for PHP APIs - -### PHP code analysis and testing - -You can build tools that analyze, lint, or test PHP code: - -- Static analysis tools -- Code formatters and validators -- Unit test runners -- Documentation generators - -### Legacy code integration - -You can bridge PHP and JavaScript ecosystems: - -- Migrate PHP applications incrementally -- Use PHP libraries in JavaScript projects -- Run PHP alongside modern JavaScript frameworks -- Provide backward compatibility - -### Educational platforms - -You can create interactive PHP learning environments: - -- Online coding tutorials -- Interactive documentation -- Code playgrounds and sandboxes -- Programming challenges - -### WordPress development tools - -You can build WordPress development utilities: - -- Plugin/theme validators -- WordPress CLI alternatives -- Development environment bootstrapping -- Automated testing tools +- CI/CD jobs and developer tooling. +- Support education and WordPress workflows: Power interactive tutorials, sandboxes, and coding challenges. +- Generate content and prototype server behavior. +- Render HTML using PHP templates, and quickly stand up mock API endpoints to simulate requests. ## Practical demos +We will list some examples using the PHP-WASM package. + ### Demo 1: File system operations Execute PHP scripts that interact with the file system: diff --git a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 724770e5fe..081afb5dd7 100644 --- a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -4,18 +4,32 @@ slug: /developers/local-development/php-wasm-node description: WordPress Playground trae PHP con WebAssembly a Node.js para ejecución del lado del servidor, procesamiento de datos y pruebas sin instalación nativa. --- + + # Usando WordPress Playground en Node.js + + Como un proyecto WebAssembly, también puedes usar WordPress Playground en Node.js. + + Si necesitas control de bajo nivel sobre la compilación WebAssembly de PHP subyacente, echa un vistazo al [paquete @php-wasm/node](https://npmjs.org/@php-wasm/node) que incluye el runtime WebAssembly de PHP. Este paquete está en el núcleo de todas las herramientas de WordPress Playground para Node.js. + + Consulta la [lista completa](/api/node) de Clases, Funciones, Interfaces y Alias de Tipos. + + ## WebAssembly PHP para Node.js + + Este paquete incluye binarios WebAssembly de PHP y la API JavaScript optimizada para Node.js. Utiliza el sistema de archivos del host directamente y puede acceder a la red si conectas un proxy WS personalizado. + + ### Uso básico ```javascript @@ -29,83 +43,37 @@ const output = await php.runStream({ console.log(await output.stdoutText); ``` -## Casos de uso - -### Ejecución PHP del lado del servidor - -Puedes ejecutar scripts PHP en entornos Node.js sin instalar PHP nativamente. Perfecto para: - -- Pipelines CI/CD que necesitan ejecución PHP -- Microservicios que ocasionalmente necesitan funcionalidad PHP -- Herramientas de desarrollo y scripts de construcción - -### Procesamiento y transformación de datos - -Puedes procesar y manipular datos usando el rico ecosistema de PHP: + -- Transformación de datos CSV, JSON y XML -- Operaciones de base de datos SQLite sin dependencias externas -- Creación y extracción de archivos (ZIP, TAR) -- Procesamiento y manipulación de imágenes - -### Renderización de plantillas y generación de contenido - -Puedes generar contenido dinámico usando plantillas PHP: - -- Renderización de plantillas de email -- Generación de informes HTML -- Generación de documentación -- Creación dinámica de archivos de configuración - -### Desarrollo y pruebas de APIs - -Puedes construir y probar endpoints de API: - -- Servidores de API simulados para pruebas -- Simulación de petición/respuesta -- Prototipado de endpoints de API -- Pruebas de integración para APIs PHP - -### Análisis y prueba de código PHP - -Puedes construir herramientas que analizan, hacen lint o prueban código PHP: - -- Herramientas de análisis estático -- Formateadores y validadores de código -- Ejecutores de pruebas unitarias -- Generadores de documentación - -### Integración de código heredado - -Puedes hacer puente entre los ecosistemas PHP y JavaScript: +## Casos de uso -- Migrar aplicaciones PHP incrementalmente -- Usar bibliotecas PHP en proyectos JavaScript -- Ejecutar PHP junto con frameworks JavaScript modernos -- Proporcionar compatibilidad hacia atrás + + + + + -### Plataformas educativas +Ejecuta PHP dentro de Node.js sin instalación nativa de PHP. Permite al desarrollador producir las siguientes soluciones: -Puedes crear entornos de aprendizaje PHP interactivos: +- Tareas de CI/CD y herramientas de desarrollo. +- Soporte a educación y flujos de trabajo de WordPress: potencia tutoriales interactivos, sandboxes y retos de código. +- Generar contenido y prototipar comportamiento de servidor. +- Renderizar HTML usando plantillas PHP y levantar rápidamente endpoints de API simulados para simular peticiones. -- Tutoriales de codificación en línea -- Documentación interactiva -- Playgrounds y sandboxes de código -- Desafíos de programación + -### Herramientas de desarrollo WordPress +## Demos prácticas -Puedes construir utilidades de desarrollo WordPress: + -- Validadores de plugin/tema -- Alternativas al WordPress CLI -- Inicialización de entorno de desarrollo -- Herramientas de prueba automatizada +Enumeraremos algunos ejemplos usando el paquete PHP-WASM. -## Demos prácticas + ### Demo 1: Operaciones en el sistema de archivos + + Ejecuta scripts PHP que interactúan con el sistema de archivos: ```javascript @@ -150,8 +118,12 @@ const result = await php.runStream({ scriptPath: '/app/index.php' }); console.log(await result.stdoutText); ``` + + ### Demo 2: Operaciones de base de datos SQLite + + Usa la extensión SQLite de PHP para almacenamiento de datos: ```javascript @@ -210,8 +182,12 @@ const dbExists = await php.fileExists('/data/app.db'); console.log('\nDatabase persisted:', dbExists); ``` + + ### Demo 3: Procesamiento de archivos subidos (archivos ZIP) + + Procesa archivos ZIP usando la extensión Libzip de PHP: ```javascript @@ -271,8 +247,12 @@ foreach ($files as $file) { console.log(await result.stdoutText); ``` + + ### Demo 4: Patrón de petición/respuesta HTTP + + Simula comportamiento de servidor web con manejadores de peticiones: ```javascript @@ -346,8 +326,12 @@ const postResponse = await php.runStream({ console.log('\\nPOST Response:', await postResponse.stdoutText); ``` + + ### Demo 5: Motor de renderización de plantillas + + Usa PHP como motor de plantillas para contenido dinámico: ```javascript @@ -421,8 +405,12 @@ console.log(await result.stdoutText); // Ahora tienes HTML renderizado que puede enviarse por email o guardarse ``` + + ### Demo 6: Ejecución de código en tiempo real y streaming + + Procesa la salida de PHP conforme se genera: ```javascript @@ -458,10 +446,16 @@ console.log(await streamedResponse.stdoutText); console.log(`\nExit code: ${streamedResponse.exitCode}`); ``` + + ## Patrones de integración + + ### Patrón 1: Middleware Express.js + + Integra procesamiento PHP en una aplicación Express.js: ```TypeScript @@ -498,8 +492,12 @@ app.listen(3000, () => { }); ``` + + ### Patrón 2: Pruebas automatizadas + + Crea pruebas automatizadas para código PHP: ```TypeScript @@ -546,8 +544,12 @@ describe('PHP Functions', () => { }); ``` + + ### Patrón 3: Integración con herramientas de construcción + + Usa en scripts de construcción con otras herramientas Node.js: ```javascript @@ -587,8 +589,12 @@ echo "Documentation generated successfully!\\n"; generateDocumentation().catch(console.error); ``` + + ## Características avanzadas + + ### Trabajando con variables de entorno ```javascript @@ -607,6 +613,8 @@ const result = await php.runStream({ console.log(await result.stdoutText); ``` + + ### Manejo de errores ```javascript @@ -634,8 +642,15 @@ try { } ``` + + ## Consideraciones de rendimiento + + + + + - **Reutiliza instancias PHP**: Crear una nueva instancia PHP es costoso. Reutiliza la misma instancia cuando sea posible. - **Operaciones por lotes**: Agrupa múltiples operaciones de archivos juntas en lugar de ejecutar scripts separados. - **Gestión de memoria**: Los archivos grandes pueden impactar el rendimiento. Considera streaming para grandes conjuntos de datos. diff --git a/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md new file mode 100644 index 0000000000..3cac0a4b0e --- /dev/null +++ b/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -0,0 +1,657 @@ +--- +title: php-wasm/node +slug: /developers/local-development/php-wasm-node +description: WordPress Playground apporte PHP propulsé par WebAssembly à Node.js pour l'exécution côté serveur, le traitement de données et les tests sans installation native. +--- + + + +# Utiliser WordPress Playground dans Node.js + + + +En tant que projet WebAssembly, vous pouvez aussi utiliser WordPress Playground dans Node.js. + + + +Si vous avez besoin d'un contrôle bas niveau sur la build WebAssembly de PHP, consultez le [paquet @php-wasm/node](https://npmjs.org/@php-wasm/node) qui fournit le runtime PHP WebAssembly. Ce paquet est au cœur de tous les outils WordPress Playground pour Node.js. + + + +Consultez la [liste complète](/api/node) des classes, fonctions, interfaces et alias de types. + + + +## PHP WebAssembly pour Node.js + + + +Ce paquet fournit les binaires PHP WebAssembly et l'API JavaScript optimisée pour Node.js. Il utilise directement le système de fichiers hôte et peut accéder au réseau si vous branchez un proxy WS personnalisé. + + + +### Utilisation de base + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); +const output = await php.runStream({ + code: '', +}); +console.log(await output.stdoutText); +``` + + + +## Cas d'usage + + + + + + + +Exécutez PHP dans Node.js sans installation native de PHP. Cela permet au développeur de produire les solutions suivantes : + +- Tâches CI/CD et outils de développement. +- Support pour l'éducation et les workflows WordPress : didacticiels interactifs, sandboxes et défis de code. +- Génération de contenu et prototypage du comportement serveur. +- Rendu HTML via des templates PHP et création rapide d'endpoints d'API simulés pour simuler des requêtes. + + + +## Démonstrations pratiques + + + +Nous listerons quelques exemples utilisant le paquet PHP-WASM. + + + +### Démo 1 : Opérations sur le système de fichiers + + + +Exécutez des scripts PHP qui interagissent avec le système de fichiers : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create directory structure +php.mkdir('/app/data'); + +// Write configuration file +await php.writeFile( + '/app/config.json', + JSON.stringify({ + app: 'MyApp', + version: '1.0.0', + debug: true, + }) +); + +// Create and run PHP script that reads the config +await php.writeFile( + '/app/index.php', + `` +); + +const result = await php.runStream({ scriptPath: '/app/index.php' }); +console.log(await result.stdoutText); +``` + + + +### Démo 2 : Opérations SQLite + + + +Utilisez l'extension SQLite de PHP pour le stockage des données : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create directory for database +php.mkdir('/data'); + +// Create database, insert data, and query +const result = await php.runStream({ + code: `exec('CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + email TEXT UNIQUE NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP +)'); + +// Insert sample data +$stmt = $db->prepare('INSERT INTO users (name, email) VALUES (?, ?)'); +$users = [ + ['Alice Johnson', 'alice@example.com'], + ['Bob Smith', 'bob@example.com'], + ['Charlie Davis', 'charlie@example.com'] +]; + +foreach ($users as $user) { + $stmt->bindValue(1, $user[0]); + $stmt->bindValue(2, $user[1]); + $stmt->execute(); +} + +// Query data +echo "All Users:\\n"; +echo str_repeat('-', 50) . "\\n"; +$results = $db->query('SELECT * FROM users ORDER BY name'); +while ($row = $results->fetchArray(SQLITE3_ASSOC)) { + echo "ID: {$row['id']} | {$row['name']} ({$row['email']})\\n"; +} + +$db->close(); +?>`, +}); + +console.log(await result.stdoutText); + +// Database file persists in the virtual file system +const dbExists = await php.fileExists('/data/app.db'); +console.log('\nDatabase persisted:', dbExists); +``` + + + +### Démo 3 : Traitement de fichiers téléchargés (archives ZIP) + + + +Traitez des fichiers ZIP avec l'extension Libzip de PHP : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create sample files +php.mkdir('/uploads'); +await php.writeFile('/uploads/readme.txt', 'This is a sample text file'); +await php.writeFile('/uploads/data.json', JSON.stringify({ name: 'Test', version: '1.0' })); + +// Create, process, and extract ZIP archive +const result = await php.runStream({ + code: `open('/uploads/archive.zip', ZipArchive::CREATE); +$zip->addFromString('readme.txt', file_get_contents('/uploads/readme.txt')); +$zip->addFromString('data.json', file_get_contents('/uploads/data.json')); +$zip->addFromString('info.txt', 'Created with PHP WASM'); +$zip->close(); + +echo "ZIP archive created successfully\\n\\n"; + +// Read and display archive contents +$zip->open('/uploads/archive.zip'); +echo "Archive Contents:\\n"; +echo str_repeat('=', 50) . "\\n"; + +for ($i = 0; $i < $zip->numFiles; $i++) { + $stat = $zip->statIndex($i); + $size = round($stat['size'] / 1024, 2); + echo sprintf("%-40s %10s KB\\n", $stat['name'], $size); +} + +// Extract files +$zip->extractTo('/uploads/extracted/'); +$zip->close(); + +echo "\\nExtracted successfully to /uploads/extracted/\\n"; + +// List extracted files +echo "\\nExtracted Files:\\n"; +$files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator('/uploads/extracted/') +); +foreach ($files as $file) { + if ($file->isFile()) { + echo " " . $file->getPathname() . "\\n"; + } +} +?>`, +}); + +console.log(await result.stdoutText); +``` + + + +### Démo 4 : Modèle requête/réponse HTTP + + + +Simulez le comportement d'un serveur web avec des gestionnaires de requêtes : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Set up a simple API endpoint +await php.mkdir('/www/api'); +await php.writeFile( + '/www/api/users.php', + ` [ + ['id' => 1, 'name' => 'John Doe'], + ['id' => 2, 'name' => 'Jane Smith'] + ] + ]); + break; + + case 'POST': + $name = $input['name'] ?? 'Unknown'; + echo json_encode([ + 'success' => true, + 'user' => [ + 'id' => 3, + 'name' => $name + ], + 'message' => "User $name created" + ]); + break; + + default: + http_response_code(405); + echo json_encode(['error' => 'Method not allowed']); +} +?>` +); + +// Make GET request +const getResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'GET', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, +}); +console.log('GET Response:', await getResponse.stdoutText); + +// Make POST request +const postResponse = await php.runStream({ + scriptPath: '/www/api/users.php', + env: { + REQUEST_METHOD: 'POST', + SERVER_NAME: 'localhost', + SERVER_PORT: '80', + }, + body: JSON.stringify({ name: 'Alice Wonder' }), +}); +console.log('\\nPOST Response:', await postResponse.stdoutText); +``` + + + +### Démo 5 : Moteur de rendu de templates + + + +Utilisez PHP comme moteur de templates pour du contenu dynamique : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +// Create templates directory +php.mkdir('/templates'); + +// Create template +await php.writeFile( + '/templates/email.php', + ` + + + + + +
+

Welcome, !

+
+
+

Thank you for registering with .

+

Your account details:

+
    +
  • Email:
  • +
  • Member Since:
  • +
+

You now have access to the following features:

+
    + +
  • + +
+
+ + +` +); + +// Render template with data +const templateData = { + name: 'Priya Sharma', + email: 'priya@example.com', + appName: 'MyAwesomeApp', + timestamp: Math.floor(Date.now() / 1000), + features: ['Dashboard Access', 'API Integration', 'Premium Support', 'Custom Branding'], +}; + +// Pass data to template via environment variables or files +await php.writeFile('/template-data.json', JSON.stringify(templateData)); + +const result = await php.runStream({ + code: ``, +}); + +console.log(await result.stdoutText); +// Now you have rendered HTML that can be sent via email or saved +``` + + + +### Démo 6 : Exécution en temps réel et streaming + + + +Traitez la sortie PHP au fur et à mesure : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +await php.writeFile( + '/stream-demo.php', + `` +); + +// Run PHP script +const streamedResponse = await php.runStream({ + scriptPath: '/stream-demo.php', +}); + +console.log('Processing PHP output:\n'); +console.log(await streamedResponse.stdoutText); +console.log(`\nExit code: ${streamedResponse.exitCode}`); +``` + + + +## Modèles d'intégration + + + +### Modèle 1 : Middleware Express.js + + + +Intégrez l'exécution PHP dans une application Express.js : + +```TypeScript +import express from 'express'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const app = express(); +const php = new PHP(await loadNodeRuntime('8.3')); + +// PHP execution middleware +app.use('/php', async (req, res, next) => { + try { + const phpScript = req.query.script || 'index.php'; + const result = await php.runStream({ + scriptPath: `/www/${phpScript}`, + env: { + REQUEST_METHOD: req.method, + QUERY_STRING: new URLSearchParams( + req.query as Record + ).toString(), + REQUEST_URI: req.url, + }, + }); + + res.send(await result.stdoutText); + } catch (error) { + next(error); + } +}); + +app.listen(3000, () => { + console.log('Server with PHP support running on port 3000'); +}); +``` + + + +### Modèle 2 : Tests automatisés + + + +Créez des tests automatisés pour du code PHP : + +```TypeScript +import { describe, it, expect, beforeAll } from '@jest/globals'; +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +describe('PHP Functions', () => { + let php: PHP; + + beforeAll(async () => { + php = new PHP(await loadNodeRuntime('8.3')); + }); + + it('should calculate sum correctly', async () => { + const result = await php.run({ + code: ``, + }); + + expect(result.text).toBe('8'); + }); + + it('should handle JSON operations', async () => { + const input = { name: 'Test', value: 42 }; + const result = await php.run({ + code: ` $input, + 'doubled' => $input['value'] * 2 + ]; + echo json_encode($output); + ?>`, + }); + + const output = JSON.parse(result.text); + expect(output.doubled).toBe(84); + }); +}); +``` + + + +### Modèle 3 : Intégration aux outils de build + + + +Utilisez-le dans des scripts de build avec d'autres outils Node.js : + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; +import fs from 'fs/promises'; + +async function generateDocumentation() { + const php = new PHP(await loadNodeRuntime('8.3')); + + // Create output directory + php.mkdir('/output'); + + // Generate documentation + const result = await php.runStream({ + code: ``, + }); + + console.log(await result.stdoutText); + + // Extract generated docs back to Node.js file system + await fs.mkdir('./docs', { recursive: true }); + const summaryContent = await php.readFileAsText('/output/summary.md'); + await fs.writeFile('./docs/summary.md', summaryContent); + + console.log('Documentation saved to ./docs/summary.md'); +} + +generateDocumentation().catch(console.error); +``` + + + +## Fonctionnalités avancées + + + +### Travailler avec des variables d'environnement + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +const result = await php.runStream({ + code: '', + env: { + CUSTOM_VAR: 'Hello from Node.js!', + }, +}); + +console.log(await result.stdoutText); +``` + + + +### Gestion des erreurs + +```javascript +import { PHP } from '@php-wasm/universal'; +import { loadNodeRuntime } from '@php-wasm/node'; + +const php = new PHP(await loadNodeRuntime('8.3')); + +try { + const result = await php.runStream({ + code: '', + }); + + const stdout = await result.stdoutText; + const stderr = await result.stderrText; + + console.log('stdout:', stdout); + console.log('stderr:', stderr); + + if (stderr) { + console.error('PHP produced errors:', stderr); + } +} catch (error: any) { + console.error('JavaScript Error:', error.message); +} +``` + + + +## Considérations de performance + + + + + + +- **Réutiliser les instances PHP** : créer une nouvelle instance PHP est coûteux. Réutilisez la même instance lorsque c'est possible. +- **Regrouper les opérations** : groupez plusieurs opérations de fichiers plutôt que d'exécuter des scripts séparés. +- **Gestion de la mémoire** : les fichiers volumineux peuvent impacter les performances. Envisagez le streaming pour de grands jeux de données. +- **Mise en cache** : mettez en cache les scripts PHP compilés et les données souvent consultées. diff --git a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 52891a028e..932790f555 100644 --- a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -4,18 +4,32 @@ slug: /developers/local-development/php-wasm-node description: WordPress Playground traz PHP com WebAssembly para Node.js para execução do lado do servidor, processamento de dados e testes sem instalação nativa. --- + + # Usando WordPress Playground no Node.js + + Como um projeto WebAssembly, você também pode usar o WordPress Playground no Node.js. + + Se você precisa de controle de baixo nível sobre a compilação WebAssembly do PHP subjacente, dê uma olhada no [pacote @php-wasm/node](https://npmjs.org/@php-wasm/node) que inclui o runtime WebAssembly do PHP. Este pacote está no centro de todas as ferramentas WordPress Playground para Node.js. + + Consulte a [lista completa](/api/node) de Classes, Funções, Interfaces e Aliases de Tipo. + + ## WebAssembly PHP para Node.js + + Este pacote inclui binários WebAssembly do PHP e a API JavaScript otimizada para Node.js. Ele usa o sistema de arquivos do host diretamente e pode acessar a rede se você conectar um proxy WS customizado. + + ### Uso básico ```javascript @@ -29,83 +43,37 @@ const output = await php.runStream({ console.log(await output.stdoutText); ``` -## Casos de uso - -### Execução PHP do lado do servidor - -Você pode executar scripts PHP em ambientes Node.js sem instalar PHP nativamente. Perfeito para: - -- Pipelines CI/CD que precisam de execução PHP -- Microserviços que ocasionalmente precisam de funcionalidade PHP -- Ferramentas de desenvolvimento e scripts de build - -### Processamento e transformação de dados - -Você pode processar e manipular dados usando o rico ecossistema do PHP: + -- Transformação de dados CSV, JSON e XML -- Operações de banco de dados SQLite sem dependências externas -- Criação e extração de arquivos (ZIP, TAR) -- Processamento e manipulação de imagens - -### Renderização de templates e geração de conteúdo - -Você pode gerar conteúdo dinâmico usando templates PHP: - -- Renderização de templates de email -- Geração de relatórios HTML -- Geração de documentação -- Criação dinâmica de arquivos de configuração - -### Desenvolvimento e teste de APIs - -Você pode construir e testar endpoints de API: - -- Servidores de API mock para testes -- Simulação de requisição/resposta -- Prototipagem de endpoints de API -- Testes de integração para APIs PHP - -### Análise e teste de código PHP - -Você pode construir ferramentas que analisam, fazem lint ou testam código PHP: - -- Ferramentas de análise estática -- Formatadores e validadores de código -- Executores de testes unitários -- Geradores de documentação - -### Integração de código legado - -Você pode fazer a ponte entre os ecossistemas PHP e JavaScript: +## Casos de uso -- Migrar aplicações PHP incrementalmente -- Usar bibliotecas PHP em projetos JavaScript -- Executar PHP junto com frameworks JavaScript modernos -- Fornecer compatibilidade retroativa + + + + + -### Plataformas educacionais +Execute PHP dentro do Node.js sem instalação nativa de PHP. Permite ao desenvolvedor produzir as seguintes soluções: -Você pode criar ambientes de aprendizado PHP interativos: +- Tarefas de CI/CD e ferramentas de desenvolvimento. +- Suporte à educação e fluxos de trabalho WordPress: potencialize tutoriais interativos, sandboxes e desafios de código. +- Gerar conteúdo e prototipar comportamento de servidor. +- Renderizar HTML usando templates PHP e levantar rapidamente endpoints de API simulados para simular requisições. -- Tutoriais de codificação online -- Documentação interativa -- Playgrounds e sandboxes de código -- Desafios de programação + -### Ferramentas de desenvolvimento WordPress +## Demos práticas -Você pode construir utilitários de desenvolvimento WordPress: + -- Validadores de plugin/tema -- Alternativas ao WordPress CLI -- Inicialização de ambiente de desenvolvimento -- Ferramentas de teste automatizado +Listaremos alguns exemplos usando o pacote PHP-WASM. -## Demos práticas + ### Demo 1: Operações no sistema de arquivos + + Execute scripts PHP que interagem com o sistema de arquivos: ```javascript @@ -150,8 +118,12 @@ const result = await php.runStream({ scriptPath: '/app/index.php' }); console.log(await result.stdoutText); ``` + + ### Demo 2: Operações de banco de dados SQLite + + Use a extensão SQLite do PHP para armazenamento de dados: ```javascript @@ -210,8 +182,12 @@ const dbExists = await php.fileExists('/data/app.db'); console.log('\nDatabase persisted:', dbExists); ``` + + ### Demo 3: Processamento de arquivos enviados (arquivos ZIP) + + Processe arquivos ZIP usando a extensão Libzip do PHP: ```javascript @@ -271,8 +247,12 @@ foreach ($files as $file) { console.log(await result.stdoutText); ``` + + ### Demo 4: Padrão de requisição/resposta HTTP + + Simule comportamento de servidor web com manipuladores de requisição: ```javascript @@ -346,8 +326,12 @@ const postResponse = await php.runStream({ console.log('\\nPOST Response:', await postResponse.stdoutText); ``` + + ### Demo 5: Motor de renderização de templates + + Use PHP como um motor de templates para conteúdo dinâmico: ```javascript @@ -421,8 +405,12 @@ console.log(await result.stdoutText); // Agora você tem HTML renderizado que pode ser enviado por email ou salvo ``` + + ### Demo 6: Execução de código em tempo real e streaming + + Processe a saída do PHP conforme ela é gerada: ```javascript @@ -458,10 +446,16 @@ console.log(await streamedResponse.stdoutText); console.log(`\nExit code: ${streamedResponse.exitCode}`); ``` + + ## Padrões de integração + + ### Padrão 1: Middleware Express.js + + Integre processamento PHP em uma aplicação Express.js: ```TypeScript @@ -498,8 +492,12 @@ app.listen(3000, () => { }); ``` + + ### Padrão 2: Testes automatizados + + Crie testes automatizados para código PHP: ```TypeScript @@ -546,8 +544,12 @@ describe('PHP Functions', () => { }); ``` + + ### Padrão 3: Integração com ferramentas de build + + Use em scripts de build com outras ferramentas Node.js: ```javascript @@ -587,8 +589,12 @@ echo "Documentation generated successfully!\\n"; generateDocumentation().catch(console.error); ``` + + ## Recursos avançados + + ### Trabalhando com variáveis de ambiente ```javascript @@ -607,6 +613,8 @@ const result = await php.runStream({ console.log(await result.stdoutText); ``` + + ### Tratamento de erros ```javascript @@ -634,8 +642,15 @@ try { } ``` + + ## Considerações de desempenho + + + + + - **Reutilize instâncias PHP**: Criar uma nova instância PHP é custoso. Reutilize a mesma instância quando possível. - **Operações em lote**: Agrupe múltiplas operações de arquivo juntas em vez de executar scripts separados. - **Gerenciamento de memória**: Arquivos grandes podem impactar o desempenho. Considere streaming para grandes conjuntos de dados. From f38a2d8082a322ab6c61382a30ebda632779d01f Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Wed, 12 Nov 2025 15:22:01 +0000 Subject: [PATCH 8/8] updating code examples and French translation --- .../05-local-development/03-php-wasm-node.md | 10 +++++++--- .../05-local-development/03-php-wasm-node.md | 10 +++++++--- .../05-local-development/03-php-wasm-node.md | 14 +++++++++----- .../05-local-development/03-php-wasm-node.md | 10 +++++++--- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md index d14f89623a..c68043fcc0 100644 --- a/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/docs/developers/05-local-development/03-php-wasm-node.md @@ -391,9 +391,13 @@ const streamedResponse = await php.runStream({ scriptPath: '/stream-demo.php', }); -console.log('Processing PHP output:\n'); -console.log(await streamedResponse.stdoutText); -console.log(`\nExit code: ${streamedResponse.exitCode}`); +streamedResponse.stdout.pipeTo( + new WritableStream({ + write(chunk) { + console.log(chunk); + }, + }) +); ``` ## Integration patterns diff --git a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 081afb5dd7..06f878509e 100644 --- a/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/es/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -441,9 +441,13 @@ const streamedResponse = await php.runStream({ scriptPath: '/stream-demo.php', }); -console.log('Processing PHP output:\n'); -console.log(await streamedResponse.stdoutText); -console.log(`\nExit code: ${streamedResponse.exitCode}`); +streamedResponse.stdout.pipeTo( + new WritableStream({ + write(chunk) { + console.log(chunk); + }, + }) +); ``` diff --git a/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 3cac0a4b0e..623c5b9fb7 100644 --- a/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/fr/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -14,7 +14,7 @@ En tant que projet WebAssembly, vous pouvez aussi utiliser WordPress Playground -Si vous avez besoin d'un contrôle bas niveau sur la build WebAssembly de PHP, consultez le [paquet @php-wasm/node](https://npmjs.org/@php-wasm/node) qui fournit le runtime PHP WebAssembly. Ce paquet est au cœur de tous les outils WordPress Playground pour Node.js. +Si vous avez besoin d'un contrôle bas niveau sur la build WebAssembly de PHP, consultez le [paquet @php-wasm/node](https://npmjs.org/@php-wasm/node) qui fournit l'environnement d'exécution PHP WebAssembly. Ce paquet est au cœur de tous les outils WordPress Playground pour Node.js. @@ -26,7 +26,7 @@ Consultez la [liste complète](/api/node) des classes, fonctions, interfaces et -Ce paquet fournit les binaires PHP WebAssembly et l'API JavaScript optimisée pour Node.js. Il utilise directement le système de fichiers hôte et peut accéder au réseau si vous branchez un proxy WS personnalisé. +Ce paquet fournit les exécutables PHP WebAssembly et l'API JavaScript optimisée pour Node.js. Il utilise directement le système de fichiers hôte et peut accéder au réseau si vous branchez un proxy WS personnalisé. @@ -441,9 +441,13 @@ const streamedResponse = await php.runStream({ scriptPath: '/stream-demo.php', }); -console.log('Processing PHP output:\n'); -console.log(await streamedResponse.stdoutText); -console.log(`\nExit code: ${streamedResponse.exitCode}`); +streamedResponse.stdout.pipeTo( + new WritableStream({ + write(chunk) { + console.log(chunk); + }, + }) +); ``` diff --git a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md index 932790f555..cbe46564d7 100644 --- a/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md +++ b/packages/docs/site/i18n/pt-BR/docusaurus-plugin-content-docs/current/developers/05-local-development/03-php-wasm-node.md @@ -441,9 +441,13 @@ const streamedResponse = await php.runStream({ scriptPath: '/stream-demo.php', }); -console.log('Processing PHP output:\n'); -console.log(await streamedResponse.stdoutText); -console.log(`\nExit code: ${streamedResponse.exitCode}`); +streamedResponse.stdout.pipeTo( + new WritableStream({ + write(chunk) { + console.log(chunk); + }, + }) +); ```