function RootComposer::writeFiles

Writes the root composer files.

The files written are:

  • composer.json
  • composer.lock
  • vendor/composer/installed.json
  • vendor/composer/installed.php

Throws

\RuntimeException If the root composer could not be updated.

File

composer/Plugin/RecipeUnpack/RootComposer.php, line 95

Class

RootComposer
Provides access to and manipulation of the root composer files.

Namespace

Drupal\Composer\Plugin\RecipeUnpack

Code

public function writeFiles() : void {
  // Write composer.json.
  $composer_json = Factory::getComposerFile();
  $composer_content = $this->getComposerManipulator()
    ->getContents();
  if (!file_put_contents($composer_json, $composer_content)) {
    throw new \RuntimeException(sprintf('Could not update %s', $composer_json));
  }
  // Create package lists for lock file update.
  $local_repo = $this->composer
    ->getRepositoryManager()
    ->getLocalRepository();
  $packages = $dev_packages = [];
  $dev_package_names = $local_repo->getDevPackageNames();
  foreach ($local_repo->getPackages() as $package) {
    if (in_array($package->getName(), $dev_package_names, TRUE)) {
      $dev_packages[] = $package;
    }
    else {
      $packages[] = $package;
    }
  }
  $lock_file_path = Factory::getLockFile(Factory::getComposerFile());
  $lock_file = new JsonFile($lock_file_path, io: $this->io);
  $old_locker = $this->composer
    ->getLocker();
  $locker = new Locker($this->io, $lock_file, $this->composer
    ->getInstallationManager(), $composer_content);
  $composer_locker_content = $this->getComposerLockedContent();
  // Write the lock file.
  $locker->setLockData($packages, $dev_packages, $composer_locker_content['platform'], $composer_locker_content['platform-dev'], $composer_locker_content['aliases'], $old_locker->getMinimumStability(), $old_locker->getStabilityFlags(), $old_locker->getPreferStable(), $old_locker->getPreferLowest(), $old_locker->getPlatformOverrides());
  $this->composer
    ->setLocker($locker);
  // Update installed.json and installed.php.
  $local_repo->write($local_repo->getDevMode() ?? TRUE, $this->composer
    ->getInstallationManager());
  $this->io
    ->write("Unpacking has updated the root composer files.", verbosity: IOInterface::VERBOSE);
  assert(self::checkRootPackage($composer_content, $this->composer
    ->getPackage()), 'Composer root package and composer.json match');
}

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