Skip to content

Support of closures in Attributes #649

@tandev

Description

@tandev

Description
With the PHP 8.5-Feature of https://www.php.net/releases/8.5/en.php#closures-in-const-expr Closures can be used in Attributes.

Using this Feature with Symfony-Container leads to an Error as Attributes are serialized inside of the SymfonyContainer.

Exception: Serialization of 'Closure' is not allowed
ecotone-symfony-closure/vendor/ecotone/ecotone/src/Messaging/Config/Container/DefinitionHelper.php:23
ecotone-symfony-closure/vendor/ecotone/ecotone/src/Messaging/Config/Container/DefinitionHelper.php:40
ecotone-symfony-closure/vendor/ecotone/symfony-bundle/DependencyInjection/SymfonyContainerAdapter.php:73
ecotone-symfony-closure/vendor/ecotone/symfony-bundle/DependencyInjection/SymfonyContainerAdapter.php:52
ecotone-symfony-closure/vendor/ecotone/ecotone/src/Messaging/Config/Container/ContainerBuilder.php:81
ecotone-symfony-closure/vendor/ecotone/symfony-bundle/DependencyInjection/EcotoneExtension.php:99
ecotone-symfony-closure/vendor/symfony/dependency-injection/Compiler/MergeExtensionConfigurationPass.php:81

Example

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
final readonly class Locking
{
    public function __construct(
        public \Closure $resource
    ) {}
}
interface RunPayment extends QueryBus
{
    #[Locking(static function (): string { return 'foo'; })]
    #[BusinessMethod('foo')]
    public function pay(): void;
}
class AttributeReadingMiddleware
{
    #[Around(pointcut: Locking::class)]
    public function lock(MethodInvocation $methodInvocation, Locking $locking): mixed
    {
        return $methodInvocation->proceed();
    }
}

A Reproducer can be found here: https://github.com/tandev/ecotone-symfony-attribute-closures by running the BootTest

Ecotone-Version: 1.305

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions