xref: /dokuwiki/inc/pluginutils.php (revision a46d0d658b98649869f6c9660e168af3940d7c30)
1ee20e7d1Sandi<?php
2ee20e7d1Sandi/**
3ee20e7d1Sandi * Utilities for handling plugins
4ee20e7d1Sandi *
5ee20e7d1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee20e7d1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7ee20e7d1Sandi */
8ee20e7d1Sandi
9ee20e7d1Sandi/**
10ee20e7d1Sandi * Returns a list of available plugins of given type
11ee20e7d1Sandi *
12ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
13ee20e7d1Sandi */
14ee20e7d1Sandifunction plugin_list($type){
15ee20e7d1Sandi  $plugins = array();
16ee20e7d1Sandi  if ($dh = opendir(DOKU_PLUGIN)) {
17ee20e7d1Sandi    while (false !== ($file = readdir($dh))) {
18ee20e7d1Sandi      if ($file == '.' || $file == '..') continue;
19ee20e7d1Sandi      if (is_file(DOKU_PLUGIN.$file)) continue;
20ee20e7d1Sandi
21ee20e7d1Sandi      if (@file_exists(DOKU_PLUGIN.$file.'/'.$type.'.php')){
22ee20e7d1Sandi        $plugins[] = $file;
23ee20e7d1Sandi      }
24ee20e7d1Sandi    }
25ee20e7d1Sandi    closedir($dh);
26ee20e7d1Sandi  }
27ee20e7d1Sandi  return $plugins;
28ee20e7d1Sandi}
29ee20e7d1Sandi
30ee20e7d1Sandi/**
31ee20e7d1Sandi * Loads the given plugin and creates an object of it
32ee20e7d1Sandi *
33ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
34ee20e7d1Sandi *
35ee20e7d1Sandi * @param  $type string 	type of plugin to load
36ee20e7d1Sandi * @param  $name string 	name of the plugin to load
37*a46d0d65SAndreas Gohr * @return object         the plugin object or null on failure
38ee20e7d1Sandi */
39*a46d0d65SAndreas Gohrfunction &plugin_load($type,$name){
40ee20e7d1Sandi  //we keep all loaded plugins available in global scope for reuse
41ee20e7d1Sandi  global $DOKU_PLUGINS;
42ee20e7d1Sandi
43ee20e7d1Sandi	//plugin already loaded?
44ee20e7d1Sandi	if($DOKU_PLUGINS[$type][$name] != null){
45*a46d0d65SAndreas Gohr		return $DOKU_PLUGINS[$type][$name];
46ee20e7d1Sandi	}
47ee20e7d1Sandi
48ee20e7d1Sandi  //try to load the wanted plugin file
49107b01d6Sandi  if(!include_once(DOKU_PLUGIN.$name.'/'.$type.'.php')){
50*a46d0d65SAndreas Gohr    return null;
51ee20e7d1Sandi  }
52ee20e7d1Sandi
53ee20e7d1Sandi  //construct class and instanciate
54ee20e7d1Sandi  $class = $type.'_plugin_'.$name;
55ee20e7d1Sandi  $DOKU_PLUGINS[$type][$name] = new $class;
56*a46d0d65SAndreas Gohr  return $DOKU_PLUGINS[$type][$name];
57ee20e7d1Sandi}
58ee20e7d1Sandi
59ee20e7d1Sandi
60