Description
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