Skip to content

Commit

Permalink
Rename mainPackage to rootPackage in AutoloadGenerator and ensure we …
Browse files Browse the repository at this point in the history
…use RootPackageInterface
  • Loading branch information
Seldaek committed Nov 12, 2020
1 parent 07352ea commit b574f10
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 86 deletions.
64 changes: 34 additions & 30 deletions src/Composer/Autoload/AutoloadGenerator.php
Expand Up @@ -18,6 +18,7 @@
use Composer\IO\IOInterface;
use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Repository\PlatformRepository;
use Composer\Semver\Constraint\Bound;
Expand Down Expand Up @@ -136,7 +137,7 @@ public function setIgnorePlatformRequirements($ignorePlatformReqs)
}
}

public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsrPackages = false, $suffix = '')
public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, $targetDir, $scanPsrPackages = false, $suffix = '')
{
if ($this->classMapAuthoritative) {
// Force scanPsrPackages when classmap is authoritative
Expand Down Expand Up @@ -193,8 +194,8 @@ public function dump(Config $config, InstalledRepositoryInterface $localRepo, Pa

// Collect information from all packages.
$devPackageNames = $localRepo->getDevPackageNames();
$packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getCanonicalPackages());
$autoloads = $this->parseAutoloads($packageMap, $mainPackage, $this->devMode === false);
$packageMap = $this->buildPackageMap($installationManager, $rootPackage, $localRepo->getCanonicalPackages());
$autoloads = $this->parseAutoloads($packageMap, $rootPackage, $this->devMode === false);

// Process the 'psr-0' base directories.
foreach ($autoloads['psr-0'] as $namespace => $paths) {
Expand Down Expand Up @@ -234,9 +235,9 @@ public function dump(Config $config, InstalledRepositoryInterface $localRepo, Pa

// add custom psr-0 autoloading if the root package has a target dir
$targetDirLoader = null;
$mainAutoload = $mainPackage->getAutoload();
if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
$levels = substr_count($filesystem->normalizePath($mainPackage->getTargetDir()), '/') + 1;
$mainAutoload = $rootPackage->getAutoload();
if ($rootPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
$levels = substr_count($filesystem->normalizePath($rootPackage->getTargetDir()), '/') + 1;
$prefixes = implode(', ', array_map(function ($prefix) {
return var_export($prefix, true);
}, array_keys($mainAutoload['psr-0'])));
Expand Down Expand Up @@ -395,10 +396,13 @@ private function generateClassMap($dir, $excluded, $namespaceFilter, $autoloadTy
return ClassMapGenerator::createMap($dir, $excluded, $showAmbiguousWarning ? $this->io : null, $namespaceFilter, $autoloadType, $scannedFiles);
}

public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
/**
* @param RootPackageInterface $rootPackage
*/
public function buildPackageMap(InstallationManager $installationManager, PackageInterface $rootPackage, array $packages)
{
// build package => install path map
$packageMap = array(array($mainPackage, ''));
$packageMap = array(array($rootPackage, ''));

foreach ($packages as $package) {
if ($package instanceof AliasPackage) {
Expand Down Expand Up @@ -440,26 +444,26 @@ protected function validatePackage(PackageInterface $package)
/**
* Compiles an ordered list of namespace => path mappings
*
* @param array $packageMap array of array(package, installDir-relative-to-composer.json)
* @param PackageInterface $mainPackage root package instance
* @param bool $filterOutRequireDevPackages whether to filter out require-dev packages
* @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
* @param array $packageMap array of array(package, installDir-relative-to-composer.json)
* @param RootPackageInterface $rootPackage root package instance
* @param bool $filterOutRequireDevPackages whether to filter out require-dev packages
* @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
*/
public function parseAutoloads(array $packageMap, PackageInterface $mainPackage, $filterOutRequireDevPackages = false)
public function parseAutoloads(array $packageMap, PackageInterface $rootPackage, $filterOutRequireDevPackages = false)
{
$mainPackageMap = array_shift($packageMap);
$rootPackageMap = array_shift($packageMap);
if ($filterOutRequireDevPackages) {
$packageMap = $this->filterPackageMap($packageMap, $mainPackage);
$packageMap = $this->filterPackageMap($packageMap, $rootPackage);
}
$sortedPackageMap = $this->sortPackageMap($packageMap);
$sortedPackageMap[] = $mainPackageMap;
array_unshift($packageMap, $mainPackageMap);
$sortedPackageMap[] = $rootPackageMap;
array_unshift($packageMap, $rootPackageMap);

$psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $mainPackage);
$psr4 = $this->parseAutoloadsType($packageMap, 'psr-4', $mainPackage);
$classmap = $this->parseAutoloadsType(array_reverse($sortedPackageMap), 'classmap', $mainPackage);
$files = $this->parseAutoloadsType($sortedPackageMap, 'files', $mainPackage);
$exclude = $this->parseAutoloadsType($sortedPackageMap, 'exclude-from-classmap', $mainPackage);
$psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $rootPackage);
$psr4 = $this->parseAutoloadsType($packageMap, 'psr-4', $rootPackage);
$classmap = $this->parseAutoloadsType(array_reverse($sortedPackageMap), 'classmap', $rootPackage);
$files = $this->parseAutoloadsType($sortedPackageMap, 'files', $rootPackage);
$exclude = $this->parseAutoloadsType($sortedPackageMap, 'exclude-from-classmap', $rootPackage);

krsort($psr0);
krsort($psr4);
Expand Down Expand Up @@ -1051,31 +1055,31 @@ public static function getInitializer(ClassLoader \$loader)
INITIALIZER;
}

protected function parseAutoloadsType(array $packageMap, $type, PackageInterface $mainPackage)
protected function parseAutoloadsType(array $packageMap, $type, RootPackageInterface $rootPackage)
{
$autoloads = array();

foreach ($packageMap as $item) {
list($package, $installPath) = $item;

$autoload = $package->getAutoload();
if ($this->devMode && $package === $mainPackage) {
if ($this->devMode && $package === $rootPackage) {
$autoload = array_merge_recursive($autoload, $package->getDevAutoload());
}

// skip misconfigured packages
if (!isset($autoload[$type]) || !is_array($autoload[$type])) {
continue;
}
if (null !== $package->getTargetDir() && $package !== $mainPackage) {
if (null !== $package->getTargetDir() && $package !== $rootPackage) {
$installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
}

foreach ($autoload[$type] as $namespace => $paths) {
foreach ((array) $paths as $path) {
if (($type === 'files' || $type === 'classmap' || $type === 'exclude-from-classmap') && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) {
// remove target-dir from file paths of the root package
if ($package === $mainPackage) {
if ($package === $rootPackage) {
$targetDir = str_replace('\\<dirsep\\>', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '<dirsep>', $package->getTargetDir())));
$path = ltrim(preg_replace('{^'.$targetDir.'}', '', ltrim($path, '\\/')), '\\/');
} else {
Expand Down Expand Up @@ -1141,11 +1145,11 @@ protected function getFileIdentifier(PackageInterface $package, $path)
/**
* Filters out dev-dependencies
*
* @param array $packageMap
* @param PackageInterface $mainPackage
* @param array $packageMap
* @param RootPackageInterface $rootPackage
* @return array
*/
protected function filterPackageMap(array $packageMap, PackageInterface $mainPackage)
protected function filterPackageMap(array $packageMap, RootPackageInterface $rootPackage)
{
$packages = array();
$include = array();
Expand Down Expand Up @@ -1174,7 +1178,7 @@ protected function filterPackageMap(array $packageMap, PackageInterface $mainPac
}
}
};
$add($mainPackage);
$add($rootPackage);

return array_filter(
$packageMap,
Expand Down
3 changes: 2 additions & 1 deletion src/Composer/Plugin/PluginManager.php
Expand Up @@ -17,6 +17,7 @@
use Composer\IO\IOInterface;
use Composer\Package\CompletePackage;
use Composer\Package\Package;
use Composer\Package\RootPackage;
use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\InstalledRepository;
Expand Down Expand Up @@ -188,7 +189,7 @@ public function registerPackage(PackageInterface $package, $failOnMissingClasses
$autoloads[] = array($autoloadPackage, $downloadPath);
}

$map = $generator->parseAutoloads($autoloads, new Package('dummy', '1.0.0.0', '1.0.0'));
$map = $generator->parseAutoloads($autoloads, new RootPackage('dummy/root-package', '1.0.0.0', '1.0.0'));
$classLoader = $generator->createLoader($map);
$classLoader->register();

Expand Down

0 comments on commit b574f10

Please sign in to comment.