Browse Source

Merge pull request #8812 from nicolas-grekas/ext-check

Optimize extension checks
Jordi Boggiano 4 years ago
parent
commit
c9846d4d3c

+ 18 - 12
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 = <<<EXT_CHECKS
+
+\$missingExtensions = array();
+$requiredExtensions
+if (\$missingExtensions) {
+    \$issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', \$missingExtensions);
+}
+
+EXT_CHECKS;
+        }
 
         return <<<PLATFORM_CHECK
 <?php
@@ -646,14 +658,8 @@ EOF;
 if (!(PHP_VERSION_ID $lowestOperator $lowestPhpVersionId && PHP_VERSION_ID $highestOperator $highestPhpVersionId)) {
     \$issues[] = 'Your Composer dependencies require a PHP version "$lowestOperator $lowestPhpVersion" and "$highestOperator $highestPhpVersion". You are running ' . PHP_VERSION  .  '.';
 }
-
-\$missingExtensions = array_diff($requiredExtensions, 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)) {
+$requiredExtensions
+if (\$issues) {
     die('Composer detected issues in your platform:' . "\\n\\n" . implode("\\n", \$issues));
 }
 

+ 1 - 8
tests/Composer/Test/Autoload/Fixtures/platform/no_extensions_required.php

@@ -8,13 +8,6 @@ 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 (
-), 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));
 }

+ 1 - 8
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));
 }

+ 5 - 6
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));
 }

+ 1 - 8
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));
 }

+ 1 - 8
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));
 }

+ 5 - 6
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));
 }