-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a very simple framework agnostic Event Dispatcher and Named…
…Event interface
- Loading branch information
1 parent
29d4a9f
commit f002526
Showing
5 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
src/EventBus.php → ...ntDispatcher/EventDispatcherInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters