You are here

private static function UnusedModulesHelperService::addInfoFileInformation in Unused Modules 8

Add module information from <module>.info file.

Parameters

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

Adds properties to $module objects inside $modules:

  • project.
  • error (default FALSE).
1 call to UnusedModulesHelperService::addInfoFileInformation()
UnusedModulesHelperService::getAvailableModules in src/UnusedModulesHelperService.php
Returns an array of available modules.

File

src/UnusedModulesHelperService.php, line 176

Class

UnusedModulesHelperService
Common Unused Modules functionality.

Namespace

Drupal\unused_modules

Code

private static function addInfoFileInformation(&$modules = []) {

  // The Drupal packaging script adds project information to the .info file.
  foreach ($modules as $module) {
    try {
      if (!file_exists($module
        ->getPathname())) {
        $error_message = "No .info.yml file found for module '" . $module
          ->getName() . "'";
        throw new UnusedModulesException($error_message);
      }
      $info_file = file($module
        ->getPathname(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

      // Traverse all lines in .info and look for the line that starts
      // with "project". When found, add the project name to the module object.
      foreach ($info_file as $line) {
        if (substr($line, 0, 7) === "project") {

          // Remove "project = " prefix.
          $project = str_replace("project: ", "", $line);

          // Remove surrounding single-quotes.
          $project = str_replace("'", "", $project);
          $module->projectName = $project;
        }
      }

      // Throw error if no project information is found.
      // This should only be the case for sandbox modules.
      if (!$module->projectName) {
        $error_message = "No project information found for module '" . $module
          ->getName() . "'";
        throw new UnusedModulesException($error_message);
      }
    } catch (UnusedModulesException $e) {
      $module->parsingError = TRUE;
      $module->projectName = "_NO_PROJECT_INFORMATION_";

      // Don't write warnings during site_audit execution.
      $show_warnings = TRUE;
      if (function_exists('drush_get_command')) {
        $command = drush_get_command();
        if ($command['command'] == 'audit_extensions') {
          $show_warnings = FALSE;
        }
      }
      if ($show_warnings) {
        \Drupal::messenger()
          ->addWarning($e
          ->getMessage());
      }
    }
  }
}