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 () ;
43 * GetPlugins() - get a list of installed plugins
45 * @return hash of plugin id => plugin names
47 function GetPlugins () {
48 if (!isset($this->plugins_data)) {
49 $this->plugins_data = array () ;
50 $res = db_query_params ('SELECT plugin_id, plugin_name FROM plugins',
52 $rows = db_numrows($res);
53 for ($i=0; $i<$rows; $i++) {
54 $plugin_id = db_result($res,$i,'plugin_id');
55 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
58 return $this->plugins_data ;
62 * GetPluginObject() - get a particular plugin object
64 * @param pluginname - name of plugin
65 * @return a plugin object
67 function GetPluginObject ($pluginname) {
68 return @$this->plugins_objects [$pluginname] ;
72 * PluginIsInstalled() - is a plugin installed?
74 * @param pluginname - name of plugin
75 * @return boolean, true if installed
77 function PluginIsInstalled ($pluginname) {
78 $plugins_data = $this->getPlugins() ;
79 foreach ($plugins_data as $p_id => $p_name) {
80 if ($p_name == $pluginname) {
87 function activate($pluginname) {
88 $res = db_query_params('INSERT INTO plugins (plugin_name,plugin_desc) VALUES ($1,$2)',
89 array($pluginname, "This is the $pluginname plugin"));
91 $res = db_query_params ('SELECT plugin_id, plugin_name FROM plugins WHERE plugin_name=$1',
93 if (db_numrows($res) == 1) {
94 $plugin_id = db_result($res,0,'plugin_id');
95 $this->plugins_data[$plugin_id] = db_result($res,0,'plugin_name');
102 function deactivate($pluginname) {
103 $res = db_query_params('DELETE FROM plugins WHERE plugin_name = $1', array($pluginname));
105 foreach ($this->plugins_data as $i => $n) {
106 if ($n == $pluginname) {
110 unset($this->plugins_data[$p_id]);
115 * LoadPlugin() - load a specific plugin
118 function LoadPlugin ($p_name) {
121 $plugins_data = $this->GetPlugins() ;
122 $include_path = $GLOBALS['sys_plugins_path'] ;
123 $filename = $include_path . '/'. $p_name . "/common/".$p_name."-init.php" ;
124 if (file_exists ($filename)) {
125 require_once ($filename) ;
126 } else { //if we didn't found it in common/ it may be an old plugin that has it's files in include/
127 $filename = $include_path . '/' . $p_name . "/include/".$p_name."-init.php" ;
128 if (file_exists ($filename)) {
129 require_once ($filename) ;
131 // we can't find the plugin so we remove it from the array
132 foreach ($plugins_data as $i => $n) {
137 unset($this->plugins_data[$p_id]);
144 * LoadPlugins() - load available plugins
147 function LoadPlugins () {
148 $plugins_data = $this->GetPlugins() ;
149 foreach ($plugins_data as $p_id => $p_name) {
150 if (!$this->LoadPlugin($p_name)) {
151 // we can't find the plugin so we remove it from the array
152 unset($this->plugins_data[$p_id]);
159 * SetupHooks() - setup all hooks for installed plugins
162 function SetupHooks () {
163 $this->hooks_to_plugins = array();
164 foreach ($this->plugins_to_hooks as $p_name => $hook_list) {
165 foreach ($hook_list as $hook_name) {
166 if (!isset ($this->hooks_to_plugins[$hook_name])) {
167 $this->hooks_to_plugins[$hook_name] = array () ;
169 $this->hooks_to_plugins[$hook_name][] = $p_name ;
176 * RegisterPlugin() - register a plugin
178 * @param pluginobject - an object of a subclass of the Plugin class
180 function RegisterPlugin (&$pluginobject) {
181 if (!$pluginobject->GetName ()) {
182 exit_error ("Some plugin did not provide a name. I'd gladly tell you which one, but obviously I can't. Sorry.") ;
184 $p_name = $pluginobject->GetName () ;
185 $this->plugins_objects [$p_name] =& $pluginobject ;
186 $this->plugins_to_hooks [$p_name] = $pluginobject->GetHooks () ;
191 * RunHooks() - call hooks from a particular point
193 * @param hookname - name of the hook
194 * @param params - array of extra parameters
196 * @return boolean, true if all returned true.
198 function RunHooks ($hookname, & $params) {
200 if (isset($this->hooks_to_plugins[$hookname])) {
201 $p_list = $this->hooks_to_plugins[$hookname];
202 foreach ($p_list as $p_name) {
203 $p_obj = $this->plugins_objects[$p_name] ;
204 $returned = $p_obj->CallHook ($hookname, $params);
205 $this->returned_value[$hookname] = $returned;
206 $result = $result && $returned ;
210 // Return true only if all the plugins have returned true.
214 function getReturnedValue($hookname) {
215 return $this->returned_value[$hookname];
219 * CountHookListeners() - number of listeners on a particular hook
221 * @param hookname - name of the hook
223 function CountHookListeners ($hookname) {
224 if (isset($this->hooks_to_plugins[$hookname])) {
225 $p_list = $this->hooks_to_plugins[$hookname];
226 return count ($p_list) ;
235 * plugin_manager_get_object() - get the PluginManager object
237 * @return the PluginManager object
239 function &plugin_manager_get_object() {
240 global $PLUGINMANAGER_OBJ;
241 if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
242 $PLUGINMANAGER_OBJ = new PluginManager ;
244 return $PLUGINMANAGER_OBJ ;
248 * plugin_get_object() - get a particular Plugin object
250 * @param pluginname - a plugin name
251 * @return the Plugin object
253 function &plugin_get_object ($pluginname) {
254 global $PLUGINMANAGER_OBJ;
255 $result=$PLUGINMANAGER_OBJ->Getpluginobject ($pluginname) ;
260 * register_plugin () - register a plugin
262 * @param pluginobject - an object of a subclass of the Plugin class
264 function register_plugin (&$pluginobject) {
265 $pm =& plugin_manager_get_object () ;
266 return $pm->RegisterPlugin ($pluginobject) ;
270 * plugin_hook () - run a set of hooks
272 * @param hookname - name of the hook
273 * @param params - parameters for the hook
275 function plugin_hook ($hookname, $params = false) {
276 $pm =& plugin_manager_get_object () ;
277 return $pm->RunHooks ($hookname, $params) ;
281 * plugin_hook_by_reference () - run a set of hooks with params passed by reference
283 * @param hookname - name of the hook
284 * @param params - parameters for the hook
286 function plugin_hook_by_reference ($hookname, & $params) {
287 $pm =& plugin_manager_get_object () ;
288 return $pm->RunHooks ($hookname, $params) ;
292 * plugin_hook_listeners () - count the number of listeners on a hook
294 * @param hookname - name of the hook
296 function plugin_hook_listeners ($hookname, $params=false) {
297 $pm =& plugin_manager_get_object () ;
298 return $pm->CountHookListeners ($hookname) ;
302 * setup_plugin_manager () - initialise the plugin infrastructure
305 function setup_plugin_manager () {
306 $pm =& plugin_manager_get_object() ;
307 $pm->LoadPlugins () ;
314 // c-file-style: "bsd"