function composer_manager_relative_json_property in Composer Manager 7.2
Helper function for converting value into a relative path.
Parameters
mixed $value: The value to change, passed by reference.
$options: An associative array of additional options, with the following properties:
- 'depth': Maximum depth of recursion.
- 'keys': (bool) Flag indicating whether to iterate over the keys of $value if it's an array instead of its values.
- 'paths': (string[]) An array of path prefixes to prepend to $value to check for a valid path.
- 'recurse': When TRUE, the directory scan will recurse the entire tree starting at the provided directory. Defaults to TRUE.
See also
2 calls to composer_manager_relative_json_property()
- composer_manager_build_json in ./
composer_manager.writer.inc - Builds the JSON array containing the combined requirements of each module's composer.json file.
- composer_manager_relative_autoload_path in ./
composer_manager.writer.inc - Returns the path for the autoloaded directory or class relative to the directory containing the composer.json file.
File
- ./
composer_manager.writer.inc, line 303 - Functions related to the creation of the consolidated composer.json file.
Code
function composer_manager_relative_json_property(&$value, array $options = array()) {
static $composer_dir;
if (!isset($composer_dir)) {
$composer_dir = composer_manager_file_dir();
}
static $vendor_dir;
if (!isset($vendor_dir)) {
$vendor_dir = composer_manager_relative_dir(composer_manager_vendor_dir(), $composer_dir);
}
static $drupal_root;
if (!isset($drupal_root)) {
$drupal_root = composer_manager_relative_dir(DRUPAL_ROOT, $composer_dir);
}
$options += array(
'depth' => 25,
'keys' => FALSE,
'paths' => array(),
'recurse' => TRUE,
);
// Recurse through array.
if ($options['recurse'] && $options['depth'] && is_array($value)) {
$options['depth']--;
$new_value = array();
foreach ($value as $k => &$v) {
if ($options['keys']) {
composer_manager_relative_json_property($k, $options);
}
else {
composer_manager_relative_json_property($v, $options);
}
$new_value[$k] = $v;
}
$value = $new_value;
}
// Immediately return if value is not a string.
if (!is_string($value)) {
return;
}
// Allow and replace a COMPOSER_DIR constant.
if (strpos($value, 'COMPOSER_DIR') === 0) {
$value = preg_replace('/\\/+/', '/', str_replace('COMPOSER_DIR/', './', $value));
return;
}
// Allow and replace a COMPOSER_VENDOR_DIR constant.
if (strpos($value, 'COMPOSER_VENDOR_DIR') === 0) {
$value = preg_replace('/\\/+/', '/', str_replace('COMPOSER_VENDOR_DIR/', "{$vendor_dir}/", $value));
return;
}
// Allow and replace a DRUPAL_ROOT constant.
if (strpos($value, 'DRUPAL_ROOT') === 0) {
$value = preg_replace('/\\/+/', '/', str_replace('DRUPAL_ROOT', "{$drupal_root}/", $value));
return;
}
// Attempt to retrieve an absolute path using the prefixed path and the value.
$absolute = FALSE;
foreach ($options['paths'] as $path) {
$absolute = drupal_realpath("{$path}/{$value}");
if ($absolute) {
break;
}
}
// If there still is no valid "to", attempt with just using the value.
if (!$absolute) {
$absolute = drupal_realpath($value);
}
// If an absolute path was ascertained, change value to a relative path.
if ($absolute) {
$value = composer_manager_relative_dir($absolute, $composer_dir);
}
}