29 lines
758 B
PHP
29 lines
758 B
PHP
<?php
|
|
|
|
namespace Picea;
|
|
|
|
trait EventTrait
|
|
{
|
|
protected $eventTraitMethod = "execute";
|
|
|
|
public array $eventTraitList = [];
|
|
|
|
protected array $eventTraitReturnList = [];
|
|
|
|
public function eventRegister(object $event) : void
|
|
{
|
|
$this->eventTraitList[] = $event;
|
|
}
|
|
|
|
public function eventFromType(string $type) : array
|
|
{
|
|
return array_filter($this->eventTraitList, fn($ev) => $ev instanceof $type);
|
|
}
|
|
|
|
public function eventExecute(string $type, ...$arguments) : void
|
|
{
|
|
foreach($this->eventFromType($type) as $event) {
|
|
$this->eventTraitReturnList[$event::class][] = call_user_func_array([ $event, $this->eventTraitMethod ], $arguments);
|
|
}
|
|
}
|
|
} |