v0.1.10
This commit is contained in:
41
kernel/Hook.php
Normal file
41
kernel/Hook.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace kernel;
|
||||
|
||||
class Hook
|
||||
{
|
||||
|
||||
protected array $pool = [];
|
||||
|
||||
public function add(string $entity, \Closure $handler): void
|
||||
{
|
||||
$this->pool[] = [
|
||||
'entity' => $entity,
|
||||
'handler' => $handler,
|
||||
];
|
||||
}
|
||||
|
||||
public function getHooksByEntity(string $entity): array
|
||||
{
|
||||
$hooks = [];
|
||||
foreach ($this->pool as $item){
|
||||
if ($item['entity'] === $entity){
|
||||
$hooks[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
public function runHooksByEntity(string $entity, array $params = []): void
|
||||
{
|
||||
$response = '';
|
||||
$hooks = $this->getHooksByEntity($entity);
|
||||
foreach ($hooks as $hook){
|
||||
$response .= call_user_func_array($hook['handler'], $params ?? []);
|
||||
}
|
||||
|
||||
echo $response;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user