@@ -18,6 +18,7 @@ import {
1818 NativePhpSupportedVersion ,
1919 validateNativePhpVersion ,
2020} from '@studio/common/lib/php-binary-metadata' ;
21+ import { getConfigDirectory } from '@studio/common/lib/well-known-paths' ;
2122import { z } from 'zod' ;
2223import {
2324 managerMessageSchema ,
@@ -61,13 +62,35 @@ type SpawnPhpProcessOptions = {
6162 cwd ?: string ;
6263 signal ?: AbortSignal ;
6364 mode ?: 'pipe' | 'capture-stdout' ;
65+ opcacheSiteId ?: string ;
6466} ;
6567
68+ function getDefaultPhpArgs ( siteId ?: string ) : string [ ] {
69+ if ( process . platform !== 'win32' || ! siteId ) {
70+ return [ ] ;
71+ }
72+
73+ const cacheId = siteId . replace ( / [ ^ A - Z a - z 0 - 9 _ . - ] / g, '-' ) ;
74+ const cacheDirectory = path . join ( getConfigDirectory ( ) , 'php-bin' , 'opcache' , cacheId ) ;
75+ fs . mkdirSync ( cacheDirectory , { recursive : true } ) ;
76+
77+ return [
78+ '-d' ,
79+ `opcache.file_cache=${ cacheDirectory } ` ,
80+ '-d' ,
81+ 'opcache.file_cache_fallback=1' ,
82+ '-d' ,
83+ `opcache.cache_id=studio-${ cacheId } ` ,
84+ ] ;
85+ }
86+
6687function spawnPhpProcess (
6788 args : string [ ] ,
68- { phpVersion, cwd, signal, mode = 'pipe' } : SpawnPhpProcessOptions
89+ { phpVersion, cwd, signal, mode = 'pipe' , opcacheSiteId } : SpawnPhpProcessOptions
6990) : ChildProcess {
70- const phpScriptProcess = spawn ( getPhpBinaryPath ( phpVersion ) , args , {
91+ const defaultArgs = getDefaultPhpArgs ( opcacheSiteId ) ;
92+ const phpArgs = [ ...defaultArgs , ...args ] ;
93+ const phpScriptProcess = spawn ( getPhpBinaryPath ( phpVersion ) , phpArgs , {
7194 cwd,
7295 stdio : [ 'ignore' , 'pipe' , 'pipe' ] ,
7396 signal,
@@ -332,6 +355,7 @@ async function startServer( config: ServerConfig, signal: AbortSignal ): Promise
332355 const serverChild = spawnPhpProcess ( [ '-S' , phpAddress , ROUTER_PATH ] , {
333356 phpVersion,
334357 cwd : config . sitePath ,
358+ opcacheSiteId : config . siteId ,
335359 } ) ;
336360 spawnedChild = serverChild ;
337361
0 commit comments