- Basic event manager middleware
This commit is contained in:
parent
4d73913bef
commit
f36228b7a6
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Mcnd\Event;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class EventDefinition
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public array $list
|
||||||
|
) {}
|
||||||
|
}
|
|
@ -29,17 +29,25 @@ class EventManager
|
||||||
|
|
||||||
public function eventFromName(string $name) : array
|
public function eventFromName(string $name) : array
|
||||||
{
|
{
|
||||||
return array_filter($this->eventList, fn($ev) => $ev->name === $name);
|
return array_filter($this->eventList, fn($ev) => property_exists($ev, 'name') && $ev->name === $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(string $name, ...$arguments) : void
|
public function execute(string $name, &...$arguments) : void
|
||||||
{
|
{
|
||||||
|
$continue = true;
|
||||||
|
|
||||||
foreach($this->eventFromName($name) as $event) {
|
foreach($this->eventFromName($name) as $event) {
|
||||||
$event->execute($this->container, ...$arguments);
|
if ($continue) {
|
||||||
|
$event->execute($this->container, ...$arguments);
|
||||||
|
$continue = empty($event->stopPropagation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->eventfromType($name) as $event) {
|
foreach($this->eventfromType($name) as $event) {
|
||||||
call_user_func_array([ $event, 'execute'], $arguments);
|
if ($continue) {
|
||||||
|
call_user_func_array([ $event, 'execute'], $arguments);
|
||||||
|
$continue = empty($event->stopPropagation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -34,4 +34,13 @@ class EventMiddleware implements MiddlewareInterface
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function fromDefinition(EventDefinition $definition) : self
|
||||||
|
{
|
||||||
|
foreach($definition->list as $event) {
|
||||||
|
$this->eventManager->eventRegister($event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Mcnd\Event;
|
||||||
|
|
||||||
|
trait EventTrait
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected bool $stopPropagation = false,
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue