First Commit
This commit is contained in:
Executable
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Kernel;
|
||||
|
||||
class HookManager
|
||||
{
|
||||
protected array $actions = [];
|
||||
protected array $filters = [];
|
||||
|
||||
public function addAction(string $hook, callable $listener, int $priority = 10): void
|
||||
{
|
||||
$this->actions[$hook][$priority][] = $listener;
|
||||
}
|
||||
|
||||
public function doAction(string $hook, ...$args): void
|
||||
{
|
||||
if (!isset($this->actions[$hook])) {
|
||||
return;
|
||||
}
|
||||
ksort($this->actions[$hook]);
|
||||
foreach ($this->actions[$hook] as $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener(...$args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addFilter(string $hook, callable $listener, int $priority = 10): void
|
||||
{
|
||||
$this->filters[$hook][$priority][] = $listener;
|
||||
}
|
||||
|
||||
public function applyFilters(string $hook, $value, ...$args)
|
||||
{
|
||||
if (!isset($this->filters[$hook])) {
|
||||
return $value;
|
||||
}
|
||||
ksort($this->filters[$hook]);
|
||||
foreach ($this->filters[$hook] as $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$value = $listener($value, ...$args);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user