@@ -50,6 +50,7 @@ export async function bootWordPressAndRequestHandler(
5050export interface BootRequestHandlerOptions {
5151 createPhpRuntime : ( isPrimary ?: boolean ) => Promise < number > ;
5252 onPHPInstanceCreated ?: PHPInstanceCreatedHook ;
53+ maxPhpInstances ?: number ;
5354 /**
5455 * PHP SAPI name to be returned by get_sapi_name(). Overriding
5556 * it is useful for running programs that check for this value,
@@ -62,7 +63,7 @@ export interface BootRequestHandlerOptions {
6263 */
6364 siteUrl : string ;
6465 documentRoot ?: string ;
65- spawnHandler ?: ( instanceManager : PHPInstanceManager ) => SpawnHandler ;
66+ spawnHandler ?: ( instanceManager ? : PHPInstanceManager ) => SpawnHandler ;
6667 /**
6768 * PHP.ini entries to define before running any code. They'll
6869 * be used for all requests.
@@ -356,8 +357,8 @@ async function assertValidDatabaseConnection(
356357export async function bootRequestHandler ( options : BootRequestHandlerOptions ) {
357358 const spawnHandler = options . spawnHandler ?? sandboxedSpawnHandlerFactory ;
358359 async function createPhp (
359- requestHandler : PHPRequestHandler ,
360- isPrimary : boolean
360+ requestHandler ? : PHPRequestHandler ,
361+ isPrimary = false
361362 ) {
362363 const runtimeId = await options . createPhpRuntime ( isPrimary ) ;
363364 const php = new PHP ( runtimeId ) ;
@@ -415,7 +416,7 @@ export async function bootRequestHandler(options: BootRequestHandlerOptions) {
415416 // `popen()`, `proc_open()` etc. calls.
416417 if ( spawnHandler ) {
417418 await php . setSpawnHandler (
418- spawnHandler ( requestHandler . processManager )
419+ spawnHandler ( requestHandler ? .processManager )
419420 ) ;
420421 }
421422
@@ -434,14 +435,30 @@ export async function bootRequestHandler(options: BootRequestHandlerOptions) {
434435 }
435436
436437 const requestHandler : PHPRequestHandler = new PHPRequestHandler ( {
437- phpFactory : async ( { isPrimary } ) =>
438- createPhp ( requestHandler , isPrimary ) ,
439438 documentRoot : options . documentRoot || '/wordpress' ,
440439 absoluteUrl : options . siteUrl ,
441440 rewriteRules : wordPressRewriteRules ,
442441 getFileNotFoundAction :
443442 options . getFileNotFoundAction ?? getFileNotFoundActionForWordPress ,
444443 cookieStore : options . cookieStore ,
444+
445+ /**
446+ * If maxPhpInstances is 1, we use a single PHP instance for all requests.
447+ */
448+ php :
449+ options . maxPhpInstances === 1
450+ ? await createPhp ( undefined , true )
451+ : undefined ,
452+
453+ /**
454+ * If maxPhpInstances is not 1, we use a PHPProcessManager that dynamically
455+ * spawns and reaps PHP instances as needed.
456+ */
457+ phpFactory :
458+ options . maxPhpInstances !== 1
459+ ? async ( { isPrimary } ) => createPhp ( requestHandler , isPrimary )
460+ : ( undefined as any ) ,
461+ maxPhpInstances : options . maxPhpInstances ,
445462 } ) ;
446463
447464 return requestHandler ;
0 commit comments