function Unpacker::updateComposerJsonPackages

Updates the composer.json content with the package being unpacked.

This method will add all the package dependencies to the root composer.json content and also remove the package itself from the root composer.json.

Throws

\RuntimeException If the composer.json could not be updated.

1 call to Unpacker::updateComposerJsonPackages()
Unpacker::unpackDependencies in composer/Plugin/RecipeUnpack/Unpacker.php
Unpacks the package's dependencies to the root composer.json and lock file.

File

composer/Plugin/RecipeUnpack/Unpacker.php, line 102

Class

Unpacker
Handles the details of unpacking a specific recipe.

Namespace

Drupal\Composer\Plugin\RecipeUnpack

Code

private function updateComposerJsonPackages() : void {
    $composer_manipulator = $this->rootComposer
        ->getComposerManipulator();
    $composer_config = $this->composer
        ->getConfig();
    $sort_packages = $composer_config->get('sort-packages');
    $root_package = $this->composer
        ->getPackage();
    $root_requires = $root_package->getRequires();
    $root_dev_requires = $root_package->getDevRequires();
    foreach ($this->processPackageDependencies($this->package
        ->getRequires()) as $package_dependency) {
        $dependency_name = $package_dependency->getTarget();
        $recipe_constraint_string = $package_dependency->getPrettyConstraint();
        if (isset($root_requires[$dependency_name])) {
            $recipe_constraint_string = SemVer::minimizeConstraints($this->versionParser, $recipe_constraint_string, $root_requires[$dependency_name]->getPrettyConstraint());
            if ($recipe_constraint_string === $root_requires[$dependency_name]) {
                // This dependency is already in the required section with the
                // correct constraint.
                continue;
            }
        }
        elseif (isset($root_dev_requires[$dependency_name])) {
            $recipe_constraint_string = SemVer::minimizeConstraints($this->versionParser, $recipe_constraint_string, $root_dev_requires[$dependency_name]->getPrettyConstraint());
            // This dependency is already in the require-dev section. We will
            // move it to the require section.
            $composer_manipulator->removeSubNode('require-dev', $dependency_name);
        }
        // Add the dependency to the required section. If it cannot be added, then
        // throw an exception.
        if (!$composer_manipulator->addLink('require', $dependency_name, $recipe_constraint_string, $sort_packages)) {
            throw new \RuntimeException(sprintf('Unable to manipulate composer.json during the unpack of %s', $dependency_name));
        }
        $link = new Link($root_package->getName(), $dependency_name, $this->versionParser
            ->parseConstraints($recipe_constraint_string), Link::TYPE_REQUIRE, $recipe_constraint_string);
        $root_requires[$dependency_name] = $link;
        unset($root_dev_requires[$dependency_name]);
        $this->io
            ->write(sprintf('Adding <info>%s</info> (<comment>%s</comment>) to composer.json during the unpack of <info>%s</info>', $dependency_name, $recipe_constraint_string, $this->package
            ->getName()), verbosity: IOInterface::VERBOSE);
    }
    // Ensure the written packages are no longer in the dev package names.
    $local_repo = $this->composer
        ->getRepositoryManager()
        ->getLocalRepository();
    $local_repo->setDevPackageNames(array_diff($local_repo->getDevPackageNames(), array_keys($root_requires)));
    // Update the root package to reflect the changes.
    $root_package->setDevRequires($root_dev_requires);
    $root_package->setRequires($root_requires);
    $composer_manipulator->removeSubNode(UnpackManager::isDevRequirement($this->package) ? 'require-dev' : 'require', $this->package
        ->getName());
    $this->io
        ->write(sprintf('Removing <info>%s</info> from composer.json', $this->package
        ->getName()), verbosity: IOInterface::VERBOSE);
    $composer_manipulator->removeMainKeyIfEmpty('require-dev');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.