You are here

function _unused_modules_add_project_path in Unused Modules 6

Same name and namespace in other branches
  1. 7 unused_modules.inc \_unused_modules_add_project_path()

Add project path.

Group modules by 'project' and extract their lowest common basepath.

1 call to _unused_modules_add_project_path()
_unused_modules_get_available_modules in ./unused_modules.module
Returns an array of available modules.

File

./unused_modules.module, line 284
This module lists modules and projects that are unused.

Code

function _unused_modules_add_project_path(&$modules) {

  // Group modules by project.
  $modules_grouped_by_project = array();
  foreach ($modules as $module) {
    $modules_grouped_by_project[$module->project][$module->name] = $module;
  }

  // Add project_path to module.
  foreach ($modules_grouped_by_project as $project) {

    // Determine common basepath by looking for needle "/<project>/" in uri.
    // As a fallback use the shortest path method.
    foreach ($project as $module) {
      if ($module->error !== TRUE) {
        $needle = "/" . $module->project . "/";
        $before_needle = TRUE;
        $project_path = strstr($module->uri, $needle, $before_needle) . "/" . $module->project;
        $module->project_path = $project_path;
      }
    }

    // Fallback: determine common basepath by picking the shortest path of all
    // project modules.
    if (!isset($module->project_path)) {
      $project_paths = array();
      foreach ($project as $module) {
        $project_paths[] = $module->module_path;
      }

      // Get length of each module path in a project.
      $lengths = array_map('strlen', $project_paths);

      // Sort by value (lowest number first)
      asort($lengths);

      // Get lowest key.
      reset($lengths);
      $key = key($lengths);

      // Shortest path.
      $shortest_path = $project_paths[$key];

      // Add the project_path to each module.
      foreach ($project as $module) {
        $module->project_path = $shortest_path;
      }
    }
    unset($project_paths);
  }
}