Method Invocation
Method Invocation PHP
Last updated
Was this helpful?
Method Invocation PHP
Last updated
Was this helpful?
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 :
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 . 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.
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.
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.
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
:
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.
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.
If don't define attribute, headers will be default converter set up for second method parameter, if is type hinted array
.
headerName
(Required) - Allow for performing transformations before passing argument to parameter
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 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 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 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
is parameter registered in your configuration.
It contains following configuration:
name
(Optional) - Defines the configuration parameter name, otherwise variable name is taken.
Payload
converter is responsible for passing payload of the to given parameter.
It contains of two attributes:
The message may contains of special header contentType
which describes content type of Message as . Based on this information, if payload of message is not compatible with parameter's type hint, Ecotone
do the .
Expression does use of great feature of Symfony, called .
Headers
converter is responsible for passing all headers of the as array to given parameter.
Header
converter is responsible for passing specific header from headers to given parameter.
It contains following configuration:
expression
- Allow for performing transformations before passing argument to parameter, same as in
expression
- Allow for performing transformations before passing argument to parameter, same as in