Skip to content

Commit dea8cff

Browse files
Preload code from first-party dependencies on startup for Composer installations
Co-authored-by: Sebastian Bergmann <sb@sebastian-bergmann.de> Co-authored-by: Arne Blankerts <arne@blankerts.de>
1 parent 30c0ac3 commit dea8cff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/TextUI/Application.php

+31
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use const PHP_VERSION;
1414
use function assert;
1515
use function class_exists;
16+
use function defined;
17+
use function dirname;
1618
use function explode;
1719
use function function_exists;
1820
use function is_file;
@@ -22,6 +24,7 @@
2224
use function realpath;
2325
use function sprintf;
2426
use function str_contains;
27+
use function str_starts_with;
2528
use function trim;
2629
use function unlink;
2730
use PHPUnit\Event\EventFacadeIsSealedException;
@@ -100,6 +103,8 @@
100103
*/
101104
public function run(array $argv): int
102105
{
106+
$this->preload();
107+
103108
try {
104109
EventFacade::emitter()->applicationStarted();
105110

@@ -841,4 +846,30 @@ private function configureDeprecationTriggers(Configuration $configuration): voi
841846

842847
ErrorHandler::instance()->useDeprecationTriggers($deprecationTriggers);
843848
}
849+
850+
private function preload(): void
851+
{
852+
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
853+
return;
854+
}
855+
856+
$classMapFile = dirname(PHPUNIT_COMPOSER_INSTALL) . '/composer/autoload_classmap.php';
857+
858+
if (!is_file($classMapFile)) {
859+
return;
860+
}
861+
862+
foreach (require $classMapFile as $codeUnitName => $sourceCodeFile) {
863+
if (!str_starts_with($codeUnitName, 'PHPUnit\\') &&
864+
!str_starts_with($codeUnitName, 'SebastianBergmann\\')) {
865+
continue;
866+
}
867+
868+
if (str_contains($sourceCodeFile, '/tests/')) {
869+
continue;
870+
}
871+
872+
require_once $sourceCodeFile;
873+
}
874+
}
844875
}

0 commit comments

Comments
 (0)