<?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;
    }

}