From 6463ab9e49c35ce71452b8a8a536b6ad194fce60 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 21 Apr 2020 09:35:34 +0200 Subject: [PATCH] Optimize extension checks --- src/Composer/Autoload/AutoloadGenerator.php | 30 +++++++++++-------- .../platform/no_extensions_required.php | 9 +----- .../Fixtures/platform/no_php_lower_bound.php | 9 +----- .../Fixtures/platform/no_php_required.php | 11 ++++--- .../Fixtures/platform/no_php_upper_bound.php | 9 +----- .../platform/specific_php_release.php | 9 +----- .../Autoload/Fixtures/platform/typical.php | 11 ++++--- 7 files changed, 32 insertions(+), 56 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 6500e8b24..0a9e2928e 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -591,13 +591,13 @@ EOF; } if (preg_match('{^ext-(.+)$}iD', $link->getTarget(), $match)) { - $requiredExtensions[] = $match[1]; + $extension = var_export($match[1], true); + $requiredExtensions[$extension] = "extension_loaded($extension) || \$missingExtensions[] = $extension;\n"; } } } - $requiredExtensions = array_values(array_unique($requiredExtensions)); - sort($requiredExtensions); + ksort($requiredExtensions); $formatToPhpVersionId = function (Bound $bound) { if ($bound->isZero()) { @@ -634,7 +634,19 @@ EOF; $highestPhpVersionId = $formatToPhpVersionId($highestPhpVersion); $lowestPhpVersion = $formatToHumanReadable($lowestPhpVersion); $highestPhpVersion = $formatToHumanReadable($highestPhpVersion); - $requiredExtensions = var_export($requiredExtensions, true); + $requiredExtensions = implode('', $requiredExtensions); + + if ('' !== $requiredExtensions) { + $requiredExtensions = <<= 70200 && PHP_VERSION_ID < 80000)) { $issues[] = 'Your Composer dependencies require a PHP version ">= 7.2.0" and "< 8.0.0". You are running ' . PHP_VERSION . '.'; } -$missingExtensions = array_diff(array ( -), array_map('strtolower', get_loaded_extensions())); - -if (0 !== count($missingExtensions)) { - $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); -} - -if (0 !== count($issues)) { +if ($issues) { die('Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues)); } diff --git a/tests/Composer/Test/Autoload/Fixtures/platform/no_php_lower_bound.php b/tests/Composer/Test/Autoload/Fixtures/platform/no_php_lower_bound.php index c6f3082e5..5d1c368ad 100644 --- a/tests/Composer/Test/Autoload/Fixtures/platform/no_php_lower_bound.php +++ b/tests/Composer/Test/Autoload/Fixtures/platform/no_php_lower_bound.php @@ -8,13 +8,6 @@ if (!(PHP_VERSION_ID >= 0 && PHP_VERSION_ID < 80000)) { $issues[] = 'Your Composer dependencies require a PHP version ">= 0" and "< 8.0.0". You are running ' . PHP_VERSION . '.'; } -$missingExtensions = array_diff(array ( -), array_map('strtolower', get_loaded_extensions())); - -if (0 !== count($missingExtensions)) { - $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); -} - -if (0 !== count($issues)) { +if ($issues) { die('Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues)); } diff --git a/tests/Composer/Test/Autoload/Fixtures/platform/no_php_required.php b/tests/Composer/Test/Autoload/Fixtures/platform/no_php_required.php index 4f45a440f..ed2ecf25e 100644 --- a/tests/Composer/Test/Autoload/Fixtures/platform/no_php_required.php +++ b/tests/Composer/Test/Autoload/Fixtures/platform/no_php_required.php @@ -8,15 +8,14 @@ if (!(PHP_VERSION_ID >= 0 && PHP_VERSION_ID < 99999)) { $issues[] = 'Your Composer dependencies require a PHP version ">= 0" and "< 99999". You are running ' . PHP_VERSION . '.'; } -$missingExtensions = array_diff(array ( - 0 => 'json', - 1 => 'xml', -), array_map('strtolower', get_loaded_extensions())); +$missingExtensions = array(); +extension_loaded('json') || $missingExtensions[] = 'json'; +extension_loaded('xml') || $missingExtensions[] = 'xml'; -if (0 !== count($missingExtensions)) { +if ($missingExtensions) { $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); } -if (0 !== count($issues)) { +if ($issues) { die('Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues)); } diff --git a/tests/Composer/Test/Autoload/Fixtures/platform/no_php_upper_bound.php b/tests/Composer/Test/Autoload/Fixtures/platform/no_php_upper_bound.php index 7ae0cf1e7..f11188b31 100644 --- a/tests/Composer/Test/Autoload/Fixtures/platform/no_php_upper_bound.php +++ b/tests/Composer/Test/Autoload/Fixtures/platform/no_php_upper_bound.php @@ -8,13 +8,6 @@ if (!(PHP_VERSION_ID >= 70200 && PHP_VERSION_ID < 99999)) { $issues[] = 'Your Composer dependencies require a PHP version ">= 7.2.0" and "< 99999". You are running ' . PHP_VERSION . '.'; } -$missingExtensions = array_diff(array ( -), array_map('strtolower', get_loaded_extensions())); - -if (0 !== count($missingExtensions)) { - $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); -} - -if (0 !== count($issues)) { +if ($issues) { die('Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues)); } diff --git a/tests/Composer/Test/Autoload/Fixtures/platform/specific_php_release.php b/tests/Composer/Test/Autoload/Fixtures/platform/specific_php_release.php index d837add39..3ba4d2cc4 100644 --- a/tests/Composer/Test/Autoload/Fixtures/platform/specific_php_release.php +++ b/tests/Composer/Test/Autoload/Fixtures/platform/specific_php_release.php @@ -8,13 +8,6 @@ if (!(PHP_VERSION_ID >= 70208 && PHP_VERSION_ID < 80000)) { $issues[] = 'Your Composer dependencies require a PHP version ">= 7.2.8" and "< 8.0.0". You are running ' . PHP_VERSION . '.'; } -$missingExtensions = array_diff(array ( -), array_map('strtolower', get_loaded_extensions())); - -if (0 !== count($missingExtensions)) { - $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); -} - -if (0 !== count($issues)) { +if ($issues) { die('Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues)); } diff --git a/tests/Composer/Test/Autoload/Fixtures/platform/typical.php b/tests/Composer/Test/Autoload/Fixtures/platform/typical.php index f2cafaf3b..45d85c8c2 100644 --- a/tests/Composer/Test/Autoload/Fixtures/platform/typical.php +++ b/tests/Composer/Test/Autoload/Fixtures/platform/typical.php @@ -8,15 +8,14 @@ if (!(PHP_VERSION_ID >= 70200 && PHP_VERSION_ID < 80000)) { $issues[] = 'Your Composer dependencies require a PHP version ">= 7.2.0" and "< 8.0.0". You are running ' . PHP_VERSION . '.'; } -$missingExtensions = array_diff(array ( - 0 => 'json', - 1 => 'xml', -), array_map('strtolower', get_loaded_extensions())); +$missingExtensions = array(); +extension_loaded('json') || $missingExtensions[] = 'json'; +extension_loaded('xml') || $missingExtensions[] = 'xml'; -if (0 !== count($missingExtensions)) { +if ($missingExtensions) { $issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', $missingExtensions); } -if (0 !== count($issues)) { +if ($issues) { die('Composer detected issues in your platform:' . "\n\n" . implode("\n", $issues)); }