You are here

function composer_manager_build_json in Composer Manager 7

Same name and namespace in other branches
  1. 6.2 composer_manager.writer.inc \composer_manager_build_json()
  2. 6 composer_manager.writer.inc \composer_manager_build_json()
  3. 7.2 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',
        ),
        'prefer-stable' => TRUE,
      );
      $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',
      'prefer-stable',
      'config',
    );
    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_blocks = array(
      'autoload',
      'autoload-dev',
    );
    foreach ($autoload_blocks as $autoload_block) {
      $autoload_options = array(
        'psr-0',
        'psr-4',
      );
      foreach ($autoload_options as $option) {
        if (isset($json[$autoload_block][$option])) {
          $namespaces = (array) $json[$autoload_block][$option];
          foreach ($json[$autoload_block][$option] as $namespace => $dirs) {
            $dirs = (array) $dirs;
            array_walk($dirs, 'composer_manager_relative_autoload_path', $module);
            if (!isset($combined[$autoload_block][$option][$namespace])) {
              $combined[$autoload_block][$option][$namespace] = array();
            }
            $combined[$autoload_block][$option][$namespace] = array_merge($combined[$autoload_block][$option][$namespace], $dirs);
          }
        }
      }

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