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) {
88 * LoadPlugin() - load a specifig plugin
91 function LoadPlugin ($p_name) {
94 $plugins_data = $this->GetPlugins() ;
95 $include_path = $GLOBALS['sys_plugins_path'] ;
96 $filename = $include_path . '/'. $p_name . "/common/".$p_name."-init.php" ;
97 if (file_exists ($filename)) {
98 require_once ($filename) ;
99 } else { //if we didn't found it in common/ it may be an old plugin that has it's files in include/
100 $filename = $include_path . '/' . $p_name . "/include/".$p_name."-init.php" ;
101 if (file_exists ($filename)) {
102 require_once ($filename) ;
104 // we can't find the plugin so we remove it from the array
105 foreach ($plugins_data as $i => $n) {
110 unset($this->plugins_data[$p_id]);
117 * LoadPlugins() - load available plugins
120 function LoadPlugins () {
121 $plugins_data = $this->GetPlugins() ;
122 foreach ($plugins_data as $p_id => $p_name) {
123 if (!$this->LoadPlugin($p_name)) {
124 // we can't find the plugin so we remove it from the array
125 unset($this->plugins_data[$p_id]);
132 * SetupHooks() - setup all hooks for installed plugins
135 function SetupHooks () {
136 foreach ($this->plugins_to_hooks as $p_name => $hook_list) {
137 foreach ($hook_list as $hook_name) {
138 if (!isset ($this->hooks_to_plugins[$hook_name])) {
139 $this->hooks_to_plugins[$hook_name] = array () ;
141 $this->hooks_to_plugins[$hook_name][] = $p_name ;
148 * RegisterPlugin() - register a plugin
150 * @param pluginobject - an object of a subclass of the Plugin class
152 function RegisterPlugin (&$pluginobject) {
153 if (!$pluginobject->GetName ()) {
154 exit_error ("Some plugin did not provide a name. I'd gladly tell you which one, but obviously I can't. Sorry.") ;
156 $p_name = $pluginobject->GetName () ;
157 $this->plugins_objects [$p_name] =& $pluginobject ;
158 $this->plugins_to_hooks [$p_name] = $pluginobject->GetHooks () ;
163 * RunHooks() - call hooks from a particular point
165 * @param hookname - name of the hook
166 * @param params - array of extra parameters
168 * @return boolean, true if all returned true.
170 function RunHooks ($hookname, & $params) {
172 if (isset($this->hooks_to_plugins[$hookname])) {
173 $p_list = $this->hooks_to_plugins[$hookname];
174 foreach ($p_list as $p_name) {
175 $p_obj = $this->plugins_objects[$p_name] ;
176 $returned = $p_obj->CallHook ($hookname, $params);
177 $this->returned_value[$hookname] = $returned;
178 $result = $result && $returned ;
182 // Return true only if all the plugins have returned true.
186 function getReturnedValue($hookname) {
187 return $this->returned_value[$hookname];
191 * CountHookListeners() - number of listeners on a particular hook
193 * @param hookname - name of the hook
195 function CountHookListeners ($hookname) {
196 if (isset($this->hooks_to_plugins[$hookname])) {
197 $p_list = $this->hooks_to_plugins[$hookname];
198 return count ($p_list) ;
207 * plugin_manager_get_object() - get the PluginManager object
209 * @return the PluginManager object
211 function &plugin_manager_get_object() {
212 global $PLUGINMANAGER_OBJ;
213 if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
214 $PLUGINMANAGER_OBJ = new PluginManager ;
216 return $PLUGINMANAGER_OBJ ;
220 * plugin_get_object() - get a particular Plugin object
222 * @param pluginname - a plugin name
223 * @return the Plugin object
225 function &plugin_get_object ($pluginname) {
226 global $PLUGINMANAGER_OBJ;
227 $result=$PLUGINMANAGER_OBJ->Getpluginobject ($pluginname) ;
232 * register_plugin () - register a plugin
234 * @param pluginobject - an object of a subclass of the Plugin class
236 function register_plugin (&$pluginobject) {
237 $pm =& plugin_manager_get_object () ;
238 return $pm->RegisterPlugin ($pluginobject) ;
242 * plugin_hook () - run a set of hooks
244 * @param hookname - name of the hook
245 * @param params - parameters for the hook
247 function plugin_hook ($hookname, $params = false) {
248 $pm =& plugin_manager_get_object () ;
249 return $pm->RunHooks ($hookname, $params) ;
253 * plugin_hook_by_reference () - run a set of hooks with params passed by reference
255 * @param hookname - name of the hook
256 * @param params - parameters for the hook
258 function plugin_hook_by_reference ($hookname, & $params) {
259 $pm =& plugin_manager_get_object () ;
260 return $pm->RunHooks ($hookname, $params) ;
264 * plugin_hook_listeners () - count the number of listeners on a hook
266 * @param hookname - name of the hook
268 function plugin_hook_listeners ($hookname, $params=false) {
269 $pm =& plugin_manager_get_object () ;
270 return $pm->CountHookListeners ($hookname) ;
274 * setup_plugin_manager () - initialise the plugin infrastructure
277 function setup_plugin_manager () {
278 $pm =& plugin_manager_get_object() ;
279 $pm->LoadPlugins () ;
286 // c-file-style: "bsd"