2020-04-18 00:51:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
$hooks = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registered a hook on a specific event
|
|
|
|
*
|
2022-04-24 17:43:44 +02:00
|
|
|
* @param $event string The event name
|
|
|
|
* @param $callable callable The callable to run on event
|
|
|
|
* @param $param mixed Paremeter that will be pass to the callable
|
2020-04-18 00:51:33 +02:00
|
|
|
* Use an array if you have multiple parameters to pass
|
|
|
|
*
|
2022-04-24 17:43:44 +02:00
|
|
|
* @return void
|
2020-04-18 00:51:33 +02:00
|
|
|
*/
|
|
|
|
function register_hook($event, $callable, $param=NULL) {
|
2022-04-24 17:43:44 +02:00
|
|
|
global $hooks;
|
|
|
|
if (!array_key_exists($event, $hooks))
|
|
|
|
$hooks[$event] = array();
|
|
|
|
$hooks[$event][] = array (
|
|
|
|
'callable' => $callable,
|
|
|
|
'param' => $param,
|
|
|
|
);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run triggered actions on specific event
|
|
|
|
*
|
2022-04-24 17:43:44 +02:00
|
|
|
* @param $event string Event name
|
2020-04-18 00:51:33 +02:00
|
|
|
*
|
2022-04-24 17:43:44 +02:00
|
|
|
* @return boolean True if all triggered actions succefully runned, false otherwise
|
2020-04-18 00:51:33 +02:00
|
|
|
*/
|
|
|
|
function trigger_hook($event_name, $event_data=null) {
|
2022-04-24 17:43:44 +02:00
|
|
|
global $hooks;
|
|
|
|
$return = true;
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
if (isset($hooks[$event_name]) && is_array($hooks[$event_name])) {
|
|
|
|
if ($event_name == 'all')
|
|
|
|
$event = new Event($event_data['event_name'], $event_data['event_data']);
|
|
|
|
else
|
|
|
|
$event = new Event($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) {
|
|
|
|
logException(
|
|
|
|
$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.");
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
// Handle 'all' event
|
|
|
|
if ($event_name != 'all') {
|
|
|
|
trigger_hook (
|
|
|
|
'all',
|
|
|
|
array (
|
|
|
|
'event_name' => $event_name,
|
|
|
|
'event_data' => $event_data,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
return $return;
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Event implements JsonSerializable {
|
2022-04-24 17:43:44 +02:00
|
|
|
private $name;
|
|
|
|
private $data;
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
function __construct($name, $data) {
|
|
|
|
$this -> name = $name;
|
|
|
|
$this -> data = $data;
|
|
|
|
}
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2022-04-24 17:43:44 +02:00
|
|
|
public function jsonSerialize() {
|
|
|
|
return array (
|
|
|
|
'name' => $this -> name,
|
|
|
|
'data' => $this -> data,
|
|
|
|
);
|
|
|
|
}
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|