1 system.module _system_rebuild_module_data()

Helper function to scan and collect module .info data.

Return value

An associative array of module information.

File

core/modules/system/system.module, line 2797
Configuration system that lets administrators modify the workings of the site.

Code

function _system_rebuild_module_data() {
  // Find modules
  $modules = backdrop_system_listing('/^' . BACKDROP_PHP_FUNCTION_PATTERN . '\.module$/', 'modules', 'name', 0);

  // Include the installation profile in modules that are loaded.
  $profile = backdrop_get_profile();
  $profile_uri = 'profiles/' . $profile . '/' . $profile . '.profile';
  if (file_exists(BACKDROP_ROOT . '/core/profiles/' . $profile . '/' . $profile . '.profile')) {
    $profile_uri = 'core/' . $profile_uri;
  }
  $modules[$profile] = new stdClass();
  $modules[$profile]->name = $profile;
  $modules[$profile]->uri = $profile_uri;
  $modules[$profile]->filename = $profile . '.profile';

  // Installation profile hooks are always executed last.
  $modules[$profile]->weight = 1000;

  // Set defaults for module info.
  $defaults = array(
    'dependencies' => array(),
    'description' => '',
    'package' => 'Other',
    'version' => NULL,
    'php' => BACKDROP_MINIMUM_PHP,
    'files' => array(),
    'bootstrap' => 0,
  );

  // Read info files for each module.
  foreach ($modules as $key => $module) {
    // The module system uses the key 'filename' instead of 'uri' so copy the
    // value so it will be used by the modules system.
    $modules[$key]->filename = $module->uri;

    // Look for the info file.
    $module->info = backdrop_parse_info_file(dirname($module->uri) . '/' . $module->name . '.info');

    // Skip modules that don't provide info.
    if (empty($module->info)) {
      unset($modules[$key]);
      continue;
    }

    // Add the info file modification time, so it becomes available for
    // contributed modules to use for ordering module lists.
    $module->info['mtime'] = filemtime(dirname($module->uri) . '/' . $module->name . '.info');

    // Merge in defaults and save.
    $modules[$key]->info = $module->info + $defaults;

    // The "name" key is required, but to avoid a fatal error in the menu system
    // we set a reasonable default if it is not provided.
    $modules[$key]->info += array('name' => $key);

    // Prefix stylesheets and scripts with module path.
    $path = dirname($module->uri);
    if (isset($module->info['stylesheets'])) {
      $module->info['stylesheets'] = _system_info_add_path($module->info['stylesheets'], $path);
    }
    if (isset($module->info['scripts'])) {
      $module->info['scripts'] = _system_info_add_path($module->info['scripts'], $path);
    }

    // Installation profiles are hidden by default, unless explicitly specified
    // otherwise in the .info file.
    if ($key == $profile && !isset($modules[$key]->info['hidden'])) {
      $modules[$key]->info['hidden'] = TRUE;
    }

    // Apply best guess version information if this is a Git checkout.
    if (empty($modules[$key]->info['version'])) {
      $module_base_path = BACKDROP_ROOT . '/' . preg_replace('!(.*modules/[^/]+).*!', '$1', $module->uri);
      $git_version = FALSE;
      if (file_exists($module_base_path . '/.git/HEAD')) {
        // Branches names are directly in the HEAD file.
        $git_head_contents = trim(file_get_contents($module_base_path . '/.git/HEAD'));
        if (strpos($git_head_contents, 'ref') === 0) {
          $git_version = trim(preg_replace('!^.*refs/heads/(.*)$!', '$1', $git_head_contents));
        }
        // Tags are named by sha1 hash, loop through each tag file to match it.
        elseif (is_dir($module_base_path . '/.git/refs/tags')) {
          $tag_files = file_scan_directory($module_base_path . '/.git/refs/tags', '/.*/', array('key' => 'filename', 'recurse' => FALSE));
          $tag_files = array_reverse($tag_files, TRUE);
          foreach ($tag_files as $tag_name => $tag_file) {
            if (trim(file_get_contents($tag_file->uri)) == $git_head_contents) {
              $git_version = $tag_name;
              break;
            }
          }
        }
        if ($git_version) {
          $git_version = preg_replace('/^' . preg_quote(BACKDROP_CORE_COMPATIBILITY, '/') . '-/', '', $git_version);
          $modules[$key]->info['version'] = $git_version . '-dev';
        }
      }
    }

    // Invoke hook_system_info_alter() to give installed modules a chance to
    // modify the data in the .info files if necessary.
    $type = 'module';
    backdrop_alter('system_info', $modules[$key]->info, $modules[$key], $type);
  }

  if (isset($modules[$profile])) {
    // The installation profile is required, if it's a valid module.
    $modules[$profile]->info['required'] = TRUE;
    // Add a default distribution name if the profile did not provide one. This
    // matches the default value used in install_profile_info().
    if (!isset($modules[$profile]->info['distribution_name'])) {
      $modules[$profile]->info['distribution_name'] = 'Backdrop CMS';
    }
  }

  return $modules;
}