Skip to content

Commit 18a8e5b

Browse files
author
Nil Portugués Calderó
committed
Finished README
1 parent f8f0177 commit 18a8e5b

1 file changed

Lines changed: 137 additions & 4 deletions

File tree

README.md

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ $commandBus($command);
277277

278278
### 1.3 - Custom Middlewares
279279

280+
In order to write custom middleware a new class implementing the `NilPortugues\MessageBus\CommandBus\Contracts\CommandBusMiddleware` interface is required.
281+
280282
----
281283

282284
## 2. QueryBus
@@ -468,17 +470,19 @@ $userQueryResponse = $queryBus($query);
468470

469471
**CacheQueryBusMiddleware**
470472

471-
- Class: `NilPortugues\MessageBus\QueryBus\CacheQueryBusMiddleware`
473+
- **Class**: `NilPortugues\MessageBus\QueryBus\CacheQueryBusMiddleware`
472474
- Class construct method expects a Serializer (see below), a PSR6 Caching implementation and queue name.
473475

474476
**LoggerQueryBusMiddleware**
475477

476-
- Class: `NilPortugues\MessageBus\QueryBus\LoggerQueryBusMiddleware`
478+
- **Class**: `NilPortugues\MessageBus\QueryBus\LoggerQueryBusMiddleware`
477479
- Class construct method expects a PSR3 Logger implementation.
478480

479481

480482
### 2.3 - Custom Middlewares
481483

484+
In order to write custom middleware a new class implementing the `NilPortugues\MessageBus\QueryBus\Contracts\QueryBusMiddleware` interface is required.
485+
482486
----
483487

