Event.php

<?php

namespace Lia\Compo;

class Event extends \Lia\Compo {
    
    protected $events = [];

    public function onReady(){
        $this->lia->addApi('lia:event.schedule', [$this, 'schedule']);
        $this->lia->addApiMethod('lia:event.schedule', 'schedule');

        $this->lia->addApi('lia:event.prefix', [$this, 'scheduleFromPrefix']);
        $this->lia->addApiPrefix('lia:event.prefix', 'onEmit');

        $this->lia->addApi('lia:event.emit', [$this, 'emit']);
        $this->lia->addApiMethod('lia:event.emit', 'emit');
    }
    
    public function schedule($eventName, $callable){
        $eventName = str_replace('_','.',$eventName);
        $this->events[$eventName][] = $callable;
    }
    public function scheduleFromPrefix($eventName, $callable){
        if (substr($eventName,0,1)=='_')$eventName = substr($eventName,1);
        $this->lia->api('lia:event.schedule', $eventName, $callable);
    }
    
    public function emit($eventName, ...$args){
        $eventName = str_replace('_','.',$eventName);
        foreach ($this->events[$eventName]??[] as $index=>$callable){
            $callable(...$args);
        }
    }
}