diff --git a/includes/core.php b/includes/core.php index 42c5f6e..a977f4a 100644 --- a/includes/core.php +++ b/includes/core.php @@ -69,7 +69,6 @@ $status_list = array ( 'archived' => ___('Archived'), ); -require_once('hooks.php'); require_once('cli.php'); require_once('translation-cli.php'); require_once('smarty.php'); diff --git a/includes/db.php b/includes/db.php index 7067767..17b699b 100644 --- a/includes/db.php +++ b/includes/db.php @@ -1,6 +1,8 @@ $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 { - Log :: error( - "The hook ".format_callable($e['callable'])." on event $event_name is not callable."); - $return = false; - } - } - } - else - Log :: 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 diff --git a/src/Hook.php b/src/Hook.php new file mode 100644 index 0000000..ade57df --- /dev/null +++ b/src/Hook.php @@ -0,0 +1,89 @@ + $callable, + 'param' => $param, + ); + } + + /** + * Run triggered actions on specific event + * + * @param $event string Hook event name + * @param $event_data mixed Hook event data (optional, default: null) + * + * @return boolean True if all triggered actions succefully runned, false otherwise + */ + public static function trigger($event_name, $event_data=null) { + $return = true; + + if (isset(self :: $hooks[$event_name]) && is_array(self :: $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 (self :: $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 { + Log :: error( + "The hook ".format_callable($e['callable'])." on event $event_name is not callable."); + $return = false; + } + } + } + else + Log :: debug("No hook registered for event $event_name."); + + // Handle 'all' event + if ($event_name != 'all') { + self :: trigger ( + 'all', + array ( + 'event_name' => $event_name, + 'event_data' => $event_data, + ) + ); + } + + return $return; + } +} + +# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab diff --git a/src/HookEvent.php b/src/HookEvent.php new file mode 100644 index 0000000..e819b5e --- /dev/null +++ b/src/HookEvent.php @@ -0,0 +1,34 @@ + 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