You are here

function _module_build_dependencies in Drupal 6

Same name and namespace in other branches
  1. 7 includes/module.inc \_module_build_dependencies()

Find dependencies any level deep and fill in dependents information too.

If module A depends on B which in turn depends on C then this function will add C to the list of modules A depends on. This will be repeated until module A has a list of all modules it depends on. If it depends on itself, called a circular dependency, that's marked by adding a nonexistent module, called -circular- to this list of modules. Because this does not exist, it'll be impossible to switch module A on.

Also we fill in a dependents array in $file->info. Using the names above, the dependents array of module B lists A.

Parameters

$files: The array of filesystem objects used to rebuild the cache.

Return value

The same array with dependencies and dependents added where applicable.

1 call to _module_build_dependencies()
module_rebuild_cache in includes/module.inc
Rebuild the database cache of module files.

File

includes/module.inc, line 186
API for loading and interacting with Drupal modules.

Code

function _module_build_dependencies($files) {
  do {
    $new_dependency = FALSE;
    foreach ($files as $filename => $file) {

      // We will modify this object (module A, see doxygen for module A, B, C).
      $file =& $files[$filename];
      if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
        foreach ($file->info['dependencies'] as $dependency_name) {

          // This is a nonexistent module.
          if ($dependency_name == '-circular-' || !isset($files[$dependency_name])) {
            continue;
          }

          // $dependency_name is module B (again, see doxygen).
          $files[$dependency_name]->info['dependents'][$filename] = $filename;
          $dependency = $files[$dependency_name];
          if (isset($dependency->info['dependencies']) && is_array($dependency->info['dependencies'])) {

            // Let's find possible C modules.
            foreach ($dependency->info['dependencies'] as $candidate) {
              if (array_search($candidate, $file->info['dependencies']) === FALSE) {

                // Is this a circular dependency?
                if ($candidate == $filename) {

                  // As a module name can not contain dashes, this makes
                  // impossible to switch on the module.
                  $candidate = '-circular-';

                  // Do not display the message or add -circular- more than once.
                  if (array_search($candidate, $file->info['dependencies']) !== FALSE) {
                    continue;
                  }
                  drupal_set_message(t('%module is part of a circular dependency. This is not supported and you will not be able to switch it on.', array(
                    '%module' => $file->info['name'],
                  )), 'error');
                }
                else {

                  // We added a new dependency to module A. The next loop will
                  // be able to use this as "B module" thus finding even
                  // deeper dependencies.
                  $new_dependency = TRUE;
                }
                $file->info['dependencies'][] = $candidate;
              }
            }
          }
        }
      }

      // Don't forget to break the reference.
      unset($file);
    }
  } while ($new_dependency);
  return $files;
}