andreo / eventsauce-messenger
Symfony messenger for EventSauce message dispatcher.
Installs: 3 405
Dependents: 1
Suggesters: 1
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=8.2
- eventsauce/eventsauce: ^3.0
- symfony/dependency-injection: ^6.2
- symfony/messenger: ^6.2
Requires (Dev)
- phpstan/phpstan: ^1.3
- phpunit/phpunit: ^9.4
- roave/security-advisories: dev-latest
- symfony/amqp-messenger: ^6.1
- symfony/framework-bundle: ^6.1
README
Integration symfony messenger for EventSauce
Installation
composer require andreo/eventsauce-messenger
Previous versions doc
Requirements
- PHP >=8.2
- Symfony messenger ^6.2
Event consumption
See more about Event Consumers
Message handler example
use Andreo\EventSauce\Messenger\EventConsumer\InjectedHandleMethodInflector; use EventSauce\EventSourcing\EventConsumption\EventConsumer; use EventSauce\EventSourcing\EventConsumption\HandleMethodInflector; use Andreo\EventSauce\Messenger\Attribute\AsEventSauceMessageHandler; use EventSauce\EventSourcing\Message; final class FooBarBazMessageHandler extends EventConsumer { // copy-paste trait for inject HandleMethodInflector of EventSauce // This example use EventSauce\EventSourcing\EventConsumption\InflectHandlerMethodsFromType. Remember, register your way use InjectedHandleMethodInflector; public function __construct( private HandleMethodInflector $handleMethodInflector ) {} #[AsEventSauceMessageHandler(bus: 'eventBus')] public function onFooCreated(FooCreated $fooCreated, Message $message): void { } // You can define more handlers also union types(only with InflectHandlerMethodsFromType) if you want as below #[AsEventSauceMessageHandler(bus: 'eventBus')] public function onBarOrBazCreated(BarCreated|BazCreated $barCreated, Message $message): void { } }
Configuration
AsEventSauceMessageHandler
attribute works with symfony autoconfigure feature.
If you want to use you need to register attribute.
use Andreo\EventSauce\Messenger\DependencyInjection\RegisterEventSauceMessageHandlerAttribute; class Kernel extends BaseKernel { use MicroKernelTrait; protected function build(ContainerBuilder $container): void { RegisterEventSauceMessageHandlerAttribute::register($container); } }
If you don't want to use autoconfiguration, you can register the handlers manually.
services: # ... App\Handler\FooBarBazMessageHandler: tags: - name: messenger.message_handler handles: App\Message\FooCreated bus: eventBus method: handle # must be set handle method of EventSauce EventConsumer - name: messenger.message_handler handles: App\Message\BarCreated bus: eventBus method: handle - name: messenger.message_handler handles: App\Message\BazCreated bus: eventBus method: handle
Rest configuration
Your services
services: # ... Andreo\EventSauce\Messenger\Dispatcher\MessengerMessageDispatcher: arguments: $eventBus: 'eventBus' # bus alias from messenger config Andreo\EventSauce\Messenger\Middleware\HandleEventSauceMessageMiddleware: arguments: # change handlers locator prefix to your bus alias from messenger config $handlersLocator: '@eventBus.messenger.handlers_locator'
Messenger config
framework: messenger: # ... buses: eventBus: # disable default config of messenger middleware default_middleware: false # minimal middleware config. Note that there are other middleware you may want to use - check messenger docs middleware: - 'send_message' - 'Andreo\EventSauce\Messenger\Middleware\HandleEventSauceMessageMiddleware' - 'handle_message' # if you want to use default handling also, this middleware must be last set