function ComposerInspector::show

Gets the installed packages data from running `composer show`.

Parameters

string $working_dir: The directory in which to run `composer show`.

Return value

array[] The installed packages data, keyed by package name.

1 call to ComposerInspector::show()
ComposerInspector::getInstalledPackagesList in core/modules/package_manager/src/ComposerInspector.php
Returns the installed packages list.

File

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

Class

ComposerInspector
Defines a class to get information from Composer.

Namespace

Drupal\package_manager

Code

protected function show(string $working_dir) : array {
  $data = [];
  $options = [
    'show',
    '--format=json',
    "--working-dir={$working_dir}",
  ];
  // We don't get package installation paths back from `composer show` unless
  // we explicitly pass the --path option to it. However, for some
  // inexplicable reason, that option hides *other* relevant information
  // about the installed packages. So, to work around this maddening quirk, we
  // call `composer show` once without the --path option, and once with it,
  // then merge the results together. Composer, for its part, will not support
  // returning the install path from `composer show`: see
  // https://github.com/composer/composer/pull/11340.
  $this->runner
    ->run($options, callback: $this->processCallback
    ->reset());
  $output = $this->processCallback
    ->parseJsonOutput();
  // $output['installed'] will not be set if no packages are installed.
  if (isset($output['installed'])) {
    foreach ($output['installed'] as $installed_package) {
      $data[$installed_package['name']] = $installed_package;
    }
    $options[] = '--path';
    $this->runner
      ->run($options, callback: $this->processCallback
      ->reset());
    $output = $this->processCallback
      ->parseJsonOutput();
    foreach ($output['installed'] as $installed_package) {
      $data[$installed_package['name']]['path'] = $installed_package['path'];
    }
  }
  return $data;
}

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