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

Breadcrumb

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

function RootPackageLoader::load

@inheritDoc

@phpstan-param class-string<RootPackage> $class

Return value

RootPackage|RootAliasPackage

Overrides ArrayLoader::load

File

vendor/composer/composer/src/Composer/Package/Loader/RootPackageLoader.php, line 74

Class

RootPackageLoader
ArrayLoader built for the sole purpose of loading the root package

Namespace

Composer\Package\Loader

Code

public function load(array $config, string $class = 'Composer\\Package\\RootPackage', ?string $cwd = null) : BasePackage {
    if ($class !== 'Composer\\Package\\RootPackage') {
        trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', E_USER_DEPRECATED);
    }
    if (!isset($config['name'])) {
        $config['name'] = '__root__';
    }
    elseif ($err = ValidatingArrayLoader::hasPackageNamingError($config['name'])) {
        throw new \RuntimeException('Your package name ' . $err);
    }
    $autoVersioned = false;
    if (!isset($config['version'])) {
        $commit = null;
        // override with env var if available
        if (Platform::getEnv('COMPOSER_ROOT_VERSION')) {
            $config['version'] = $this->versionGuesser
                ->getRootVersionFromEnv();
        }
        else {
            $versionData = $this->versionGuesser
                ->guessVersion($config, $cwd ?? Platform::getCwd(true));
            if ($versionData) {
                $config['version'] = $versionData['pretty_version'];
                $config['version_normalized'] = $versionData['version'];
                $commit = $versionData['commit'];
            }
        }
        if (!isset($config['version'])) {
            if ($this->io !== null && $config['name'] !== '__root__' && 'project' !== ($config['type'] ?? '')) {
                $this->io
                    ->warning(sprintf("Composer could not detect the root package (%s) version, defaulting to '1.0.0'. See https://getcomposer.org/root-version", $config['name']));
            }
            $config['version'] = '1.0.0';
            $autoVersioned = true;
        }
        if ($commit) {
            $config['source'] = [
                'type' => '',
                'url' => '',
                'reference' => $commit,
            ];
            $config['dist'] = [
                'type' => '',
                'url' => '',
                'reference' => $commit,
            ];
        }
    }
    
    /** @var RootPackage|RootAliasPackage $package */
    $package = parent::load($config, $class);
    if ($package instanceof RootAliasPackage) {
        $realPackage = $package->getAliasOf();
    }
    else {
        $realPackage = $package;
    }
    if (!$realPackage instanceof RootPackage) {
        throw new \LogicException('Expecting a Composer\\Package\\RootPackage at this point');
    }
    if ($autoVersioned) {
        $realPackage->replaceVersion($realPackage->getVersion(), RootPackage::DEFAULT_PRETTY_VERSION);
    }
    if (isset($config['minimum-stability'])) {
        $realPackage->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability']));
    }
    $aliases = [];
    $stabilityFlags = [];
    $references = [];
    foreach ([
        'require',
        'require-dev',
    ] as $linkType) {
        if (isset($config[$linkType])) {
            $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
            $method = 'get' . ucfirst($linkInfo['method']);
            $links = [];
            foreach ($realPackage->{$method}() as $link) {
                $links[$link->getTarget()] = $link->getConstraint()
                    ->getPrettyString();
            }
            $aliases = $this->extractAliases($links, $aliases);
            $stabilityFlags = self::extractStabilityFlags($links, $realPackage->getMinimumStability(), $stabilityFlags);
            $references = self::extractReferences($links, $references);
            if (isset($links[$config['name']])) {
                throw new \RuntimeException(sprintf('Root package \'%s\' cannot require itself in its composer.json' . PHP_EOL . 'Did you accidentally name your root package after an external package?', $config['name']));
            }
        }
    }
    foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) {
        if (isset($config[$linkType])) {
            foreach ($config[$linkType] as $linkName => $constraint) {
                if ($err = ValidatingArrayLoader::hasPackageNamingError($linkName, true)) {
                    throw new \RuntimeException($linkType . '.' . $err);
                }
            }
        }
    }
    $realPackage->setAliases($aliases);
    $realPackage->setStabilityFlags($stabilityFlags);
    $realPackage->setReferences($references);
    if (isset($config['prefer-stable'])) {
        $realPackage->setPreferStable((bool) $config['prefer-stable']);
    }
    if (isset($config['config'])) {
        $realPackage->setConfig($config['config']);
    }
    $repos = RepositoryFactory::defaultRepos(null, $this->config, $this->manager);
    foreach ($repos as $repo) {
        $this->manager
            ->addRepository($repo);
    }
    $realPackage->setRepositories($this->config
        ->getRepositories());
    return $package;
}
RSS feed
Powered by Drupal