event/src/EventManager.php
2023-05-30 15:40:31 -04:00

53 lines
1.3 KiB
PHP

<?php
namespace Mcnd\Event;
use Psr\Container\ContainerInterface;
class EventManager
{
public array $eventList = [];
public function __construct(
public ContainerInterface $container,
) {}
public function eventRegister(object $event) : void
{
$this->eventList[] = $event;
}
public function eventFromType(string $type) : array
{
return array_filter($this->eventList, fn($ev) => $ev instanceof $type);
}
public function pushEvent(Event $event) : void
{
$this->eventList[] = $event;
}
public function eventFromName(string $name) : array
{
return array_filter($this->eventList, fn($ev) => property_exists($ev, 'name') && $ev->name === $name);
}
public function execute(string $name, &...$arguments) : void
{
$continue = true;
foreach($this->eventFromName($name) as $event) {
if ($continue) {
$event->execute($this->container, ...$arguments);
$continue = empty($event->stopPropagation);
}
}
foreach($this->eventfromType($name) as $event) {
if ($continue) {
call_user_func_array([ $event, 'execute'], $arguments);
$continue = empty($event->stopPropagation);
}
}
}
}