Changeset 27238


Ignore:
Timestamp:
Feb 6, 2014, 2:02:22 PM (10 years ago)
Author:
mistic100
Message:

feature 3041: Give include path to add_event_handler
$accepted_args is replaced by $include_path (retrocompatible)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_plugins.inc.php

    r27221 r27238  
    173173 * @param Callable $func the callback function
    174174 * @param int $priority greater priority will be executed at last
     175 * @param string $include_path file to include before executing the callback
     176 * @return bool false is handler already exists
    175177 */
    176178function add_event_handler($event, $func,
    177     $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1)
     179    $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $include_path=null)
    178180{
    179181  global $pwg_event_handlers;
    180182
    181   if ( isset($pwg_event_handlers[$event][$priority]) )
    182   {
    183     foreach($pwg_event_handlers[$event][$priority] as $handler)
    184     {
    185       if ( $handler['function'] == $func )
     183  if (isset($pwg_event_handlers[$event][$priority]))
     184  {
     185    foreach ($pwg_event_handlers[$event][$priority] as $handler)
     186    {
     187      if ($handler['function'] == $func)
    186188      {
    187189        return false;
     
    190192  }
    191193
    192   $pwg_event_handlers[$event][$priority][] =
    193     array(
    194       'function'=>$func,
    195       'accepted_args'=>$accepted_args);
    196   ksort( $pwg_event_handlers[$event] );
     194  $pwg_event_handlers[$event][$priority][] = array(
     195    'function' => $func,
     196    'include_path' => is_string($include_path) ? $include_path : null,
     197    );
     198
     199  ksort($pwg_event_handlers[$event]);
    197200  return true;
    198201}
     
    211214  global $pwg_event_handlers;
    212215
    213   if (!isset( $pwg_event_handlers[$event][$priority] ) )
     216  if (!isset($pwg_event_handlers[$event][$priority]))
    214217  {
    215218    return false;
     
    223226        array_values($pwg_event_handlers[$event][$priority]);
    224227
    225       if ( empty($pwg_event_handlers[$event][$priority]) )
    226       {
    227         unset( $pwg_event_handlers[$event][$priority] );
    228         if (empty( $pwg_event_handlers[$event] ) )
     228      if (empty($pwg_event_handlers[$event][$priority]))
     229      {
     230        unset($pwg_event_handlers[$event][$priority]);
     231        if (empty($pwg_event_handlers[$event]))
    229232        {
    230           unset( $pwg_event_handlers[$event] );
     233          unset($pwg_event_handlers[$event]);
    231234        }
    232235      }
     
    265268  global $pwg_event_handlers;
    266269
    267   if ( isset($pwg_event_handlers['trigger']) )
    268   {// just for debugging
     270  if (isset($pwg_event_handlers['trigger']))
     271  {// debugging
    269272    trigger_action('trigger',
    270         array('type'=>'event', 'event'=>$event, 'data'=>$data) );
    271   }
    272 
    273   if ( !isset($pwg_event_handlers[$event]) )
     273      array('type'=>'event', 'event'=>$event, 'data'=>$data)
     274      );
     275  }
     276
     277  if (!isset($pwg_event_handlers[$event]))
    274278  {
    275279    return $data;
     
    279283  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
    280284  {
    281     foreach($handlers as $handler)
    282     {
    283       $function_name = $handler['function'];
    284       $accepted_args = $handler['accepted_args'];
     285    foreach ($handlers as $handler)
     286    {
    285287      $args[1] = $data;
    286       $data = call_user_func_array($function_name, array_slice($args,1,$accepted_args) );
    287     }
    288   }
    289   trigger_action('trigger',
    290        array('type'=>'post_event', 'event'=>$event, 'data'=>$data) );
     288
     289      if (!empty($handler['include_path']))
     290      {
     291        include_once($handler['include_path']);
     292      }
     293
     294      $data = call_user_func_array($handler['function'], array_slice($args, 1));
     295    }
     296  }
     297
     298  if (isset($pwg_event_handlers['trigger']))
     299  {// debugging
     300    trigger_action('trigger',
     301      array('type'=>'post_event', 'event'=>$event, 'data'=>$data)
     302      );
     303  }
     304
    291305  return $data;
    292306}
     
    315329{
    316330  global $pwg_event_handlers;
    317   if ( isset($pwg_event_handlers['trigger']) and $event!='trigger' )
    318   {// special case for debugging - avoid recursive calls
     331
     332  if (isset($pwg_event_handlers['trigger']) and $event!='trigger')
     333  {// debugging - avoid recursive calls
    319334    trigger_action('trigger',
    320         array('type'=>'action', 'event'=>$event, 'data'=>null) );
    321   }
    322 
    323   if ( !isset($pwg_event_handlers[$event]) )
     335      array('type'=>'action', 'event'=>$event, 'data'=>null)
     336      );
     337  }
     338
     339  if (!isset($pwg_event_handlers[$event]))
    324340  {
    325341    return;
     
    329345  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
    330346  {
    331     foreach($handlers as $handler)
    332     {
    333       $function_name = $handler['function'];
    334       $accepted_args = $handler['accepted_args'];
    335 
    336       call_user_func_array($function_name, array_slice($args,1,$accepted_args) );
     347    foreach ($handlers as $handler)
     348    {
     349      if (!empty($handler['include_path']))
     350      {
     351        include_once($handler['include_path']);
     352      }
     353
     354      call_user_func_array($handler['function'], array_slice($args,1));
    337355    }
    338356  }
Note: See TracChangeset for help on using the changeset viewer.