diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 9b9710d9e..b881a2163 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -388,6 +388,7 @@ EOF; public function parseAutoloads(array $packageMap, PackageInterface $mainPackage) { $mainPackageMap = array_shift($packageMap); + $packageMap = $this->filterPackageMap($packageMap, $mainPackage); $sortedPackageMap = $this->sortPackageMap($packageMap); $sortedPackageMap[] = $mainPackageMap; array_unshift($packageMap, $mainPackageMap); @@ -899,6 +900,48 @@ INITIALIZER; return md5($package->getName() . ':' . $path); } + /** + * Filters out dev-dependencies when not in dev-mode + * + * @param array $packageMap + * @param PackageInterface $mainPackage + * @return array + */ + protected function filterPackageMap(array $packageMap, PackageInterface $mainPackage) { + if ($this->devMode === true) { + return $packageMap; + } + + $packages = array(); + $include = array(); + + foreach ($packageMap as $item) { + $package = $item[0]; + $name = $package->getName(); + $packages[$name] = $package; + } + + $add = function (PackageInterface $package) use (&$add, $mainPackage, $packages, &$include) { + foreach ($package->getRequires() as $link) { + $target = $link->getTarget(); + $include[$target] = true; + if (isset($packages[$target])) { + $add($packages[$target]); + } + } + }; + $add($mainPackage); + + return array_filter( + $packageMap, + function ($item) use ($include) { + $package = $item[0]; + $name = $package->getName(); + return isset($include[$name]); + } + ); + } + /** * Sorts packages by dependency weight * diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 75cbcd058..f491a62b5 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -360,6 +360,10 @@ class AutoloadGeneratorTest extends TestCase public function testVendorsAutoloading() { $package = new Package('a', '1.0', '1.0'); + $package->setRequires(array( + new Link('a', 'a/a'), + new Link('a', 'b/b') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -406,6 +410,10 @@ class AutoloadGeneratorTest extends TestCase public function testVendorsClassMapAutoloading() { $package = new Package('a', '1.0', '1.0'); + $package->setRequires(array( + new Link('a', 'a/a'), + new Link('a', 'b/b') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -441,6 +449,10 @@ class AutoloadGeneratorTest extends TestCase public function testVendorsClassMapAutoloadingWithTargetDir() { $package = new Package('a', '1.0', '1.0'); + $package->setRequires(array( + new Link('a', 'a/a'), + new Link('a', 'b/b') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -476,6 +488,11 @@ class AutoloadGeneratorTest extends TestCase public function testClassMapAutoloadingEmptyDirAndExactFile() { $package = new Package('a', '1.0', '1.0'); + $package->setRequires(array( + new Link('a', 'a/a'), + new Link('a', 'b/b'), + new Link('a', 'c/c') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -515,6 +532,11 @@ class AutoloadGeneratorTest extends TestCase public function testClassMapAutoloadingAuthoritativeAndApcu() { $package = new Package('a', '1.0', '1.0'); + $package->setRequires(array( + new Link('a', 'a/a'), + new Link('a', 'b/b'), + new Link('a', 'c/c') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -559,6 +581,11 @@ class AutoloadGeneratorTest extends TestCase { $package = new Package('a', '1.0', '1.0'); $package->setAutoload(array('files' => array('root.php'))); + $package->setRequires(array( + new Link('a', 'a/a'), + new Link('a', 'b/b'), + new Link('a', 'c/c') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -604,6 +631,14 @@ class AutoloadGeneratorTest extends TestCase $notAutoloadPackage = new Package('a', '1.0', '1.0'); + $requires = array( + new Link('a', 'a/a'), + new Link('a', 'b/b'), + new Link('a', 'c/c') + ); + $autoloadPackage->setRequires($requires); + $notAutoloadPackage->setRequires($requires); + $autoloadPackages = array(); $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0'); $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0'); @@ -667,9 +702,12 @@ class AutoloadGeneratorTest extends TestCase { $package = new Package('a', '1.0', '1.0'); $package->setAutoload(array('files' => array('root2.php'))); - $package->setRequires(array(new Link('a', 'z/foo'))); - $package->setRequires(array(new Link('a', 'd/d'))); - $package->setRequires(array(new Link('a', 'e/e'))); + $package->setRequires(array( + new Link('a', 'z/foo'), + new Link('a', 'b/bar'), + new Link('a', 'd/d'), + new Link('a', 'e/e') + )); $packages = array(); $packages[] = $z = new Package('z/foo', '1.0', '1.0'); @@ -736,7 +774,10 @@ class AutoloadGeneratorTest extends TestCase 'psr-0' => array('A\\B' => $this->workingDir.'/lib'), 'classmap' => array($this->workingDir.'/src'), )); - $mainPackage->setRequires(array(new Link('z', 'a/a'))); + $mainPackage->setRequires(array( + new Link('z', 'a/a'), + new Link('z', 'b/b') + )); $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); @@ -993,6 +1034,9 @@ EOF; 'classmap' => array('classmap'), 'files' => array('test.php'), )); + $package->setRequires(array( + new Link('a', 'b/b') + )); $vendorPackage = new Package('b/b', '1.0', '1.0'); $vendorPackage->setAutoload(array(