function ComposerInspector::getConfig

Returns a config value from Composer.

Parameters

string $key: The config key to get.

string $context: The path of either the directory in which to run Composer, or a specific configuration file (such as a particular package's `composer.json`) from which to read specific values.

Return value

string|null The output data. Note that the caller must know the shape of the requested key's value: if it's a string, no further processing is needed, but if it is a boolean, an array or a map, JSON decoding should be applied.

See also

::getAllowPluginsConfig()

\Composer\Command\ConfigCommand::execute()

File

core/modules/package_manager/src/ComposerInspector.php, line 227

Class

ComposerInspector
Defines a class to get information from Composer.

Namespace

Drupal\package_manager

Code

public function getConfig(string $key, string $context) : ?string {
    $this->validateExecutable();
    $command = [
        'config',
        $key,
    ];
    // If we're consulting a specific file for the config value, we don't need
    // to validate the project as a whole.
    if (is_file($context)) {
        $command[] = "--file={$context}";
    }
    else {
        $this->validateProject($context);
        $command[] = "--working-dir={$context}";
    }
    try {
        $this->runner
            ->run($command, callback: $this->processCallback
            ->reset());
    } catch (RuntimeException $e) {
        // Assume any error from `composer config` is about an undefined key-value
        // pair which may have a known default value.
        return match ($key) {    'extra' => '{}',
            default => throw $e,
        
        };
    }
    $output = $this->processCallback
        ->getOutput();
    return $output ? trim(implode('', $output)) : NULL;
}

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