Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AutoloadGenerator.php

function AutoloadGenerator::getPlatformCheck

Parameters

array<int, array{0: PackageInterface, 1: string|null}> $packageMap:

bool|'php-only' $checkPlatform:

string[] $devPackageNames:

Return value

?string

1 call to AutoloadGenerator::getPlatformCheck()
AutoloadGenerator::dump in vendor/composer/composer/src/Composer/Autoload/AutoloadGenerator.php

File

vendor/composer/composer/src/Composer/Autoload/AutoloadGenerator.php, line 776

Class

AutoloadGenerator
@author Igor Wiedler <igor@wiedler.ch> @author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Autoload

Code

protected function getPlatformCheck(array $packageMap, $checkPlatform, array $devPackageNames) {
    $lowestPhpVersion = Bound::zero();
    $requiredPhp64bit = false;
    $requiredExtensions = [];
    $extensionProviders = [];
    foreach ($packageMap as $item) {
        $package = $item[0];
        foreach (array_merge($package->getReplaces(), $package->getProvides()) as $link) {
            if (Preg::isMatch('{^ext-(.+)$}iD', $link->getTarget(), $match)) {
                $extensionProviders[$match[1]][] = $link->getConstraint();
            }
        }
    }
    foreach ($packageMap as $item) {
        $package = $item[0];
        // skip dev dependencies platform requirements as platform-check really should only be a production safeguard
        if (in_array($package->getName(), $devPackageNames, true)) {
            continue;
        }
        foreach ($package->getRequires() as $link) {
            if ($this->platformRequirementFilter
                ->isIgnored($link->getTarget())) {
                continue;
            }
            if (in_array($link->getTarget(), [
                'php',
                'php-64bit',
            ], true)) {
                $constraint = $link->getConstraint();
                if ($constraint->getLowerBound()
                    ->compareTo($lowestPhpVersion, '>')) {
                    $lowestPhpVersion = $constraint->getLowerBound();
                }
            }
            if ('php-64bit' === $link->getTarget()) {
                $requiredPhp64bit = true;
            }
            if ($checkPlatform === true && Preg::isMatch('{^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 ($provided->matches($link->getConstraint())) {
                            continue 2;
                        }
                    }
                }
                if ($match[1] === 'zend-opcache') {
                    $match[1] = 'zend opcache';
                }
                $extension = var_export($match[1], true);
                if ($match[1] === 'pcntl' || $match[1] === 'readline') {
                    $requiredExtensions[$extension] = "PHP_SAPI !== 'cli' || extension_loaded({$extension}) || \$missingExtensions[] = {$extension};\n";
                }
                else {
                    $requiredExtensions[$extension] = "extension_loaded({$extension}) || \$missingExtensions[] = {$extension};\n";
                }
            }
        }
    }
    ksort($requiredExtensions);
    $formatToPhpVersionId = static function (Bound $bound) : int {
        if ($bound->isZero()) {
            return 0;
        }
        if ($bound->isPositiveInfinity()) {
            return 99999;
        }
        $version = str_replace('-', '.', $bound->getVersion());
        $chunks = array_map('intval', explode('.', $version));
        return $chunks[0] * 10000 + $chunks[1] * 100 + $chunks[2];
    };
    $formatToHumanReadable = static function (Bound $bound) {
        if ($bound->isZero()) {
            return 0;
        }
        if ($bound->isPositiveInfinity()) {
            return 99999;
        }
        $version = str_replace('-', '.', $bound->getVersion());
        $chunks = explode('.', $version);
        $chunks = array_slice($chunks, 0, 3);
        return implode('.', $chunks);
    };
    $requiredPhp = '';
    $requiredPhpError = '';
    if (!$lowestPhpVersion->isZero()) {
        $operator = $lowestPhpVersion->isInclusive() ? '>=' : '>';
        $requiredPhp = 'PHP_VERSION_ID ' . $operator . ' ' . $formatToPhpVersionId($lowestPhpVersion);
        $requiredPhpError = '"' . $operator . ' ' . $formatToHumanReadable($lowestPhpVersion) . '"';
    }
    if ($requiredPhp) {
        $requiredPhp = <<<PHP_CHECK

if (!({<span class="php-variable">$requiredPhp</span>})) {
    \$issues[] = 'Your Composer dependencies require a PHP version {<span class="php-variable">$requiredPhpError</span>}. You are running ' . PHP_VERSION . '.';
}

PHP_CHECK;
    }
    if ($requiredPhp64bit) {
        $requiredPhp .= <<<PHP_CHECK

if (PHP_INT_SIZE !== 8) {
    \$issues[] = 'Your Composer dependencies require a 64-bit build of PHP.';
}

PHP_CHECK;
    }
    $requiredExtensions = implode('', $requiredExtensions);
    if ('' !== $requiredExtensions) {
        $requiredExtensions = <<<EXT_CHECKS

\$missingExtensions = array();
{<span class="php-variable">$requiredExtensions</span>}
if (\$missingExtensions) {
    \$issues[] = 'Your Composer dependencies require the following PHP extensions to be installed: ' . implode(', ', \$missingExtensions) . '.';
}

EXT_CHECKS;
    }
    if (!$requiredPhp && !$requiredExtensions) {
        return null;
    }
    return <<<PLATFORM_CHECK
<?php

// platform_check.php @generated by Composer

\$issues = array();
{<span class="php-variable">$requiredPhp</span>}{<span class="php-variable">$requiredExtensions</span>}
if (\$issues) {
    if (!headers_sent()) {
        header('HTTP/1.1 500 Internal Server Error');
    }
    if (!ini_get('display_errors')) {
        if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
            fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, \$issues) . PHP_EOL.PHP_EOL);
        } elseif (!headers_sent()) {
            echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, \$issues)) . PHP_EOL.PHP_EOL;
        }
    }
    trigger_error(
        'Composer detected issues in your platform: ' . implode(' ', \$issues),
        E_USER_ERROR
    );
}

PLATFORM_CHECK;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal