cptburke/symfony-messenger-application
Laravel package that integrates Symfony Messenger into a Laravel application, providing an application-level wrapper to configure transports and routing, dispatch messages, and run workers/consumers using familiar Laravel-friendly commands and configuration.
composer require cptburke/symfony-messenger-application
app/MessagingApplication.php):
use CptBurke\MessengerApplication\Application;
use Symfony\Component\Messenger\MessageBusInterface;
class MessagingApplication extends Application
{
protected function configureBus(): MessageBusInterface
{
return $this->busFactory->createBus([
// Configure transports, handlers, etc.
]);
}
}
console/messaging.php):
require __DIR__.'/../vendor/autoload.php';
$app = new MessagingApplication();
$app->run();
// Dispatch a message via CLI
$app = new MessagingApplication();
$app->getBus()->dispatch(new YourMessageClass());
console/worker.php):
$app = new MessagingApplication();
$app->runWorkers([
'transport' => 'async', // e.g., 'doctrine', 'amqp', etc.
'workers' => 4,
]);
configureWorkers():
protected function configureWorkers(): array
{
return [
'transport' => 'async',
'workers' => env('MESSENGER_WORKERS', 4),
'options' => [
'time_limit' => 300,
],
];
}
class LaravelMessagingApplication extends MessagingApplication
{
public function __construct(private Container $container)
{
parent::__construct();
}
protected function getBusFactory(): BusFactoryInterface
{
return $this->container->get(BusFactoryInterface::class);
}
}
public function __construct(private MessageBusInterface $bus)
{
// Resolve via Laravel's DI or manually via $app->getBus()
}
public function handle()
{
$this->bus->dispatch(new YourMessage());
}
configureBus():
protected function configureBus(): MessageBusInterface
{
return $this->busFactory->createBus([
'transports' => [
'async' => [
'dsn' => env('MESSENGER_TRANSPORT_DSN'),
'options' => ['queue_name' => 'your_queue'],
],
],
'handlers' => [
YourMessage::class => [YourHandler::class],
],
]);
}
Bus Configuration Overrides:
configureBus() returns a fully configured bus. Partial configs may throw InvalidArgumentException.// ❌ Missing 'transports' key
return $this->busFactory->createBus(['handlers' => [...]]);
Worker Lifecycle:
configureWorkers().SIGTERM/SIGINT to gracefully shut down workers (handled by the package).Transport DSN Validation:
amqp://, doctrine://) in .env or config. Invalid DSNs may silently fail.$bus = $this->busFactory->createBus([
'debug' => true, // Logs dispatched messages
]);
stderr by default. Redirect to a file for debugging:
php console/worker.php > worker.log 2>&1 &
Custom Bus Factories:
getBusFactory() to use a custom factory (e.g., for testing):
protected function getBusFactory(): BusFactoryInterface
{
return new CustomBusFactory();
}
Middleware Integration:
configureBus():
'middleware' => [
new YourMiddleware(),
],
Event Listeners:
$app->on('before.run', function () {
// Pre-run logic
});
$this->app->singleton(MessagingApplication::class, function ($app) {
return new MessagingApplication($app);
});
use Illuminate\Console\Command;
use Symfony\Component\Messenger\MessageBusInterface;
class DispatchMessageCommand extends Command
{
public function __construct(private MessageBusInterface $bus)
{
parent::__construct();
}
public function handle()
{
$this->bus->dispatch(new YourMessage());
}
}
How can I help you explore Laravel packages today?