doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 504 forks source link

Cannot override the class metadata factory via configuration since 2.5.0 #2551

Closed alekitto closed 1 year ago

alekitto commented 1 year ago

BC Break Report

Q A
BC Break yes
Version 2.5.0

Summary

Since version 2.5.0, the class metadata factory cannot be overridden.

Previous behavior

The document manager has been instantiated taking the class name from the configuration on document manager construction

Current behavior

Since https://github.com/doctrine/mongodb-odm/commit/5987555a4a2f19125089f0061a978f1d8b9e818f the override is broken as the document manager is wired to the final class Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory. To allow overriding via configuration, it should be wired against Doctrine\Persistence\Mapping\ClassMetadataFactory interface.

How to reproduce

$config = new Configuration();
$config->setClassMetadataFactoryName(FakeMetadataFactory::class);
DocumentManager::create(config: $config);
malarzm commented 1 year ago

This one's more tricky because of the initialization step

https://github.com/doctrine/mongodb-odm/blob/9b7ed61f4e12ac0f4f8a18f65dfcfd8a05066dab/lib/Doctrine/ODM/MongoDB/DocumentManager.php#L158-L160

for which Persistence's interface is not sufficient. Most probably we should introduce our own interface.

Offhand, what's your use case for custom metadata factory?

alekitto commented 1 year ago

Most probably we should introduce our own interface.

Agree

Offhand, what's your use case for custom metadata factory?

It is mostly used for testing actually: I'm using a DocumentManager with a mocked connection and a custom metadata factory in order to precisely control what is returned from the factory itself

malarzm commented 1 year ago

Closing as #2552 got merged