You are here

class Composer in Drupal 9

Same name in this branch
  1. 9 composer/Composer.php \Drupal\Composer\Composer
  2. 9 core/lib/Drupal/Core/Composer/Composer.php \Drupal\Core\Composer\Composer
Same name and namespace in other branches
  1. 8 composer/Composer.php \Drupal\Composer\Composer

Provides static functions for composer script events. See also core/lib/Drupal/Composer/Composer.php, which contains similar scripts needed by projects that include drupal/core. Scripts that are only needed by drupal/drupal go here.

Hierarchy

Expanded class hierarchy of Composer

See also

https://getcomposer.org/doc/articles/scripts.md

5 files declare their use of Composer
BuilderTest.php in core/tests/Drupal/Tests/Composer/Generator/BuilderTest.php
ComposerProjectTemplatesTest.php in core/tests/Drupal/BuildTests/Composer/Template/ComposerProjectTemplatesTest.php
ComposerTest.php in core/tests/Drupal/Tests/Composer/ComposerTest.php
DrupalCoreRecommendedBuilder.php in composer/Generator/Builder/DrupalCoreRecommendedBuilder.php
DrupalPinnedDevDependenciesBuilder.php in composer/Generator/Builder/DrupalPinnedDevDependenciesBuilder.php

File

composer/Composer.php, line 20

Namespace

Drupal\Composer
View source
class Composer {

  /**
   * Update metapackages whenever composer.lock is updated.
   *
   * @param \Composer\Script\Event $event
   *   The Composer event.
   */
  public static function generateMetapackages(Event $event) : void {
    $generator = new PackageGenerator();
    $generator
      ->generate($event
      ->getIO(), getcwd());
  }

  /**
   * Set the version of Drupal; used in release process and by the test suite.
   *
   * @param string $root
   *   Path to root of drupal/drupal repository.
   * @param string $version
   *   Semver version to set Drupal's version to.
   *
   * @return string
   *   Stability level of the provided version (stable, RC, alpha, etc.)
   *
   * @throws \UnexpectedValueException
   */
  public static function setDrupalVersion(string $root, string $version) : void {

    // We use VersionParser::normalize to validate that $version is valid.
    // It will throw an exception if it is not.
    $versionParser = new VersionParser();
    $versionParser
      ->normalize($version);

    // Rewrite Drupal.php with the provided version string.
    $drupal_static_path = "{$root}/core/lib/Drupal.php";
    $drupal_static_source = file_get_contents($drupal_static_path);
    $drupal_static_source = preg_replace('#const VERSION = [^;]*#', "const VERSION = '{$version}'", $drupal_static_source);
    file_put_contents($drupal_static_path, $drupal_static_source);

    // Update the template project stability to match the version we set.
    static::setTemplateProjectStability($root, $version);
  }

  /**
   * Set the stability of the template projects to match the Drupal version.
   *
   * @param string $root
   *   Path to root of drupal/drupal repository.
   * @param string $version
   *   Semver version that Drupal was set to.
   *
   * @return string
   *   Stability level of the provided version (stable, RC, alpha, etc.)
   */
  protected static function setTemplateProjectStability(string $root, string $version) : void {
    $stability = VersionParser::parseStability($version);
    $templateProjectPaths = static::composerSubprojectPaths($root, 'Template');
    foreach ($templateProjectPaths as $path) {
      $dir = dirname($path);
      exec("composer --working-dir={$dir} config minimum-stability {$stability}", $output, $status);
      if ($status) {
        throw new \Exception('Could not set minimum-stability for template project ' . basename($dir));
      }
    }
  }

  /**
   * Ensure that the minimum required version of Composer is running.
   * Throw an exception if Composer is too old.
   */
  public static function ensureComposerVersion() : void {
    $composerVersion = method_exists(ComposerApp::class, 'getVersion') ? ComposerApp::getVersion() : ComposerApp::VERSION;
    if (Comparator::lessThan($composerVersion, '1.9.0')) {
      throw new \RuntimeException("Drupal core development requires Composer 1.9.0, but Composer {$composerVersion} is installed. Please run 'composer self-update'.");
    }
  }

  /**
   * Return the branch name the current Drupal version is associated with.
   *
   * @return string
   *   A branch name, e.g. 8.9.x or 9.0.x.
   */
  public static function drupalVersionBranch() : string {
    return preg_replace('#\\.[0-9]+-dev#', '.x-dev', \Drupal::VERSION);
  }

  /**
   * Return the list of subprojects of a given type.
   *
   * @param string $root
   *   Path to root of drupal/drupal repository.
   * @param string $subprojectType
   *   Type of subproject - one of Metapackage, Plugin, or Template
   *
   * @return \Symfony\Component\Finder\Finder
   */
  public static function composerSubprojectPaths(string $root, string $subprojectType) : Finder {
    return Finder::create()
      ->files()
      ->name('composer.json')
      ->in("{$root}/composer/{$subprojectType}");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Composer::composerSubprojectPaths public static function Return the list of subprojects of a given type.
Composer::drupalVersionBranch public static function Return the branch name the current Drupal version is associated with.
Composer::ensureComposerVersion public static function Ensure that the minimum required version of Composer is running. Throw an exception if Composer is too old.
Composer::generateMetapackages public static function Update metapackages whenever composer.lock is updated.
Composer::setDrupalVersion public static function Set the version of Drupal; used in release process and by the test suite.
Composer::setTemplateProjectStability protected static function Set the stability of the template projects to match the Drupal version.