484488
## 3. EventBus
@@ -652,27 +656,156 @@ final class SendWelcomeEmailHandler implements EventHandler, EventHandlerPriorit
652656
```
653657

654658
#### 3.1.4 - Register the EventHandler
659+
660+
I'm assuming you're using some kind Service Container. Now it's time to register your Event Handlers.
661+
662+
For instance, in a `Interop\Container` compliant Service Container, we can do this as follows:
663+
664+
```php
665+
<?php
666+
//...your other registered classes
667+
668+
$container['UserFriendRepository'] = function() use ($container) {
669+
return []; //your repository
670+
};
671+
672+
$container['UserCreditsRepository'] = function() use ($container) {
673+
return []; //your repository
674+
};
675+
676+
$container['EmailProvider'] = function() use ($container) {
677+
return []; //your email provider
678+
};
679+
680+
$container['SetupUserAccountHandler'] = function() use ($container) {
681+
return new SetupUserAccountHandler(
682+
$container['UserFriendRepository'],
683+
$container['UserCreditsRepository']
684+
);
685+
};
686+
$container['SendWelcomeEmailHandler'] = function() use ($container) {
687+
return new SendWelcomeEmailHandler($container['EmailProvider']);
688+
};
689+
```
690+
691+
655692
#### 3.1.5 - Setting up the EventBusMiddleware
693+
694+
The Event Bus Middleware requires two classes to be injected. First one is the Event translator, and second one the handler resolver.
695+
696+
**EventTranslator**
697+
698+
Classes implementing this interface will provide the FQN for the Handler class given a Event.
699+
700+
This package provides an implementation, `NilPortugues\MessageBus\EventBus\Translator\AppendStrategy` which basically appends the word `Handler` to the provided `Event` class.
701+
702+
For custom strategies, you may write your own implementing the `NilPortugues\MessageBus\EventBus\Contracts\EventTranslator` interface.
703+
704+
**EventHandlerResolver**
705+
706+
Classes implementing this interface will be resolving the class for the instance required based on the output of the EventTranslator used.
707+
708+
This package provides an implementation, `NilPortugues\MessageBus\EventBus\Resolver\InteropContainerResolver`, that expects any Service Container implementing the `Interop\Container` interface.
709+
710+
For custom strategies, such as Symfony Container, you may write your own implementing the `NilPortugues\MessageBus\EventBus\Contracts\EventHandlerResolver` interface.
711+
656712
#### 3.1.6 - Registering the remaining EventBus classes
713+
714+
715+
The minimum set up to get the Event Bus working is:
716+
717+
```php
718+
<?php
719+
//...your other registered classes
720+
721+
$container['EventTranslator'] = function() use ($container) {
722+
return new \NilPortugues\MessageBus\EventBus\Translator\AppendStrategy('Handler');
723+
};
724+
725+
$container['EventHandlerResolver'] = function() use ($container) {
726+
return new \NilPortugues\MessageBus\EventBus\Resolver\InteropContainerResolver($container);
727+
};
728+
729+
$container['EventBusMiddleware'] = function() use ($container) {
730+
return new \NilPortugues\MessageBus\EventBus\EventBusMiddleware(
731+
$container['EventTranslator'],
732+
$container['EventHandlerResolver'],
733+
);
734+
};
735+
736+
$container['EventBus'] = function() use ($container) {
737+
return new \NilPortugues\MessageBus\EventBus\EventBus([
738+
$container['EventBusMiddleware'],
739+
]);
740+
};
741+
```
742+
743+
If for instance, we want to log everything happening in the Event Bus, we'll add to the middleware list the logger middleware. This will wrap the Event Bus, being able to log before and after it ran, and if there was an error.
744+
745+
```php
746+
<?php
747+
//...your other registered classes
748+
749+
$container['LoggerEventBusMiddleware'] = function() use ($container) {
750+
return new \NilPortugues\MessageBus\EventBus\LoggerEventBusMiddleware(
751+
$container['Monolog']
752+
);
753+
};
754+
755+
//Update the EventBus with the LoggerEventBusMiddleware
756+
$container['EventBus'] = function() use ($container) {
757+
return new \NilPortugues\MessageBus\EventBus\EventBus([
758+
$container['LoggerEventBusMiddleware'],
759+
$container['EventBusMiddleware'],
760+
]);
761+
};
762+
```
763+
657764
#### 3.1.7 - Running the EventBus
658765

766+
Finally, to make use of the EventBus, all you need to do is run this code:
767+
```php
768+
<?php
769+
$eventBus = $container->get('EventBus');
770+
$Event = new GetUser(1):
771+
$userEventResponse = $eventBus($Event);
772+
```
659773

660774
#### 3.1.8 - (Optional) Running the EventBus as a Queue
661775

776+
**Save your users time and load your pages faster! Go asynchronous using a queue.**
777+
662778
To do so, you'll have to require an additional package: **EventBus Queue**. This extension can be downloaded using composer:
663779

664780
```
665781
composer require nilportugues/eventbus-queue
666782
```
667783

668-
[Documentation can be found in its repository](https://github.com/PHPMessageBus/event-bus-queue).
784+
[Documentation and installation guide can be found in its repository](https://github.com/PHPMessageBus/event-bus-queue).
669785

786+
### 3.2 - Predefined Middlewares
670787

788+
**TransactionalEventBusMiddleware**
789+
790+
- **Class**: `NilPortugues\MessageBus\EventBus\TransactionalEventBusMiddleware`
791+
- Class construct method expects a PDO connection. It will wrap all the underlying middleware calls with beginTransaction-commit and rollback if any kind of exception is thrown.
792+
793+
**LoggerEventBusMiddleware**
794+
795+
- **Class**: `NilPortugues\MessageBus\EventBus\LoggerEventBusMiddleware`
796+
- Class construct method expects a PSR3 Logger implementation.
797+
798+
**ProducerEventBusMiddleware**
799+
800+
- **Class**: `NilPortugues\MessageBus\EventBusQueue\ProducerEventBusMiddleware`
801+
- Adds events to an Event Queue. Required running `composer require nilportugues/eventbus-queue` first.
671802

672-
### 3.2 - Predefined Middlewares
673803

674804
### 3.3 - Custom Middlewares
675805

806+
In order to write custom middleware a new class implementing the `NilPortugues\MessageBus\EventBus\Contracts\EventBusMiddleware` interface is required.
807+
808+
676809
---
677810

678811
## 4 - Serializers

0 commit comments

Comments
 (0)