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

Breadcrumb

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

function PackageDiscoveryTrait::findBestVersionAndNameForPackage

Given a package name, this determines the best version to use in the require key.

This returns a version with the ~ operator prefixed when possible.

Return value

array{string, string} name version

Throws

\InvalidArgumentException

1 call to PackageDiscoveryTrait::findBestVersionAndNameForPackage()
PackageDiscoveryTrait::determineRequirements in vendor/composer/composer/src/Composer/Command/PackageDiscoveryTrait.php

File

vendor/composer/composer/src/Composer/Command/PackageDiscoveryTrait.php, line 284

Class

PackageDiscoveryTrait
@internal

Namespace

Composer\Command

Code

private function findBestVersionAndNameForPackage(IOInterface $io, InputInterface $input, string $name, ?PlatformRepository $platformRepo = null, string $preferredStability = 'stable', bool $fixed = false) : array {
    // handle ignore-platform-reqs flag if present
    if ($input->hasOption('ignore-platform-reqs') && $input->hasOption('ignore-platform-req')) {
        $platformRequirementFilter = $this->getPlatformRequirementFilter($input);
    }
    else {
        $platformRequirementFilter = PlatformRequirementFilterFactory::ignoreNothing();
    }
    // find the latest version allowed in this repo set
    $repoSet = $this->getRepositorySet($input);
    $versionSelector = new VersionSelector($repoSet, $platformRepo);
    $effectiveMinimumStability = $this->getMinimumStability($input);
    $package = $versionSelector->findBestCandidate($name, null, $preferredStability, $platformRequirementFilter, 0, $this->getIO());
    if (false === $package) {
        // platform packages can not be found in the pool in versions other than the local platform's has
        // so if platform reqs are ignored we just take the user's word for it
        if ($platformRequirementFilter->isIgnored($name)) {
            return [
                $name,
                '*',
            ];
        }
        // Check if it is a virtual package provided by others
        $providers = $repoSet->getProviders($name);
        if (count($providers) > 0) {
            $constraint = '*';
            if ($input->isInteractive()) {
                $constraint = $this->getIO()
                    ->askAndValidate('Package "<info>' . $name . '</info>" does not exist but is provided by ' . count($providers) . ' packages. Which version constraint would you like to use? [<info>*</info>] ', static function ($value) {
                    $parser = new VersionParser();
                    $parser->parseConstraints($value);
                    return $value;
                }, 3, '*');
            }
            return [
                $name,
                $constraint,
            ];
        }
        // Check whether the package requirements were the problem
        if (!$platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter && false !== ($candidate = $versionSelector->findBestCandidate($name, null, $preferredStability, PlatformRequirementFilterFactory::ignoreAll()))) {
            throw new \InvalidArgumentException(sprintf('Package %s has requirements incompatible with your PHP version, PHP extensions and Composer version' . $this->getPlatformExceptionDetails($candidate, $platformRepo), $name));
        }
        // Check whether the minimum stability was the problem but the package exists
        if (false !== ($package = $versionSelector->findBestCandidate($name, null, $preferredStability, $platformRequirementFilter, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES))) {
            // we must first verify if a valid package would be found in a lower priority repository
            if (false !== ($allReposPackage = $versionSelector->findBestCandidate($name, null, $preferredStability, $platformRequirementFilter, RepositorySet::ALLOW_SHADOWED_REPOSITORIES))) {
                throw new \InvalidArgumentException('Package ' . $name . ' exists in ' . $allReposPackage->getRepository()
                    ->getRepoName() . ' and ' . $package->getRepository()
                    ->getRepoName() . ' which has a higher repository priority. The packages from the higher priority repository do not match your minimum-stability and are therefore not installable. That repository is canonical so the lower priority repo\'s packages are not installable. See https://getcomposer.org/repoprio for details and assistance.');
            }
            throw new \InvalidArgumentException(sprintf('Could not find a version of package %s matching your minimum-stability (%s). Require it with an explicit version constraint allowing its desired stability.', $name, $effectiveMinimumStability));
        }
        // Check whether the PHP version was the problem for all versions
        if (!$platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter && false !== ($candidate = $versionSelector->findBestCandidate($name, null, $preferredStability, PlatformRequirementFilterFactory::ignoreAll(), RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES))) {
            $additional = '';
            if (false === $versionSelector->findBestCandidate($name, null, $preferredStability, PlatformRequirementFilterFactory::ignoreAll())) {
                $additional = PHP_EOL . PHP_EOL . 'Additionally, the package was only found with a stability of "' . $candidate->getStability() . '" while your minimum stability is "' . $effectiveMinimumStability . '".';
            }
            throw new \InvalidArgumentException(sprintf('Could not find package %s in any version matching your PHP version, PHP extensions and Composer version' . $this->getPlatformExceptionDetails($candidate, $platformRepo) . '%s', $name, $additional));
        }
        // Check for similar names/typos
        $similar = $this->findSimilar($name);
        if (count($similar) > 0) {
            if (in_array($name, $similar, true)) {
                throw new \InvalidArgumentException(sprintf("Could not find package %s. It was however found via repository search, which indicates a consistency issue with the repository.", $name));
            }
            if ($input->isInteractive()) {
                $result = $io->select("<error>Could not find package {$name}.</error>\nPick one of these or leave empty to abort:", $similar, false, 1);
                if ($result !== false) {
                    return $this->findBestVersionAndNameForPackage($io, $input, $similar[$result], $platformRepo, $preferredStability, $fixed);
                }
            }
            throw new \InvalidArgumentException(sprintf("Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n    %s", $name, implode("\n    ", $similar)));
        }
        throw new \InvalidArgumentException(sprintf('Could not find a matching version of package %s. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (%s).', $name, $effectiveMinimumStability));
    }
    return [
        $package->getPrettyName(),
        $fixed ? $package->getPrettyVersion() : $versionSelector->findRecommendedRequireVersion($package),
    ];
}

API Navigation

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