- First commit

This commit is contained in:
mcndave committed 2023-03-29 15:18:13 +00:00
commit 4d73913bef
5 files changed
+141

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Mcnd\Event;
use Psr\Container\ContainerInterface;
class Event
{
public function __construct(
public string $name,
public string $class,
public string $method,
public \Notes\Attribute $attribute,
) {}
public function execute(ContainerInterface $container, ... $arguments) : mixed
{
return $container->make($this->class)->{$this->method}(... $arguments);
}
}
+45
View File
@@ -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);
}
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Mcnd\Event;
use Notes\Event\EventFetcher;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
class EventMiddleware implements MiddlewareInterface
{
public ContainerInterface $container;
public EventManager $eventManager;
public function __construct(ContainerInterface $container, EventManager $eventManager) {
$this->container = $container;
$this->eventManager = $eventManager;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
return $handler->handle($request);
}
public function fromAttributes(EventFetcher $fetcher) : self
{
foreach($fetcher->compile() as $event) {
$this->eventManager->pushEvent($event);
}
return $this;
}
}