function composer_manager_build_json in Composer Manager 7.2
Same name and namespace in other branches
- 6.2 composer_manager.writer.inc \composer_manager_build_json()
- 6 composer_manager.writer.inc \composer_manager_build_json()
- 7 composer_manager.writer.inc \composer_manager_build_json()
Builds the JSON array containing 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 composer.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(
'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;
}
// Retrieve JSON map.
static $json_map;
if (!isset($json_map)) {
$cid = 'composer_manager:json_map';
if (($cache = cache_get($cid)) && isset($cache->data) && is_array($cache->data)) {
$json_map = $cache->data;
}
else {
$default_map = array(
'properties' => array(),
'relative_paths' => array(
'keys' => array(),
'values' => array(),
),
);
$json_map = array(
'properties' => array(
'autoload',
'autoload-dev',
'config',
'conflict',
'provide',
'prefer-stable',
'replace',
'repositories',
'require',
'require-dev',
'suggest',
// Only support the following "extra" properties.
// Installers (https://github.com/composer/installers).
array(
'extra',
'installer-paths',
),
// Patches (https://github.com/cweagans/composer-patches).
array(
'extra',
'patches',
),
array(
'extra',
'patches-ignore',
),
),
'relative_paths' => array(
'keys' => array(
array(
'extra',
'installer-paths',
),
),
'values' => array(
'autoload',
'autoload-dev',
array(
'extra',
'patches',
),
array(
'extra',
'patches-ignore',
),
),
),
);
// Allow modules to alter JSON map.
drupal_alter('composer_json_map', $json_map);
// Ensure JSON map has default keys so we don't have to do isset() checks.
$json_map = drupal_array_merge_deep($default_map, $json_map);
// Cache JSON map.
cache_set($cid, $json_map);
}
}
// Iterate over each module's JSON.
foreach ($data as $module => $json) {
// Merge in mapped JSON properties from the module.
foreach ($json_map['properties'] as $parents) {
$parents = (array) $parents;
// Retrieve value from the module's JSON and skip if it doesn't exist.
$value = drupal_array_get_nested_value($json, $parents, $key_exists);
if (!$key_exists) {
continue;
}
// Retrieve the existing data.
$existing = drupal_array_get_nested_value($combined, $parents, $key_exists);
// If existing value is an array, type cast module data and merge it in.
if (isset($existing) && is_array($existing)) {
$value = drupal_array_merge_deep($existing, (array) $value);
}
// Set the value.
drupal_array_set_nested_value($combined, $parents, $value, TRUE);
}
// Fix property keys and values that contain relative paths.
foreach ($json_map['relative_paths'] as $type => $data) {
foreach ($data as $parents) {
$parents = (array) $parents;
$property =& drupal_array_get_nested_value($combined, $parents, $key_exists);
if (!$key_exists || !is_array($property)) {
continue;
}
composer_manager_relative_json_property($property, array(
'keys' => $type === 'keys',
'paths' => array(
DRUPAL_ROOT . '/' . drupal_get_path('module', $module),
),
));
}
}
// Take the lowest stability.
if (isset($json['minimum-stability'])) {
if (!isset($combined['minimum-stability']) || composer_manager_compare_stability($json['minimum-stability'], $combined['minimum-stability']) === -1) {
$combined['minimum-stability'] = $json['minimum-stability'];
}
}
}
// Allow extensions to alter the composer JSON.
drupal_alter('composer_json', $combined);
return $combined;
}