{"id":13614007,"url":"https://github.com/soyuka/php-wasm","last_synced_at":"2025-04-13T18:31:54.456Z","repository":{"id":37751424,"uuid":"300839220","full_name":"soyuka/php-wasm","owner":"soyuka","description":"PHP WebAssembly docker image. Original Project by Oraoto and Sean Morris. ","archived":false,"fork":true,"pushed_at":"2024-10-16T13:46:32.000Z","size":50512,"stargazers_count":80,"open_issues_count":1,"forks_count":6,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-11-07T22:41:55.125Z","etag":null,"topics":["docker","php","wasm","webassembly"],"latest_commit_sha":null,"homepage":"https://soyuka.github.io/php-wasm/","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"seanmorris/php-wasm","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soyuka.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["soyuka"]}},"created_at":"2020-10-03T09:05:03.000Z","updated_at":"2024-10-22T14:28:08.000Z","dependencies_parsed_at":"2023-01-17T17:00:53.786Z","dependency_job_id":"92ef5e68-7ce7-41e2-a2f4-0581289b0879","html_url":"https://github.com/soyuka/php-wasm","commit_stats":null,"previous_names":[],"tags_count":2,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyuka%2Fphp-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyuka%2Fphp-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyuka%2Fphp-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyuka%2Fphp-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soyuka","download_url":"https://codeload.github.com/soyuka/php-wasm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248760426,"owners_count":21157354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","php","wasm","webassembly"],"created_at":"2024-08-01T20:00:55.747Z","updated_at":"2025-04-13T18:31:54.071Z","avatar_url":"https://github.com/soyuka.png","language":"Dockerfile","readme":"# WASM PHP\n\nProject based on https://github.com/seanmorris/php-wasm which was forked from https://github.com/oraoto/pib\n\nI fixed some inconsistencies in the Makefile and removed non-essential things. This fork:\n  - builds sqlite3\n  - builds libxml\n  - no javascript abstraction\n  - exposes FS and builds with [IDBFS](https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.syncfs)\n  - does not build https://github.com/seanmorris/vrzno which allows javascript access from PHP (TODO add this back opt-in cause it's really cool)\n  - does not add preloaded data, having this separatly from php-wasm builds allows for more flexibility (see [preload data section](#preload-data))\n\n## Build\n\n```\ndocker buildx bake\n```\n\nBuilded files will be located in `build/php-web.js` and `build/php-web.wasm`.\nThe module we export in this image is called `createPhpModule`.\n\n### Build arguments\n\nUse this as template to build PHP with emscripten. At build these arguments are available:\n\n```console\nLIBXML2_TAG=v2.9.10\nPHP_BRANCH=PHP-8.3.0\n```\n\nThe next args are used for [emcc options](https://github.com/soyuka/php-wasm/blob/513f284e1ba8f26d66e08a97291f484b3dd7de1b/Dockerfile#L108) `-sOPTION`\nsee [settings.js](https://github.com/emscripten-core/emscripten/blob/9bdb310b89472a0f4d64f36e4a79273d8dc7fa98/src/settings.js#L633).\nIn fact it's even easier for you to set them directly in [the Dockerfile](https://github.com/soyuka/php-wasm/blob/513f284e1ba8f26d66e08a97291f484b3dd7de1b/Dockerfile#L108).\n\n```console\nWASM_ENVIRONMENT=web\nASSERTIONS=0\nOPTIMIZE=-O2\nINITIAL_MEMORY=256mb\nJAVASCRIPT_EXTENSION=mjs # change by js if needed\nMODULARIZE=1\nEXPORT_ES6=1\nEXPORT_NAME=createPhpModule\n```\n\n### Preload data\n\nMy preferred option is to use the [`file_packager`](https://github.com/emscripten-core/emscripten/blob/9bdb310b89472a0f4d64f36e4a79273d8dc7fa98/tools/file_packager) tool to build the preloaded data in a `php-web.data.js` (and `php-web.data` file). These are preloaded into IDBFS. That can be changed changing the `-lidbfs.js` argument to `emcc`.\n\nThis will preload `SOME_DIR` into the `/src` directory inside the WASM filesystem:\n\n```\nmkdir -p php-wasm\ndocker run -v SOME_DIR:/src -v $(pwd)/php-wasm:/dist -w /dist soyuka/php-wasm:8.2.9 python3 /emsdk/upstream/emscripten/tools/file_packager.py php-web.data --use-preload-cache --lz4 --preload \"/src\" --js-output=php-web.data.js --no-node --exclude '*/.*' --export-name=createPhpModule\nls php-wasm/\n```\n\nNote that the `php-web.data.js` must be either used as `PRE_JS` argument to emcc or it needs to be included inside the `php-web.js`:\n\n```\nsed '/--pre-js/r php-wasm/php-web.data.js' php-wasm/php-web.mjs \u003e this-has-preloaded-data-php-web.mjs\n```\n\nWe match the `export-name` with the emcc `EXPORT_NAME` option. Use excludes to downsize the preloaded data weight.\n\n## Usage\n\nTo execute some php, call `phpw_exec` using [`ccall`](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-cwrap), for example:\n\n```javascript\nconst phpBinary = require('build/php-web');\n\nreturn phpBinary({\n    onAbort: function(reason) {\n      console.error('WASM aborted: ' + reason)\n    },\n    print: function (...args) {\n      console.log('stdout: ', args)\n    },\n    printErr: function (...args) {\n      console.log('stderr: ', args)\n    }\n})\n.then(({ccall, FS}) =\u003e {\n  const phpVersion = ccall(\n    'phpw_exec'\n    , 'string'\n    , ['string']\n    , [`phpversion();`]\n  );\n})\n```\n\n### API\n\n```javascript\nphpw_exec(string code): string\nphpw_run(string code): void\nphpw(string filePath): void\n```\n\n### Example calls:\n\n\n```javascript\nconst STR = 'string';\nccall(\"phpw\", null, [STR], [\"public/index.php\"]);\nconsole.log(ccall(\"phpw_exec\", STR, [STR], [\"phpversion();\"]));\n```\n\n[More about how to call exposed functions](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html?highlight=call#interacting-with-code-ccall-cwrap)\n\n## Demo\n\nBefore running the demo, you need to build the library.\n\n```bash\ndocker buildx bake\n```\n\nThen you need to copy\nthe `php-web.js` and `php-web.wasm` to the `demo` directory.\n\n```console\n$ cp build/php-web.* demo/public/\n```\n\nThen to build the preloaded data:\n\n```bash\ndocker run \\\n  -v $(pwd)/demo/src:/src \\\n  -v $(pwd)/demo/public:/dist \\\n  -w /dist \\\n  soyuka/php-wasm:8.2.9 \\\n  python3 \\\n    /emsdk/upstream/emscripten/tools/file_packager.py \\\n    php-web.data \\\n      --use-preload-cache \\\n      --lz4 \\\n      --preload \"/src\" \\\n      --js-output=php-web.data.js \\\n      --no-node \\\n      --exclude '*/.*' \\\n      --export-name=createPhpModule\n```\n\nThen attach the `php-web.data.js` to the `demo/public/php-web.mjs`:\n\n```bash\nsed '/--pre-js/r demo/public/php-web.data.js' demo/public/php-web.mjs \u003e this-has-preloaded-data-php-web.mjs ; mv this-has-preloaded-data-php-web.mjs demo/public/php-web.mjs\n```\n\nAnd finally open a web sserver in the `demo` directory:\n\n```console\n$ php -S 127.0.0.1:8000 -t demo/public\n```\n\n## TODO\n\n- add opt-in / opt-out sqlite libxml vrzno and mb more\n","funding_links":["https://github.com/sponsors/soyuka"],"categories":["Dockerfile"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyuka%2Fphp-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoyuka%2Fphp-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyuka%2Fphp-wasm/lists"}