Skip to content

Commit

Permalink
CLI and networking support (#119)
Browse files Browse the repository at this point in the history
### Description

Adds support for CLI SAPI and networking in node.js:

```
> npm run build

> node ./build-cli/php-cli.js -r 'echo "Hello from PHP !";'
Hello from PHP !

> node ./build-cli/php-cli.js -r 'echo substr(file_get_contents("https://wordpress.org"), 0, 16);'
<!DOCTYPE html>

> node ./build-cli/php-cli.js -r 'echo phpversion();'
8.2.0-dev

> PHP=5.6 node ./build-cli/php-cli.js -r 'echo phpversion();'
5.6.40
```

### Highlights:

* Networking is supported (including MySQL and HTTPS)
* Switching PHP versions is supported.
* [Most WordPress PHPUnit tests pass](WordPress/wordpress-playground#111). The failures are caused by missing extensions and a few misconfigured settings
* PHP Interactive mode is supported but [the arrow keys don't work](WordPress/wordpress-playground#118)
* `wp-cli` works

### In broad strokes:

* CLI SAPI is compiled with libedit (readline replacement) and ncurses. 
* Network calls are asynchronous. Emscripten's Asyncify enables calling asynchronous code from synchronous code. TCP sockets are shimmed with a WebSocket connection to a built-in proxy server running on localhost. It supports data transfer, arbitrary connection targets, and setting a few TCP socket options.
* PHP's OpenSSL uses the same CA certs as Node.js
* PHP 5.6 is patched to work with OpenSSL 1.1.0 and many other small patches are introduced. For more details, see [patches overview](WordPress/wordpress-playground#119 (comment)), Dockerfile, and `phpwasm-emscripten-library.js`

### Future work:

* PHP Interactive server isn't supported yet. Adding support is a matter of making the incoming connection polling non-blocking using Asyncify.
* Use a more recent OpenSSL version
* [Better support for CLI interactive mode](WordPress/wordpress-playground#118)
  • Loading branch information
Pookie717 committed Jan 30, 2023
1 parent b16eaa5 commit 3b11b1d
Show file tree
Hide file tree
Showing 66 changed files with 68,553 additions and 814 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/**/*
build/*
packages/*/build*/*
src/php-wasm/wasm/build-assets/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
node_modules
package-lock.json
build/*
build-cli/*
build-api
build-wp
build-php
Expand Down
Empty file added build-cli/.gitkeep
Empty file.
24 changes: 24 additions & 0 deletions build-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "php-cli",
"version": "0.1.4",
"description": "PHP CLI via WebAssembly. Part of WordPress Playground.",
"main": "php-cli.js",
"author": "The WordPress contributors",
"contributors": [
{
"name": "Adam Zielinski",
"email": "[email protected]",
"url": "https://github.com/adamziel"
}
],
"license": "Apache-2.0",
"dependencies": {
"ws": "^8.12.0"
},
"bin": "php-cli.js",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/wordpress-playground"
},
"homepage": "https://developer.wordpress.org/playground"
}
13 changes: 13 additions & 0 deletions build-cli/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error_reporting=E_ALL
display_errors=1
html_errors=1
display_startup_errors=On
log_errors=1
always_populate_raw_post_data=-1
upload_max_filesize=2000M
post_max_size=2000M
disable_functions=proc_open,popen,curl_exec,curl_multi_exec
allow_url_fopen=On
session.save_path=/home/web_user
implicit_flush=1
output_buffering=0
6 changes: 4 additions & 2 deletions build/php-5.6.js

Large diffs are not rendered by default.

Loading

0 comments on commit 3b11b1d

Please sign in to comment.