48 lines
1.2 KiB
PHP
Executable File
48 lines
1.2 KiB
PHP
Executable File
<?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;
|
|
}
|
|
}
|
|
|