@capsulescodes/browser-php 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Capsules Codes <hello@capsules.codes>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,145 @@
1
+
2
+ <p align="center"><img src="art/capsules-browser-php-image.svg" width="400px" height="265px" alt="Browser PHP" /></p>
3
+
4
+ Run any PHP stuff within your browser.
5
+
6
+ The Browser PHP package offers a collection of commands for running PHP from the Node CLI or for launching a PHP server from Node. Perfect for running a Laravel project in CodeSandbox, for example.
7
+
8
+ <br>
9
+
10
+ [This article](https://capsules.codes/en/blog/fyi/en-fyi-run-laravel-on-your-browser-with-browser-php) provides an in-depth exploration of the package.
11
+
12
+ <br>
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install --save-dev @capsulescodes/browser-php
18
+ ```
19
+
20
+ <br>
21
+ <br>
22
+
23
+ ## Usage
24
+
25
+ This package gives access to three binaries. `php-cli` and `php-server` from `node_modules`, `composer` from `vendor`.
26
+
27
+ <br>
28
+
29
+ ### PHP
30
+ ```
31
+ node node_modules/.bin/php-cli
32
+ ```
33
+
34
+ <br>
35
+
36
+ Or add `script` in `package.json`
37
+ ```
38
+ "scripts" : {
39
+ "php" : "php-cli"
40
+ },
41
+ ```
42
+
43
+ <br>
44
+
45
+ Example
46
+ ```
47
+ npm run php -r "echo 'Hello Browser PHP World!';"
48
+
49
+
50
+ > Hello Browser PHP World!
51
+ ```
52
+
53
+ <br>
54
+ <br>
55
+
56
+ ### Serve
57
+ ```
58
+ node node_modules/.bin/php-server
59
+ ```
60
+
61
+ <br>
62
+
63
+ Or add `script` in `package.json`
64
+ ```
65
+ "scripts" : {
66
+ "serve" : "php-server"
67
+ },
68
+ ```
69
+
70
+ <br>
71
+
72
+ Example
73
+ ```
74
+ npm run serve
75
+
76
+
77
+ > PHP server is listening on url http://localhost:2222
78
+ ```
79
+
80
+ <br>
81
+ <br>
82
+
83
+
84
+ ### Composer
85
+ ```
86
+ node node_modules/.bin/php vendor/bin/composer
87
+ ```
88
+
89
+ <br>
90
+
91
+ Or add `script` in `package.json`
92
+ ```
93
+ "scripts" : {
94
+ "composer": "php-cli vendor/bin/composer"
95
+ },
96
+ ```
97
+
98
+ <br>
99
+
100
+ Example
101
+ ```
102
+ npm run composer
103
+
104
+ > ______
105
+ > / ____/___ ____ ___ ____ ____ ________ _____
106
+ > / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
107
+ > / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
108
+ > \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
109
+ > /_/
110
+ > Composer version 2.7.7 2024-06-10 22:11:12
111
+ ```
112
+
113
+ <br>
114
+ <br>
115
+
116
+ ## Configuration
117
+
118
+ The scripts can be configured with environment variables
119
+
120
+ - `BROWSER_PHP_VERSION` : The PHP version you need | default : `8.2`
121
+
122
+ - `BROWSER_PHP_COMPOSER_VERSION` : The Composer executable version | default : `2.7.7`
123
+ - `BROWSER_PHP_COMPOSER_PATH` : The Composer executable path | default : `vendor/bin`
124
+ - `BROWSER_PHP_COMPOSER_NAME` : The Composer executable name | default : `composer`
125
+
126
+ - `BROWSER_PHP_SERVER_HOST` : The host name you need | default : `http://localhost`
127
+ - `BROWSER_PHP_SERVER_PORT` : The port you need | default : `2222`
128
+ - `BROWSER_PHP_SERVER_PATH` : The directory path you need | default : `public`
129
+ - `BROWSER_PHP_SERVER_DEBUG` : The debug mode you need | default : `false`
130
+
131
+ <br>
132
+ <br>
133
+
134
+ ## Contributing
135
+
136
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
137
+ Please make sure to update tests as appropriate.
138
+
139
+ ## Credits
140
+
141
+ - [Capsules Codes](https://github.com/capsulescodes)
142
+
143
+ ## License
144
+
145
+ [MIT](https://choosealicense.com/licenses/mit/)
package/bin/cli.js ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ import { PHP as i } from "@php-wasm/universal";
3
+ import { loadNodeRuntime as n, createNodeFsMountHandler as c } from "@php-wasm/node";
4
+ import r from "../dist/env.js";
5
+ const o = new i(await n(r.php.version));
6
+ o.mkdir(process.cwd());
7
+ o.mount(process.cwd(), c(process.cwd()));
8
+ o.chdir(process.cwd());
9
+ let e = process.argv.slice(2);
10
+ e.includes("--disable-functions") && (e.splice(e.indexOf("--disable-functions"), 1), e = ["-d", "disable_functions=proc_open,popen", ...e]);
11
+ o.cli(["php", ...e]).catch((s) => {
12
+ throw s;
13
+ }).finally(() => process.exit(0));
package/bin/server.js ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ import h from "http";
3
+ import r from "../dist/env.js";
4
+ import { PHPRequestHandler as u, PHP as m } from "@php-wasm/universal";
5
+ import { loadNodeRuntime as w, createNodeFsMountHandler as f } from "@php-wasm/node";
6
+ let n, d;
7
+ const H = h.createServer(async (e, o) => {
8
+ if (n) {
9
+ if (e.url) {
10
+ const s = {};
11
+ if (e.rawHeaders && e.rawHeaders.length)
12
+ for (let t = 0; t < e.rawHeaders.length; t += 2)
13
+ s[e.rawHeaders[t]] = e.rawHeaders[t + 1];
14
+ const l = new Promise((t) => {
15
+ const i = [];
16
+ e.on("data", (p) => i.push(p)).on("end", () => t(Buffer.concat(i).toString()));
17
+ }), c = { method: e.method, url: e.url, headers: s, body: await l }, a = await n.request(c);
18
+ r.server.debug && console.log(c, a), delete a.headers["x-frame-options"], Object.keys(a.headers).forEach((t) => o.setHeader(t, a.headers[t])), o.statusCode = a.httpStatusCode, o.end(a.bytes);
19
+ }
20
+ } else if (!d) {
21
+ d = !0, n = new u({ phpFactory: async () => new m(await w(r.php.version)), documentRoot: r.server.path, absoluteUrl: `${r.server.host}:${r.server.port}` });
22
+ const s = await n.getPrimaryPhp();
23
+ s.mkdir(process.cwd()), s.mount(process.cwd(), f(process.cwd())), s.chdir(process.cwd()), d = !1, o.statusCode = 302, o.setHeader("location", e.url ?? ""), o.end();
24
+ }
25
+ });
26
+ H.listen(r.server.port, async () => console.log(`
27
+ PHP server is listening on ${r.server.host}:${r.server.port}
28
+ `));
package/dist/env.js ADDED
@@ -0,0 +1,19 @@
1
+ import { config as e } from "dotenv";
2
+ e({ path: process.env.INIT_CWD ? `${process.env.INIT_CWD}/.env` : ".env" });
3
+ const s = {
4
+ php: { version: process.env.BROWSER_PHP_VERSION != null ? process.env.BROWSER_PHP_VERSION : "8.2" },
5
+ composer: {
6
+ name: process.env.BROWSER_PHP_COMPOSER_NAME ?? "composer",
7
+ version: process.env.BROWSER_PHP_COMPOSER_VERSION ?? "2.7.7",
8
+ path: process.env.BROWSER_PHP_COMPOSER_PATH ?? "vendor/bin"
9
+ },
10
+ server: {
11
+ host: process.env.BROWSER_PHP_SERVER_HOST ?? "http://localhost",
12
+ port: process.env.BROWSER_PHP_SERVER_PORT ?? "2222",
13
+ path: process.env.BROWSER_PHP_SERVER_PATH ?? "public",
14
+ debug: process.env.BROWSER_PHP_SERVER_DEBUG != null && process.env.BROWSER_PHP_SERVER_DEBUG != "false"
15
+ }
16
+ };
17
+ export {
18
+ s as default
19
+ };
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ import s from "fs";
3
+ import t from "https";
4
+ import e from "./env.js";
5
+ process.env.INIT_CWD === process.cwd() && !process.env.VITEST && process.exit();
6
+ t.get(`https://getcomposer.org/download/${e.composer.version}/composer.phar`, (o) => {
7
+ const r = process.env.INIT_CWD ? `${process.cwd()}/${e.composer.path}` : e.composer.path;
8
+ s.existsSync(r) || s.mkdirSync(r, { recursive: !0 });
9
+ const p = s.createWriteStream(`${r}/${e.composer.name}`);
10
+ o.pipe(p);
11
+ }).on("error", (o) => {
12
+ throw o;
13
+ });
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name" : "@capsulescodes/browser-php",
3
+ "description" : "PHP CLI and Web server on browser",
4
+ "version" : "0.5.5",
5
+ "keywords" : [
6
+ "node",
7
+ "browser",
8
+ "php"
9
+ ],
10
+ "homepage" : "https://github.com/capsulescodes/browser-php",
11
+ "repository" : {
12
+ "type" : "git",
13
+ "url" : "git+https://github.com/capsulescodes/browser-php.git"
14
+ },
15
+ "license" : "MIT",
16
+ "author" : {
17
+ "name" : "Yannick Decat",
18
+ "email" : "yannick@capsules.codes",
19
+ "url" : "https://capsules.codes/"
20
+ },
21
+ "scripts" : {
22
+ "build" : "vite build",
23
+ "test" : "vitest run",
24
+ "postinstall" : "node dist/installer.js"
25
+ },
26
+ "files" : [
27
+ "!*",
28
+ "bin",
29
+ "dist"
30
+ ],
31
+ "bin" : {
32
+ "php-server" : "bin/server.js",
33
+ "php-cli" : "bin/cli.js"
34
+ },
35
+ "dependencies" : {
36
+ "@php-wasm/node" : "^1.0.8",
37
+ "@php-wasm/universal" : "^1.0.8",
38
+ "dotenv" : "^16.4.5",
39
+ "vite" : "^5.4.10"
40
+ },
41
+ "devDependencies" : {
42
+ "@stylistic/eslint-plugin" : "^2.10.1",
43
+ "@types/node" : "^22.8.6",
44
+ "eslint" : "^9.13.0",
45
+ "tsx" : "^4.19.2",
46
+ "typescript" : "^5.6.3",
47
+ "typescript-eslint" : "^8.12.2",
48
+ "vitest" : "^2.1.4g"
49
+ },
50
+ "type" : "module"
51
+ }