Console Commands

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 "[email protected]" "welcome"

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.

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?