You are here

function composer_manager_build_json in Composer Manager 6

Same name and namespace in other branches
  1. 6.2 composer_manager.writer.inc \composer_manager_build_json()
  2. 7.2 composer_manager.writer.inc \composer_manager_build_json()
  3. 7 composer_manager.writer.inc \composer_manager_build_json()

Builds the JSON array ccontaining the combined requirements of each module's composer.json file.

Parameters

array $data: An array of JSON arrays parsed from composer.json files keyed by the module that defines it. This is usually the return value of the composer_manager_fetch_data() function.

Return value

array The consolidated JSON array that will be written to a compsoer.json file.

Throws

\RuntimeException

1 call to composer_manager_build_json()
composer_manager_write_file in ./composer_manager.module
Writes the consolidated composer.json file for all modules that require third-party packages managed by Composer.

File

./composer_manager.writer.inc, line 54
Functions related to the creation of the consolidated composer.json file.

Code

function composer_manager_build_json(array $data) {
  $combined = array();
  foreach ($data as $module => $json) {
    if (!$combined) {
      $combined = array(
        'require' => array(),
        'config' => array(
          'autoloader-suffix' => 'ComposerManager',
        ),
      );
      $vendor_dir = composer_manager_relative_vendor_dir();
      if (0 !== strlen($vendor_dir) && 'vendor' != $vendor_dir) {
        $combined['config']['vendor-dir'] = $vendor_dir;
      }
    }

    // @todo Detect duplicates, maybe add an "ignore" list. Figure out if this
    // encompases all keys that should be merged.
    $to_merge = array(
      'require',
      'require-dev',
      'conflict',
      'replace',
      'provide',
      'suggest',
      'repositories',
    );
    foreach ($to_merge as $key) {
      if (isset($json[$key])) {
        if (isset($combined[$key]) && is_array($combined[$key])) {
          $combined[$key] = array_merge($combined[$key], $json[$key]);
        }
        else {
          $combined[$key] = $json[$key];
        }
      }
    }
    $autoload_options = array(
      'psr-0',
      'psr-4',
    );
    foreach ($autoload_options as $option) {
      if (isset($json['autoload'][$option])) {
        $namespaces = (array) $json['autoload'][$option];
        foreach ($json['autoload'][$option] as $namesapce => $dirs) {
          $dirs = (array) $dirs;
          array_walk($dirs, 'composer_manager_relative_autoload_path', $module);
          if (!isset($combined['autoload'][$option][$namesapce])) {
            $combined['autoload'][$option][$namesapce] = array();
          }
          $combined['autoload'][$option][$namesapce] = array_merge($combined['autoload'][$option][$namesapce], $dirs);
        }
      }
    }

    // Merge in the "classmap" and "files" autoload options.
    $autoload_options = array(
      'classmap',
      'files',
    );
    foreach ($autoload_options as $option) {
      if (isset($json['autoload'][$option])) {
        $dirs = (array) $json['autoload'][$option];
        array_walk($dirs, 'composer_manager_relative_autoload_path', $module);
        if (!isset($combined['autoload'][$option])) {
          $combined['autoload'][$option] = array();
        }
        $combined['autoload'][$option] = array_merge($combined['autoload'][$option], $dirs);
      }
    }

    // Take the lowest stability.
    if (isset($json['minimum-stability'])) {
      if (!isset($combined['minimum-stability']) || -1 == composer_manager_compare_stability($json['minimum-stability'], $combined['minimum-stability'])) {
        $combined['minimum-stability'] = $json['minimum-stability'];
      }
    }
  }
  drupal_alter('composer_json', $combined);
  return $combined;
}