-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #451 from live-codes/php-wasm
php-wasm
- Loading branch information
Showing
18 changed files
with
347 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# PHP (Wasm) | ||
|
||
import LiveCodes from '../../src/components/LiveCodes.tsx'; | ||
|
||
PHP in Browser, powered by WebAssembly (using [php-wasm](https://github.com/seanmorris/php-wasm)). | ||
|
||
`php-wasm` runs PHP in the browser using WebAssembly. This matches the behavior of the official PHP interpreter and allows using PHP's standard library. However, it requires relatively large download and has limited capabilities for client-side DOM manipulation. | ||
|
||
:::info Note | ||
|
||
If you need a lighter-weight interpreter with more capable client-side DOM manipulation in PHP and do not need to use PHP's standard library, you may want to use the [PHP interpreter written in JavaScript](./php.md). | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
### Standard Library | ||
|
||
The PHP standard library is supported. | ||
|
||
```php | ||
<?php | ||
phpinfo(); | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
export const infoConfig = {script: {language: "php-wasm", content: `<?php\n\nphpinfo();`}, activeEditor: "script", mode: "result"}; | ||
|
||
<LiveCodes config={infoConfig} height="80vh"></LiveCodes> | ||
|
||
### JavaScript Interoperability | ||
|
||
[JavaScript Interoperability](https://github.com/seanmorris/php-wasm#accessing-the-dom) is achieved via the [VRZNO](https://github.com/seanmorris/vrzno) php extension. | ||
|
||
Example: | ||
|
||
<!-- prettier-ignore --> | ||
export const jsOpParams = { phpwasm: `<?php\n\n// read from DOM\n$oldTitle = vrzno_eval('document.querySelector("#title").innerText');\necho $oldTitle;\n\n$newTitle = 'Changed@' . date('h:i:s');\n\n// set DOM properties\nvrzno_eval('document.querySelector("#title").innerText = "' . $newTitle . '"' );\n\n// run console.log\nvrzno_eval('console.log("Hello, World!")');\n`, html: `<h1 id="title">Hello, PHP!</h1>\n`, console: 'open' }; | ||
|
||
<LiveCodes params={jsOpParams} height="80vh"></LiveCodes> | ||
|
||
Check the [starter template](#example-usage) for another example. | ||
|
||
## Language Info | ||
|
||
### Name | ||
|
||
`php-wasm` | ||
|
||
### Extension | ||
|
||
`.wasm.php` | ||
|
||
### Alias | ||
|
||
`phpwasm` | ||
|
||
### Editor | ||
|
||
`script` | ||
|
||
## Compiler | ||
|
||
[php-wasm](https://github.com/seanmorris/php-wasm) | ||
|
||
### Version | ||
|
||
`php-wasm` v0.0.7, running PHP v8.2.4 | ||
|
||
## Code Formatting | ||
|
||
Using [prettier](https://prettier.io/) and [Prettier PHP Plugin](https://github.com/prettier/plugin-php). | ||
|
||
## Example Usage | ||
|
||
<LiveCodes template="php-wasm" height="80vh"></LiveCodes> | ||
|
||
## Starter Template | ||
|
||
https://livecodes.io/?template=php-wasm | ||
|
||
## Links | ||
|
||
- [PHP](https://php.net/) | ||
- [PHP documentation](https://www.php.net/manual/en/) | ||
- [php-wasm](https://github.com/seanmorris/php-wasm) | ||
- [PHP using Uniter](./php.md) in LiveCodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,60 @@ | ||
# PHP | ||
|
||
TODO... | ||
import LiveCodes from '../../src/components/LiveCodes.tsx'; | ||
|
||
PHP running client-side in the browser using [Uniter](https://phptojs.com/). | ||
|
||
Uniter is a light-weight PHP-to-JavaScript transpiler and runtime with JavaScript interoperability and client-side DOM manipulation. However, only a small subset of PHP's standard library is supported. | ||
|
||
:::info Note | ||
|
||
If you need better standard library support, you may want to use [`php-wasm`](./php-wasm.md). | ||
|
||
`php-wasm` runs PHP in the browser using WebAssembly. This matches the behavior of the official PHP interpreter and allows using PHP's standard library. However, it requires relatively large download and has limited capabilities for client-side DOM manipulation. | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
### JavaScript Interoperability | ||
|
||
Example: | ||
|
||
<LiveCodes template="php" height="80vh"></LiveCodes> | ||
|
||
## Language Info | ||
|
||
### Name | ||
|
||
`php` | ||
|
||
### Extension | ||
|
||
`.php` | ||
|
||
### Editor | ||
|
||
`script` | ||
|
||
## Compiler | ||
|
||
[Uniter](https://phptojs.com/) | ||
|
||
### Version | ||
|
||
Uniter v2.18.0 | ||
|
||
## Code Formatting | ||
|
||
Using [prettier](https://prettier.io/) and [Prettier PHP Plugin](https://github.com/prettier/plugin-php). | ||
|
||
## Starter Template | ||
|
||
https://livecodes.io/?template=php | ||
|
||
## Links | ||
|
||
- [PHP](https://php.net/) | ||
- [PHP documentation](https://www.php.net/manual/en/) | ||
- [Uniter](https://phptojs.com/) | ||
- [PHP using `php-wasm`](./php-wasm.md) in LiveCodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lang-php-wasm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// based on https://github.com/seanmorris/php-wasm/blob/master/source/PhpWeb.js | ||
|
||
declare const phpWasm: any; | ||
const php = new phpWasm.PHP(); | ||
|
||
php.addEventListener('ready', () => { | ||
// set to browser timezone (by default it is set to 'UTC') | ||
const timeZone = window.Intl?.DateTimeFormat?.().resolvedOptions().timeZone || 'UTC'; | ||
php.run(`<?php date_default_timezone_set('${timeZone}');`); | ||
}); | ||
|
||
const runPhpScript = (element: HTMLElement) => { | ||
const inlineCode = element?.innerText?.trim(); | ||
if (!inlineCode) return; | ||
|
||
const output = document.createElement('div'); | ||
element.parentNode?.insertBefore(output, element.nextSibling); | ||
let buffer = ''; | ||
|
||
php.addEventListener('output', (event: CustomEvent) => (buffer += event.detail)); | ||
|
||
php.addEventListener('ready', () => { | ||
php.run(inlineCode).then(() => { | ||
output.innerHTML = buffer; | ||
}); | ||
}); | ||
|
||
php.addEventListener('error', (event: CustomEvent) => { | ||
event.detail.forEach((error: string) => { | ||
error = error.trim(); | ||
// eslint-disable-next-line no-console | ||
if (error) console.log(error); | ||
}); | ||
}); | ||
}; | ||
|
||
addEventListener('load', () => { | ||
const phpSelector = 'script[type="text/php-wasm"]'; | ||
const phpNodes = document.querySelectorAll<HTMLElement>(phpSelector); | ||
for (const phpNode of phpNodes) { | ||
runPhpScript(phpNode); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { LanguageSpecs } from '../../models'; | ||
import { parserPlugins } from '../prettier'; | ||
import { vendorsBaseUrl } from '../../vendors'; | ||
|
||
export const phpWasm: LanguageSpecs = { | ||
name: 'php-wasm', | ||
title: 'PHP (Wasm)', | ||
parser: { | ||
name: 'php', | ||
pluginUrls: [parserPlugins.php], | ||
}, | ||
compiler: { | ||
factory: () => async (code) => code, | ||
scripts: ({ baseUrl }) => [ | ||
vendorsBaseUrl + 'php-wasm/php-wasm.js', | ||
baseUrl + '{{hash:lang-php-wasm-script.js}}', | ||
], | ||
scriptType: 'text/php-wasm', | ||
compiledCodeLanguage: 'php', | ||
}, | ||
extensions: ['wasm.php', 'phpwasm'], | ||
editor: 'script', | ||
editorLanguage: 'php', | ||
}; |
Oops, something went wrong.