zelenin/message-bus

Lightweight message bus

0.0.3 2017-07-07 16:53 UTC

This package is not auto-updated.

Last update: 2024-11-10 03:00:19 UTC


README

Installation

Composer

The preferred way to install this extension is through Composer.

Either run

php composer.phar require zelenin/message-bus "~0.0.1"

or add

"zelenin/message-bus": "~0.0.1"

to the require section of your composer.json

Usage

Example

$handlers = [
    CreatePost::class => new CreatePostHandler($postRepository)
];

$middlewares = [
    new HandlerMiddleware(new MemoryLocator($handlers))
];

$commandBus = new MiddlewareBus(new MiddlewareStack($middlewares));

$message = new CreatePost('Post title', 'Post content');

$context = $commandBus->handle($message);

ProviderLocator:

$provider = new AnnotationProvider(__DIR__ . '/src');
if ($isProduction) {
    $provider = new CacheProvider(__DIR__ . '/data/handlers-cache.php', $provider);
}
$locator = new ProviderLocator($provider, new ContainerHandlerResolver($container));

return new MiddlewareBus(new MiddlewareStack([
    new HandlerMiddleware($locator),
]));

ContainerHandlerResolver may be used for PSR-11 Container support.

Context

The Message bus uses the immutable Context to transfer data through middlewares and return it to consumer.

Author

Aleksandr Zelenin, e-mail: aleksandr@zelenin.me