Console Commands
Console commands for managing Ecotone message consumers and projections
Ecotone provides support for creating Console Commands. Just like the other parts of Ecotone's modules, we register Command in decoupled way using Attributes. This way creating new Console Commands become effortless and clean from extending or implementing framework specific classes and can be placed in code wherever it feels best in given context.
Commands with Arguments
We register new Console Command using ConsoleCommand attribute:
class EmailSender
{
#[ConsoleCommand('sendEmail')]
public function execute(string $email, string $type): void
{
}
}Ecotone will register given under "sendEmail" name with two arguments "email" and "type".
Executing Command
bin/console sendEmail "test@example.com" "welcome"php artisan sendEmail "test@example.com" "welcome"$messagingSystem->runConsoleCommand(
'sendEmail',
[
"email" => "test@example.com",
"type" => "welcome"
]
)Command Description
We may provide description for our Console Command, which will be visible in the Command listing of given framework (bin/console list, php artisan list):
Commands with Options
We register new Console Command using ConsoleCommand attribute:
Ecotone will register given under "sendEmail" name with two arguments "email" and "type".
Executing Command
Providing default values
You may provide default value for your parameters, so there will be no need to pass them if not needed:
We register new Console Command using ConsoleCommand attribute:
Executing Command
Array of Options
When needed we can expect array of Options to be passed:
Executing Command
Passing Services
When given Service is only needed for execution of specific Console Command, there is no need to pass it via constructor. We can scope the injection and inject it directly to our Console Command:
Using Reference attribute, we can inject any Service available in Dependency Container, to our Console Command method.
Passing Message Headers
When running Console Commands we may pass additional Message Headers. This way we can provide context, which may be needed in order to handle given Console Command or for later sub-flows (Message Headers are automatically propagated).
Executing Command
We pass Message Headers in follow format --header={name}:{value}. We may passs as many Message Headers as we want.
Writing to the Console
To write output from our Console Command, we type hint for ConsoleWriter. Ecotone will inject writer connected to the console of given framework - Symfony Console, Laravel Artisan or Tempest Console. This way our Command stays decoupled from framework specific classes, yet output lands in the actual console with native formatting:
info, success, warning and error write colored messages (cyan, green, yellow, red), while write and writeln write plain text.
Tables
We may render table using column headers and rows:
Progress Bars
For long running Commands we may show the progress:
Testing Console Output
During flow testing output is collected in memory, so we can simply assert against it:
Database Transaction
When Dbal Module is enabled it will automatically wrap your Command in Database Transaction. When you want to trigger CommandBus which is wrapped in it's own transaction, it may have sense to turn transactions for Console Commands off:
Last updated
Was this helpful?