module.inc 1.97 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

// initialize modules:
function module_init() {
Dries's avatar
 
Dries committed
6
  // Note: changing this also requires changing system_admin() @ modules/system.module.
Kjartan's avatar
Kjartan committed
7 8 9
  require_once "modules/user.module";
  require_once "modules/system.module";
  require_once "modules/watchdog.module";
10 11 12 13 14
  module_list();
}

// apply function $function to every known module:
function module_iterate($function, $argument = "") {
Kjartan's avatar
Kjartan committed
15 16 17
  foreach (module_list() as $name) {
    $function($name, $argument);
  }
18 19 20
}

// invoke hook $hook of module $name with optional arguments:
Dries's avatar
 
Dries committed
21 22
function module_invoke($name, $hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
  $function = $name ."_". $hook;
23
  if (function_exists($function)) {
Dries's avatar
 
Dries committed
24
    return $function($a1, $a2, $a3, $a4);
25 26 27
  }
}

Dries's avatar
 
Dries committed
28
// invoke $hook for all appropriate modules:
Dries's avatar
 
Dries committed
29
function module_invoke_all($hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
Dries's avatar
 
Dries committed
30 31
  $return = array();
  foreach (module_list() as $name) {
Dries's avatar
 
Dries committed
32
    if (module_hook($name, $hook)) {
33 34
      $result = module_invoke($name, $hook, $a1, $a2, $a3, $a4);
      if (isset($result)) {
Dries's avatar
 
Dries committed
35 36 37 38 39 40 41 42
        $return = array_merge($return, $result);
      }
    }
  }

  return $return;
}

43 44 45 46 47
// return array of module names (includes lazy module loading):
function module_list() {
  static $list;

  if (!$list) {
48 49 50
    $list = array("drupal" => "drupal", "system" => "system", "user" => "user", "watchdog" => "watchdog");
    $result = db_query("SELECT name, filename FROM system WHERE type = 'module' AND status = '1' ORDER BY name");
    while ($module = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
51
      if (file_exists($module->filename)) {
Kjartan's avatar
Kjartan committed
52
        $list[$module->name] = $module->name;
Dries's avatar
 
Dries committed
53
        include_once $module->filename;
54
      }
55
    }
56
    asort($list);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  }

  return $list;
}

// return 1 if module $name exists, 0 otherwise:
function module_exist($name) {
  $list = module_list();
  return ($list[$name]) ? 1 : 0;
}

// return 1 if module $name implements hook $hook, 0 otherwise:
function module_hook($name, $hook) {
  return function_exists($name ."_". $hook);
}

73
?>