module.inc 2.02 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.
Dries's avatar
 
Dries committed
7
  require_once "modules/admin.module";
Kjartan's avatar
Kjartan committed
8 9 10
  require_once "modules/user.module";
  require_once "modules/system.module";
  require_once "modules/watchdog.module";
11
  module_list();
Dries's avatar
 
Dries committed
12
  module_invoke_all("init");
13 14 15 16
}

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

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

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

  return $return;
}

43
// return array of module names (includes lazy module loading):
Dries's avatar
 
Dries committed
44
function module_list($refresh = 0) {
45 46
  static $list;

Dries's avatar
 
Dries committed
47 48 49 50
  if ($refresh) {
    unset($list);
  }

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

  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
77
?>