From 0545d318c9ca68e611dcf8f2f06712ad0fad3101 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 10 Jun 2022 06:34:32 +0800 Subject: [PATCH] Hook filter and action sep --- app/Repositories/TrackerRepository.php | 2 +- nexus/Plugin/Hook.php | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/Repositories/TrackerRepository.php b/app/Repositories/TrackerRepository.php index d1b23826..5a6f1b16 100644 --- a/app/Repositories/TrackerRepository.php +++ b/app/Repositories/TrackerRepository.php @@ -679,7 +679,7 @@ class TrackerRepository extends BaseRepository $spStateReal = $torrent->spStateReal; $log .= "[SP_STATE_REAL]: $spStateReal"; $promotionInfo = apply_filter('torrent_promotion', $torrent->toArray()); - do_log("promotionInfo from filter torrent_promotion by torrent: " . $torrent->toArray() . ", get : " . json_encode($promotionInfo)); + do_log("promotionInfo from filter torrent_promotion by torrent: " . $torrent->id . ", get : " . json_encode($promotionInfo)); if ($promotionInfo['sp_state'] != $spStateReal) { $spStateReal = $promotionInfo['sp_state']; $log .= "[CHANGE_SP_STATE_REAL_BY_FILTER_TORRENT_PROMOTION]: $spStateReal"; diff --git a/nexus/Plugin/Hook.php b/nexus/Plugin/Hook.php index bf469034..a5e24232 100644 --- a/nexus/Plugin/Hook.php +++ b/nexus/Plugin/Hook.php @@ -6,8 +6,6 @@ class Hook { private static array $callbacks = []; - private bool $isDoingAction = false; - public function addFilter($name, $function, $priority, $argc) { $id = $this->buildUniqueId($function); @@ -43,20 +41,16 @@ class Hook } $args = func_get_args(); reset(self::$callbacks[$name]); - do_log("name: $name, argc: " . (func_num_args() - 1)); +// do_log("name: $name, argc: " . (func_num_args() - 1)); do { foreach ((array)current(self::$callbacks[$name]) as $id => $callback) { $args[1] = $value; // do_log("name: $name, id: $id, before, params: " . json_encode(array_slice($args, 1, $callback['argc']))); $value = call_user_func_array($callback['function'], array_slice($args, 1, $callback['argc'])); - if ($this->isDoingAction) { - $value = $args[1]; - } // do_log("name: $name, id: $id, after, value: " . var_export($value, true)); } } while (next(self::$callbacks[$name]) !== false); - $this->isDoingAction = false; return $value; } @@ -65,10 +59,22 @@ class Hook return $this->addFilter($name, $function, $priority, $argc); } - public function doAction($name, ...$args) + public function doAction($name, $value = '') { - $this->isDoingAction = true; - $this->applyFilter(...func_get_args()); + if (!isset(self::$callbacks[$name])) { + do_log("No this hook: $name"); + return; + } + $args = func_get_args(); + reset(self::$callbacks[$name]); +// do_log("name: $name, argc: " . (func_num_args() - 1)); + do { + foreach ((array)current(self::$callbacks[$name]) as $id => $callback) { +// do_log("name: $name, id: $id, before, params: " . json_encode(array_slice($args, 1, $callback['argc']))); + call_user_func_array($callback['function'], array_slice($args, 1, $callback['argc'])); + } + } + while (next(self::$callbacks[$name]) !== false); } public function dump()