- First commit
This commit is contained in:
commit
4d73913bef
5 files changed
+141
No files matched your search
@@ -0,0 +1,45 @@
|
||||
<?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) => $ev->name === $name);
|
||||
}
|
||||
|
||||
public function execute(string $name, ...$arguments) : void
|
||||
{
|
||||
foreach($this->eventFromName($name) as $event) {
|
||||
$event->execute($this->container, ...$arguments);
|
||||
}
|
||||
|
||||
foreach($this->eventfromType($name) as $event) {
|
||||
call_user_func_array([ $event, 'execute'], $arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user