autobus-php/autobus-bus-sample-bundle
Installation Add the package via Composer:
composer require autobus-php/autobus-bus-sample-bundle
Register the bundle in config/app.php under extra.bundles:
Autobus\BusSampleBundle\AutobusBusSampleBundle::class,
First Use Case The bundle provides a sample implementation of the Autobus event bus pattern. To send a command or event:
use Autobus\BusSampleBundle\Command\SampleCommand;
use Autobus\BusSampleBundle\Event\SampleEvent;
// Send a command
$bus->dispatch(new SampleCommand('Hello, Autobus!'));
// Listen to an event
$bus->subscribe(SampleEvent::class, function (SampleEvent $event) {
logger()->info('Event received:', ['message' => $event->getMessage()]);
});
Where to Look First
src/Command/ and src/Event/ for predefined examples.src/Handler/ for how commands/events are processed.config/packages/autobus_bus_sample.yaml for customization.Command Dispatching Use the bus to trigger actions (e.g., sending emails, processing payments):
$bus->dispatch(new ProcessPaymentCommand($userId, $amount));
Autobus\BusSampleBundle\Handler\CommandHandlerInterface for custom logic.Event Subscribing React to domain events (e.g., user registration, order updates):
$bus->subscribe(UserRegisteredEvent::class, function (UserRegisteredEvent $event) {
Mail::to($event->getEmail())->send(new WelcomeEmail());
});
Autobus\BusSampleBundle\Subscriber\SubscriberInterface::getPriority() for ordering.Middleware Integration Add cross-cutting concerns (logging, auth) via middleware:
$bus->pipe(new LoggingMiddleware());
$bus->pipe(new AuthMiddleware());
register():
$this->app->bind('bus', function ($app) {
return new Autobus\BusSampleBundle\Bus($app['autobus_bus_sample']);
});
public function __construct(private Bus $bus) {}
$bus = Mockery::mock(Bus::class);
$bus->shouldReceive('dispatch')->once();
Circular Dependencies Avoid circular references between handlers/subcribers (e.g., Handler A calls Handler B, which calls Handler A).
Autobus\BusSampleBundle\Bus::dispatchLater() for deferred execution.Event Ordering Subscribers with the same priority may execute in arbitrary order.
getPriority(): int { return 100; }).Middleware Conflicts Middleware may interfere with expected behavior (e.g., logging before validation).
Bus::pipe() or Bus::unpipe().# config/packages/autobus_bus_sample.yaml
logging: true
Autobus\BusSampleBundle\Validator\ValidatorInterface to reject malformed commands/events early.Custom Handlers
Extend Autobus\BusSampleBundle\Handler\AbstractHandler for reusable logic:
class CustomHandler extends AbstractHandler {
public function handle(SampleCommand $command) {
// Custom logic
}
}
Dynamic Subscribers Register subscribers dynamically (e.g., based on user roles):
if (auth()->check()) {
$bus->subscribe(UserEvent::class, new RoleBasedSubscriber());
}
Async Processing Offload heavy tasks to queues by integrating with Laravel Queues:
$bus->dispatch(new AsyncCommand())->delay(now()->addMinutes(5));
How can I help you explore Laravel packages today?