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:
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 referenceName
which 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
thenHeaders
converter is takenIf class type hint is provided for parameter, then
Reference
converter is pickedOtherwise, 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.
Parameter Converter Types
Payload Converter
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 `
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:
But the message payload may contains JSON
:
The message may contains of special header contentType
which 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.
There are three types of variables available within expression.
payload
- which is just payload of currently handled Messageheaders
- contains of all headers available within Messagereference
- 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.
Headers Converter (Headers)
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)
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 parameterexpression
- 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)
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:
There are four types of variables available within expression.
service
- the servicepayload
- which is just payload of currently handled Messageheaders
- contains of all headers available within Messagereference
- 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
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