From 6a6ea6057f0cb8e7ed4b8e4ce6f45a4831e61083 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 24 Apr 2020 13:38:50 +0200 Subject: [PATCH] Handle provider/replacer packages and avoid checking extensions which are provided by packages, refs #8546 --- src/Composer/Autoload/AutoloadGenerator.php | 20 +++++++++++++ .../Test/Autoload/AutoloadGeneratorTest.php | 30 +++++++++++++++++-- .../platform/replaced_provided_exts.php | 21 +++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/platform/replaced_provided_exts.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index c0b71c566..bc334c927 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -20,6 +20,7 @@ use Composer\Package\AliasPackage; use Composer\Package\PackageInterface; use Composer\Repository\InstalledRepositoryInterface; use Composer\Semver\Constraint\Bound; +use Composer\Semver\Constraint\EmptyConstraint; use Composer\Util\Filesystem; use Composer\Script\ScriptEvents; use Composer\Util\PackageSorter; @@ -573,6 +574,16 @@ EOF; $lowestPhpVersion = Bound::zero(); $highestPhpVersion = Bound::positiveInfinity(); $requiredExtensions = array(); + $extensionProviders = array(); + + foreach ($packageMap as $item) { + list($package, $installPath) = $item; + foreach (array_merge($package->getReplaces(), $package->getProvides()) as $link) { + if (preg_match('{^ext-(.+)$}iD', $link->getTarget(), $match)) { + $extensionProviders[$match[1]][] = $link->getConstraint() ?: new EmptyConstraint(); + } + } + } foreach ($packageMap as $item) { list($package, $installPath) = $item; @@ -587,6 +598,15 @@ EOF; } if (preg_match('{^ext-(.+)$}iD', $link->getTarget(), $match)) { + // skip extension checks if they have a valid provider/replacer + if (isset($extensionProviders[$match[1]])) { + foreach ($extensionProviders[$match[1]] as $provided) { + if (!$link->getConstraint() || $provided->matches($link->getConstraint())) { + continue 2; + } + } + } + $extension = var_export($match[1], true); if ($match[1] === 'pcntl') { $requiredExtensions[$extension] = "PHP_SAPI !== 'cli' || extension_loaded($extension) || \$missingExtensions[] = $extension;\n"; diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 4159cf003..298801b41 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -1648,11 +1648,19 @@ EOF; /** * @dataProvider platformCheckProvider */ - public function testGeneratesPlatformCheck(array $requires, $expectedFixture) + public function testGeneratesPlatformCheck(array $requires, $expectedFixture, array $provides = array(), array $replaces = array()) { $package = new Package('a', '1.0', '1.0'); $package->setRequires($requires); + if ($provides) { + $package->setProvides($provides); + } + + if ($replaces) { + $package->setReplaces($replaces); + } + $this->repository->expects($this->once()) ->method('getCanonicalPackages') ->will($this->returnValue(array())); @@ -1705,7 +1713,25 @@ EOF; new Link('a', 'php', $versionParser->parseConstraints('^7.2')), ), 'no_extensions_required' - ) + ), + 'Replaced/provided extensions are not checked for + checking case insensitivity' => array( + array( + new Link('a', 'ext-xml', $versionParser->parseConstraints('^7.2')), + new Link('a', 'ext-Pdo', $versionParser->parseConstraints('^7.2')), + new Link('a', 'ext-bcMath', $versionParser->parseConstraints('^7.2')), + ), + 'replaced_provided_exts', + array( + // constraint does not satisfy all the ^7.2 requirement so we do not accept it as being replaced + new Link('a', 'ext-PDO', $versionParser->parseConstraints('7.1.*')), + // valid replace of bcmath so no need to check for it + new Link('a', 'ext-BCMath', $versionParser->parseConstraints('^7.1')), + ), + array( + // valid provide of ext-xml so no need to check for it + new Link('a', 'ext-XML', $versionParser->parseConstraints('*')), + ), + ), ); } diff --git a/tests/Composer/Test/Autoload/Fixtures/platform/replaced_provided_exts.php b/tests/Composer/Test/Autoload/Fixtures/platform/replaced_provided_exts.php new file mode 100644 index 000000000..d57294f0b --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/platform/replaced_provided_exts.php @@ -0,0 +1,21 @@ += 0 && PHP_VERSION_ID < 99999)) { + $issues[] = 'Your Composer dependencies require a PHP version ">= 0" and "< 99999". You are running ' . PHP_VERSION . '.'; +} + +$missingExtensions = array(); +extension_loaded('pdo') || $missingExtensions[] = 'pdo'; + +if ($missingExtensions) { + $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); +} + +if ($issues) { + echo 'Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues); + exit(104); +}