77use RecursiveDirectoryIterator ;
88use RecursiveIteratorIterator ;
99use Throwable ;
10+ use function class_exists ;
1011use function explode ;
1112use function extension_loaded ;
1213use function filemtime ;
1516use function filesize ;
1617use function function_exists ;
1718use function get_cfg_var ;
19+ use function getenv ;
1820use function implode ;
1921use function in_array ;
2022use function ini_get ;
@@ -106,8 +108,16 @@ final class TurboProcessRestarter
106108 'opcache.max_accelerated_files ' ,
107109 ];
108110
109- /** The resolved file cache directory, memoized per process; false while unresolved */
110- private static string |false |null $ fileCacheDirectory = false ;
111+ /**
112+ * Environment variables that CI services set (to a non-empty value other
113+ * than "false"): a CI job usually starts with an empty temp dir, so a file
114+ * cache there costs its writes on every run and is never read
115+ */
116+ private const CI_ENVIRONMENT_VARIABLES = ['CI ' , 'GITHUB_ACTIONS ' , 'GITLAB_CI ' , 'BUILDKITE ' , 'TF_BUILD ' , 'JENKINS_URL ' , 'TEAMCITY_VERSION ' ];
117+
118+ private static ?string $ fileCacheDirectory = null ;
119+
120+ private static bool $ fileCacheDirectoryResolved = false ;
111121
112122 /**
113123 * The extension path this process was given through -d — by the restart,
@@ -289,20 +299,26 @@ public static function getOpcacheArgs(): array
289299 * as opcodes, unchecked, inside PHPStan.
290300 *
291301 * Only runs from the phar get one: a source checkout changes all the time
292- * and has no build to key the directory by. Not on Windows either: every
302+ * and has no build to key the directory by. Not in CI (see
303+ * resolveContinuousIntegration()): an empty temp dir at the start of every
304+ * job would make the cache pure cost, and with a file cache the
305+ * extension's trusted-types pass is off. Not on Windows either: every
293306 * spawned worker there gets its own opcache.cache_id (see ProcessHelper),
294307 * and OPcache then keeps a separate file cache per worker that no later
295308 * run reuses: 2 GB after one benchmark run on a GitHub runner, and cold
296309 * runs 27-50% slower.
297310 */
298311 private static function getFileCacheDirectory (): ?string
299312 {
300- if (self ::$ fileCacheDirectory !== false ) {
313+ if (self ::$ fileCacheDirectoryResolved ) {
301314 return self ::$ fileCacheDirectory ;
302315 }
303316
304- self ::$ fileCacheDirectory = null ;
305- if (PHP_OS_FAMILY === 'Windows ' || !function_exists ('posix_geteuid ' )) {
317+ self ::$ fileCacheDirectoryResolved = true ;
318+ if (PHP_OS_FAMILY === 'Windows ' || !function_exists ('posix_geteuid ' ) || !class_exists ('Phar ' , false )) {
319+ return null ;
320+ }
321+ if (self ::resolveContinuousIntegration (getenv ())) {
306322 return null ;
307323 }
308324
@@ -357,6 +373,21 @@ public static function resolveFileCacheKey(string $pharSignature, string $turboB
357373 return substr (sha1 (implode ("\0" , [$ pharSignature , $ turboBinary , $ debug ? 'debug ' : '' ])), 0 , 16 );
358374 }
359375
376+ /**
377+ * @param array<string, string> $environment getenv()
378+ */
379+ public static function resolveContinuousIntegration (array $ environment ): bool
380+ {
381+ foreach (self ::CI_ENVIRONMENT_VARIABLES as $ name ) {
382+ $ value = $ environment [$ name ] ?? '' ;
383+ if ($ value !== '' && strtolower ($ value ) !== 'false ' ) {
384+ return true ;
385+ }
386+ }
387+
388+ return false ;
389+ }
390+
360391 /**
361392 * The same answer before the restart and in the restarted process: the
362393 * binary the restart loads with -d, or the version of one loaded by the
@@ -378,7 +409,7 @@ private static function describeTurboBinary(): string
378409 return 'none ' ;
379410 }
380411
381- private static function isPrivateDirectory (string $ directory , int $ userId ): bool
412+ public static function isPrivateDirectory (string $ directory , int $ userId ): bool
382413 {
383414 if (is_link ($ directory ) || !is_dir ($ directory )) {
384415 return false ;
@@ -395,7 +426,7 @@ private static function isPrivateDirectory(string $directory, int $userId): bool
395426 * no longer installed. Runs only when a new key directory was created, so
396427 * about once per update.
397428 */
398- private static function pruneFileCacheDirectories (string $ baseDirectory , string $ currentKey , int $ now ): void
429+ public static function pruneFileCacheDirectories (string $ baseDirectory , string $ currentKey , int $ now ): void
399430 {
400431 $ entries = @scandir ($ baseDirectory );
401432 if ($ entries === false ) {
0 commit comments