Composer.php in Drupal 9
File
composer/Composer.php
View source
<?php
namespace Drupal\Composer;
use Composer\Composer as ComposerApp;
use Composer\Script\Event;
use Composer\Semver\Comparator;
use Composer\Semver\VersionParser;
use Drupal\Composer\Generator\PackageGenerator;
use Symfony\Component\Finder\Finder;
class Composer {
public static function generateMetapackages(Event $event) : void {
$generator = new PackageGenerator();
$generator
->generate($event
->getIO(), getcwd());
}
public static function setDrupalVersion(string $root, string $version) : void {
$versionParser = new VersionParser();
$versionParser
->normalize($version);
$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);
static::setTemplateProjectStability($root, $version);
}
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));
}
}
}
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'.");
}
}
public static function drupalVersionBranch() : string {
return preg_replace('#\\.[0-9]+-dev#', '.x-dev', \Drupal::VERSION);
}
public static function composerSubprojectPaths(string $root, string $subprojectType) : Finder {
return Finder::create()
->files()
->name('composer.json')
->in("{$root}/composer/{$subprojectType}");
}
}
Classes
Name |
Description |
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. |