3 * FusionForge plugin system
5 * Copyright 2002, 2009, Roland Mas
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 class PluginManager extends Error {
26 var $plugins_objects ;
27 var $plugins_to_hooks ;
28 var $hooks_to_plugins ;
29 var $returned_value = array();
32 * PluginManager() - constructor
35 function PluginManager () {
37 $this->plugins_objects = array () ;
38 $this->plugins_to_hooks = array () ;
39 $this->hooks_to_plugins = array () ;
41 //to work with Codendi files
43 global $PLUGINMANAGER_OBJ;
44 if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
45 $PLUGINMANAGER_OBJ = new PluginManager ;
47 return $PLUGINMANAGER_OBJ ;
51 * GetPlugins() - get a list of installed plugins
53 * @return hash of plugin id => plugin names
55 function GetPlugins () {
56 if (!isset($this->plugins_data)) {
57 $this->plugins_data = array () ;
58 $res = db_query_params ('SELECT plugin_id, plugin_name FROM plugins',
60 $rows = db_numrows($res);
61 for ($i=0; $i<$rows; $i++) {
62 $plugin_id = db_result($res,$i,'plugin_id');
63 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
66 return $this->plugins_data ;
70 * GetPluginObject() - get a particular plugin object
72 * @param pluginname - name of plugin
73 * @return a plugin object
75 function GetPluginObject ($pluginname) {
76 return @$this->plugins_objects [$pluginname] ;
79 //Added for Codendi compatibility
80 function getPluginByName ($pluginname) {
81 return @$this->plugins_objects [$pluginname] ;
84 * PluginIsInstalled() - is a plugin installed?
86 * @param pluginname - name of plugin
87 * @return boolean, true if installed
89 function PluginIsInstalled ($pluginname) {
90 $plugins_data = $this->getPlugins() ;
91 foreach ($plugins_data as $p_id => $p_name) {
92 if ($p_name == $pluginname) {
98 function isPluginAvailable ($plugin) {
99 $pluginname = $plugin->GetName();
100 $plugins_data = $this->getPlugins() ;
101 foreach ($plugins_data as $p_id => $p_name) {
102 if ($p_name == $pluginname) {
108 function activate($pluginname) {
109 $res = db_query_params('INSERT INTO plugins (plugin_name,plugin_desc) VALUES ($1,$2)',
110 array($pluginname, "This is the $pluginname plugin"));
112 $res = db_query_params ('SELECT plugin_id, plugin_name FROM plugins WHERE plugin_name=$1',
113 array ($pluginname));
114 if (db_numrows($res) == 1) {
115 $plugin_id = db_result($res,0,'plugin_id');
116 $this->plugins_data[$plugin_id] = db_result($res,0,'plugin_name');
123 function deactivate($pluginname) {
124 $res = db_query_params('DELETE FROM plugins WHERE plugin_name = $1', array($pluginname));
126 foreach ($this->plugins_data as $i => $n) {
127 if ($n == $pluginname) {
131 unset($this->plugins_data[$p_id]);
136 * LoadPlugin() - load a specific plugin
139 function LoadPlugin ($p_name) {
142 $plugins_data = $this->GetPlugins() ;
143 $include_path = $GLOBALS['sys_plugins_path'] ;
144 $filename = $include_path . '/'. $p_name . "/common/".$p_name."-init.php" ;
145 if (file_exists ($filename)) {
146 require_once ($filename) ;
147 } else { //if we didn't found it in common/ it may be an old plugin that has it's files in include/
148 $filename = $include_path . '/' . $p_name . "/include/".$p_name."-init.php" ;
149 if (file_exists ($filename)) {
150 require_once ($filename) ;
152 // we can't find the plugin so we remove it from the array
153 foreach ($plugins_data as $i => $n) {
158 unset($this->plugins_data[$p_id]);
165 * LoadPlugins() - load available plugins
168 function LoadPlugins () {
169 $plugins_data = $this->GetPlugins() ;
170 foreach ($plugins_data as $p_id => $p_name) {
171 if (!$this->LoadPlugin($p_name)) {
172 // we can't find the plugin so we remove it from the array
173 unset($this->plugins_data[$p_id]);
180 * SetupHooks() - setup all hooks for installed plugins
183 function SetupHooks () {
184 $this->hooks_to_plugins = array();
185 foreach ($this->plugins_to_hooks as $p_name => $hook_list) {
186 foreach ($hook_list as $hook_name) {
187 if (!isset ($this->hooks_to_plugins[$hook_name])) {
188 $this->hooks_to_plugins[$hook_name] = array () ;
190 $this->hooks_to_plugins[$hook_name][] = $p_name ;
197 * RegisterPlugin() - register a plugin
199 * @param pluginobject - an object of a subclass of the Plugin class
201 function RegisterPlugin (&$pluginobject) {
202 if (!$pluginobject->GetName ()) {
203 exit_error ("Some plugin did not provide a name. I'd gladly tell you which one, but obviously I can't. Sorry.") ;
205 $p_name = $pluginobject->GetName () ;
206 $this->plugins_objects [$p_name] =& $pluginobject ;
207 $this->plugins_to_hooks [$p_name] = $pluginobject->GetHooks () ;
212 * RunHooks() - call hooks from a particular point
214 * @param hookname - name of the hook
215 * @param params - array of extra parameters
217 * @return boolean, true if all returned true.
219 function RunHooks ($hookname, & $params) {
221 if (isset($this->hooks_to_plugins[$hookname])) {
222 $p_list = $this->hooks_to_plugins[$hookname];
223 foreach ($p_list as $p_name) {
224 $p_obj = $this->plugins_objects[$p_name] ;
225 $returned = $p_obj->CallHook ($hookname, $params);
226 $this->returned_value[$hookname] = $returned;
227 $result = $result && $returned ;
231 // Return true only if all the plugins have returned true.
235 function getReturnedValue($hookname) {
236 return $this->returned_value[$hookname];
240 * CountHookListeners() - number of listeners on a particular hook
242 * @param hookname - name of the hook
244 function CountHookListeners ($hookname) {
245 if (isset($this->hooks_to_plugins[$hookname])) {
246 $p_list = $this->hooks_to_plugins[$hookname];
247 return count ($p_list) ;
253 function isPluginAllowedForProject($p, $group_id) {
254 $Group = group_get_object($group_id);
255 return $Group->usesPlugin($p->getName());
260 * plugin_manager_get_object() - get the PluginManager object
262 * @return the PluginManager object
264 function &plugin_manager_get_object() {
265 global $PLUGINMANAGER_OBJ;
266 if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
267 $PLUGINMANAGER_OBJ = new PluginManager ;
269 return $PLUGINMANAGER_OBJ ;
273 * plugin_get_object() - get a particular Plugin object
275 * @param pluginname - a plugin name
276 * @return the Plugin object
278 function &plugin_get_object ($pluginname) {
279 global $PLUGINMANAGER_OBJ;
280 $result=$PLUGINMANAGER_OBJ->Getpluginobject ($pluginname) ;
285 * register_plugin () - register a plugin
287 * @param pluginobject - an object of a subclass of the Plugin class
289 function register_plugin (&$pluginobject) {
290 $pm =& plugin_manager_get_object () ;
291 return $pm->RegisterPlugin ($pluginobject) ;
295 * plugin_hook () - run a set of hooks
297 * @param hookname - name of the hook
298 * @param params - parameters for the hook
300 function plugin_hook ($hookname, $params = false) {
301 $pm =& plugin_manager_get_object () ;
302 return $pm->RunHooks ($hookname, $params) ;
306 * plugin_hook_by_reference () - run a set of hooks with params passed by reference
308 * @param hookname - name of the hook
309 * @param params - parameters for the hook
311 function plugin_hook_by_reference ($hookname, & $params) {
312 $pm =& plugin_manager_get_object () ;
313 return $pm->RunHooks ($hookname, $params) ;
317 * plugin_hook_listeners () - count the number of listeners on a hook
319 * @param hookname - name of the hook
321 function plugin_hook_listeners ($hookname, $params=false) {
322 $pm =& plugin_manager_get_object () ;
323 return $pm->CountHookListeners ($hookname) ;
327 * setup_plugin_manager () - initialise the plugin infrastructure
330 function setup_plugin_manager () {
331 $pm =& plugin_manager_get_object() ;
332 $pm->LoadPlugins () ;
339 // c-file-style: "bsd"