Event.php
<?php
namespace Lia\Compo;
class Event extends \Lia\Compo {
protected $events = [];
public function apiSchedule_lia_event_schedule($eventName, $callable){
// if ($this->lia->debugEnabled()){
// $fancy = new \Lia\Utility\FancyClosure($callable);
// $this->lia->addDebug('Event', 'schedule_from_prefix', ['eventName'=>$eventName,
// 'class'=>$fancy->class, 'functionName'=>$fancy->funcName]);
// }
$eventName = str_replace('_','.',$eventName);
$this->events[$eventName][] = $callable;
}
public function apiPrefix_lia_event_onEmit_scheduleFromPrefix($eventName, $callable){
if (substr($eventName,0,1)=='_')$eventName = substr($eventName,1);
$this->lia->api('lia.event', 'schedule', $eventName, $callable);
}
public function apiEmit_lia_event_emit($eventName, ...$args){
// $this->lia->addDebug('Event', 'emit', ['eventName'=>$eventName]);
$eventName = str_replace('_','.',$eventName);
foreach ($this->events[$eventName]??[] as $index=>$callable){
$callable(...$args);
}
}
}