$callable, 'param' => $param, ); } /** * Run triggered actions on specific event * * @param $event string HookEvent name * * @return boolean True if all triggered actions succefully runned, false otherwise */ function trigger_hook($event_name, $event_data=null) { global $hooks; $return = true; if (isset($hooks[$event_name]) && is_array($hooks[$event_name])) { if ($event_name == 'all') $event = new HookEvent($event_data['event_name'], $event_data['event_data']); else $event = new HookEvent($event_name, $event_data); foreach ($hooks[$event_name] as $e) { if (is_callable($e['callable'])) { try { call_user_func_array($e['callable'],array($event, &$e['param'])); } catch(Exception $e) { log_exception( $e, "An exception occured running hook ".format_callable($e['callable']). " on event $event_name"); $return = false; } } else { logging( 'ERROR', "The hook ".format_callable($e['callable'])." on event $event_name is not callable."); $return = false; } } } else logging('DEBUG', "No hook registered for event $event_name."); // Handle 'all' event if ($event_name != 'all') { trigger_hook ( 'all', array ( 'event_name' => $event_name, 'event_data' => $event_data, ) ); } return $return; } class HookEvent implements JsonSerializable { private $name; private $data; function __construct($name, $data) { $this -> name = $name; $this -> data = $data; } function __get($key) { if ($key == 'name') return $this -> name; elseif ($key == 'data') return $this -> data; elseif (is_array($this -> data) && array_key_exists($key, $this -> data)) return $this -> data[$key]; return null; } public function jsonSerialize() { return array ( 'name' => $this -> name, 'data' => $this -> data, ); } } # vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab