Skip to content

Commit 1481e4c

Browse files
committed
Accept php instance directly in PHPRequestHandler configuration
PHPRequestHandler now supports three configuration options: - instanceManager: Custom PHPInstanceManager (advanced) - php: Single PHP instance (creates SinglePHPInstanceManager internally) - phpFactory + maxPhpInstances: Factory function (creates PHPProcessManager) When a php instance is provided directly, the same transformations (mkdir documentRoot, chdir, set requestHandler) are applied as with phpFactory, keeping the behavior consistent.
1 parent 11dbdf4 commit 1481e4c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/php-wasm/universal/src/lib/php-request-handler.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { encodeAsMultipart } from './encode-as-multipart';
1313
import type { PHPFactoryOptions } from './php-process-manager';
1414
import { MaxPhpInstancesError, PHPProcessManager } from './php-process-manager';
1515
import type { PHPInstanceManager, AcquiredPHP } from './php-instance-manager';
16+
import { SinglePHPInstanceManager } from './single-php-instance-manager';
1617
import { HttpCookieStore } from './http-cookie-store';
1718
import mimeTypes from './mime-types.json';
1819

@@ -96,6 +97,14 @@ export type PHPRequestHandlerConfiguration = BaseConfiguration & {
9697
*/
9798
instanceManager: PHPInstanceManager;
9899
}
100+
| {
101+
/**
102+
* Provide a single PHP instance directly.
103+
* PHPRequestHandler will create a SinglePHPInstanceManager internally.
104+
* This is the simplest option for CLI contexts with a single PHP instance.
105+
*/
106+
php: PHP;
107+
}
99108
| {
100109
/**
101110
* Provide a factory function to create PHP instances.
@@ -207,6 +216,19 @@ export class PHPRequestHandler implements AsyncDisposable {
207216

208217
if ('instanceManager' in config) {
209218
this.processManager = config.instanceManager;
219+
} else if ('php' in config) {
220+
const php = config.php;
221+
222+
// Always set managed PHP's cwd to the document root.
223+
if (!php.isDir(documentRoot)) {
224+
php.mkdir(documentRoot);
225+
}
226+
php.chdir(documentRoot);
227+
228+
// @TODO: Decouple PHP and request handler
229+
(php as any).requestHandler = this;
230+
231+
this.processManager = new SinglePHPInstanceManager({ php });
210232
} else {
211233
this.processManager = new PHPProcessManager({
212234
phpFactory: async (info) => {

0 commit comments

Comments
 (0)