Reformatting the EventDispatcher class.

This commit is contained in:
Ilya Bakhlin 2021-12-28 19:50:49 +01:00
parent 336000b8d4
commit c89cd500a1

View file

@ -1,33 +1,36 @@
<?php declare(strict_types=1); <?php
declare(strict_types = 1);
namespace Chandler\Eventing; namespace Chandler\Eventing;
use Chandler\Patterns\TSimpleSingleton; use Chandler\Patterns\TSimpleSingleton;
/**
* @package Chandler\Eventing
*/
class EventDispatcher class EventDispatcher
{ {
private $hooks = []; private $hooks = [];
function addListener($hook): bool function addListener($hook): bool
{ {
$this->hooks[] = $hook; $this->hooks[] = $hook;
return true; return true;
} }
function pushEvent(Events\Event $event): Events\Event function pushEvent(Events\Event $event): Events\Event
{ {
foreach($hooks as $hook) { foreach ($hooks as $hook) {
if($event instanceof Events\Cancelable) if ($event instanceof Events\Cancelable)
if($event->isCancelled()) if ($event->isCancelled())
break; break;
$method = "on" . str_replace("Event", "", get_class($event)); $method = "on" . str_replace("Event", "", get_class($event));
if(!method_exists($hook, $methodName)) continue; if (!method_exists($hook, $methodName)) continue;
$hook->$method($event); $hook->$method($event);
} }
return $event; return $event;
} }
use TSimpleSingleton; use TSimpleSingleton;
} }