From b2a86c7df7d5a473e80034832a01b21444fa50e6 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 6 Mar 2022 00:48:14 +0100 Subject: Refactor --- src/HookManager.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/HookManager.php (limited to 'src/HookManager.php') diff --git a/src/HookManager.php b/src/HookManager.php new file mode 100644 index 0000000..96ca8d3 --- /dev/null +++ b/src/HookManager.php @@ -0,0 +1,42 @@ +action[$name][$priority][] = compact('name', 'function', 'priority'); + } + + public function addFilter(string $name, callable $function, int $priority = 10): void + { + $this->filters[$name][$priority][] = compact('name', 'callable', 'priority'); + } + + public function doAction(string $name, mixed ...$arguments): void + { + foreach ($this->actions[$name] as $actions) { + foreach ($actions as $action) { + call_user_func_array($action['function'], $arguments); + } + } + } + + public function applyFilter(string $name, mixed $value, mixed ...$arguments): mixed + { + // set $value as first argument + array_unshift($arguments, $value); + + foreach ($this->filters[$name] as $filters) { + foreach ($filters as $filter) { + $value = call_user_func_array($filter['function'], $arguments); + } + } + + return $value; + } +} -- cgit v1.2.3