Skip to content

Commit ad31298

Browse files
committed
[CLI] When PHP is spawned in the spawn handler, create a new OS process
1 parent 9964612 commit ad31298

File tree

7 files changed

+152
-243
lines changed

7 files changed

+152
-243
lines changed

packages/php-wasm/node/src/test/php-part-1.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,11 @@ describe('sandboxedSpawnHandlerFactory', () => {
18501850
'Hello, world!'
18511851
);
18521852
await php.setSpawnHandler(
1853-
sandboxedSpawnHandlerFactory(processManager)
1853+
sandboxedSpawnHandlerFactory(() =>
1854+
processManager.acquirePHPInstance({
1855+
considerPrimary: false,
1856+
})
1857+
)
18541858
);
18551859
return php;
18561860
},

packages/php-wasm/node/src/test/php-part-2.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,11 @@ describe('sandboxedSpawnHandlerFactory', () => {
11801180
'Hello, world!'
11811181
);
11821182
await php.setSpawnHandler(
1183-
sandboxedSpawnHandlerFactory(processManager)
1183+
sandboxedSpawnHandlerFactory(() =>
1184+
processManager.acquirePHPInstance({
1185+
considerPrimary: false,
1186+
})
1187+
)
11841188
);
11851189
return php;
11861190
},

packages/php-wasm/universal/src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ export { sandboxedSpawnHandlerFactory } from './sandboxed-spawn-handler-factory'
8888

8989
export * from './api';
9090
export type { WithAPIState as WithIsReady } from './api';
91+
export type { Remote } from './comlink-sync';

packages/php-wasm/universal/src/lib/sandboxed-spawn-handler-factory.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { createSpawnHandler } from '@php-wasm/util';
2-
import type { PHPProcessManager } from './php-process-manager';
2+
import type { PHP } from './php';
3+
import type { PHPWorker } from './php-worker';
4+
import type { Remote } from './comlink-sync';
35

46
/**
57
* An isomorphic proc_open() handler that implements typical shell in TypeScript
@@ -12,7 +14,10 @@ import type { PHPProcessManager } from './php-process-manager';
1214
* parser.
1315
*/
1416
export function sandboxedSpawnHandlerFactory(
15-
processManager: PHPProcessManager
17+
getPHPInstance: () => Promise<{
18+
php: PHP | Remote<PHPWorker>;
19+
reap: () => void;
20+
}>
1621
) {
1722
return createSpawnHandler(async function (args, processApi, options) {
1823
processApi.notifySpawn();
@@ -63,16 +68,14 @@ export function sandboxedSpawnHandlerFactory(
6368
return;
6469
}
6570

66-
const { php, reap } = await processManager.acquirePHPInstance({
67-
considerPrimary: false,
68-
});
71+
const { php, reap } = await getPHPInstance();
6972

7073
try {
7174
if (options.cwd) {
72-
php.chdir(options.cwd as string);
75+
await php.chdir(options.cwd as string);
7376
}
7477

75-
const cwd = php.cwd();
78+
const cwd = await php.cwd();
7679
switch (binaryName) {
7780
case 'php': {
7881
// Figure out more about setting env, putenv(), etc.
@@ -105,7 +108,7 @@ export function sandboxedSpawnHandlerFactory(
105108
break;
106109
}
107110
case 'ls': {
108-
const files = php.listFiles(args[1] ?? cwd);
111+
const files = await php.listFiles(args[1] ?? cwd);
109112
for (const file of files) {
110113
processApi.stdout(file + '\n');
111114
}

0 commit comments

Comments
 (0)