We are currently working on a project that is evoliving in such a way that
using a messagebus would simplify code a lot, and would help us deliver
higher quality in less time. After looking around on packagist for
available event/command busses, I decided to try out the SimpleBus.
The docs are a bit sparse, they do give you all the necessary information, and
it is a quick setup.
We use Zend Service Manager as DIC and it is fairly
straight forward to implement it.
Following the example from the docs, first you would declare your event
And now for the important part, you would create a factory for
ServiceLocatorAwareCallableResolver in which it is instantiated with
a service locator callable that will instantiate the subscriber service
when needed with Zend Service Manager as shown on lines 20-22
Then put together the example as described in docs
1
2
3
4
5
6
7
8
9
10
11
12
<?php
$eventBus =newMessageBusSupportingMiddleware();
$eventBus->appendMiddleware(newFinishesHandlingMessageBeforeHandlingNext());
// Provide a map of event names to callables. You can provide actual callables, or lazy-loading ones.
$eventSubscribersByEventName = [
UserRegistered::class=> [
SendWelcomeMailWhenUserRegistered::class,
SendWelcomeMailWhenUserRegisteredAndSomething::class
]
];
With a slight difference when declaring a subscriber collection, on line 5
Service Manager is used to resolve the ServiceLocatorAwareCallableResolver which
will inject it with callable setup to resolve the requested subscribers