function _drush_pm_find_common_path

8.0.x pm.drush.inc _drush_pm_find_common_path($project_type, $extensions)
6.x pm.drush.inc _drush_pm_find_common_path($project_type, $extensions)
7.x pm.drush.inc _drush_pm_find_common_path($project_type, $extensions)
5.x pm.drush.inc _drush_pm_find_common_path($project_type, $extensions)
master pm.drush.inc _drush_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.

Related topics

1 call to _drush_pm_find_common_path()
drush_get_projects in commands/pm/pm.drush.inc
Obtain an array of installed projects off the extensions available.

File

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

Code

function _drush_pm_find_common_path($project_type, $extensions) {
  // Select the first path as the candidate to be the common prefix.
  $extension = array_pop($extensions);
  while (!($path = drupal_get_path($project_type, $extension))) {
    drush_log(dt('Unknown path for !extension !type.', array('!extension' => $extension, '!type' => $project_type)), LogLevel::WARNING);
    $extension = 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 ($extension = array_pop($extensions)) {
      $path2 = drupal_get_path($project_type, $extension);
      if (!$path2) {
        drush_log(dt('Unknown path for !extension !type.', array('!extension' => $extension, '!type' => $project_type)), LogLevel::DEBUG);
        continue;
      }
      // 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;
}