Skip to content

[Messenger] Document the ActivationMiddlewareDecorator #10035

Closed
@ogizanagi

Description

@ogizanagi

Relates to symfony/symfony#27320

This middleware is used to enable/disable a middleware based on an arbitrary condition. Main use-case is for maintaining a unique list of middleware across environments, but disabling/enabling some in test env for instance (but there are many others). It accepts either a simple boolean or a callable returning a boolean.

Usage when using the component standalone should be straightforward enough, but not as obvious when using within the framework.
When used with the framework, it leverages Symfony DI capabilities (using service decoration). It can uses a simple parameter value, the result of an expression, or even a callable injected in its constructor (by extending the decorator or by using DI):

TestOnlyMiddleware: ~

test.TestOnlyMiddleware:
    class: Symfony\Component\Messenger\Middleware\Enhancers\ActivationMiddlewareDecorator
    decorates: TestOnlyMiddleware
    arguments:
        - "@test.TestOnlyMiddleware.inner"
        - "@=parameter('kernel.environment') === 'test'"

using an invokable service:

TestOnlyMiddleware: ~
ActivationStrategyService: ~ # invokable service

test.TestOnlyMiddleware:
    class: Symfony\Component\Messenger\Middleware\Enhancers\ActivationMiddlewareDecorator
    decorates: TestOnlyMiddleware
    arguments:
        - "@test.TestOnlyMiddleware.inner"
        - "@StrategyService"

using a method of a service as a closure (dynamically executed, as previously):

TestOnlyMiddleware: ~
ActivationStrategyService: ~

test.TestOnlyMiddleware:
    class: Symfony\Component\Messenger\Middleware\Enhancers\ActivationMiddlewareDecorator
    decorates: TestOnlyMiddleware
    arguments:
        - "@test.TestOnlyMiddleware.inner"
        - ["@StrategyService", 'isEnabled']

Note: a more concise syntax can be used leveraging anonymous services:

TestOnlyMiddleware:
    class: Symfony\Component\Messenger\Middleware\Enhancers\ActivationMiddlewareDecorator
    arguments:
        - !service { class: TestOnlyMiddleware }
        - "@=parameter('kernel.environment') === 'test'"

[...]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions