Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 25 additions & 7 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,37 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
}
}

// Check config for $composerPackages.
$only = $composerPackages['only'] ?? [];
$exclude = $composerPackages['exclude'] ?? [];

if ($only !== [] && $exclude !== []) {
throw new ConfigException('Cannot use "only" and "exclude" at the same time in "Config\Modules::$composerPackages".');
}

// When no package filtering is configured (the default), all PSR-4 prefixes
// from Composer can be added directly without expensive InstalledVersions scanning.
if ($only === [] && $exclude === []) {
$newPaths = [];

foreach ($namespacePaths as $namespace => $srcPaths) {
// Composer stores namespaces with trailing slash. We don't.
$newPaths[rtrim($namespace, '\\ ')] = $srcPaths;
}

$this->addNamespace($newPaths);

return;
}

if (! method_exists(InstalledVersions::class, 'getAllRawData')) { // @phpstan-ignore function.alreadyNarrowedType
throw new RuntimeException(
'Your Composer version is too old.'
. ' Please update Composer (run `composer self-update`) to v2.0.14 or later'
. ' and remove your vendor/ directory, and run `composer update`.',
);
}

// This method requires Composer 2.0.14 or later.
$allData = InstalledVersions::getAllRawData();
$packageList = [];
Expand All @@ -419,15 +443,9 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
$packageList = array_merge($packageList, $list['versions']);
}

// Check config for $composerPackages.
$only = $composerPackages['only'] ?? [];
$exclude = $composerPackages['exclude'] ?? [];
if ($only !== [] && $exclude !== []) {
throw new ConfigException('Cannot use "only" and "exclude" at the same time in "Config\Modules::$composerPackages".');
}

// Get install paths of packages to add namespace for auto-discovery.
$installPaths = [];

if ($only !== []) {
foreach ($packageList as $packageName => $data) {
if (in_array($packageName, $only, true) && isset($data['install_path'])) {
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,26 @@ public function testFindsComposerRoutesWithComposerPathNotFound(): void
$this->assertArrayNotHasKey('Laminas\\Escaper', $namespaces);
}

public function testComposerPackagesDefaultLoadsAllNamespacesDirectly(): void
{
$config = new Autoload();
$config->psr4 = [];
$modules = new Modules();
$modules->discoverInComposer = true;
$modules->composerPackages = [];

$loader = new Autoloader();
$loader->initialize($config, $modules);

$namespaces = $loader->getNamespace();

$this->assertArrayHasKey('Laminas\Escaper', $namespaces);
$this->assertArrayHasKey('Psr\Log', $namespaces);
$this->assertArrayNotHasKey('CodeIgniter', $namespaces);
$this->assertArrayNotHasKey(APP_NAMESPACE, $namespaces);
$this->assertArrayNotHasKey('Config', $namespaces);
}

public function testAutoloaderLoadsNonClassFiles(): void
{
$config = new Autoload();
Expand Down
Loading