Method Invocation

Method Invocation PHP

Injecting arguments

Ecotone inject arguments to invoked method based on Parameter Converters. Parameter converters tells Ecotone how to resolve specific parameter and what kind of argument is it expecting.

Suppose that we have Command Handler Endpoint:

#[CommandHandler] 
public function changePrice(
    #[Payload] ChangeProductPriceCommand $command, 
    #[Headers] array $metadata, 
    #[Reference] UserService $userService
) : void
{
    $userId = $metadata["userId"];
    if (!$userService->isAdmin($userId)) {
        throw new \InvalidArgumentException("You need to be administrator in order to register new product");
    }

    $this->price = $command->getPrice();
}

Our Command Handler method declaration is built from three parameters. Ecotone does resolve parameters based on given attribute types. Payload - Does inject payload of the message. In our case it will be the command itself Headers - Does inject all headers as array. Reference- Does inject service from Dependency Container. If referenceNamewhich is name of the service in the container is not given, then it will take the class name as default.

Default Converters

Ecotone, if parameter converters are not passed provides default converters.

  • First parameter is always Payload.

  • The second parameter, if is array then Headers converter is taken

  • If class type hint is provided for parameter, then Reference converter is picked

  • Otherwise, if no default converter can be applied exception will be thrown with information about missing parameter.

Our Command Handler can benefit from default converters, so we don't need to use any additional configuration.

#[CommandHandler] 
public function changePrice(ChangeProductPriceCommand $command, array $metadata, UserService $userService) : void
{
    $userId = $metadata["userId"];
    if (!$userService->isAdmin($userId)) {
        throw new \InvalidArgumentException("You need to be administrator in order to register new product");
    }

    $this->price = $command->getPrice();
}

Parameter Converter Types

Payload Converter

public function handle(#[Payload] string $payload): void

Payload converter is responsible for passing payload of the message to given parameter. It contains of two attributes:

  • expression (Optional) - Allow for performing transformations before passing argument to parameter `

    public function handle(#[Payload("reference('calculatingService').multiply(payload)"] int $amount): void

If don't define attribute, payload will be default converter set up for first method parameter.

Converting payload

Message's payload is not always the same type as expected in method declaration. As in above example, we may expect:

ChangeProductPriceCommand $command

But the message payload may contains JSON:

{"productId": 123, "price": 100}

The message may contains of special header contentTypewhich describes content type of Message as media type. Based on this information, if payload of message is not compatible with parameter's type hint, Ecotone do the conversion.

Thanks to conversion on the level of endpoint, Ecotone does not expect running Command Bus with specific class instance. It may receive anything xml, json etc as long as Converter for specific Media Type is registered in the system.

Expression

Expression does use of great feature of Symfony, called Expression Language.

Payload(expression: "payload * 2")

There are three types of variables available within expression.

  • payload - which is just payload of currently handled Message

  • headers - contains of all headers available within Message

  • reference - which allow for retrieving service from Dependency Container and calling a method on it. The result of the expression will be passed to parameter after optional conversion.

    Payload(expression:"reference('calculatingService').multiply(payload, 2)")

Headers Converter (Headers)

public function handle(#[Headers] array $headers): mixed

Headers converter is responsible for passing all headers of the message as array to given parameter.

If don't define attribute, headers will be default converter set up for second method parameter, if is type hinted array.

Header Converter (Header)

public function handle(#[Header("executorId")] string $executorId): mixed

Header converter is responsible for passing specific header from message headers to given parameter. It contains following configuration:

  • headerName (Required) - Allow for performing transformations before passing argument to parameter

  • expression - Allow for performing transformations before passing argument to parameter, same as in Payload expression

If you type hint Header nullable, then header will become optional. In case is non-nullable and header does not exists, exception will be thrown.

Reference Converter (DI Service)

public function handle(
    PlaceOrder $command, 
    #[Reference] OrderRepository $orderRepository
): void

Reference converter is responsible for injecting Service from DI into your method. It contains attributes:

  • referenceName - Allow for defining custom Service Id from DI Containter, if not registered under class name.

  • expression - Allow for performing transformations before passing argument to parameter, same as in Payload expression

Expression

Expression used with reference can be used for dynamically calling given Service before method execution and injecting an value:

#[Reference(
    referenceName: "globalConfigurationService",
    expression: "service.getCurrentConfiguration()"
)] LocalConfiguration $localConfiguration

There are four types of variables available within expression.

  • service - the service

  • payload - which is just payload of currently handled Message

  • headers - contains of all headers available within Message

  • reference - which allow for retrieving service from Dependency Container and calling a method on it. The result of the expression will be passed to parameter after optional conversion.

Configuration Variable Converter

public function handle(
    #[ConfigurationVariable("isDevelopmentMode")] bool $isDelevopment
): mixed

Configuration Variable is parameter registered in your configuration. It contains following configuration:

  • name (Optional) - Defines the configuration parameter name, otherwise variable name is taken.

Last updated