function ComponentGenerator::generateComponentPackage

Same name in other branches
  1. 10 composer/Generator/ComponentGenerator.php \Drupal\Composer\Generator\ComponentGenerator::generateComponentPackage()
  2. 11.x composer/Generator/ComponentGenerator.php \Drupal\Composer\Generator\ComponentGenerator::generateComponentPackage()

Generate the component JSON files.

Parameters

\Composer\Script\Event $event: The Composer event.

string $component_pathname: Relative path to the composer.json file for a component.

Return value

bool TRUE if the generated component package is different from what is on disk.

1 call to ComponentGenerator::generateComponentPackage()
ComponentGenerator::generate in composer/Generator/ComponentGenerator.php
Reconcile Drupal's components whenever composer.lock is updated.

File

composer/Generator/ComponentGenerator.php, line 108

Class

ComponentGenerator
Reconciles Drupal component dependencies with core.

Namespace

Drupal\Composer\Generator

Code

protected function generateComponentPackage(Event $event, string $component_pathname) : bool {
    $io = $event->getIO();
    $composer_json_path = $this->componentBaseDir . '/' . $component_pathname;
    $original_composer_json = file_exists($composer_json_path) ? file_get_contents($composer_json_path) : '';
    // Modify the original data.
    $composer_json_data = $this->getPackage($io, $original_composer_json);
    $updated_composer_json = static::encode($composer_json_data);
    // Exit early if nothing changed.
    if (trim($original_composer_json, " \t\r\x00\v") === trim($updated_composer_json, " \t\r\x00\v")) {
        return FALSE;
    }
    // Warn the user that a component file has been updated.
    $display_path = static::$relativeComponentPath . '/' . $component_pathname;
    $io->write("Updated component file <info>{$display_path}</info>.");
    // Write the composer.json file back to disk.
    $fs = new Filesystem();
    $fs->ensureDirectoryExists(dirname($composer_json_path));
    file_put_contents($composer_json_path, $updated_composer_json);
    return TRUE;
}

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