From b0be87177d62611e0bf22825942be4820e7295c4 Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Tue, 8 May 2018 02:24:37 +0200 Subject: [PATCH 1/3] Filter dev-dependencies from "dump-autoload --no-dev" , fixes #4343 --- src/Composer/Autoload/AutoloadGenerator.php | 43 +++++++++++++++ .../Test/Autoload/AutoloadGeneratorTest.php | 52 +++++++++++++++++-- 2 files changed, 91 insertions(+), 4 deletions(-) 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( From af1dccb1fb67ae3fc337ae95376ef77658d9a711 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 15 May 2018 15:20:18 +0200 Subject: [PATCH 2/3] Avoid showing virtual packages in search results, fixes #7310 --- src/Composer/Repository/ComposerRepository.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 60ab0dfbd..bba67f999 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -201,9 +201,17 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $hostname = parse_url($url, PHP_URL_HOST) ?: $url; $json = $this->rfs->getContents($hostname, $url, false); - $results = JsonFile::parseJson($json, $url); + $search = JsonFile::parseJson($json, $url); - return $results['results']; + $results = array(); + foreach ($search['results'] as $result) { + // do not show virtual packages in results as they are not directly useful from a composer perspective + if (empty($result['virtual'])) { + $results[] = $result; + } + } + + return $results; } if ($this->hasProviders()) { From c6d53abf8991b022cb9c2156a00ccc5ed744d991 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 15 May 2018 16:06:56 +0200 Subject: [PATCH 3/3] Fix tests --- src/Composer/Repository/ComposerRepository.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index bba67f999..53c16129f 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -203,6 +203,10 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $json = $this->rfs->getContents($hostname, $url, false); $search = JsonFile::parseJson($json, $url); + if (empty($search['results'])) { + return array(); + } + $results = array(); foreach ($search['results'] as $result) { // do not show virtual packages in results as they are not directly useful from a composer perspective