Move hooks stuff in EesyPHP namespace

This commit is contained in:
Benjamin Renard 2023-01-29 19:57:26 +01:00
parent c10ed0ca20
commit cf6ea5480b
5 changed files with 128 additions and 110 deletions

View file

@ -69,7 +69,6 @@ $status_list = array (
'archived' => ___('Archived'), 'archived' => ___('Archived'),
); );
require_once('hooks.php');
require_once('cli.php'); require_once('cli.php');
require_once('translation-cli.php'); require_once('translation-cli.php');
require_once('smarty.php'); require_once('smarty.php');

View file

@ -1,6 +1,8 @@
<?php <?php
use EesyPHP\Hook;
use EesyPHP\Log; use EesyPHP\Log;
use Unidecode\Unidecode; use Unidecode\Unidecode;
if (!isset($db_dsn)) { if (!isset($db_dsn)) {
@ -151,7 +153,7 @@ function add_item($values) {
if ($result !== false) { if ($result !== false) {
$item = get_item($result); $item = get_item($result);
Log :: info("New item #$result added"); Log :: info("New item #$result added");
trigger_hook('item_added', $item); Hook :: trigger('item_added', $item);
return $item; return $item;
} }
} }
@ -182,7 +184,7 @@ function update_item($id, $changes) {
if ($result !== false) { if ($result !== false) {
Log :: info("Item #$id updated"); Log :: info("Item #$id updated");
trigger_hook('item_updated', $item); Hook :: trigger('item_updated', $item);
return true; return true;
} }
} }
@ -464,7 +466,7 @@ function restore_items($fd=null) {
Log :: info("$restored items restored"); Log :: info("$restored items restored");
// Trigger hooks // Trigger hooks
trigger_hook('items_restored'); Hook :: trigger('items_restored');
return !$error; return !$error;
} }

View file

@ -1,106 +0,0 @@
<?php
use EesyPHP\Log;
$hooks = array();
/**
* Registered a hook on a specific event
*
* @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
* Use an array if you have multiple parameters to pass
*
* @return void
*/
function register_hook($event, $callable, $param=NULL) {
global $hooks;
if (!array_key_exists($event, $hooks))
$hooks[$event] = array();
$hooks[$event][] = array (
'callable' => $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

89
src/Hook.php Normal file
View file

@ -0,0 +1,89 @@
<?php
namespace EesyPHP;
use EesyPHP\Log;
use Exception;
class Hook {
/**
* Registered hooks
* @var array
*/
protected static $hooks = array();
/**
* Registered a hook on a specific event
*
* @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
* Use an array if you have multiple parameters to pass
*
* @return void
*/
public static function register($event, $callable, $param=NULL) {
if (!array_key_exists($event, self :: $hooks))
self :: $hooks[$event] = array();
self :: $hooks[$event][] = array (
'callable' => $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

34
src/HookEvent.php Normal file
View file

@ -0,0 +1,34 @@
<?php
namespace EesyPHP;
use JsonSerializable;
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