telephantast / message-bus
Thesis Message Bus
Fund package maintenance!
www.tinkoff.ru/cf/5MqZQas2dk7
0.2.x-dev
2025-04-05 21:25 UTC
Requires
- php: ^8.3
- psr/clock: ^1.0
- psr/container: ^2.0
- psr/log: ^3
- thesis/amqp: ^0.2.0
- thesis/message: ^0.2@dev
- typhoon/describe: ^0.1@dev
Requires (Dev)
- ext-pdo: *
- amphp/postgres: ^2.1.1
- bamarni/composer-bin-plugin: ^1.8.2
- phpunit/phpunit: ^12.1.0
- symfony/var-dumper: ^6.4.15 || ^7.2.3
README
Installation
composer require thesis/message-bus
Quick Start
use Thesis\Message\Command; use Thesis\Message\Event; use Thesis\Message\Message; use Thesis\MessageBus\MessageBus; use Thesis\MessageBus\Handler\ArrayHandlerRegistry; use Thesis\MessageBus\Handler\CallableHandler; use Thesis\MessageBus\Context; // First, declare a bunch of messages. final readonly class RegisterUser implements Command { public function __construct( public string $nickname, public string $firstName, ) {} } final readonly class UserRegistered implements Event { public function __construct( public string $nickname, ) {} } /** * @implements Message<string> */ final readonly class GetUserName implements Message { public function __construct( public string $nickname, ) {} } // Then assemble a simple synchronous MessageBus. $firstNames = []; $messageBus = new MessageBus( handlerRegistry: new ArrayHandlerRegistry([ RegisterUser::class => new CallableHandler( handler: function (RegisterUser $command, Context $context) use (&$firstNames): void { $firstNames[$command->nickname] = $command->firstName; $context->dispatch(new UserRegistered($command->nickname)); }, ), UserRegistered::class => new CallableHandler( handler: function (UserRegistered $event): void { echo "User {$event->nickname} has just registered!\n"; }, ), GetUserName::class => new CallableHandler( handler: function (GetUserName $query) use (&$firstNames): string { return $firstNames[$query->nickname]; }, ), ]), ); // Give it a try! $messageBus->dispatch(new RegisterUser('vudaltsov', 'Valentin')); $firstName = $messageBus->dispatch(new GetUserName('vudaltsov')); echo "Get acquainted with {$firstName}!\n";