function _pm_find_common_path

4.x pm.drush.inc _pm_find_common_path($project_type, $extensions)

Helper function to find the common path for a list of extensions in the aim to obtain the project name.

Parameters

$project_type: Type of project we're trying to find. Valid values: module, theme.

$extensions: Array of extension names.

1 call to _pm_find_common_path()
_pm_get_project_path in commands/pm/pm.drush.inc
Completes projects' update data with the path to install location on disk.

File

commands/pm/pm.drush.inc, line 1130
The drush Project Manager

Code

function _pm_find_common_path($project_type, $extensions) {
  // Select the first path as the candidate to be the common prefix.
  $path = drupal_get_path($project_type, array_pop($extensions));
  // If there's only one extension we are done. Otherwise, we need to find
  // the common prefix for all of them.
  if (count($extensions) > 0) {
    // Iterate over the other projects.
    while ($project = array_pop($extensions)) {
      $path2 = drupal_get_path($project_type, $project);
      // Option 1: same path.
      if ($path == $path2) {
        continue;
      }
      // Option 2: $path is a prefix of $path2.
      if (strpos($path2, $path) === 0) {
        continue;
      }
      // Option 3: $path2 is a prefix of $path.
      if (strpos($path, $path2) === 0) {
        $path = $path2;
        continue;
      }
      // Option 4: no one is a prefix of the other. Find the common
      // prefix by iteratively strip the rigthtmost piece of $path.
      // We will iterate until a prefix is found or path = '.', that on the
      // other hand is a condition theorically impossible to reach.
      do {
        $path = dirname($path);
        if (strpos($path2, $path) === 0) {
          break;
        }
      } while ($path != '.');
    }
  }

  return $path;
}