Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Packagist Build Status Code Quality Code Coverage Downloads

ICanBoogie/MessageBus provides a very simple message dispatcher that tries to be as flexible as possible: the handler provider is defined as a simple callable that you can implement or decorate with your favorite resolver.


namespace ICanBoogie\MessageBus;

/* @var HandlerProvider $handler_provider */

$dispatcher = new SimpleDispatcher($handler_provider);

/* @var object $message */

// The message is dispatched by an handler
$result = $dispatcher->dispatch($message);

/* @var callable $assertion */

$asserting_dispatcher = new AssertingDispatcher($dispatcher, $assertion);

Message handler provider

The message handler provider is a callable with a signature similar to the HandlerProvider interface, the package provides a simple message handler provider that only requires an array of key/value pairs, where key is a message class and value a message handler callable.

Providing handlers

The following example demonstrates how to define a message handler provider with a selection of messages and their handlers:


use App\Application\Message;
use ICanBoogie\MessageBus\SimpleHandlerProvider;

$handler_provider = new SimpleHandlerProvider([

	Message\CreateArticle::class => function (Message\CreateArticle $message) {

		// create an article


	Message\DeleteArticle::class => function (Message\DeleteArticle $message) {

        // delete an article



Providing handlers with icanboogie/service

Of course, if you're using the icanboogie/service package, you can use service references instead of callables (well, technically, they are also callables):


use App\Application\Message;
use ICanBoogie\MessageBus\SimpleHandlerProvider;

use function ICanBoogie\Service\ref;

$handler_provider = new SimpleHandlerProvider([

	Message\CreateArticle::class => ref('handler.article.create'),
	Message\DeleteArticle::class => ref('handler.article.delete'),


Providing handlers with a PSR container

Use an instance of PSR\ContainerHandlerProvider to provide handlers from a PSR container:


use App\Application\Message;
use ICanBoogie\MessageBus\PSR\ContainerHandlerProvider;

/* @var $container \Psr\Container\ContainerInterface */

$handler_provider = new ContainerHandlerProvider([

	Message\CreateArticle::class => 'handler.article.create',
	Message\DeleteArticle::class => 'handler.article.delete',

], $container);

If you're using symfony/dependency-injection you can add an instance of HandlerProviderPass to your compilation pass to automatically generate the provider:


    autowire: true
  	synthetic: true

    class: App\Domain\Article\Handler\CreateArticleHandler
      - name: message_dispatcher.handler
        message: App\Application\Message\CreateArticle

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use ICanBoogie\MessageBus\Symfony\HandlerProviderPass;

/* @var string $config */

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$container->addCompilerPass(new HandlerProviderPass);

Asserting messages before dispatching

The AssertingDispatcher decorator can be used to assert messages before they are dispatched. One could use the assertion to reject messages that require special permissions.


use ICanBoogie\MessageBus\AssertingDispatcher;

/* @var ICanBoogie\MessageBus\Dispatcher $dispatcher */

$asserting_dispatcher = new AssertingDispatcher($dispatcher, function ($message) {

	if (/* some condition */)
		throw new \LogicException("The message should not be dispatched.");



The package requires PHP 7.2 or later.


The recommended way to install this package is through Composer:

$ composer require icanboogie/message-bus

Cloning the repository

The package is available on GitHub, its repository can be cloned with the following Message line:

$ git clone


The package is documented as part of the ICanBoogie framework documentation. You can generate the documentation for the package and its dependencies with the make doc Message. The documentation is generated in the build/docs directory. ApiGen is required. The directory can later be cleaned with the make clean Message.


The test suite is ran with the make test Message. PHPUnit and Composer need to be globally available to run the suite. The Message installs dependencies as required. The make test-coverage Message runs test suite and also creates an HTML coverage report in build/coverage. The directory can later be cleaned with the make clean Message.

The package is continuously tested by Travis CI.

Build Status Code Coverage


icanboogie/message-bus is licensed under the New BSD License - See the LICENSE file for details.


A simple message bus built with a functional approach to be as flexible as possible.





No packages published
You can’t perform that action at this time.