Skip to content

Commit

Permalink
Move messaging base classes to prooph/common
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Miertsch committed May 1, 2015
1 parent 3cab290 commit 62be6ce
Show file tree
Hide file tree
Showing 63 changed files with 423 additions and 1,431 deletions.
4 changes: 4 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# for php-coveralls
service_name: travis-ci
src_dir: src
coverage_clover: build/logs/clover.xml
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ php:
- 5.5
- 5.6

services:
- redis-server


before_script:
- composer self-update
- composer --dev install

script:
- php ./vendor/bin/phpunit -c ./tests/.
- php ./vendor/bin/phpunit --coverage-text --coverage-clover ./../build/logs/clover.xml -c ./tests/.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ $commandBus->utilize($router);
//Expand command bus with the callback invoke strategy
$commandBus->utilize(new CallbackStrategy());

//Create a new Command
$echoText = EchoText::fromPayload('It works');
//We create a new Command
$echoText = EchoText::fromString('It works');

//... and dispatch it
$commandBus->dispatch($echoText);
Expand Down
8 changes: 2 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
"require": {
"php": ">=5.5",
"beberlei/assert": "~2.0",
"prooph/common" : "~1.4",
"rhumsaa/uuid" : "~2.5"
"prooph/common" : "~1.5"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"chrisboulton/php-resque": "~1.2",
"prooph/event-store": "~1.0",
"prooph/event-sourcing": "~1.0",
"zendframework/zend-servicemanager": "~2.3"
"satooshi/php-coveralls": "dev-master"
},
"suggest": {
"prooph/event-store": "Use ProophEventStore and let the EventBus dispatch persisted DomainEvents",
Expand Down
6 changes: 3 additions & 3 deletions docs/command_bus.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Following action events are triggered in the listed order:
- `initialize`: This action event is triggered right after CommandBus::dispatch($command) is invoked. At this time the CommandDispatch only contains the command.

- `detect-message-name` (optional): Before a command handler can be located, the CommandBus needs to know how the command is named. Their are two
possibilities to provide the information. The command can implement the [Prooph\ServiceBus\Message\MessageNameProvider](../src/Prooph/ServiceBus/Message/MessageNameProvider.php) interface.
possibilities to provide the information. The command can implement the [Prooph\Common\Messaging\HasMessageName](https://github.com/prooph/common/blob/master/src/Messaging/HasMessageName.php) interface.
In this case the CommandBus picks the command name directly from the command and inject it manually in the CommandDispatch. The `detect-message-name` event is not triggered. If the command
does not implement the interface the `detect-message-name` event is triggered and a plugin needs to inject the name using `CommandDispatch::setCommandName`.

Expand All @@ -98,8 +98,8 @@ attach a monitoring plugin.
# Commands

A command can nearly be everything. PSB tries to get out of your way as much as it can. You are ask to use your own command implementation or you use the
default [Command](../src/Prooph/ServiceBus/Command.php) class provided by PSB. It is a very good base class and PSB ships with translator plugins to translate a Command into a message
that can be send to a remote interface. Check the [Asynchronous Message Dispatcher](message_dispatcher.md) for more details. However, you can provide
default [Command](https://github.com/prooph/common/blob/master/src/Messaging/Command.php) class provided by prooph/common. It is a very good base class and PSB ships with translator plugins to translate a Command into a remote message
that can be send to a remote interface. Check the [Remote Message Dispatcher](message_dispatcher.md) for more details. However, you can provide
your own message translator plugin, a plugin that is capable of detecting the name of the command and an invoke strategy that knows how to invoke
your command handlers with the command. Mix and match the plugins provided by PSB with your own ones to decouple your implementation from the PSB infrastructure.

Expand Down
6 changes: 3 additions & 3 deletions docs/event_bus.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Following action events are triggered in the listed order:
- `initialize`: This action event is triggered right after EventBus::dispatch($event) is invoked. At this time the EventDispatch only contains the event message.

- `detect-message-name` (optional): Before an event message listener can be located, the EventBus needs to know how the event message is named. Their are two
possibilities to provide the information. The event message can implement the [Prooph\ServiceBus\Message\MessageNameProvider](../src/Prooph/ServiceBus/Message/MessageNameProvider.php) interface.
possibilities to provide the information. The event message can implement the [Prooph\Common\Messaging\HasMessageName](https://github.com/prooph/common/blob/master/src/Messaging/HasMessageName.php) interface.
In this case the EventBus picks the message name directly from the event message and inject it manually in the EventDispatch. The `detect-message-name` action event is not triggered. If the event message
does not implement the interface the `detect-message-name` action event is triggered and a plugin needs to inject the name using `EventDispatch::setEventName`.

Expand Down Expand Up @@ -102,8 +102,8 @@ attach a monitoring plugin.
# Event Messages

An event message can nearly be everything. PSB tries to get out of your way as much as it can. You are ask to use your own event message implementation or you use the
default [Event](../src/Prooph/ServiceBus/Event.php) class provided by PSB. It is a very good base class and PSB ships with translator plugins to translate an event message into a Prooph\ServiceBus\Message\StandardMessage
that can be send to a remote interface. Check the [Asynchronous Message Dispatcher](message_dispatcher.md) for more details. However, you can provide
default [DomainEvent](https://github.com/prooph/common/blob/master/src/Messaging/DomainEvent.php) class provided by prooph/common. It is a very good base class and PSB ships with translator plugins to translate an event message into a remote message
that can be send to a remote interface. Check the [Remote Message Dispatcher](message_dispatcher.md) for more details. However, you can provide
your own message translator plugin, a plugin that is capable of detecting the name of the event message and an invoke strategy that knows how to invoke
your event message listeners with the event message. Mix and match the plugins provided by PSB with your own ones to decouple your implementation from the PSB infrastructure.

Expand Down
34 changes: 17 additions & 17 deletions docs/message_dispatcher.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Asynchronous MessageDispatcher
RemoteMessageDispatcher
==============================

[Back to documentation](../README.md#documentation)
Expand All @@ -9,7 +9,7 @@ Messaging becomes really interesting when you process your messages asynchronous
set up a cron job to periodically check the queue for new messages and process them. The bus implementations of PSB can
hide such an asynchronous workflow behind a unified interface. You can start with synchronous message dispatching by
routing your messages directly to message handlers and if you later want to improve response times you can switch to
async processing on message basis by routing the appropriate messages to a [MessageDispatcher](../src/Prooph/ServiceBus/Message/MessageDispatcherInterface.php).
async processing on message basis by routing the appropriate messages to a [RemoteMessageDispatcher](../src/Prooph/ServiceBus/Message/RemoteMessageDispatcher.php).

## Synchronous Dispatch
```php
Expand Down Expand Up @@ -43,31 +43,31 @@ $router->route('SomethingDone')->to(new My\Async\MessageDispatcher());

$eventBus->utilize($router);

//The event needs to be translated to a Prooph\ServiceBus\Message\MessageInterface
//The ForwardToMessageDispatcherStrategy checks if the listener of the event
//is an instance of Prooph\ServiceBus\Message\MessageDispatcherInterface
//and translates the event with the help of a Prooph\ServiceBus\Message\ToMessageTranslatorInterface
$eventBus->utilize(new ForwardToMessageDispatcherStrategy(new ToMessageTranslator()));
//The domain event needs to be translated to a Prooph\Common\Messaging\RemoteMessage
//The ForwardToRemoteMessageDispatcherStrategy checks if the listener of the event
//is an instance of Prooph\ServiceBus\Message\RemoteMessageDispatcher
//and translates the event with the help of a Prooph\ServiceBus\Message\ToRemoteMessageTranslator
$eventBus->utilize(new ForwardToRemoteMessageDispatcherStrategy(new ProophDomainMessageToRemoteMessageTranslator()));


//Now the event is dispatched to a MessageDispatcher instead of a listener
//Now the event is dispatched to a RemoteMessageDispatcher instead of a listener
$eventBus->dispatch(new SomethingDone());

//Behind the scenes the message is translated to an array and pushed on a message queue
//Behind the scenes the remote message is translated to an array and pushed on a message queue
//There are various messaging tools available. We try to support the most important ones and
//continue to add more. Check the list below for available adapters. If your favorite adapter is
//not on the list you can easily implement it
//by implementing the Prooph\ServiceBus\Message\MessageDispatcherInterface
//by implementing the Prooph\ServiceBus\Message\RemoteMessageDispatcher interface
//
//Now imagine that a worker has pulled the message array from the queue and want to process it
//It can simply create a message object from the array ...
$message = \Prooph\ServiceBus\Message\StandardMessage::fromArray($messageArr);
//It can simply create a remote message object from the array ...
$message = \Prooph\Common\Messaging\RemoteMessage::fromArray($messageArr);

//... set up another EventBus with a Prooph\ServiceBus\Message\FromMessageTranslator
//to translate the incoming message back to an event ...
//... set up another EventBus with a Prooph\ServiceBus\Message\FromRemoteMessageTranslator
//to translate the incoming message back to a domain event ...
$eventBus = new EventBus();

$eventBus->utilize(new FromMessageTranslator());
$eventBus->utilize(new FromRemoteMessageTranslator());

$router = new EventRouter();

Expand All @@ -81,9 +81,9 @@ $eventBus->utilize(new OnInvokeStrategy());
$eventBus->dispatch($message);
```

# Available MessageDispatchers
# Available RemoteMessageDispatchers

- [InMemoryMessageDispatcher](../src/Prooph/ServiceBus/Message/InMemoryMessageDispatcher.php): useful for tests,
- [InMemoryRemoteMessageDispatcher](../src/Prooph/ServiceBus/Message/InMemoryRemoteMessageDispatcher.php): useful for tests,
you can replace your async dispatcher with this one
- [PhpResqueMessageDispatcher](https://github.com/prooph/psb-php-resque-dispatcher): Perfect choice for async
command processing using a ultra fast redis queue
Expand Down
24 changes: 12 additions & 12 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ by extending the [AbstractInvokeStrategy](../src/Prooph/ServiceBus/InvokeStrateg
- `HandleCommandStrategy`: Is responsible for invoking a `handle` method of a command handler. Forces the rule that a command handler should only be responsible for handling one specific command.
- `OnEventStrategy`: Prefixes the short class name of an event with `on`. A listener should
have a public method named this way: OrderCartUpdater::onArticleWasBought.
- `ForwardToMessageDispatcherStrategy`: This is a special invoke strategy that is capable of translating a command or event to
a [StandardMessage](../src/Prooph/ServiceBus/Message/StandardMessage.php) and invoke a [MessageDispatcher](message_dispatcher.md).
Add this strategy to a bus together with a [ToMessageTranslator](../src/Prooph/ServiceBus/Message/ToMessageTranslatorInterface.php) and
route a command or event to a MessageDispatcher to process the message async:
- `ForwardToRemoteMessageDispatcherStrategy`: This is a special invoke strategy that is capable of translating a command or event to
a [RemoteMessage](https://github.com/prooph/common/blob/master/src/Messaging/RemoteMessage.php) and invoke a [RemoteMessageDispatcher](message_dispatcher.md).
Add this strategy to a bus together with a [ToRemoteMessageTranslator](../src/Prooph/ServiceBus/Message/ToRemoteMessageTranslator.php) and
route a command or event to a RemoteMessageDispatcher to process the message async:

```php
$eventBus->utilize(new ForwardToMessageDispatcherStrategy(new ToMessageTranslator()));
$eventBus->utilize(new ForwardToRemoteMessageDispatcherStrategy(new ProophDomainMessageToRemoteMessageTranslator()));

$router = new EventRouter();

Expand All @@ -114,16 +114,16 @@ $eventBus->utilize($router);
$eventBus->dispatch(new SomethingDone());
```

# FromMessageTranslator
# FromRemoteMessageTranslator

The [FromMessageTranslator](../src/Prooph/ServiceBus/Message/FromMessageTranslator.php) plugin does the opposite of the `ForwardToMessageDispatcherStrategy`.
It listens on the `initialize` dispatch action event of a CommandBus or EventBus and if it detects an incoming [message](../src/Prooph/ServiceBus/Message/MessageInterface.php)
it translates the message to a [Command](../src/Prooph/ServiceBus/Command.php) or [Event](../src/Prooph/ServiceBus/Event.php) depending on the type
provided in the [MessageHeader](../src/Prooph/ServiceBus/Message/MessageHeaderInterface.php). A receiver of an asynchronous dispatched message, for example a worker of a
message queue, can pull a [message](../src/Prooph/ServiceBus/Message/MessageInterface.php) from the queue and forward it to a appropriate configured CommandBus or EventBus without additional work.
The [FromRemoteMessageTranslator](../src/Prooph/ServiceBus/Message/FromRemoteMessageTranslator.php) plugin does the opposite of the `ForwardToRemoteMessageDispatcherStrategy`.
It listens on the `initialize` dispatch action event of a CommandBus or EventBus and if it detects an incoming [RemoteMessage](https://github.com/prooph/common/blob/master/src/Messaging/RemoteMessage.php)
it translates the message to a [Command](https://github.com/prooph/common/blob/master/src/Messaging/Command.php) or [DomainEvent](https://github.com/prooph/common/blob/master/src/Messaging/DomainEvent.php) depending on the type
provided in the [MessageHeader](https://github.com/prooph/common/blob/master/src/Messaging/MessageHeader.php). A receiver of an asynchronous dispatched message, for example a worker of a
message queue, can pull a [RemoteMessage](https://github.com/prooph/common/blob/master/src/Messaging/RemoteMessage.php) from the queue and forward it to a appropriate configured CommandBus or EventBus without additional work.

*Note: If the message name is an existing class it is used instead of the default implementation.
But the constructor of the class should accept the same arguments as the default implementation does, otherwise you need to use your own message translator.
But the custom message class MUST provide a static `fromRemoteMessage` factory method, otherwise the translator will break!

# ServiceLocatorProxy

Expand Down
2 changes: 1 addition & 1 deletion docs/service_bus_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ PSB provides both possibilities behind a unified interface.
Remember the statement "Messaging means fire and forget".
The message sender never knows if the message is processed synchronous or asynchronous. It depends on the bus
configuration and/or the used plugins. A message can directly be routed to it's handler. In this case we talk about synchronous
message processing. If the receiver of the message is a [Prooph\ServiceBus\Message\MessageDispatcherInterface](message_dispatcher.md)
message processing. If the receiver of the message is a [Prooph\ServiceBus\Message\RemoteMessageDispatcher](message_dispatcher.md)
the message is normally processed asynchronously.
13 changes: 11 additions & 2 deletions examples/quick-start.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@

namespace Prooph\ServiceBus\Example\Command {

use Prooph\ServiceBus\Command;
use Prooph\Common\Messaging\Command;

class EchoText extends Command
{
/**
* @param string $text
* @return EchoText
*/
public static function fromString($text)
{
return new self(__CLASS__, $text);
}

protected function convertPayload($textOrPayload)
{
if (is_string($textOrPayload)) {
Expand Down Expand Up @@ -57,7 +66,7 @@ public function getText()
$commandBus->utilize(new CallbackStrategy());

//We create a new Command
$echoText = EchoText::fromPayload('It works');
$echoText = EchoText::fromString('It works');

//... and dispatch it
$commandBus->dispatch($echoText);
Expand Down
Loading

0 comments on commit 62be6ce

Please sign in to comment.