Changeset 1590 for trunk/include/functions_plugins.inc.php
- Timestamp:
- Nov 1, 2006, 6:54:35 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_plugins.inc.php
r1584 r1590 36 36 define('PHPWG_PLUGINS_PATH',PHPWG_ROOT_PATH.'plugins/'); 37 37 38 define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50); 39 38 40 /* Register a event handler. 39 41 * @param string $event the name of the event to listen to 40 42 * @param mixed $func the function that will handle the event 41 */ 42 function add_event_handler($event, $func, $priority=50, $accepted_args=1) 43 { 44 global $pwg_event_handlers; 45 46 if ( isset($pwg_event_handlers[$event]["$priority"]) ) 47 { 48 foreach($pwg_event_handlers[$event]["$priority"] as $handler) 43 * @param int $priority optional priority (greater priority will 44 * be executed at last) 45 */ 46 function add_event_handler($event, $func, 47 $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1) 48 { 49 global $pwg_event_handlers; 50 51 if ( isset($pwg_event_handlers[$event][$priority]) ) 52 { 53 foreach($pwg_event_handlers[$event][$priority] as $handler) 49 54 { 50 55 if ( $handler['function'] == $func ) … … 55 60 } 56 61 57 trigger_event('add_event_handler', 58 array('event'=>$event, 'function'=>$func) 59 ); 60 61 $pwg_event_handlers[$event]["$priority"][] = 62 $pwg_event_handlers[$event][$priority][] = 62 63 array( 63 64 'function'=>$func, 64 65 'accepted_args'=>$accepted_args); 65 66 ksort( $pwg_event_handlers[$event] ); 66 67 return true; 67 68 } 68 69 70 /* Register a event handler. 71 * @param string $event the name of the event to listen to 72 * @param mixed $func the function that needs removal 73 * @param int $priority optional priority (greater priority will 74 * be executed at last) 75 */ 76 function remove_event_handler($event, $func, 77 $priority=EVENT_HANDLER_PRIORITY_NEUTRAL) 78 { 79 global $pwg_event_handlers; 80 81 if (!isset( $pwg_event_handlers[$event][$priority] ) ) 82 { 83 return false; 84 } 85 for ($i=0; $i<count($pwg_event_handlers[$event][$priority]); $i++) 86 { 87 if ($pwg_event_handlers[$event][$priority][$i]['function']==$func) 88 { 89 unset($pwg_event_handlers[$event][$priority][$i]); 90 $pwg_event_handlers[$event][$priority] = 91 array_values($pwg_event_handlers[$event][$priority]); 92 93 if ( empty($pwg_event_handlers[$event][$priority]) ) 94 { 95 unset( $pwg_event_handlers[$event][$priority] ); 96 if (empty( $pwg_event_handlers[$event] ) ) 97 { 98 unset( $pwg_event_handlers[$event] ); 99 } 100 } 101 return true; 102 } 103 } 104 return false; 105 } 69 106 70 107 /* Triggers an event and calls all registered event handlers … … 75 112 { 76 113 global $pwg_event_handlers; 77 if ($event!='pre_trigger_event' and $event!='post_trigger_event') 78 {// special case 79 trigger_event('pre_trigger_event', 80 array('event'=>$event, 'data'=>$data) ); 81 if ( !isset($pwg_event_handlers[$event]) ) 82 { 83 trigger_event('post_trigger_event', 84 array('event'=>$event, 'data'=>$data) ); 85 } 86 } 114 115 // just for debugging 116 trigger_action('pre_trigger_event', 117 array('event'=>$event, 'data'=>$data) ); 87 118 88 119 if ( !isset($pwg_event_handlers[$event]) ) 89 120 { 121 trigger_action('post_trigger_event', 122 array('event'=>$event, 'data'=>$data) ); 90 123 return $data; 91 124 } … … 115 148 } 116 149 } 117 118 if ($event!='pre_trigger_event' and $event!='post_trigger_event') 119 { 120 trigger_event('post_trigger_event', 121 array('event'=>$event, 'data'=>$data) ); 122 } 123 150 trigger_action('post_trigger_event', 151 array('event'=>$event, 'data'=>$data) ); 124 152 return $data; 125 153 } 126 154 127 155 156 function trigger_action($event, $data=null) 157 { 158 global $pwg_event_handlers; 159 if ($event!='pre_trigger_event' 160 and $event!='post_trigger_event' 161 and $event!='trigger_action') 162 {// special case for debugging - avoid recursive calls 163 trigger_action('trigger_action', 164 array('event'=>$event, 'data'=>$data) ); 165 } 166 167 if ( !isset($pwg_event_handlers[$event]) ) 168 { 169 return; 170 } 171 $args = array_slice(func_get_args(), 2); 172 173 foreach ($pwg_event_handlers[$event] as $priority => $handlers) 174 { 175 if ( !is_null($handlers) ) 176 { 177 foreach($handlers as $handler) 178 { 179 $all_args = array_merge( array($data), $args); 180 $function_name = $handler['function']; 181 $accepted_args = $handler['accepted_args']; 182 183 if ( $accepted_args == 1 ) 184 $the_args = array($data); 185 elseif ( $accepted_args > 1 ) 186 $the_args = array_slice($all_args, 0, $accepted_args); 187 elseif ( $accepted_args == 0 ) 188 $the_args = NULL; 189 else 190 $the_args = $all_args; 191 192 call_user_func_array($function_name, $the_args); 193 } 194 } 195 } 196 } 128 197 129 198 … … 174 243 foreach( $plugins as $plugin) 175 244 { 176 @include_once( PHPWG_PLUGINS_PATH.$plugin['id'].'/index.php' ); 177 } 178 trigger_event('plugins_loaded'); 245 $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php'; 246 if ( file_exists($file_name) ) 247 { 248 include_once( $file_name ); 249 } 250 } 251 trigger_action('plugins_loaded'); 179 252 } 180 253 ?>
Note: See TracChangeset
for help on using the changeset viewer.