You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+137-4Lines changed: 137 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -277,6 +277,8 @@ $commandBus($command);
277
277
278
278
### 1.3 - Custom Middlewares
279
279
280
+
In order to write custom middleware a new class implementing the `NilPortugues\MessageBus\CommandBus\Contracts\CommandBusMiddleware` interface is required.
- Class construct method expects a PSR3 Logger implementation.
478
480
479
481
480
482
### 2.3 - Custom Middlewares
481
483
484
+
In order to write custom middleware a new class implementing the `NilPortugues\MessageBus\QueryBus\Contracts\QueryBusMiddleware` interface is required.
485
+
482
486
----
483
487
484
488
## 3. EventBus
@@ -652,27 +656,156 @@ final class SendWelcomeEmailHandler implements EventHandler, EventHandlerPriorit
652
656
```
653
657
654
658
#### 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
+
655
692
#### 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
+
656
712
#### 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
+
657
764
#### 3.1.7 - Running the EventBus
658
765
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
+
```
659
773
660
774
#### 3.1.8 - (Optional) Running the EventBus as a Queue
661
775
776
+
**Save your users time and load your pages faster! Go asynchronous using a queue.**
777
+
662
778
To do so, you'll have to require an additional package: **EventBus Queue**. This extension can be downloaded using composer:
663
779
664
780
```
665
781
composer require nilportugues/eventbus-queue
666
782
```
667
783
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).
- 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.
- Adds events to an Event Queue. Required running `composer require nilportugues/eventbus-queue` first.
671
802
672
-
### 3.2 - Predefined Middlewares
673
803
674
804
### 3.3 - Custom Middlewares
675
805
806
+
In order to write custom middleware a new class implementing the `NilPortugues\MessageBus\EventBus\Contracts\EventBusMiddleware` interface is required.
0 commit comments