Ecotone
SponsorBlogGithubSupport and ContactCommunity Channel
  • About
  • Installation
  • How to use
    • CQRS PHP
    • Event Handling PHP
    • Aggregates & Sagas
    • Scheduling in PHP
    • Asynchronous PHP
    • Event Sourcing PHP
    • Microservices PHP
    • Resiliency and Error Handling
    • Laravel Demos
    • Symfony Demos
      • Doctrine ORM
  • Tutorial
    • Before we start tutorial
    • Lesson 1: Messaging Concepts
    • Lesson 2: Tactical DDD
    • Lesson 3: Converters
    • Lesson 4: Metadata and Method Invocation
    • Lesson 5: Interceptors
    • Lesson 6: Asynchronous Handling
  • Enterprise
  • Modelling
    • Introduction
    • Message Bus and CQRS
      • CQRS Introduction - Commands
        • Query Handling
        • Event Handling
      • Aggregate Introduction
        • Aggregate Command Handlers
        • Aggregate Query Handlers
        • Aggregate Event Handlers
        • Advanced Aggregate creation
      • Repositories Introduction
      • Business Interface
        • Introduction
        • Business Repository
        • Database Business Interface
          • Converting Parameters
          • Converting Results
      • Saga Introduction
      • Identifier Mapping
    • Extending Messaging (Middlewares)
      • Message Headers
      • Interceptors (Middlewares)
        • Additional Scenarios
      • Intercepting Asynchronous Endpoints
      • Extending Message Buses (Gateways)
    • Event Sourcing
      • Installation
      • Event Sourcing Introduction
        • Working with Event Streams
        • Event Sourcing Aggregates
          • Working with Aggregates
          • Applying Events
          • Different ways to Record Events
        • Working with Metadata
        • Event versioning
        • Event Stream Persistence
          • Event Sourcing Repository
          • Making Stream immune to changes
          • Snapshoting
          • Persistence Strategies
          • Event Serialization and PII Data (GDPR)
      • Projection Introduction
        • Configuration
        • Choosing Event Streams for Projection
        • Executing and Managing
          • Running Projections
          • Projection CLI Actions
          • Access Event Store
        • Projections with State
        • Emitting events
    • Recovering, Tracing and Monitoring
      • Resiliency
        • Retries
        • Error Channel and Dead Letter
          • Dbal Dead Letter
        • Idempotent Consumer (Deduplication)
        • Resilient Sending
        • Outbox Pattern
        • Concurrency Handling
      • Message Handling Isolation
      • Ecotone Pulse (Service Dashboard)
    • Asynchronous Handling and Scheduling
      • Asynchronous Message Handlers
      • Asynchronous Message Bus (Gateways)
      • Delaying Messages
      • Time to Live
      • Message Priority
      • Scheduling
      • Dynamic Message Channels
    • Distributed Bus and Microservices
      • Distributed Bus
        • Distributed Bus with Service Map
          • Configuration
          • Custom Features
          • Non-Ecotone Application integration
          • Testing
        • AMQP Distributed Bus (RabbitMQ)
          • Configuration
        • Distributed Bus Interface
      • Message Consumer
      • Message Publisher
    • Business Workflows
      • The Basics - Stateless Workflows
      • Stateful Workflows - Saga
      • Handling Failures
    • Testing Support
      • Testing Messaging
      • Testing Aggregates and Sagas with Message Flows
      • Testing Event Sourcing Applications
      • Testing Asynchronous Messaging
  • Messaging and Ecotone In Depth
    • Overview
    • Multi-Tenancy Support
      • Getting Started
        • Any Framework Configuration
        • Symfony and Doctrine ORM
        • Laravel
      • Different Scenarios
        • Hooking into Tenant Switch
        • Shared and Multi Database Tenants
        • Accessing Current Tenant in Message Handler
        • Events and Tenant Propagation
        • Multi-Tenant aware Dead Letter
      • Advanced Queuing Strategies
    • Document Store
    • Console Commands
    • Messaging concepts
      • Message
      • Message Channel
      • Message Endpoints/Handlers
        • Internal Message Handler
        • Message Router
        • Splitter
      • Consumer
      • Messaging Gateway
      • Inbound/Outbound Channel Adapter
    • Method Invocation And Conversion
      • Method Invocation
      • Conversion
        • Payload Conversion
        • Headers Conversion
    • Service (Application) Configuration
    • Contributing to Ecotone
      • How Ecotone works under the hood
      • Ecotone Phases
      • Registering new Module Package
      • Demo Integration with SQS
        • Preparation
        • Inbound and Outbound Adapters and Message Channel
        • Message Consumer and Publisher
  • Modules
    • Overview
    • Symfony
      • Symfony Configuration
      • Symfony Database Connection (DBAL Module)
      • Doctrine ORM
      • Symfony Messenger Transport
    • Laravel
      • Laravel Configuration
      • Database Connection (DBAL Module)
      • Eloquent
      • Laravel Queues
      • Laravel Octane
    • Ecotone Lite
      • Logging
      • Database Connection (DBAL Module)
    • JMS Converter
    • OpenTelemetry (Tracing and Metrics)
      • Configuration
    • RabbitMQ Support
    • Kafka Support
      • Configuration
      • Message partitioning
      • Usage
    • DBAL Support
    • Amazon SQS Support
    • Redis Support
  • Other
    • Contact, Workshops and Support
Powered by GitBook
On this page
  • Injecting arguments
  • Default Converters
  • Parameter Converter Types
  • Payload Converter
  • Headers Converter (Headers)
  • Header Converter (Header)
  • Reference Converter (DI Service)
  • Configuration Variable Converter

Was this helpful?

Export as PDF
  1. Messaging and Ecotone In Depth
  2. Method Invocation And Conversion

Method Invocation

Method Invocation PHP

PreviousMethod Invocation And ConversionNextConversion

Last updated 6 months ago

Was this helpful?

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 :

#[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 . 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
  • 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}

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

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

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
  • 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 (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

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.

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

The message may contains of special header contentTypewhich 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

Endpoint
message
message
media type
conversion
Expression Language
message
message
Payload expression
Payload expression