function ComponentGenerator::getPackage

Same name and namespace in other branches
  1. 9 composer/Generator/ComponentGenerator.php \Drupal\Composer\Generator\ComponentGenerator::getPackage()
  2. 11.x composer/Generator/ComponentGenerator.php \Drupal\Composer\Generator\ComponentGenerator::getPackage()

Reconcile component dependencies with core.

Parameters

\Composer\IO\IOInterface $io: IO object for messages to the user.

string $original_json: Contents of the component's composer.json file.

Return value

array Structured data to be turned back into JSON.

1 call to ComponentGenerator::getPackage()
ComponentGenerator::generateComponentPackage in composer/Generator/ComponentGenerator.php
Generate the component JSON files.

File

composer/Generator/ComponentGenerator.php, line 145

Class

ComponentGenerator
Reconciles Drupal component dependencies with core.

Namespace

Drupal\Composer\Generator

Code

protected function getPackage(IOInterface $io, string $original_json) : array {
  $original_data = json_decode($original_json, TRUE);
  $package_data = array_merge($original_data, $this->initialPackageMetadata());
  $core_info = $this->drupalCoreInfo
    ->rootComposerJson();
  $stability = VersionParser::parseStability(\Drupal::VERSION);
  // List of packages which we didn't find in either core requirement.
  $not_in_core = [];
  // Traverse required packages.
  foreach (array_keys($original_data['require'] ?? []) as $package_name) {
    // Reconcile locked constraints from drupal/drupal. We might have a locked
    // version of a dependency that's not present in drupal/core.
    if ($info = $this->drupalProjectInfo
      ->packageLockInfo($package_name)) {
      $package_data['require'][$package_name] = $info['version'];
    }
    elseif ($package_name !== 'php' && !str_contains($package_name, 'drupal/core-')) {
      $not_in_core[$package_name] = $package_name;
    }
    // Reconcile looser constraints from drupal/core, and we're totally OK
    // with over-writing the locked ones from above.
    if ($constraint = $core_info['require'][$package_name] ?? FALSE) {
      $package_data['require'][$package_name] = $constraint;
    }
    // Reconcile dependencies on other Drupal components, so we can set the
    // constraint to our current version.
    if (str_contains($package_name, 'drupal/core-')) {
      if ($stability === 'stable') {
        // Set the constraint to ^maj.min.
        $package_data['require'][$package_name] = SemanticVersion::majorMinorConstraint(\Drupal::VERSION);
      }
      else {
        // For non-stable releases, set the constraint to the branch version.
        $package_data['require'][$package_name] = Composer::drupalVersionBranch();
        // Also for non-stable releases which depend on another component,
        // set the minimum stability. We do this so we can test build the
        // components. Minimum-stability is otherwise ignored for packages
        // which aren't the root package, so for any other purpose, this is
        // unneeded.
        $package_data['minimum-stability'] = $stability;
      }
    }
  }
  if ($not_in_core) {
    $io->error($package_data['name'] . ' requires packages not present in drupal/drupal: ' . implode(', ', $not_in_core));
  }
  return $package_data;
}

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