Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] Using argument value resolvers outside of controllers #53915

Open
@ostrolucky

Description

@ostrolucky

Description

Value resolvers are quite useful. However, they are designed to be used in controllers only. There are other places which could benefit from them as well though, such as authenticators and listeners. For example, we have a login authenticator, where I wish to use DTO instead of array as a payload. Would be great to use MapRequestPayload there, but unfortunately there is no controller for it. I ended up with following hack

class RequestToDTOTransformer
{
    public function __construct(
        private HttpKernelInterface $httpKernel,
        #[Autowire(service: 'argument_resolver.request_payload')]
        private RequestPayloadValueResolver $requestPayloadValueResolver,
    ) {}

    /**
     * @template T of class-string
     *
     * @param T $type
     *
     * @return object<T>
     */
    public function transform(Request $request, string $type): object
    {
        $mapRequestPayload = new MapRequestPayload();
        $mapRequestPayload->metadata = new ArgumentMetadata('RequestPayloadMetadata', $type, false, false, false);
        $event = new ControllerArgumentsEvent(
            $this->httpKernel,
            static fn () => null,
            [$mapRequestPayload],
            $request,
            HttpKernelInterface::SUB_REQUEST,
        );
        $this->requestPayloadValueResolver->onKernelControllerArguments($event);

        return $event->getArguments()[0];
    }
}

This "transformer" service can then be used anywhere in project. But as you can see it's quite hacky, I have to create tons of objects and arguments that are not actually used.

I'm not sure how would the reusable design for such a thing look like, but I still decided to create an issue for this with hope that someone will have a good idea. But this could be used for pretty much any value resolver.

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions