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

Breadcrumb

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

function ComposerRepository::whatProvides

@phpstan-param array<key-of<BasePackage::STABILITIES>, BasePackage::STABILITY_*>|null $acceptableStabilities

@phpstan-param array<string, BasePackage::STABILITY_*>|null $stabilityFlags

Parameters

string $name package name:

array<string, int>|null $acceptableStabilities:

array<string, int>|null $stabilityFlags an array of package name => BasePackage::STABILITY_* value:

array<string, array<string, PackageInterface>> $alreadyLoaded:

Return value

array<string, BasePackage>

3 calls to ComposerRepository::whatProvides()
ComposerRepository::findPackage in vendor/composer/composer/src/Composer/Repository/ComposerRepository.php
@inheritDoc
ComposerRepository::findPackages in vendor/composer/composer/src/Composer/Repository/ComposerRepository.php
@inheritDoc
ComposerRepository::loadPackages in vendor/composer/composer/src/Composer/Repository/ComposerRepository.php
@inheritDoc

File

vendor/composer/composer/src/Composer/Repository/ComposerRepository.php, line 837

Class

ComposerRepository
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Repository

Code

private function whatProvides(string $name, ?array $acceptableStabilities = null, ?array $stabilityFlags = null, array $alreadyLoaded = []) : array {
    $packagesSource = null;
    if (!$this->hasPartialPackages() || !isset($this->partialPackagesByName[$name])) {
        // skip platform packages, root package and composer-plugin-api
        if (PlatformRepository::isPlatformPackage($name) || '__root__' === $name) {
            return [];
        }
        if (null === $this->providerListing) {
            $data = $this->loadRootServerFile();
            if (is_array($data)) {
                $this->loadProviderListings($data);
            }
        }
        $useLastModifiedCheck = false;
        if ($this->lazyProvidersUrl && !isset($this->providerListing[$name])) {
            $hash = null;
            $url = str_replace('%package%', $name, $this->lazyProvidersUrl);
            $cacheKey = 'provider-' . strtr($name, '/', '$') . '.json';
            $useLastModifiedCheck = true;
        }
        elseif ($this->providersUrl) {
            // package does not exist in this repo
            if (!isset($this->providerListing[$name])) {
                return [];
            }
            $hash = $this->providerListing[$name]['sha256'];
            $url = str_replace([
                '%package%',
                '%hash%',
            ], [
                $name,
                $hash,
            ], $this->providersUrl);
            $cacheKey = 'provider-' . strtr($name, '/', '$') . '.json';
        }
        else {
            return [];
        }
        $packages = null;
        if (!$useLastModifiedCheck && $hash && $this->cache
            ->sha256($cacheKey) === $hash) {
            $packages = json_decode($this->cache
                ->read($cacheKey), true);
            $packagesSource = 'cached file (' . $cacheKey . ' originating from ' . Url::sanitize($url) . ')';
        }
        elseif ($useLastModifiedCheck) {
            if ($contents = $this->cache
                ->read($cacheKey)) {
                $contents = json_decode($contents, true);
                // we already loaded some packages from this file, so assume it is fresh and avoid fetching it again
                if (isset($alreadyLoaded[$name])) {
                    $packages = $contents;
                    $packagesSource = 'cached file (' . $cacheKey . ' originating from ' . Url::sanitize($url) . ')';
                }
                elseif (isset($contents['last-modified'])) {
                    $response = $this->fetchFileIfLastModified($url, $cacheKey, $contents['last-modified']);
                    $packages = true === $response ? $contents : $response;
                    $packagesSource = true === $response ? 'cached file (' . $cacheKey . ' originating from ' . Url::sanitize($url) . ')' : 'downloaded file (' . Url::sanitize($url) . ')';
                }
            }
        }
        if (!$packages) {
            try {
                $packages = $this->fetchFile($url, $cacheKey, $hash, $useLastModifiedCheck);
                $packagesSource = 'downloaded file (' . Url::sanitize($url) . ')';
            } catch (TransportException $e) {
                // 404s are acceptable for lazy provider repos
                if ($this->lazyProvidersUrl && in_array($e->getStatusCode(), [
                    404,
                    499,
                ], true)) {
                    $packages = [
                        'packages' => [],
                    ];
                    $packagesSource = 'not-found file (' . Url::sanitize($url) . ')';
                    if ($e->getStatusCode() === 499) {
                        $this->io
                            ->error('<warning>' . $e->getMessage() . '</warning>');
                    }
                }
                else {
                    throw $e;
                }
            }
        }
        $loadingPartialPackage = false;
    }
    else {
        $packages = [
            'packages' => [
                'versions' => $this->partialPackagesByName[$name],
            ],
        ];
        $packagesSource = 'root file (' . Url::sanitize($this->getPackagesJsonUrl()) . ')';
        $loadingPartialPackage = true;
    }
    $result = [];
    $versionsToLoad = [];
    foreach ($packages['packages'] as $versions) {
        foreach ($versions as $version) {
            $normalizedName = strtolower($version['name']);
            // only load the actual named package, not other packages that might find themselves in the same file
            if ($normalizedName !== $name) {
                continue;
            }
            if (!$loadingPartialPackage && $this->hasPartialPackages() && isset($this->partialPackagesByName[$normalizedName])) {
                continue;
            }
            if (!isset($versionsToLoad[$version['uid']])) {
                if (!isset($version['version_normalized'])) {
                    $version['version_normalized'] = $this->versionParser
                        ->normalize($version['version']);
                }
                elseif ($version['version_normalized'] === VersionParser::DEFAULT_BRANCH_ALIAS) {
                    // handling of existing repos which need to remain composer v1 compatible, in case the version_normalized contained VersionParser::DEFAULT_BRANCH_ALIAS, we renormalize it
                    $version['version_normalized'] = $this->versionParser
                        ->normalize($version['version']);
                }
                // avoid loading packages which have already been loaded
                if (isset($alreadyLoaded[$name][$version['version_normalized']])) {
                    continue;
                }
                if ($this->isVersionAcceptable(null, $normalizedName, $version, $acceptableStabilities, $stabilityFlags)) {
                    $versionsToLoad[$version['uid']] = $version;
                }
            }
        }
    }
    // load acceptable packages in the providers
    $loadedPackages = $this->createPackages($versionsToLoad, $packagesSource);
    $uids = array_keys($versionsToLoad);
    foreach ($loadedPackages as $index => $package) {
        $package->setRepository($this);
        $uid = $uids[$index];
        if ($package instanceof AliasPackage) {
            $aliased = $package->getAliasOf();
            $aliased->setRepository($this);
            $result[$uid] = $aliased;
            $result[$uid . '-alias'] = $package;
        }
        else {
            $result[$uid] = $package;
        }
    }
    return $result;
}
RSS feed
Powered by Drupal