AvocadoAmber/AvocadoEdition_Light/lib/Hook/hook.extends.class.php

168 lines
4 KiB
PHP
Raw Normal View History

2024-09-19 20:46:45 +09:00
<?php
if (!defined('_GNUBOARD_'))
exit;
class GML_Hook extends Hook
{
2024-09-29 09:26:53 +09:00
protected $filters = array('count' => 0);
protected $callback_filters = array();
2024-09-19 20:46:45 +09:00
protected static $current_filter = false;
protected function runAction($action, $args)
{
$function = $action['function'];
$argsNumber = $action['arguments'];
2024-09-29 09:26:53 +09:00
$class = (is_array($function) && isset($function[0])) ? $function[0] : false;
$method = (is_array($function) && isset($function[1])) ? $function[1] : false;
2024-09-19 20:46:45 +09:00
$args = $this->getArguments($argsNumber, $args);
2024-09-29 09:26:53 +09:00
if (!($class && $method) && is_callable($function)) {
2024-09-19 20:46:45 +09:00
return call_user_func_array($function, $args);
2024-09-29 09:26:53 +09:00
} elseif ($obj = call_user_func(array($class, $this->singleton))) {
if ($obj !== false) {
return call_user_func_array(array($obj, $method), $args);
2024-09-19 20:46:45 +09:00
}
2024-09-29 09:26:53 +09:00
} elseif (class_exists($class)) {
$instance = new $class;
2024-09-29 09:26:53 +09:00
return call_user_func_array(array($instance, $method), $args);
}
2024-09-19 20:46:45 +09:00
}
protected function getFilters($tag, $remove)
{
2024-09-29 09:26:53 +09:00
if (isset($this->callback_filters[$tag])) {
$filters = $this->callback_filters[$tag];
if ($remove) {
unset($this->callback_filters[$tag]);
}
2024-09-19 20:46:45 +09:00
}
2024-09-29 09:26:53 +09:00
return (isset($filters)) ? $filters : array();
2024-09-19 20:46:45 +09:00
}
public static function get_properties($type, $is_callback = false)
{
2024-09-29 09:26:53 +09:00
2024-09-19 20:46:45 +09:00
$that = self::getInstance(self::$id);
2024-09-29 09:26:53 +09:00
2024-09-19 20:46:45 +09:00
if ($type === 'event') {
return $is_callback ? $that->callbacks : $that->actions;
}
2024-09-29 09:26:53 +09:00
2024-09-19 20:46:45 +09:00
return $is_callback ? $that->callback_filters : $that->filters;
}
public static function addFilter($tag, $func, $priority = 8, $args = 0)
{
$that = self::getInstance(self::$id);
2024-09-29 09:26:53 +09:00
$that->callback_filters[$tag][$priority][] = array(
2024-09-19 20:46:45 +09:00
'function' => $func,
'arguments' => $args,
2024-09-29 09:26:53 +09:00
);
2024-09-19 20:46:45 +09:00
return true;
}
2024-09-29 09:26:53 +09:00
public static function apply_filters($tag, $args = array(), $remove = true)
2024-09-19 20:46:45 +09:00
{
$that = self::getInstance(self::$id);
2024-09-29 09:26:53 +09:00
2024-09-19 20:46:45 +09:00
self::$current_filter = $tag;
2024-09-29 09:26:53 +09:00
2024-09-19 20:46:45 +09:00
$that->filters['count']++;
2024-09-29 09:26:53 +09:00
if (!array_key_exists($tag, $that->filters)) {
2024-09-19 20:46:45 +09:00
$that->filters[$tag] = 0;
}
2024-09-29 09:26:53 +09:00
$that->filters[$tag]++;
2024-09-19 20:46:45 +09:00
$filters = $that->getFilters($tag, $remove);
ksort($filters);
2024-09-29 09:26:53 +09:00
$value = $args[0];
2024-09-19 20:46:45 +09:00
foreach ($filters as $priority) {
foreach ($priority as $filter) {
if (isset($args[0])) {
$args[0] = $value;
}
$replace = $that->runAction($filter, $args);
2024-09-29 09:26:53 +09:00
if (!is_null($replace)) {
2024-09-19 20:46:45 +09:00
$value = $replace;
}
}
}
self::$current_filter = false;
2024-09-29 09:26:53 +09:00
2024-09-19 20:46:45 +09:00
return $value;
}
protected function getArguments($argsNumber, $arguments)
{
2024-09-29 09:26:53 +09:00
if ($argsNumber == 1 && is_string($arguments)) {
return array($arguments);
} elseif ($argsNumber === count($arguments)) {
return $arguments;
2024-09-19 20:46:45 +09:00
}
2024-09-29 09:26:53 +09:00
$args = array();
for ($i = 0; $i < $argsNumber; $i++) {
if (is_array($arguments) && array_key_exists($i, $arguments)) {
$args[] = $arguments[$i];
}
2024-09-19 20:46:45 +09:00
}
2024-09-29 09:26:53 +09:00
return $args;
2024-09-19 20:46:45 +09:00
}
public static function remove_filter($tag, $func, $priority)
{
$that = self::getInstance(self::$id);
2024-09-29 09:26:53 +09:00
$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;
2024-09-19 20:46:45 +09:00
}
public static function remove_action($tag, $func, $priority)
{
$that = self::getInstance(self::$id);
2024-09-29 09:22:07 +09:00
$is_remove = false;
2024-09-29 09:26:53 +09:00
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) {
unset($that->callbacks[$tag][$priority][$key]);
$is_remove = true;
}
2024-09-19 20:46:45 +09:00
}
}
2024-09-29 09:22:07 +09:00
return $is_remove;
2024-09-19 20:46:45 +09:00
}
}
2024-09-29 09:26:53 +09:00
// end Hook Class;