Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/autoloadFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ function isComposerClassLoader($autoloadFunction): bool // phpcs:ignore Squiz.Fu
&& get_class($autoloadFunction[0]) === ClassLoader::class;
}

/**
* Index of the given class loader in the queue, or null when it is not registered any more -
* a bootstrap file has then taken Composer's place and there is no boundary to split on.
*
* @param list<mixed> $autoloadFunctions
*/
function findClassLoaderIndex(array $autoloadFunctions, ?int $classLoaderId): ?int // phpcs:ignore Squiz.Functions.GlobalFunction.Found
{
if ($classLoaderId === null) {
return null;
}

foreach ($autoloadFunctions as $index => $autoloadFunction) {
if (!isComposerClassLoader($autoloadFunction)) {
continue;
}

if (spl_object_id($autoloadFunction[0]) === $classLoaderId) {
return $index;
}
}

return null;
}

/**
* Splits the autoload functions registered while loading Composer's autoloader
* and the bootstrap files into those registered before and after Composer's own
Expand Down Expand Up @@ -83,23 +108,17 @@ function collectNewAutoloadFunctions($autoloadFunctionsBefore, $autoloadFunction
// it below the static source locators.
$classLoaderIds = [];
foreach ($autoloadFunctionsBefore as $before) {
if (isComposerClassLoader($before)) {
$classLoaderIds[] = spl_object_id($before[0]);
if (!isComposerClassLoader($before)) {
continue;
}

$classLoaderIds[] = spl_object_id($before[0]);
}

array_shift($classLoaderIds);
$projectLoaderId = $classLoaderIds === [] ? null : $classLoaderIds[count($classLoaderIds) - 1];

$composerIndex = null;
if ($projectLoaderId !== null) {
foreach ($autoloadFunctionsAfter as $index => $after) {
if (isComposerClassLoader($after) && spl_object_id($after[0]) === $projectLoaderId) {
$composerIndex = $index;
break;
}
}
}
$composerIndex = findClassLoaderIndex($autoloadFunctionsAfter, $projectLoaderId);

foreach ($autoloadFunctionsAfter as $index => $after) {
if (is_array($after) && count($after) > 0) {
Expand Down
Loading