summaryrefslogtreecommitdiff
path: root/src/HookManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/HookManager.php')
-rw-r--r--src/HookManager.php42
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;
+ }
+}