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

// initialize modules:
function module_init() {
Kjartan's avatar
Kjartan committed
6 7 8
  require_once "modules/user.module";
  require_once "modules/system.module";
  require_once "modules/watchdog.module";
9 10 11 12 13
  module_list();
}

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

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

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

  return $return;
}

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

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

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

72
?>