VichUploaderBundle icon indicating copy to clipboard operation
VichUploaderBundle copied to clipboard

Extension points for PropertyMappingFactory::createMapping

Open aleho opened this issue 2 years ago • 2 comments

Feature Request

Q A
New Feature yes
RFC yes
BC Break ?

Summary

For my use-case I'd like to extend (only) PropertyMappingFactory::createMapping without having to copy/paste the entire factory.

I'm currently extending from PropertyMappingFactory, but this class is considered a (weak) final implementation. On the other hand, copy / pasting an entire class just to change a few aspects of how a mapping is created (and only in some circumstances) is very bad as well.

Are there any ideas on how to extend the creation of a mapping? Maybe using events to retrieve a custom $mappingData or something similar?

Is the solution for my use case – I'm deciding which storage to use based on the visibility, which then switches between mappings based on that information – wrong?

aleho avatar Aug 30 '22 10:08 aleho

I'm not sure to understand your use case, maybe you can try to go a bit deeper with it?

We don't plan to change the status of the factory, which was marked as "internal" a while ago (by the way, it's properly "final" in v2). I understand that redeclaring the whole class can be sub-optimal, but keeping it open means adding a burden of support on the maintainers, for a behaviour that is not documented.

garak avatar Sep 04 '22 14:09 garak

No, I'm not proposing to not make internal classes final as I absolutely understand why they are final!

My use-case is just needing to influence the creation of a mapping, which is universally done in createMapping. Any kind of extension point, be it an event or any other solution you could come up with, would be very welcome. I understand that exposing internal functionality and behavior to the outside adds a burden on maintenance, so that's why I haven't provided a PR yet of any kind.

    protected function createMapping($obj, string $fieldName, array $mappingData): PropertyMapping
    {
        // change mapping data here, e.g.
        $mappingData['mapping'] .= 'needs_different_name_because_reasons;
        
        return parent::createMapping($obj, $fieldName, $mappingData);
    }

Maybe a simple event allowing the mappingData to be changed would suffice?

aleho avatar Sep 05 '22 09:09 aleho

So, now that 2.0 has been released, would you consider replacing the PropertyMappingFactory with an interface so it can be decorated?

aleho avatar Nov 15 '22 09:11 aleho

I'm afraid it's too late now.

garak avatar Nov 15 '22 09:11 garak

What do you mean?

Is it too late to change an internal class to use an interface as an extension point / decoration target?

aleho avatar Nov 15 '22 10:11 aleho

You're right, it should be doable now. Feel free to propose a PR

garak avatar Nov 15 '22 14:11 garak

Done!

aleho avatar Nov 15 '22 14:11 aleho