You are here

private static function UnusedModulesHelperService::addProjectPath in Unused Modules 8

Add project path.

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

Parameters

\Drupal\unused_modules\UnusedModulesExtensionDecorator[] $modules: List of modules.

1 call to UnusedModulesHelperService::addProjectPath()
UnusedModulesHelperService::getAvailableModules in src/UnusedModulesHelperService.php
Returns an array of available modules.

File

src/UnusedModulesHelperService.php, line 98

Class

UnusedModulesHelperService
Common Unused Modules functionality.

Namespace

Drupal\unused_modules

Code

private static function addProjectPath(&$modules) {

  // Group modules by project.
  $modules_grouped_by_project = [];
  foreach ($modules as $module) {
    $modules_grouped_by_project[$module->projectName][$module
      ->getName()] = $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) {

      /** @var \Drupal\unused_modules\UnusedModulesExtensionDecorator $module */
      if (!$module->parsingError) {
        $needle = "/" . $module->projectName;
        $before_needle = TRUE;
        $project_path = strstr($module
          ->getSubpath(), $needle, $before_needle) . "/" . $module->projectName;
        $module->projectPath = $project_path;
      }
    }

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

      // 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->projectPath = $shortest_path;
      }
    }
    unset($project_paths);
  }
}