-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from codeliner/message-bus
Message bus
- Loading branch information
Showing
13 changed files
with
404 additions
and
501 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
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 |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
/** | ||
* Class Command | ||
* | ||
* Can be used as base class for commands. | ||
* The class can be used as base class for commands, but it is no requirement. | ||
* You can dispatch all kinds of messages as long as you register plugins that are able to handle your messages. | ||
* | ||
* @package Prooph\ServiceBus\Command | ||
* @author Alexander Miertsch <[email protected]> | ||
|
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 |
---|---|---|
|
@@ -14,97 +14,16 @@ | |
use Prooph\ServiceBus\Exception\CommandDispatchException; | ||
use Prooph\ServiceBus\Exception\RuntimeException; | ||
use Prooph\ServiceBus\Process\CommandDispatch; | ||
use Zend\EventManager\EventManager; | ||
use Zend\EventManager\EventManagerAwareInterface; | ||
use Zend\EventManager\EventManagerInterface; | ||
use Zend\EventManager\ListenerAggregateInterface; | ||
use Zend\Log\LoggerInterface; | ||
use Zend\Stdlib\CallbackHandler; | ||
|
||
/** | ||
* Class CommandBus | ||
* | ||
* @package Prooph\ServiceBus | ||
* @author Alexander Miertsch <[email protected]> | ||
*/ | ||
class CommandBus implements EventManagerAwareInterface | ||
class CommandBus extends MessageBus | ||
{ | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var EventManager | ||
*/ | ||
protected $events; | ||
|
||
/** | ||
* @param ListenerAggregateInterface|LoggerInterface $plugin | ||
* @return $this | ||
* @throws Exception\RuntimeException | ||
*/ | ||
public function utilize($plugin) | ||
{ | ||
if ($plugin instanceof ListenerAggregateInterface) { | ||
$plugin->attach($this->getEventManager()); | ||
} else if ($plugin instanceof LoggerInterface) { | ||
$this->logger = $plugin; | ||
} else { | ||
throw new RuntimeException( | ||
sprintf( | ||
"CommandBus cannot use plugin of type %s.", | ||
(is_object($plugin))? get_class($plugin) : gettype($plugin) | ||
) | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param ListenerAggregateInterface|LoggerInterface $plugin | ||
* @return $this | ||
* @throws Exception\RuntimeException | ||
*/ | ||
public function deactivate($plugin) | ||
{ | ||
if ($plugin instanceof ListenerAggregateInterface) { | ||
$plugin->detach($this->getEventManager()); | ||
} else if ($plugin instanceof LoggerInterface) { | ||
$this->logger = null; | ||
} else { | ||
throw new RuntimeException( | ||
sprintf( | ||
"CommandBus cannot detach plugin of type %s.", | ||
(is_object($plugin))? get_class($plugin) : gettype($plugin) | ||
) | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $eventName | ||
* @param callable $listener | ||
* @param int $priority | ||
* @return \Zend\Stdlib\CallbackHandler | ||
*/ | ||
public function on($eventName, $listener, $priority = 1) | ||
{ | ||
return $this->getEventManager()->attach($eventName, $listener, $priority); | ||
} | ||
|
||
/** | ||
* @param CallbackHandler $callbackHandler | ||
* @return bool | ||
*/ | ||
public function off(CallbackHandler $callbackHandler) | ||
{ | ||
return $this->getEventManager()->detach($callbackHandler); | ||
} | ||
|
||
/** | ||
* @param mixed $command | ||
* @throws Exception\CommandDispatchException | ||
|
@@ -168,40 +87,6 @@ public function dispatch($command) | |
$this->triggerFinalize($commandDispatch); | ||
} | ||
|
||
/** | ||
* @param CommandDispatch $commandDispatch | ||
* @throws Exception\RuntimeException | ||
*/ | ||
protected function trigger(CommandDispatch $commandDispatch) | ||
{ | ||
$result = $this->getEventManager()->trigger($commandDispatch); | ||
|
||
if ($result->stopped()) { | ||
throw new RuntimeException("Dispatch has stopped unexpectedly."); | ||
} | ||
} | ||
|
||
/** | ||
* @param CommandDispatch $commandDispatch | ||
*/ | ||
protected function triggerError(CommandDispatch $commandDispatch) | ||
{ | ||
$commandDispatch->setName(CommandDispatch::HANDLE_ERROR); | ||
|
||
$this->getEventManager()->trigger($commandDispatch); | ||
} | ||
|
||
/** | ||
* @param CommandDispatch $commandDispatch | ||
*/ | ||
protected function triggerFinalize(CommandDispatch $commandDispatch) | ||
{ | ||
$commandDispatch->setName(CommandDispatch::FINALIZE); | ||
|
||
$this->getEventManager()->trigger($commandDispatch); | ||
} | ||
|
||
|
||
/** | ||
* Inject an EventManager instance | ||
* | ||
|
@@ -215,23 +100,7 @@ public function setEventManager(EventManagerInterface $eventManager) | |
__CLASS__ | ||
)); | ||
|
||
$this->events = $eventManager; | ||
} | ||
|
||
/** | ||
* Retrieve the event manager | ||
* | ||
* Lazy-loads an EventManager instance if none registered. | ||
* | ||
* @return EventManagerInterface | ||
*/ | ||
public function getEventManager() | ||
{ | ||
if (is_null($this->events)) { | ||
$this->setEventManager(new EventManager()); | ||
} | ||
|
||
return $this->events; | ||
parent::setEventManager($eventManager); | ||
} | ||
} | ||
|
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 |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
/** | ||
* Class Event | ||
* | ||
* Can be used as base event class | ||
* The class can be used as base class for events, but it is no requirement. | ||
* You can dispatch all kinds of messages as long as you register plugins that are able to handle your messages. | ||
* | ||
* @package Prooph\ServiceBus\Event | ||
* @author Alexander Miertsch <[email protected]> | ||
|
Oops, something went wrong.