|
41 | 41 | use function array_filter; |
42 | 42 | use function array_key_exists; |
43 | 43 | use function array_map; |
44 | | -use function array_merge; |
45 | 44 | use function array_values; |
46 | 45 | use function class_exists; |
47 | 46 | use function count; |
|
57 | 56 | use function is_file; |
58 | 57 | use function is_readable; |
59 | 58 | use function is_string; |
60 | | -use function PHPStan\collectNewAutoloadFunctions; |
61 | 59 | use function register_shutdown_function; |
62 | 60 | use function spl_autoload_functions; |
63 | 61 | use function sprintf; |
@@ -96,6 +94,7 @@ public static function begin( |
96 | 94 | ?string $singleReflectionFile, |
97 | 95 | ?string $singleReflectionInsteadOfFile, |
98 | 96 | bool $cleanupContainerCache, |
| 97 | + bool $deferBootstrapFiles = false, |
99 | 98 | ): InceptionResult |
100 | 99 | { |
101 | 100 | $stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output))); |
@@ -520,23 +519,25 @@ public static function begin( |
520 | 519 | $defaultLevelUsed = false; |
521 | 520 | } |
522 | 521 |
|
523 | | - foreach ($container->getParameter('bootstrapFiles') as $bootstrapFileFromArray) { |
524 | | - self::executeBootstrapFile($bootstrapFileFromArray, $container, $errorOutput, $debugEnabled); |
525 | | - } |
526 | | - |
527 | | - /** @var list<callable(string): void>|false $autoloadFunctionsAfter */ |
528 | | - $autoloadFunctionsAfter = spl_autoload_functions(); |
529 | | - |
530 | | - if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) { |
531 | | - $collectedAutoloadFunctions = collectNewAutoloadFunctions($autoloadFunctionsBefore, $autoloadFunctionsAfter); |
532 | | - $GLOBALS['__phpstanAutoloadFunctions'] = array_merge( |
533 | | - $GLOBALS['__phpstanAutoloadFunctions'] ?? [], |
534 | | - $collectedAutoloadFunctions['appended'], |
535 | | - ); |
536 | | - $GLOBALS['__phpstanAutoloadFunctionsPrependedToComposer'] = array_merge( |
537 | | - $GLOBALS['__phpstanAutoloadFunctionsPrependedToComposer'] ?? [], |
538 | | - $collectedAutoloadFunctions['prepended'], |
539 | | - ); |
| 522 | + // merges autoloaders registered since the top of begin() - the |
| 523 | + // --autoload-file require, anything container creation pulled in. This |
| 524 | + // cannot wait for the bootstrapFiles run: when that run is deferred, |
| 525 | + // the parent analyses long before it happens, and the source locators |
| 526 | + // must see these autoloaders throughout. It also cannot fold into |
| 527 | + // BootstrapFilesRunner::run() - by the time run() snapshots its own |
| 528 | + // before-list these are already registered, so its diff would exclude |
| 529 | + // them. run() brackets only the bootstrap files themselves, in |
| 530 | + // whichever process executes them. |
| 531 | + BootstrapFilesRunner::mergeNewAutoloadFunctions($autoloadFunctionsBefore); |
| 532 | + |
| 533 | + // the analyse flow defers bootstrapFiles to the exact places that need |
| 534 | + // them: the main thread right before an in-process analysis, every |
| 535 | + // worker (spawned or forked - WorkerRunner), and the main thread after |
| 536 | + // the workers of a parallel analysis (AnalyserRunner) - resources the |
| 537 | + // files open (database connections!) must not be inherited by forked |
| 538 | + // children. Every other command runs them here. |
| 539 | + if (!$deferBootstrapFiles) { |
| 540 | + $container->getByType(BootstrapFilesRunner::class)->run($errorOutput, $debugEnabled); |
540 | 541 | } |
541 | 542 |
|
542 | 543 | if (PHP_VERSION_ID >= 80000) { |
@@ -627,7 +628,7 @@ public static function begin( |
627 | 628 | /** |
628 | 629 | * @throws InceptionNotSuccessfulException |
629 | 630 | */ |
630 | | - private static function executeBootstrapFile( |
| 631 | + public static function executeBootstrapFile( |
631 | 632 | string $file, |
632 | 633 | Container $container, |
633 | 634 | Output $errorOutput, |
|
0 commit comments