module.inc 2 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
  module_list();
Dries's avatar
 
Dries committed
11
  module_invoke_all("init");
12 13 14 15
}

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

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

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

  return $return;
}

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

  if (!$list) {
49 50 51
    $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
52
      if (file_exists($module->filename)) {
Kjartan's avatar
Kjartan committed
53
        $list[$module->name] = $module->name;
Dries's avatar
 
Dries committed
54
        include_once $module->filename;
55
      }
56
    }
57
    natcasesort($list);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  }

  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);
}

Dries's avatar
 
Dries committed
74
?>