1
0
Fork 0

Fixed topological sorting of packages in AutoloadGenerator

pull/1420/head
Martin Hasoň 2012-12-14 15:01:48 +01:00
parent 51eca2cdfc
commit 03e355f063
4 changed files with 31 additions and 34 deletions

View File

@ -179,17 +179,12 @@ EOF;
{ {
// build package => install path map // build package => install path map
$packageMap = array(); $packageMap = array();
array_unshift($packages, $mainPackage);
foreach ($packages as $package) { foreach ($packages as $package) {
if ($package instanceof AliasPackage) { if ($package instanceof AliasPackage) {
continue; continue;
} }
if ($package === $mainPackage) {
$packageMap[] = array($mainPackage, '');
continue;
}
$packageMap[] = array( $packageMap[] = array(
$package, $package,
$installationManager->getInstallPath($package) $installationManager->getInstallPath($package)
@ -209,6 +204,8 @@ EOF;
public function parseAutoloads(array $packageMap, PackageInterface $mainPackage) public function parseAutoloads(array $packageMap, PackageInterface $mainPackage)
{ {
$sortedPackageMap = $this->sortPackageMap($packageMap); $sortedPackageMap = $this->sortPackageMap($packageMap);
$sortedPackageMap[] = array($mainPackage, '');
array_unshift($packageMap, array($mainPackage, ''));
$psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $mainPackage); $psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $mainPackage);
$classmap = $this->parseAutoloadsType($sortedPackageMap, 'classmap', $mainPackage); $classmap = $this->parseAutoloadsType($sortedPackageMap, 'classmap', $mainPackage);
@ -434,6 +431,7 @@ FOOTER;
protected function parseAutoloadsType(array $packageMap, $type, PackageInterface $mainPackage) protected function parseAutoloadsType(array $packageMap, $type, PackageInterface $mainPackage)
{ {
$autoloads = array(); $autoloads = array();
foreach ($packageMap as $item) { foreach ($packageMap as $item) {
list($package, $installPath) = $item; list($package, $installPath) = $item;
@ -465,49 +463,45 @@ FOOTER;
protected function sortPackageMap(array $packageMap) protected function sortPackageMap(array $packageMap)
{ {
$groups = array(); $positions = array();
$names = array(); $names = array();
foreach ($packageMap as $key => $item) { $indexes = array();
$groups[$key] = array($item);
foreach ($packageMap as $position => $item) {
$mainName = $item[0]->getName(); $mainName = $item[0]->getName();
foreach ($item[0]->getNames() as $name) { $names = array_merge(array_fill_keys($item[0]->getNames(), $mainName), $names);
if (!isset($names[$name])) { $names[$mainName] = $mainName;
$names[$name] = $name == $mainName ? $key : $mainName; $indexes[$mainName] = $positions[$mainName] = $position;
}
}
} }
foreach ($packageMap as $item) { foreach ($packageMap as $item) {
$position = $positions[$item[0]->getName()];
foreach (array_merge($item[0]->getRequires(), $item[0]->getDevRequires()) as $link) { foreach (array_merge($item[0]->getRequires(), $item[0]->getDevRequires()) as $link) {
$target = $link->getTarget(); $target = $link->getTarget();
if (!isset($names[$target])) { if (!isset($names[$target])) {
continue; continue;
} }
$targetKey = $names[$target]; $target = $names[$target];
if (is_string($targetKey)) { if ($positions[$target] <= $position) {
if (!isset($names[$targetKey])) {
continue;
}
$targetKey = $names[$targetKey];
}
$packageKey = $names[$item[0]->getName()];
if ($targetKey <= $packageKey || !isset($groups[$packageKey])) {
continue; continue;
} }
foreach ($groups[$packageKey] as $originalItem) { foreach ($positions as $key => $value) {
$groups[$targetKey][] = $originalItem; if ($value >= $position) {
$names[$originalItem[0]->getName()] = $targetKey; break;
}
$positions[$key]--;
} }
unset($groups[$packageKey]);
$positions[$target] = $position - 1;
} }
asort($positions);
} }
$sortedPackageMap = array(); $sortedPackageMap = array();
foreach ($groups as $group) { foreach (array_keys($positions) as $packageName) {
$sortedPackageMap = array_merge($sortedPackageMap, $group); $sortedPackageMap[] = $packageMap[$indexes[$packageName]];
} }
return $sortedPackageMap; return $sortedPackageMap;

View File

@ -338,23 +338,26 @@ class AutoloadGeneratorTest extends TestCase
$package = new Package('a', '1.0', '1.0'); $package = new Package('a', '1.0', '1.0');
$package->setAutoload(array('files' => array('root.php'))); $package->setAutoload(array('files' => array('root.php')));
$package->setRequires(array(new Link('a', 'z/foo'))); $package->setRequires(array(new Link('a', 'z/foo')));
$package->setRequires(array(new Link('a', 'd/d')));
$package->setRequires(array(new Link('a', 'e/e')));
$packages = array(); $packages = array();
$packages[] = $z = new Package('z/foo', '1.0', '1.0'); $packages[] = $z = new Package('z/foo', '1.0', '1.0');
$packages[] = $b = new Package('b/bar', '1.0', '1.0'); $packages[] = $b = new Package('b/bar', '1.0', '1.0');
$packages[] = $c = new Package('c/lorem', '1.0', '1.0');
$packages[] = $d = new Package('d/d', '1.0', '1.0'); $packages[] = $d = new Package('d/d', '1.0', '1.0');
$packages[] = $c = new Package('c/lorem', '1.0', '1.0');
$packages[] = $e = new Package('e/e', '1.0', '1.0'); $packages[] = $e = new Package('e/e', '1.0', '1.0');
$z->setAutoload(array('files' => array('testA.php'))); $z->setAutoload(array('files' => array('testA.php')));
$z->setRequires(array(new Link('z/foo', 'c/lorem'))); $z->setRequires(array(new Link('z/foo', 'c/lorem')));
$b->setAutoload(array('files' => array('testB.php'))); $b->setAutoload(array('files' => array('testB.php')));
$b->setRequires(array(new Link('b/bar', 'c/lorem'))); $b->setRequires(array(new Link('b/bar', 'c/lorem'), new Link('b/bar', 'd/d')));
$c->setAutoload(array('files' => array('testC.php'))); $c->setAutoload(array('files' => array('testC.php')));
$d->setAutoload(array('files' => array('testD.php'))); $d->setAutoload(array('files' => array('testD.php')));
$d->setRequires(array(new Link('d/d', 'c/lorem')));
$e->setAutoload(array('files' => array('testE.php'))); $e->setAutoload(array('files' => array('testE.php')));
$e->setRequires(array(new Link('e/e', 'c/lorem'))); $e->setRequires(array(new Link('e/e', 'c/lorem')));

View File

@ -40,10 +40,10 @@ class ComposerAutoloaderInitFilesAutoloadOrder
require $vendorDir . '/c/lorem/testC.php'; require $vendorDir . '/c/lorem/testC.php';
require $vendorDir . '/z/foo/testA.php'; require $vendorDir . '/z/foo/testA.php';
require $baseDir . '/root.php';
require $vendorDir . '/b/bar/testB.php';
require $vendorDir . '/d/d/testD.php'; require $vendorDir . '/d/d/testD.php';
require $vendorDir . '/b/bar/testB.php';
require $vendorDir . '/e/e/testE.php'; require $vendorDir . '/e/e/testE.php';
require $baseDir . '/root.php';
return $loader; return $loader;
} }

View File

@ -38,9 +38,9 @@ class ComposerAutoloaderInitFilesAutoload
$loader->register(); $loader->register();
require $baseDir . '/root.php';
require $vendorDir . '/a/a/test.php'; require $vendorDir . '/a/a/test.php';
require $vendorDir . '/b/b/test2.php'; require $vendorDir . '/b/b/test2.php';
require $baseDir . '/root.php';
return $loader; return $loader;
} }