From 266a41e0464c8ccf23949e3794189dc4ccf3caba Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 9 Apr 2019 10:55:33 -0700 Subject: [PATCH] Refactor sortPackageMap to depend on separate sortPackage function. --- src/Composer/Autoload/AutoloadGenerator.php | 54 +++++++++++++++------ src/Composer/Plugin/PluginManager.php | 7 +-- src/Composer/Util/PackageSorter.php | 10 ++++ 3 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/Composer/Util/PackageSorter.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 272c6b920..6fc602429 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -17,6 +17,7 @@ use Composer\EventDispatcher\EventDispatcher; use Composer\Installer\InstallationManager; use Composer\IO\IOInterface; use Composer\Package\AliasPackage; +use Composer\Package\Link; use Composer\Package\PackageInterface; use Composer\Repository\InstalledRepositoryInterface; use Composer\Util\Filesystem; @@ -956,27 +957,18 @@ INITIALIZER; * * Packages of equal weight retain the original order * - * @param array $packageMap + * @param array $packages * @return array */ - public function sortPackageMap(array $packageMap) - { - $packages = array(); - $paths = array(); + public function sortPackages(array $packages) { $usageList = array(); - foreach ($packageMap as $item) { - list($package, $path) = $item; - $name = $package->getName(); - $packages[$name] = $package; - $paths[$name] = $path; - - foreach (array_merge($package->getRequires(), $package->getDevRequires()) as $link) { + foreach ($packages as $package) { /** @var PackageInterface $package */ + foreach (array_merge($package->getRequires(), $package->getDevRequires()) as $link) { /** @var Link $link */ $target = $link->getTarget(); - $usageList[$target][] = $name; + $usageList[$target][] = $package->getName(); } } - $computing = array(); $computed = array(); $computeImportance = function ($name) use (&$computeImportance, &$computing, &$computed, $usageList) { @@ -1034,9 +1026,41 @@ INITIALIZER; $stable_sort($weightList); - $sortedPackageMap = array(); + $sortedPackages = array(); foreach (array_keys($weightList) as $name) { + $sortedPackages[] = $packages[$name]; + } + return $sortedPackages; + } + + /** + * Sorts packages by dependency weight + * + * Packages of equal weight retain the original order + * + * @param array $packageMap + * @return array + */ + public function sortPackageMap(array $packageMap) + { + $packages = array(); + $paths = array(); + + foreach ($packageMap as $item) { + list($package, $path) = $item; + $name = $package->getName(); + $packages[$name] = $package; + $paths[$name] = $path; + } + + $sortedPackages = $this->sortPackages($packages); + + + $sortedPackageMap = array(); + + foreach ($sortedPackages as $package) { + $name = $package->getName(); $sortedPackageMap[] = array($packages[$name], $paths[$name]); } diff --git a/src/Composer/Plugin/PluginManager.php b/src/Composer/Plugin/PluginManager.php index cca4efe54..3810992d0 100644 --- a/src/Composer/Plugin/PluginManager.php +++ b/src/Composer/Plugin/PluginManager.php @@ -255,11 +255,8 @@ class PluginManager { $packages = $repo->getPackages(); $generator = $this->composer->getAutoloadGenerator(); - $rootPackage = $this->composer->getPackage(); /** @var PackageInterface $rootPackage */ - $packageMap = $generator->buildPackageMap($this->composer->getInstallationManager(), $rootPackage, $packages); - $sortedPackageMap = array_reverse($generator->sortPackageMap($packageMap)); - foreach ($sortedPackageMap as $fullPackage) { - $package = $fullPackage[0]; /** @var PackageInterface $package */ + $sortedPackages = array_reverse($generator->sortPackages($packages)); + foreach ($sortedPackages as $package) { if (!($package instanceof CompletePackage)) { continue; } diff --git a/src/Composer/Util/PackageSorter.php b/src/Composer/Util/PackageSorter.php new file mode 100644 index 000000000..2899fc959 --- /dev/null +++ b/src/Composer/Util/PackageSorter.php @@ -0,0 +1,10 @@ +