digitalnoise/command-launcher-bundle
Installation:
composer require digitalnoise/command-launcher-bundle
Register the bundle in config/bundles.php:
return [
// ...
DigitalNoise\CommandLauncherBundle\CommandLauncherBundle::class => ['all' => true],
];
First Use Case: Define a CLI command to trigger a message directly:
php bin/console command-launcher:dispatch ActivateUserMessage --user-id=123
Ensure your message class (ActivateUserMessage) is autoloaded and has a constructor matching the CLI arguments.
Configuration:
Check config/packages/digitalnoise_command_launcher.yaml for default settings (e.g., bus configuration, argument mapping).
CLI-Driven Message Dispatch: Use the bundle to replace manual CLI commands for complex workflows (e.g., cron jobs, admin tasks). Example:
php bin/console command-launcher:dispatch ProcessPaymentMessage --order-id=456 --amount=99.99
Integration with Symfony Messenger:
Leverage the bundle’s MessageBus integration to dispatch messages via CLI without writing custom commands.
# config/packages/digitalnoise_command_launcher.yaml
digitalnoise_command_launcher:
bus: messenger.bus.default # Use Symfony's default bus
Dynamic Argument Handling: Map CLI arguments to message properties using annotations or YAML:
# config/packages/digitalnoise_command_launcher.yaml
commands:
ProcessPaymentMessage:
arguments:
orderId: order-id
amount: amount
Event-Driven Extensions: Extend the bundle by creating custom dispatchers for non-Messenger buses (e.g., custom queues):
// src/Command/Dispatcher/CustomBusDispatcher.php
class CustomBusDispatcher implements DispatcherInterface {
public function dispatch(MessageInterface $message, array $arguments): void {
// Custom logic (e.g., RabbitMQ)
}
}
Message Class Autoloading:
Ensure message classes are PSR-4 autoloaded. Use composer dump-autoload if missing.
Argument Mismatches:
CLI arguments must match message constructor parameters exactly (type and name). Use --help to debug:
php bin/console command-launcher:dispatch ActivateUserMessage --help
Bus Configuration:
The bundle defaults to Symfony Messenger. For custom buses, override the dispatcher service:
services:
DigitalNoise\CommandLauncherBundle\CommandLauncherBundle:
arguments:
$dispatcher: '@custom_bus.dispatcher'
Environment-Specific Commands:
Restrict commands to specific environments (e.g., dev) via when in config/packages/*_command_launcher.yaml.
--dry-run to validate arguments without dispatching:
php bin/console command-launcher:dispatch ProcessPaymentMessage --order-id=456 --dry-run
APP_DEBUG=1) for detailed argument parsing logs.Custom Argument Parsers:
Override DigitalNoise\CommandLauncherBundle\ArgumentParser\ArgumentParserInterface to support complex types (e.g., dates, enums).
Pre-Dispatch Hooks: Use Symfony’s event system to validate messages before dispatch:
// src/EventListener/PreDispatchListener.php
class PreDispatchListener implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
public function onKernelController(ControllerEvent $event): void {
// Validate message here
}
}
Async Dispatch: Combine with Symfony Messenger’s async transport for background processing:
# config/packages/messenger.yaml
framework:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
How can I help you explore Laravel packages today?