Move sortPackages to static helper class.
parent
266a41e046
commit
d4150cafc4
|
@ -17,11 +17,11 @@ use Composer\EventDispatcher\EventDispatcher;
|
||||||
use Composer\Installer\InstallationManager;
|
use Composer\Installer\InstallationManager;
|
||||||
use Composer\IO\IOInterface;
|
use Composer\IO\IOInterface;
|
||||||
use Composer\Package\AliasPackage;
|
use Composer\Package\AliasPackage;
|
||||||
use Composer\Package\Link;
|
|
||||||
use Composer\Package\PackageInterface;
|
use Composer\Package\PackageInterface;
|
||||||
use Composer\Repository\InstalledRepositoryInterface;
|
use Composer\Repository\InstalledRepositoryInterface;
|
||||||
use Composer\Util\Filesystem;
|
use Composer\Util\Filesystem;
|
||||||
use Composer\Script\ScriptEvents;
|
use Composer\Script\ScriptEvents;
|
||||||
|
use Composer\Util\PackageSorter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @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
|
* Sorts packages by dependency weight
|
||||||
*
|
*
|
||||||
|
@ -1054,7 +972,7 @@ INITIALIZER;
|
||||||
$paths[$name] = $path;
|
$paths[$name] = $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sortedPackages = $this->sortPackages($packages);
|
$sortedPackages = PackageSorter::sortPackages($packages);
|
||||||
|
|
||||||
|
|
||||||
$sortedPackageMap = array();
|
$sortedPackageMap = array();
|
||||||
|
|
|
@ -24,6 +24,7 @@ use Composer\Package\Link;
|
||||||
use Composer\Semver\Constraint\Constraint;
|
use Composer\Semver\Constraint\Constraint;
|
||||||
use Composer\DependencyResolver\Pool;
|
use Composer\DependencyResolver\Pool;
|
||||||
use Composer\Plugin\Capability\Capability;
|
use Composer\Plugin\Capability\Capability;
|
||||||
|
use Composer\Util\PackageSorter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin manager
|
* Plugin manager
|
||||||
|
@ -254,8 +255,7 @@ class PluginManager
|
||||||
private function loadRepository(RepositoryInterface $repo)
|
private function loadRepository(RepositoryInterface $repo)
|
||||||
{
|
{
|
||||||
$packages = $repo->getPackages();
|
$packages = $repo->getPackages();
|
||||||
$generator = $this->composer->getAutoloadGenerator();
|
$sortedPackages = array_reverse(PackageSorter::sortPackages($packages));
|
||||||
$sortedPackages = array_reverse($generator->sortPackages($packages));
|
|
||||||
foreach ($sortedPackages as $package) {
|
foreach ($sortedPackages as $package) {
|
||||||
if (!($package instanceof CompletePackage)) {
|
if (!($package instanceof CompletePackage)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -3,8 +3,90 @@
|
||||||
|
|
||||||
namespace Composer\Util;
|
namespace Composer\Util;
|
||||||
|
|
||||||
|
use Composer\Package\Link;
|
||||||
|
use Composer\Package\PackageInterface;
|
||||||
|
|
||||||
class PackageSorter
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue