A package for leveraging the Prooph Service Bus in a Laravel project
This package supports the use of the prooph service buses inside of a Laravel application.
- Support default buses that are bound to the classes
- Support unlimited named buses
- Add loggers to all buses
- Automatically resolve handlers
- Collect profiling data in debug mode
- Async switch
composer require camuthig/service-bus-laravel-package
php artisan vendor:publish
The package will automatically be discovered by Laravel when installed, no changes to include the service provider are needed.
The default instance of each bus type can be retrieved in two ways:
<?php use \Camuthig\ServiceBus\Package\Test\Fixture\TestEvent; // Using the facade \Camuthig\ServiceBus\Package\Facade\EventBus::dispatch(new TestEvent()); // Getting the Prooph interface from the container app()->make(\Prooph\ServiceBus\EventBus::class)->dispatch(new TestEvent());
Each bus type (command, event and query) includes both a singleton interface as well as a facade.
This package also supports having more than one of each bus type. To leverage a
non-default bus, you will want to use the
ServiceBusManager. The manager gives
you access to all of the buses by name.
The service manager can be retrieved in three ways:
<?php // As a facade \Camuthig\ServiceBus\Package\Facade\ServiceBus::eventBus('other_bus'); // Getting the interface from the container $manager = app()->make(\Camuthig\ServiceBus\Package\Contracts\ServiceBusManager::class); $manager->eventBus('other_bus'); // Getting the `service_bus` from the container $manager = app()->make('service_bus'); $manager->eventBus('other_bus');
The buses can be configured using the
service_bus.php configuration file. Each
type of bus will have it's own list of of named buses with the following options:
- message_factory The service ID or class name of the message factory to use with the bus. The default is the prooph FQCNMessageFactory if this key is not provided.
- action_event_emitter The service ID or class name of the message factory to use with the bus. The default is the prooph ProophActionEventEmitter if this key is not provided.
- plugins A list of service IDs or class names of plugins to add to the the bus.
- router The configuration for the bus' router plugin. See details below
The router for each bus can be configured with:
- type The class name of a
MessageBusRouterPluginto use for this bus. It is expected that the constructor for the router should accept an array of route mappings. See the prooph
QueryRouterclasses for examples. This will default to the appropriate prooph router based on the type of the bus if not provided.
- async_switch The service ID or class name of an
AsyncSwitchMessageRouterto add to the bus. If this value is not provided, no switch will be included on the bus.
- routes A list of route mappings
- Command and query routing: Each entry in the list of routes is a "message name" to "message processor" (either a command handler or query finder).
- Event routing: Each entry in the list of routes is a "message name" to "list of listeners".