Skip to content

Commit 0f9ece1

Browse files
Tweak OPcache config on Windows
1 parent 2da8c70 commit 0f9ece1

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

apps/cli/php-server-child.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2122
import { z } from 'zod';
2223
import {
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-Za-z0-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+
6687
function 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

Comments
 (0)