diff options
author | Daniel Weipert <code@drogueronin.de> | 2022-03-06 00:48:14 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2022-03-06 00:48:14 +0100 |
commit | b2a86c7df7d5a473e80034832a01b21444fa50e6 (patch) | |
tree | 2b18e08124ff3e8602a20ba27fef3ae4d066cc28 /src/HookManager.php | |
parent | df428380e288db75b41ace5d6274f44916517f9f (diff) |
Refactor
Diffstat (limited to 'src/HookManager.php')
-rw-r--r-- | src/HookManager.php | 42 |
1 files changed, 42 insertions, 0 deletions
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 @@ +<?php + +namespace FlatFileForms; + +class HookManager +{ + private array $actions = []; + private array $filters = []; + + public function addAction(string $name, callable $function, int $priority = 10): void + { + $this->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; + } +} |