Skip to content

Commit

Permalink
Introduce ClassMetadataFactoryInterface to allow switching implementa…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
malarzm committed Sep 30, 2023
1 parent bce7e33 commit bd6d99f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
19 changes: 15 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +87,7 @@ class Configuration
* @psalm-var array{
* autoGenerateHydratorClasses?: self::AUTOGENERATE_*,
* autoGeneratePersistentCollectionClasses?: self::AUTOGENERATE_*,
* classMetadataFactoryName?: class-string<ClassMetadataFactory>,
* classMetadataFactoryName?: class-string<ClassMetadataFactoryInterface>,
* defaultCommitOptions?: CommitOptions,
* defaultDocumentRepositoryClassName?: class-string<ObjectRepository<object>>,
* defaultGridFSRepositoryClassName?: class-string<GridFSRepository<object>>,
Expand Down Expand Up @@ -409,13 +410,23 @@ public function getDefaultDB(): ?string
return $this->attributes['defaultDB'] ?? null;
}

/** @psalm-param class-string<ClassMetadataFactory> $cmfName */
/**
* @psalm-param class-string<ClassMetadataFactoryInterface> $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<ClassMetadataFactory> */
/** @psalm-return class-string<ClassMetadataFactoryInterface> */
public function getClassMetadataFactoryName(): string
{
if (! isset($this->attributes['classMetadataFactoryName'])) {
Expand Down Expand Up @@ -474,7 +485,7 @@ public function getFilterParameters(string $name): array
/**
* @psalm-param class-string<ObjectRepository<object>> $className
*
* @throws MongoDBException If not is a ObjectRepository.
* @throws MongoDBException If is not an ObjectRepository.
*/
public function setDefaultDocumentRepositoryClassName(string $className): void
{
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
20 changes: 20 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Doctrine\ODM\MongoDB\Mapping;

use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;

interface ClassMetadataFactoryInterface extends ClassMetadataFactory
{
/**
* Sets the configuration for the factory
*/
public function setConfiguration(Configuration $config): void;

/**
* Sets the document manager owning the factory
*/
public function setDocumentManager(DocumentManager $dm): void;
}
6 changes: 6 additions & 0 deletions lib/Doctrine/ODM/MongoDB/MongoDBException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactoryInterface;
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
use Doctrine\Persistence\ObjectRepository;
use Exception;
Expand Down Expand Up @@ -61,6 +62,11 @@ public static function invalidGridFSRepository(string $className): self
return new self(sprintf("Invalid repository class '%s'. It must be a %s.", $className, GridFSRepository::class));
}

public static function invalidClassMetadataFactory(string $className): self
{
return new self(sprintf("Invalid class metadata factory class '%s'. It must be a %s.", $className, ClassMetadataFactoryInterface::class));
}

/**
* @param string|string[] $expected
* @param mixed $got
Expand Down

0 comments on commit bd6d99f

Please sign in to comment.