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

Breadcrumb

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

function ShowCommand::getPackage

finds a package by name and version if provided

Parameters

ConstraintInterface|string $version:

Return value

array{CompletePackageInterface|null, array<string, string>}

Throws

\InvalidArgumentException

2 calls to ShowCommand::getPackage()
ShowCommand::addTree in vendor/composer/composer/src/Composer/Command/ShowCommand.php
Display a package tree
ShowCommand::execute in vendor/composer/composer/src/Composer/Command/ShowCommand.php
Executes the current command.

File

vendor/composer/composer/src/Composer/Command/ShowCommand.php, line 802

Class

ShowCommand
@author Robert Schönthal <seroscho@googlemail.com> @author Jordi Boggiano <j.boggiano@seld.be> @author Jérémy Romey <jeremyFreeAgent> @author Mihai Plasoianu <mihai@plasoianu.de>

Namespace

Composer\Command

Code

protected function getPackage(InstalledRepository $installedRepo, RepositoryInterface $repos, string $name, $version = null) : array {
    $name = strtolower($name);
    $constraint = is_string($version) ? $this->versionParser
        ->parseConstraints($version) : $version;
    $policy = new DefaultPolicy();
    $repositorySet = new RepositorySet('dev');
    $repositorySet->allowInstalledRepositories();
    $repositorySet->addRepository($repos);
    $matchedPackage = null;
    $versions = [];
    if (PlatformRepository::isPlatformPackage($name)) {
        $pool = $repositorySet->createPoolWithAllPackages();
    }
    else {
        $pool = $repositorySet->createPoolForPackage($name);
    }
    $matches = $pool->whatProvides($name, $constraint);
    $literals = [];
    foreach ($matches as $package) {
        // avoid showing the 9999999-dev alias if the default branch has no branch-alias set
        if ($package instanceof AliasPackage && $package->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) {
            $package = $package->getAliasOf();
        }
        // select an exact match if it is in the installed repo and no specific version was required
        if (null === $version && $installedRepo->hasPackage($package)) {
            $matchedPackage = $package;
        }
        $versions[$package->getPrettyVersion()] = $package->getVersion();
        $literals[] = $package->getId();
    }
    // select preferred package according to policy rules
    if (null === $matchedPackage && \count($literals) > 0) {
        $preferred = $policy->selectPreferredPackages($pool, $literals);
        $matchedPackage = $pool->literalToPackage($preferred[0]);
    }
    if ($matchedPackage !== null && !$matchedPackage instanceof CompletePackageInterface) {
        throw new \LogicException('ShowCommand::getPackage can only work with CompletePackageInterface, but got ' . get_class($matchedPackage));
    }
    return [
        $matchedPackage,
        $versions,
    ];
}

API Navigation

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