ekreative/uuid-extra-bundle

Using argument resolving instead of paramconverter

Opened this issue · 1 comments

garak commented

I think that using argument resolving instead of paramconverter could be a more elegant solution.

Working example from a project of mine:

<?php

namespace App\ArgumentResolver;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;

final class UuidResolver implements ArgumentValueResolverInterface
{
    public function supports(Request $request, ArgumentMetadata $argument): bool
    {
        return UuidInterface::class === $argument->getType();
    }

    public function resolve(Request $request, ArgumentMetadata $argument): ?\Generator
    {
        yield Uuid::fromString($request->attributes->get('id'));
    }
}

Then you don't need any annotation, type-hinting it's enough.
Action example:

/**
 * @Route("/simple/{uuid}")
 */
public function simpleAction(UuidInterface $uuid)
{
    return new Response($uuid->toString());
}

Looking good, havent come across that feature before - looks basically the same, though good that doesnt require an extra bundle - the param converter also works without any annotations