edit hook
This commit is contained in:
parent
28951fdeb1
commit
e63d31aa2d
4 changed files with 88 additions and 64 deletions
|
|
@ -1,3 +0,0 @@
|
||||||
<?php
|
|
||||||
if (!defined('_GNUBOARD_'))
|
|
||||||
exit;
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
<?php
|
|
||||||
if (!defined('_GNUBOARD_'))
|
|
||||||
exit;
|
|
||||||
|
|
@ -123,10 +123,10 @@ class Hook
|
||||||
{
|
{
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
|
|
||||||
$that->callbacks[$tag][$priority][] = [
|
$that->callbacks[$tag][$priority][] = array(
|
||||||
'function' => $func,
|
'function' => $func,
|
||||||
'arguments' => $args,
|
'arguments' => $args,
|
||||||
];
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -143,7 +143,7 @@ class Hook
|
||||||
public static function addActions($actions)
|
public static function addActions($actions)
|
||||||
{
|
{
|
||||||
foreach ($actions as $arguments) {
|
foreach ($actions as $arguments) {
|
||||||
call_user_func_array([__CLASS__, 'addAction'], $arguments);
|
call_user_func_array(array(__CLASS__, 'addAction'), $arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -164,9 +164,9 @@ class Hook
|
||||||
* @param mixed $args → optional arguments
|
* @param mixed $args → optional arguments
|
||||||
* @param bool $remove → delete hook after executing actions
|
* @param bool $remove → delete hook after executing actions
|
||||||
*
|
*
|
||||||
* @return bool returns the output of the last action or false
|
* @return returns the output of the last action or false
|
||||||
*/
|
*/
|
||||||
public static function doAction($tag, $args = [], $remove = true)
|
public static function doAction($tag, $args = array(), $remove = true)
|
||||||
{
|
{
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
|
|
||||||
|
|
@ -252,24 +252,24 @@ class Hook
|
||||||
$function = $action['function'];
|
$function = $action['function'];
|
||||||
$argsNumber = $action['arguments'];
|
$argsNumber = $action['arguments'];
|
||||||
|
|
||||||
$class = isset($function[0]) ? $function[0] : false;
|
$class = (isset($function[0])) ? $function[0] : false;
|
||||||
$method = isset($function[1]) ? $function[1] : false;
|
$method = (isset($function[1])) ? $function[1] : false;
|
||||||
|
|
||||||
$args = $this->getArguments($argsNumber, $args);
|
$args = $this->getArguments($argsNumber, $args);
|
||||||
|
|
||||||
if (!($class && $method) && function_exists($function)) {
|
if (!($class && $method) && function_exists($function)) {
|
||||||
return call_user_func($function, $args);
|
return call_user_func($function, $args);
|
||||||
} elseif ($obj = call_user_func([$class, $this->singleton])) {
|
} elseif ($obj = call_user_func(array($class, $this->singleton))) {
|
||||||
if ($obj !== false) {
|
if ($obj !== false) {
|
||||||
return call_user_func_array([$obj, $method], $args);
|
return call_user_func_array(array($obj, $method), $args);
|
||||||
}
|
}
|
||||||
} elseif (class_exists($class)) {
|
} elseif (class_exists($class)) {
|
||||||
$instance = new $class;
|
$instance = new $class;
|
||||||
|
|
||||||
return call_user_func_array([$instance, $method], $args);
|
return call_user_func_array(array($instance, $method), $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -291,7 +291,7 @@ class Hook
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return isset($actions) ? $actions : [];
|
return (isset($actions)) ? $actions : array();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -307,7 +307,7 @@ class Hook
|
||||||
protected function getArguments($argsNumber, $arguments)
|
protected function getArguments($argsNumber, $arguments)
|
||||||
{
|
{
|
||||||
if ($argsNumber == 1 && is_string($arguments)) {
|
if ($argsNumber == 1 && is_string($arguments)) {
|
||||||
return [$arguments];
|
return array($arguments);
|
||||||
} elseif ($argsNumber === count($arguments)) {
|
} elseif ($argsNumber === count($arguments)) {
|
||||||
return $arguments;
|
return $arguments;
|
||||||
}
|
}
|
||||||
|
|
@ -321,6 +321,6 @@ class Hook
|
||||||
return $args;
|
return $args;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,11 @@ if (!defined('_GNUBOARD_'))
|
||||||
|
|
||||||
class GML_Hook extends Hook
|
class GML_Hook extends Hook
|
||||||
{
|
{
|
||||||
protected $filters = ['count' => 0];
|
|
||||||
protected $callback_filters = [];
|
protected $filters = array('count' => 0);
|
||||||
|
|
||||||
|
protected $callback_filters = array();
|
||||||
|
|
||||||
protected static $current_filter = false;
|
protected static $current_filter = false;
|
||||||
|
|
||||||
protected function runAction($action, $args)
|
protected function runAction($action, $args)
|
||||||
|
|
@ -13,68 +16,77 @@ class GML_Hook extends Hook
|
||||||
$function = $action['function'];
|
$function = $action['function'];
|
||||||
$argsNumber = $action['arguments'];
|
$argsNumber = $action['arguments'];
|
||||||
|
|
||||||
|
$class = (is_array($function) && isset($function[0])) ? $function[0] : false;
|
||||||
|
$method = (is_array($function) && isset($function[1])) ? $function[1] : false;
|
||||||
|
|
||||||
$args = $this->getArguments($argsNumber, $args);
|
$args = $this->getArguments($argsNumber, $args);
|
||||||
|
|
||||||
if (is_callable($function)) {
|
if (!($class && $method) && is_callable($function)) {
|
||||||
return call_user_func_array($function, $args);
|
return call_user_func_array($function, $args);
|
||||||
} elseif (is_array($function) && isset($function[0], $function[1])) {
|
} elseif ($obj = call_user_func(array($class, $this->singleton))) {
|
||||||
$class = $function[0];
|
if ($obj !== false) {
|
||||||
$method = $function[1];
|
return call_user_func_array(array($obj, $method), $args);
|
||||||
|
}
|
||||||
if ($obj = call_user_func([$class, $this->singleton])) {
|
|
||||||
return $obj !== false ? call_user_func_array([$obj, $method], $args) : false;
|
|
||||||
} elseif (class_exists($class)) {
|
} elseif (class_exists($class)) {
|
||||||
$instance = new $class();
|
$instance = new $class;
|
||||||
return call_user_func_array([$instance, $method], $args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return call_user_func_array(array($instance, $method), $args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getFilters($tag, $remove)
|
protected function getFilters($tag, $remove)
|
||||||
{
|
{
|
||||||
$filters = isset($this->callback_filters[$tag]) ? $this->callback_filters[$tag] : [];
|
if (isset($this->callback_filters[$tag])) {
|
||||||
|
$filters = $this->callback_filters[$tag];
|
||||||
if ($remove) {
|
if ($remove) {
|
||||||
unset($this->callback_filters[$tag]);
|
unset($this->callback_filters[$tag]);
|
||||||
}
|
}
|
||||||
return $filters;
|
}
|
||||||
|
|
||||||
|
return (isset($filters)) ? $filters : array();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_properties($type, $is_callback = false)
|
public static function get_properties($type, $is_callback = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
|
|
||||||
if ($type === 'event') {
|
if ($type === 'event') {
|
||||||
return $is_callback ? $that->callbacks : $that->actions;
|
return $is_callback ? $that->callbacks : $that->actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $is_callback ? $that->callback_filters : $that->filters;
|
return $is_callback ? $that->callback_filters : $that->filters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function addFilter($tag, $func, $priority = 8, $args = 0)
|
public static function addFilter($tag, $func, $priority = 8, $args = 0)
|
||||||
{
|
{
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
$that->callback_filters[$tag][$priority][] = [
|
|
||||||
|
$that->callback_filters[$tag][$priority][] = array(
|
||||||
'function' => $func,
|
'function' => $func,
|
||||||
'arguments' => $args,
|
'arguments' => $args,
|
||||||
];
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function apply_filters($tag, $args = [], $remove = true)
|
public static function apply_filters($tag, $args = array(), $remove = true)
|
||||||
{
|
{
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
|
|
||||||
self::$current_filter = $tag;
|
self::$current_filter = $tag;
|
||||||
|
|
||||||
$that->filters['count']++;
|
$that->filters['count']++;
|
||||||
|
|
||||||
if (!isset($that->filters[$tag])) {
|
if (!array_key_exists($tag, $that->filters)) {
|
||||||
$that->filters[$tag] = 0;
|
$that->filters[$tag] = 0;
|
||||||
}
|
}
|
||||||
$that->filters[$tag]++;
|
|
||||||
|
|
||||||
|
$that->filters[$tag]++;
|
||||||
$filters = $that->getFilters($tag, $remove);
|
$filters = $that->getFilters($tag, $remove);
|
||||||
ksort($filters);
|
ksort($filters);
|
||||||
|
|
||||||
$value = isset($args[0]) ? $args[0] : null;
|
$value = $args[0];
|
||||||
|
|
||||||
foreach ($filters as $priority) {
|
foreach ($filters as $priority) {
|
||||||
foreach ($priority as $filter) {
|
foreach ($priority as $filter) {
|
||||||
|
|
@ -82,56 +94,74 @@ class GML_Hook extends Hook
|
||||||
$args[0] = $value;
|
$args[0] = $value;
|
||||||
}
|
}
|
||||||
$replace = $that->runAction($filter, $args);
|
$replace = $that->runAction($filter, $args);
|
||||||
if ($replace !== false) {
|
|
||||||
|
if (!is_null($replace)) {
|
||||||
$value = $replace;
|
$value = $replace;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$current_filter = false;
|
self::$current_filter = false;
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getArguments($argsNumber, $arguments)
|
protected function getArguments($argsNumber, $arguments)
|
||||||
{
|
{
|
||||||
if ($argsNumber === 1 && is_string($arguments)) {
|
if ($argsNumber == 1 && is_string($arguments)) {
|
||||||
return [$arguments];
|
return array($arguments);
|
||||||
}
|
} elseif ($argsNumber === count($arguments)) {
|
||||||
|
|
||||||
if (is_array($arguments) && $argsNumber === count($arguments)) {
|
|
||||||
return $arguments;
|
return $arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_slice((array) $arguments, 0, $argsNumber);
|
$args = array();
|
||||||
|
|
||||||
|
for ($i = 0; $i < $argsNumber; $i++) {
|
||||||
|
if (is_array($arguments) && array_key_exists($i, $arguments)) {
|
||||||
|
$args[] = $arguments[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $args;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function remove_filter($tag, $func, $priority)
|
public static function remove_filter($tag, $func, $priority)
|
||||||
{
|
{
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
return self::remove_callback($that->callback_filters, $tag, $func, $priority);
|
|
||||||
|
$is_remove = false;
|
||||||
|
|
||||||
|
if (isset($that->callback_filters[$tag]) && isset($that->callback_filters[$tag][$priority])) {
|
||||||
|
|
||||||
|
foreach ((array) $that->callback_filters[$tag][$priority] as $key => $value) {
|
||||||
|
if (isset($value['function']) && $value['function'] === $func) {
|
||||||
|
unset($that->callback_filters[$tag][$priority][$key]);
|
||||||
|
$is_remove = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $is_remove;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function remove_action($tag, $func, $priority)
|
public static function remove_action($tag, $func, $priority)
|
||||||
{
|
{
|
||||||
$that = self::getInstance(self::$id);
|
$that = self::getInstance(self::$id);
|
||||||
return self::remove_callback($that->callbacks, $tag, $func, $priority);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function remove_callback(&$array, $tag, $func, $priority)
|
|
||||||
{
|
|
||||||
if (!isset($array[$tag][$priority])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$is_remove = false;
|
$is_remove = false;
|
||||||
|
|
||||||
foreach ($array[$tag][$priority] as $key => $value) {
|
if (isset($that->callbacks[$tag]) && isset($that->callbacks[$tag][$priority])) {
|
||||||
|
|
||||||
|
foreach ((array) $that->callbacks[$tag][$priority] as $key => $value) {
|
||||||
if (isset($value['function']) && $value['function'] === $func) {
|
if (isset($value['function']) && $value['function'] === $func) {
|
||||||
unset($array[$tag][$priority][$key]);
|
unset($that->callbacks[$tag][$priority][$key]);
|
||||||
$is_remove = true;
|
$is_remove = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $is_remove;
|
return $is_remove;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// end Hook Class;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue