Skip to content

Commit

Permalink
Introduce a very simple framework agnostic Event Dispatcher and Named…
Browse files Browse the repository at this point in the history
…Event interface
  • Loading branch information
maks-rafalko committed Jan 23, 2017
1 parent 29d4a9f commit f002526
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
55 changes: 55 additions & 0 deletions src/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace BornFree\TacticianDomainEvent\EventDispatcher;


class EventDispatcher implements EventDispatcherInterface
{
/**
* @var EventListenerInterface[][]
*/
private $listeners = [];

/**
* @inheritdoc
*/
public function dispatch($event)
{
$name = $event instanceof NamedEvent ? $event->getName() : get_class($event);

foreach ($this->getListeners($name) as $listener) {
$listener->handle($event);
}
}

/**
* @param string $eventName
* @param EventListenerInterface $listener
*/
public function addListener($eventName, EventListenerInterface $listener)
{
$this->listeners[$eventName][] = $listener;
}

/**
* @param string $eventName
* @return EventListenerInterface[]
*/
public function getListeners($eventName)
{
if (! $this->hasListeners($eventName)) {
return [];
}

return $this->listeners[$eventName];
}

/**
* @param string $eventName
* @return bool
*/
public function hasListeners($eventName)
{
return isset($this->listeners[$eventName]);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace BornFree\TacticianDomainEvent;
namespace BornFree\TacticianDomainEvent\EventDispatcher;

interface EventBus
interface EventDispatcherInterface
{
/**
* Dispatches an event
Expand Down
13 changes: 13 additions & 0 deletions src/EventDispatcher/EventListenerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace BornFree\TacticianDomainEvent\EventDispatcher;

interface EventListenerInterface
{
/**
* Handles an event
*
* @param mixed $event
*/
public function handle($event);
}
11 changes: 11 additions & 0 deletions src/EventDispatcher/NamedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BornFree\TacticianDomainEvent\EventDispatcher;

interface NamedEvent
{
/**
* @return string
*/
public function getName();
}
14 changes: 7 additions & 7 deletions src/Middleware/ReleaseRecordedEventsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BornFree\TacticianDomainEvent\Middleware;

use BornFree\TacticianDomainEvent\EventBus;
use BornFree\TacticianDomainEvent\EventDispatcher\EventDispatcherInterface;
use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
use League\Tactician\Middleware;

Expand All @@ -14,20 +14,20 @@ class ReleaseRecordedEventsMiddleware implements Middleware
private $eventRecorder;

/**
* @var EventBus
* @var EventDispatcherInterface
*/
private $eventBus;
private $eventDispatcher;

/**
* ReleaseRecorderEventsMiddleware constructor.
*
* @param ContainsRecordedEvents $eventRecorder
* @param EventBus $eventBus
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(ContainsRecordedEvents $eventRecorder, EventBus $eventBus)
public function __construct(ContainsRecordedEvents $eventRecorder, EventDispatcherInterface $eventDispatcher)
{
$this->eventRecorder = $eventRecorder;
$this->eventBus = $eventBus;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -53,7 +53,7 @@ public function execute($command, callable $next)
$recordedEvents = $this->eventRecorder->releaseEvents();

foreach ($recordedEvents as $event) {
$this->eventBus->dispatch($event);
$this->eventDispatcher->dispatch($event);
}
}
}

0 comments on commit f002526

Please sign in to comment.