|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Turbo; |
| 4 | + |
| 5 | +use function extension_loaded; |
| 6 | +use function function_exists; |
| 7 | +use function get_cfg_var; |
| 8 | +use function getenv; |
| 9 | +use function ini_get; |
| 10 | +use function is_string; |
| 11 | +use function pcntl_exec; |
| 12 | +use function php_ini_loaded_file; |
| 13 | +use const PHP_BINARY; |
| 14 | + |
| 15 | +/** |
| 16 | + * Restarts the main PHPStan process via pcntl_exec() with the distributed |
| 17 | + * turbo extension loaded through -d extension=. |
| 18 | + * |
| 19 | + * Parallel analysis prefers pcntl_fork()ed workers over spawned ones (see |
| 20 | + * ForkParallelChecker) — a forked worker inherits the booted process instead |
| 21 | + * of paying a full re-boot. But a forked worker also inherits the parent's |
| 22 | + * loaded extensions: the spawn-time `-d extension=` injection |
| 23 | + * (ProcessHelper) cannot reach it, and dl() cannot load a binary from next |
| 24 | + * to the phar. So when the extension binary is available but not loaded, |
| 25 | + * the whole process is re-executed with it before anything else runs — |
| 26 | + * forked workers then inherit the extension, its stub shadowing, and (when |
| 27 | + * running from a phar) the phar-fork-guard that keeps phar:// reads safe |
| 28 | + * across fork. |
| 29 | + * |
| 30 | + * The restart carries a marker ini entry with the extension path. It doubles |
| 31 | + * as the retry stop (a binary that failed to load leaves the extension |
| 32 | + * unloaded — without the marker the restart would loop) and tells |
| 33 | + * TurboExtensionSelector that spawned workers still need the -d flag |
| 34 | + * (command-line -d flags, unlike php.ini, are not inherited). |
| 35 | + */ |
| 36 | +final class TurboProcessRestarter |
| 37 | +{ |
| 38 | + |
| 39 | + public const EXTENSION_PATH_INI = 'phpstan.turboExtensionPath'; |
| 40 | + |
| 41 | + /** |
| 42 | + * The extension path this process was restarted with, null when the |
| 43 | + * process was not restarted. |
| 44 | + */ |
| 45 | + public static function getRestartExtensionPath(): ?string |
| 46 | + { |
| 47 | + $path = get_cfg_var(self::EXTENSION_PATH_INI); |
| 48 | + if (!is_string($path) || $path === '') { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + return $path; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * On success the call never returns — the process image is replaced. |
| 57 | + * |
| 58 | + * @param list<string> $argv |
| 59 | + */ |
| 60 | + public static function restartIfSuitable(array $argv): void |
| 61 | + { |
| 62 | + if (extension_loaded('phpstan_turbo')) { |
| 63 | + return; |
| 64 | + } |
| 65 | + if (getenv('PHPSTAN_TURBO') === '0') { |
| 66 | + return; |
| 67 | + } |
| 68 | + if (self::getRestartExtensionPath() !== null) { |
| 69 | + // already restarted and the extension still did not load |
| 70 | + return; |
| 71 | + } |
| 72 | + if ( |
| 73 | + !function_exists('pcntl_exec') |
| 74 | + || !function_exists('pcntl_fork') |
| 75 | + || !function_exists('pcntl_waitpid') |
| 76 | + || !function_exists('pcntl_wifexited') |
| 77 | + || !function_exists('pcntl_wexitstatus') |
| 78 | + || !function_exists('posix_kill') |
| 79 | + ) { |
| 80 | + // fork mode is impossible here (see ForkParallelChecker) and |
| 81 | + // spawned workers get the extension from ProcessHelper already |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $extensionPath = TurboExtensionSelector::findExtension(); |
| 86 | + if ($extensionPath === null) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + $args = []; |
| 91 | + $phpIni = php_ini_loaded_file(); |
| 92 | + if ($phpIni !== false) { |
| 93 | + $args[] = '-c'; |
| 94 | + $args[] = $phpIni; |
| 95 | + } |
| 96 | + $args[] = '-d'; |
| 97 | + $args[] = 'memory_limit=' . ini_get('memory_limit'); |
| 98 | + $args[] = '-d'; |
| 99 | + $args[] = 'extension=' . $extensionPath; |
| 100 | + $args[] = '-d'; |
| 101 | + $args[] = self::EXTENSION_PATH_INI . '=' . $extensionPath; |
| 102 | + foreach ($argv as $arg) { |
| 103 | + $args[] = $arg; |
| 104 | + } |
| 105 | + |
| 106 | + pcntl_exec(PHP_BINARY, $args); |
| 107 | + // pcntl_exec() returns only on failure — continue without the extension |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments