File: PluginManager.class

package info (click to toggle)
gforge 3.1-31sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,148 kB
  • ctags: 11,865
  • sloc: sql: 27,860; php: 25,574; perl: 7,124; xml: 3,152; sh: 2,586; ansic: 315; makefile: 143
file content (199 lines) | stat: -rw-r--r-- 5,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 *	PluginManager object
 *
 *	Provides an abstract way to handle plugins
 *
 * This file is copyright (c) Roland Mas <lolando@debian.org>, 2002
 *
 * @version   $Id: PluginManager.class,v 1.7 2003/03/10 17:46:34 cbayle Exp $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

class PluginManager extends Error {
	var $plugins_objects ;
	var $plugins_to_hooks ;
	var $hooks_to_plugins ;

	/**
	 * PluginManager() - constructor
	 *
	 */
	function PluginManager () {
		$this->Error() ;
		$this->plugins_objects = array () ;
		$this->plugins_to_hooks = array () ;
		$this->hooks_to_plugins = array () ;
	}

	/**
	 * GetPlugins() - get a list of installed plugins
	 *
	 * @return hash of plugin id => plugin names
	 */
	function GetPlugins () {
		if (!isset($this->plugins_data)) {
			$this->plugins_data = array () ;
			$sql = "SELECT * FROM plugins" ;
			$res = db_query($sql);
			$rows = db_numrows($res);

			for ($i=0; $i<$rows; $i++) {
				$plugin_id = db_result($res,$i,'plugin_id');
				$this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
			}
		}
		return $this->plugins_data ;
	}

	/**
	 * PluginIsInstalled() - is a plugin installed?
	 *
	 * @param pluginname - name of plugin
	 * @return boolean, true if installed
	 */
	function PluginIsInstalled ($pluginname) {
		$plugins_data = $this->getPlugins() ;
		foreach ($plugins_data as $p_id => $p_name) {
			if ($p_name == $pluginname) {
				return true ;
			}
		}
		return false ;
	}

	/**
	 * LoadPlugins() - load available plugins
	 *
	 */
	function LoadPlugins () {
		$plugins_data = $this->GetPlugins() ;
		$include_path = "/usr/lib/gforge/plugins/" ;
		foreach ($plugins_data as $p_id => $p_name) {
			$filename = $include_path . $p_name . "/include/".$p_name."-init.php" ;
			if (file_exists ($filename)) {
				require_once ($filename) ;
			}
		}
		return true ;
	}

	/**
	 * SetupHooks() - setup all hooks for installed plugins
	 *
	 */
	function SetupHooks () {
		foreach ($this->plugins_to_hooks as $p_name => $hook_list) {
			foreach ($hook_list as $hook_name) {
				if (!isset ($this->hooks_to_plugins[$hook_name])) {
					$this->hooks_to_plugins[$hook_name] = array () ;
				}
				$this->hooks_to_plugins[$hook_name][] = $p_name ;
			}
		}
		return true ;
	}

	/**
	 * RegisterPlugin() - register a plugin
	 *
	 * @param pluginobject - an object of a subclass of the Plugin class
	 */
	function RegisterPlugin (&$pluginobject) {
		if (!$pluginobject->GetName ()) {
			exit_error ("Some plugin did not provide a name.  I'd gladly tell you which one, but obviously I can't.  Sorry.") ;
		}
		$p_name = $pluginobject->GetName () ;
		$this->plugins_objects [$p_name] =& $pluginobject ;
		$this->plugins_to_hooks [$p_name] = $pluginobject->GetHooks () ;
		return true ;
	}

	/**
	 * RunHooks() - call hooks from a particular point
	 *
	 * @param hookname - name of the hook
	 * @param params - array of extra parameters
	 */
	function RunHooks ($hookname, $params) {
		echo "\n<!-- Starting hook $hookname -->" ;
		$p_list = $this->hooks_to_plugins[$hookname] ;
		if (isset ($p_list)) {
			foreach ($p_list as $p_name) {
				echo "<!-- Hook $hookname for plugin $p_name -->" ;
				$p_obj = $this->plugins_objects[$p_name] ;
				$p_obj->CallHook ($hookname, $params) ;
				echo "<!-- End of hook $hookname for plugin $p_name -->" ;
			}
		}
		echo "<!-- End of hook $hookname -->\n" ;
		return true ;
	}
}

/**
 * plugin_manager_get_object() - get the PluginManager object
 *
 * @return the PluginManager object
 */
function &plugin_manager_get_object() {
	global $PLUGINMANAGER_OBJ;
	if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
		$PLUGINMANAGER_OBJ = new PluginManager ;
	}
	return $PLUGINMANAGER_OBJ ;
}

/**
 * register_plugin () - register a plugin
 *
 * @param pluginobject - an object of a subclass of the Plugin class
 */
function register_plugin (&$pluginobject) {
	$pm =& plugin_manager_get_object () ;
	return $pm->RegisterPlugin ($pluginobject) ;
}

/**
 * plugin_hook () - run a set of hooks
 *
 * @param hookname - name of the hook
 * @param params - parameters for the hook
 */
function plugin_hook ($hookname, $params=false) {
	$pm =& plugin_manager_get_object () ;
	return $pm->RunHooks ($hookname, $params) ;
}

/**
 * setup_plugin_manager () - initialise the plugin ingrastructure
 *
 */
function setup_plugin_manager () {
	$pm =& plugin_manager_get_object() ;
	$pm->LoadPlugins () ;
	$pm->SetupHooks () ;
	return true ;
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End:

?>