bengor-user/simple-bus-bridge
Adapter bridge integrating BenGorUser with Matthias Noback’s SimpleBus, wiring user command/event handling into the SimpleBus message bus. Install via Composer; fully tested with PHPSpec and documented in the core BenGorUser User library.
Install the Package
composer require bengor-user/simple-bus-bridge
Ensure your composer.json includes matthiasnoback/simple-bus as a dependency.
Register the Bridge
In your Laravel service provider (e.g., AppServiceProvider.php), bind the bridge to SimpleBus:
use BenGorUser\SimpleBusBridge\Bridge\SimpleBusBridge;
use MatthiasNoback\SimpleBus\MessageBus;
public function register()
{
$this->app->bind(MessageBus::class, function ($app) {
return new SimpleBusBridge($app->make('bus')); // Laravel's default queue bus
});
}
First Use Case: Dispatching Commands
Define a command class (e.g., CreateUserCommand) and dispatch it:
use BenGorUser\User\Command\CreateUserCommand;
$command = new CreateUserCommand('john@example.com', 'password123');
$this->app->make(MessageBus::class)->dispatch($command);
Define Commands/Events
Extend BenGorUser\User\Command\Command or BenGorUser\User\Event\Event for domain-specific logic.
Example:
namespace App\Commands;
use BenGorUser\User\Command\Command;
class UpdateUserProfileCommand extends Command
{
public function __construct(public string $userId, public array $data) {}
}
Register Handlers Bind handlers to commands/events in a service provider:
$this->app->bind(
\MatthiasNoback\SimpleBus\MessageHandler::class,
function ($app) {
return new UpdateUserProfileHandler($app->make(UserRepository::class));
}
);
$this->app->bind(
\MatthiasNoback\SimpleBus\MessageBus::class,
function ($app) {
$bridge = new SimpleBusBridge($app->make('bus'));
$bridge->addHandler(UpdateUserProfileCommand::class, $app->make(\MatthiasNoback\SimpleBus\MessageHandler::class));
return $bridge;
}
);
Leverage Middleware Use SimpleBus middleware (e.g., logging, validation) by wrapping the bridge:
$bus = new MessageBus([
new \MatthiasNoback\SimpleBus\Middleware\LogMessages(),
new \MatthiasNoback\SimpleBus\Middleware\ValidateMessages(),
]);
$bridge = new SimpleBusBridge($bus);
bus facade or Bus::dispatch().Mockery or PHPUnit to mock MessageBus and verify command dispatching:
$mockBus = Mockery::mock(MessageBus::class);
$mockBus->shouldReceive('dispatch')->once();
$this->app->instance(MessageBus::class, $mockBus);
Deprecated SimpleBus Version The package targets SimpleBus v1.x. Ensure compatibility by checking:
composer show matthiasnoback/simple-bus
If using SimpleBus v2+, manually adapt the bridge or fork the package.
Handler Registration Order
Handlers must be registered before the MessageBus is instantiated. Late binding (e.g., in a controller) will fail:
// ❌ Fails: Handlers not registered
$bus = $this->app->make(MessageBus::class);
$bus->dispatch($command);
// ✅ Works: Register handlers first
$bridge = new SimpleBusBridge($this->app->make('bus'));
$bridge->addHandler(...);
$bridge->dispatch($command);
Circular Dependencies Avoid circular references between commands/events and their handlers. Use dependency injection sparingly:
// ❌ Risky: Handler depends on a command it processes
class UpdateUserHandler implements MessageHandler
{
public function __construct(private UpdateUserCommand $command) {}
}
NoHandlerForMessage exceptions. Ensure:
MatthiasNoback\SimpleBus\MessageHandler.$bus = new MessageBus([
new \MatthiasNoback\SimpleBus\Middleware\LogMessages(),
]);
Custom Middleware Extend the bridge to add Laravel-specific middleware (e.g., auth checks):
class AuthMiddleware implements Middleware
{
public function handle($message, callable $next)
{
if (!auth()->check()) {
throw new \RuntimeException('Unauthorized');
}
return $next($message);
}
}
Event Listeners Convert SimpleBus events to Laravel listeners for seamless integration:
$bus->subscribeTo(UserCreatedEvent::class, function ($event) {
event(new \Illuminate\Queue\Events\JobProcessed($event->user));
});
Retry Logic
Use Laravel’s retryAfter() with SimpleBus by wrapping handlers:
$handler = new RetryHandler(
$originalHandler,
new \Illuminate\Bus\Retryable
);
How can I help you explore Laravel packages today?