diff --git a/lib/Doctrine/ODM/MongoDB/Configuration.php b/lib/Doctrine/ODM/MongoDB/Configuration.php index 18eacc5f79..6aef5f2189 100644 --- a/lib/Doctrine/ODM/MongoDB/Configuration.php +++ b/lib/Doctrine/ODM/MongoDB/Configuration.php @@ -9,6 +9,7 @@ use Doctrine\Common\Cache\Psr6\CacheAdapter; use Doctrine\Common\Cache\Psr6\DoctrineProvider; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; +use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactoryInterface; use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory; use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionGenerator; @@ -86,7 +87,7 @@ class Configuration * @psalm-var array{ * autoGenerateHydratorClasses?: self::AUTOGENERATE_*, * autoGeneratePersistentCollectionClasses?: self::AUTOGENERATE_*, - * classMetadataFactoryName?: class-string, + * classMetadataFactoryName?: class-string, * defaultCommitOptions?: CommitOptions, * defaultDocumentRepositoryClassName?: class-string>, * defaultGridFSRepositoryClassName?: class-string>, @@ -409,13 +410,23 @@ public function getDefaultDB(): ?string return $this->attributes['defaultDB'] ?? null; } - /** @psalm-param class-string $cmfName */ + /** + * @psalm-param class-string $cmfName + * + * @throws MongoDBException If is not a ClassMetadataFactoryInterface. + */ public function setClassMetadataFactoryName(string $cmfName): void { + $reflectionClass = new ReflectionClass($cmfName); + + if (! $reflectionClass->implementsInterface(ClassMetadataFactoryInterface::class)) { + throw MongoDBException::invalidClassMetadataFactory($cmfName); + } + $this->attributes['classMetadataFactoryName'] = $cmfName; } - /** @psalm-return class-string */ + /** @psalm-return class-string */ public function getClassMetadataFactoryName(): string { if (! isset($this->attributes['classMetadataFactoryName'])) { @@ -474,7 +485,7 @@ public function getFilterParameters(string $name): array /** * @psalm-param class-string> $className * - * @throws MongoDBException If not is a ObjectRepository. + * @throws MongoDBException If is not an ObjectRepository. */ public function setDefaultDocumentRepositoryClassName(string $className): void { diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index 8c07a1b603..082c5230c6 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -8,6 +8,7 @@ use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; +use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactoryInterface; use Doctrine\ODM\MongoDB\Mapping\MappingException; use Doctrine\ODM\MongoDB\Proxy\Factory\ProxyFactory; use Doctrine\ODM\MongoDB\Proxy\Factory\StaticProxyFactory; @@ -69,7 +70,7 @@ class DocumentManager implements ObjectManager /** * The metadata factory, used to retrieve the ODM metadata of document classes. */ - private ClassMetadataFactory $metadataFactory; + private ClassMetadataFactoryInterface $metadataFactory; /** * The UnitOfWork used to coordinate object-level transactions. diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php index 6f20f8d3bf..0afc9e6f93 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php @@ -43,7 +43,7 @@ * @method ClassMetadata[] getLoadedMetadata() * @method ClassMetadata getMetadataFor($className) */ -final class ClassMetadataFactory extends AbstractClassMetadataFactory +final class ClassMetadataFactory extends AbstractClassMetadataFactory implements ClassMetadataFactoryInterface { /** @var string */ protected $cacheSalt = '$MONGODBODMCLASSMETADATA'; diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactoryInterface.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactoryInterface.php new file mode 100644 index 0000000000..cf08ce2579 --- /dev/null +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactoryInterface.php @@ -0,0 +1,20 @@ +