dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 519 forks source link

Autowire services that implement NamerInterface #1448

Open tacman opened 4 months ago

tacman commented 4 months ago

Feature Request

Q A
New Feature yes
BC Break no

Summary

Instead or requiring namer services to be manually registered.

# services.yaml
    App\Service\ProjectImageNamerService:
        public: true

The compiler pass could look for services that implement NamerInterface and make them public. One less step when creating a custom namer service.

garak commented 4 months ago

If we have to add a compiler pass, why don't do something to properly inject the configured service for the namer? So, we can make all the namers private again.

garak commented 4 months ago

To elaborate a bit on my previous comment: I was thinking about injecting two services into PropertyMappingResolver, a NamerInterface and an optional DirectoryNamerInterface. The definition would be empty in the service configuration, and the compiler pass should resolve these two services.

PRs are welcome.

tacman commented 4 months ago

A directory namer would be cool. In fact, my custom namer really is only the directory.

$file = $mapping->getFile($object);
$name = $file->getClientOriginalName();

/** @var $object Project|Asset */
$name = match ($object::class) {
    Project::class => sprintf("%s/%s", $object->getSlug(), $name),
    Asset::class => sprintf("%s/%s/%s", $object->getProject()->getSlug(), $object->getItem()->getCode(), $name),
};
return $name;