From 46776c8e23b4ffbdbd4727cbbfeee54c95eee4e7 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Wed, 29 Jan 2014 14:15:32 +0100 Subject: [PATCH 1/2] Improved package sorting --- src/Composer/Autoload/AutoloadGenerator.php | 96 ++++++++++++++------- 1 file changed, 67 insertions(+), 29 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 2359265bd..5bc7cd383 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -611,45 +611,83 @@ FOOTER; protected function sortPackageMap(array $packageMap) { - $positions = array(); - $names = array(); - $indexes = array(); - - foreach ($packageMap as $position => $item) { - $mainName = $item[0]->getName(); - $names = array_merge(array_fill_keys($item[0]->getNames(), $mainName), $names); - $names[$mainName] = $mainName; - $indexes[$mainName] = $positions[$mainName] = $position; - } + $packages = array(); + $paths = array(); + $usageList = array(); foreach ($packageMap as $item) { - $position = $positions[$item[0]->getName()]; - foreach (array_merge($item[0]->getRequires(), $item[0]->getDevRequires()) as $link) { + list($package, $path) = $item; + $name = $package->getName(); + $packages[$name] = $package; + $paths[$name] = $path; + + foreach (array_merge($package->getRequires(), $package->getDevRequires()) as $link) { $target = $link->getTarget(); - if (!isset($names[$target])) { - continue; - } + $usageList[$target][] = $name; + } + } - $target = $names[$target]; - if ($positions[$target] <= $position) { - continue; - } + $computing = array(); + $computed = array(); + $compute_importance = function($name) use(&$compute_importance, &$computing, &$computed, $usageList) { + # reusing computed importance + if (isset($computed[$name])) { + return $computed[$name]; + } - foreach ($positions as $key => $value) { - if ($value >= $position) { - break; - } - $positions[$key]--; - } + # canceling circular dependency + if (isset($computing[$name])) { + return 0; + } + + $computing[$name] = true; + $weight = 0; - $positions[$target] = $position - 1; + if (isset($usageList[$name])) { + foreach ($usageList[$name] as $user) { + $weight -= 1 - $compute_importance($user); + } } - asort($positions); + + unset($computing[$name]); + $computed[$name] = $weight; + + return $weight; + }; + + $weightList = array(); + + foreach ($packages as $name => $package) { + $weight = $compute_importance($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); + $sortedPackageMap = array(); - foreach (array_keys($positions) as $packageName) { - $sortedPackageMap[] = $packageMap[$indexes[$packageName]]; + + foreach (array_keys($weightList) as $name) { + $sortedPackageMap[] = array($packages[$name], $paths[$name]); } return $sortedPackageMap; From 17278999bac14290b088fd869ca98ec0d076e158 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Wed, 29 Jan 2014 14:36:34 +0100 Subject: [PATCH 2/2] Coding style compliance --- src/Composer/Autoload/AutoloadGenerator.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) mode change 100644 => 100755 src/Composer/Autoload/AutoloadGenerator.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php old mode 100644 new mode 100755 index 5bc7cd383..e63fe77ce --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -629,13 +629,13 @@ FOOTER; $computing = array(); $computed = array(); - $compute_importance = function($name) use(&$compute_importance, &$computing, &$computed, $usageList) { - # reusing computed importance + $computeImportance = function($name) use(&$computeImportance, &$computing, &$computed, $usageList) { + // reusing computed importance if (isset($computed[$name])) { return $computed[$name]; } - # canceling circular dependency + // canceling circular dependency if (isset($computing[$name])) { return 0; } @@ -645,7 +645,7 @@ FOOTER; if (isset($usageList[$name])) { foreach ($usageList[$name] as $user) { - $weight -= 1 - $compute_importance($user); + $weight -= 1 - $computeImportance($user); } } @@ -658,7 +658,7 @@ FOOTER; $weightList = array(); foreach ($packages as $name => $package) { - $weight = $compute_importance($name); + $weight = $computeImportance($name); $weightList[$name] = $weight; }