1
0
Fork 0

Move sortPackages to static helper class.

pull/8085/head
Dane Powell 2019-04-09 10:58:36 -07:00
parent 266a41e046
commit d4150cafc4
No known key found for this signature in database
GPG Key ID: 5FCA7D7DAF2DDD5B
3 changed files with 86 additions and 86 deletions

View File

@ -17,11 +17,11 @@ 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;
use Composer\Script\ScriptEvents;
use Composer\Util\PackageSorter;
/**
* @author Igor Wiedler <igor@wiedler.ch>
@ -952,88 +952,6 @@ INITIALIZER;
);
}
/**
* Sorts packages by dependency weight
*
* Packages of equal weight retain the original order
*
* @param array $packages
* @return array
*/
public function sortPackages(array $packages) {
$usageList = array();
foreach ($packages as $package) { /** @var PackageInterface $package */
foreach (array_merge($package->getRequires(), $package->getDevRequires()) as $link) { /** @var Link $link */
$target = $link->getTarget();
$usageList[$target][] = $package->getName();
}
}
$computing = array();
$computed = array();
$computeImportance = function ($name) use (&$computeImportance, &$computing, &$computed, $usageList) {
// reusing computed importance
if (isset($computed[$name])) {
return $computed[$name];
}
// canceling circular dependency
if (isset($computing[$name])) {
return 0;
}
$computing[$name] = true;
$weight = 0;
if (isset($usageList[$name])) {
foreach ($usageList[$name] as $user) {
$weight -= 1 - $computeImportance($user);
}
}
unset($computing[$name]);
$computed[$name] = $weight;
return $weight;
};
$weightList = array();
foreach ($packages as $name => $package) {
$weight = $computeImportance($name);
$weightList[$name] = $weight;
}
$stable_sort = function (&$array) {
static $transform, $restore;
$i = 0;
if (!$transform) {
$transform = function (&$v, $k) use (&$i) {
$v = array($v, ++$i, $k, $v);
};
$restore = function (&$v, $k) {
$v = $v[3];
};
}
array_walk($array, $transform);
asort($array);
array_walk($array, $restore);
};
$stable_sort($weightList);
$sortedPackages = array();
foreach (array_keys($weightList) as $name) {
$sortedPackages[] = $packages[$name];
}
return $sortedPackages;
}
/**
* Sorts packages by dependency weight
*
@ -1054,7 +972,7 @@ INITIALIZER;
$paths[$name] = $path;
}
$sortedPackages = $this->sortPackages($packages);
$sortedPackages = PackageSorter::sortPackages($packages);
$sortedPackageMap = array();

View File

@ -24,6 +24,7 @@ use Composer\Package\Link;
use Composer\Semver\Constraint\Constraint;
use Composer\DependencyResolver\Pool;
use Composer\Plugin\Capability\Capability;
use Composer\Util\PackageSorter;
/**
* Plugin manager
@ -254,8 +255,7 @@ class PluginManager
private function loadRepository(RepositoryInterface $repo)
{
$packages = $repo->getPackages();
$generator = $this->composer->getAutoloadGenerator();
$sortedPackages = array_reverse($generator->sortPackages($packages));
$sortedPackages = array_reverse(PackageSorter::sortPackages($packages));
foreach ($sortedPackages as $package) {
if (!($package instanceof CompletePackage)) {
continue;

View File

@ -3,8 +3,90 @@
namespace Composer\Util;
use Composer\Package\Link;
use Composer\Package\PackageInterface;
class PackageSorter
{
/**
* Sorts packages by dependency weight
*
* Packages of equal weight retain the original order
*
* @param array $packages
* @return array
*/
public static function sortPackages(array $packages) {
$usageList = array();
foreach ($packages as $package) { /** @var PackageInterface $package */
foreach (array_merge($package->getRequires(), $package->getDevRequires()) as $link) { /** @var Link $link */
$target = $link->getTarget();
$usageList[$target][] = $package->getName();
}
}
$computing = array();
$computed = array();
$computeImportance = function ($name) use (&$computeImportance, &$computing, &$computed, $usageList) {
// reusing computed importance
if (isset($computed[$name])) {
return $computed[$name];
}
// canceling circular dependency
if (isset($computing[$name])) {
return 0;
}
$computing[$name] = true;
$weight = 0;
if (isset($usageList[$name])) {
foreach ($usageList[$name] as $user) {
$weight -= 1 - $computeImportance($user);
}
}
unset($computing[$name]);
$computed[$name] = $weight;
return $weight;
};
$weightList = array();
foreach ($packages as $name => $package) {
$weight = $computeImportance($name);
$weightList[$name] = $weight;
}
$stable_sort = function (&$array) {
static $transform, $restore;
$i = 0;
if (!$transform) {
$transform = function (&$v, $k) use (&$i) {
$v = array($v, ++$i, $k, $v);
};
$restore = function (&$v) {
$v = $v[3];
};
}
array_walk($array, $transform);
asort($array);
array_walk($array, $restore);
};
$stable_sort($weightList);
$sortedPackages = array();
foreach (array_keys($weightList) as $name) {
$sortedPackages[] = $packages[$name];
}
return $sortedPackages;
}
}