Skip to content

Commit 271963e

Browse files
committed
Move TypeRegistry to the Configuration class
1 parent 25c4de4 commit 271963e

23 files changed

+180
-202
lines changed

src/Aggregation/Expr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ private function prepareArgument($expression)
11211121
}
11221122

11231123
// Convert PHP types to MongoDB types for everything else
1124-
return $this->dm->getTypes()->convertToDatabaseValue($expression);
1124+
return $this->dm->getConfiguration()->getTypeRegistry()->convertToDatabaseValue($expression);
11251125
}
11261126

11271127
private function getDocumentPersister(): DocumentPersister

src/Aggregation/Stage/AbstractBucket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function convertExpression($expression)
8585
return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
8686
}
8787

88-
return $this->dm->getTypes()->convertToDatabaseValue($expression);
88+
return $this->dm->getConfiguration()->getTypeRegistry()->convertToDatabaseValue($expression);
8989
}
9090

9191
private function getDocumentPersister(): DocumentPersister

src/Aggregation/Stage/AbstractReplace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ private function convertExpression($expression)
4949
return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
5050
}
5151

52-
return $this->dm->getTypes()->convertToDatabaseValue(Expr::convertExpression($expression));
52+
return $this->dm->getConfiguration()->getTypeRegistry()->convertToDatabaseValue(Expr::convertExpression($expression));
5353
}
5454
}

src/Aggregation/Stage/GraphLookup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private function convertExpression($expression)
263263
return '$' . $this->getDocumentPersister($this->class)->prepareFieldName(substr($expression, 1));
264264
}
265265

266-
return $this->dm->getTypes()->convertToDatabaseValue($expression);
266+
return $this->dm->getConfiguration()->getTypeRegistry()->convertToDatabaseValue(Expr::convertExpression($expression));
267267
}
268268

269269
private function convertTargetFieldName(string $fieldName): string

src/Configuration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
2222
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
2323
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
24+
use Doctrine\ODM\MongoDB\Types\TypeRegistry;
2425
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
2526
use Doctrine\Persistence\ObjectRepository;
2627
use InvalidArgumentException;
@@ -154,6 +155,22 @@ class Configuration
154155

155156
private static string $version;
156157

158+
private ?TypeRegistry $typeRegistry = null;
159+
160+
public function setTypeRegistry(TypeRegistry $typeRegistry): void
161+
{
162+
if ($this->typeRegistry !== null) {
163+
throw new LogicException('TypeRegistry is already set and cannot be changed.');
164+
}
165+
166+
$this->typeRegistry = $typeRegistry;
167+
}
168+
169+
public function getTypeRegistry(): TypeRegistry
170+
{
171+
return $this->typeRegistry ?? TypeRegistry::getSharedInstance();
172+
}
173+
157174
/**
158175
* Provides the driver options to be used when creating the MongoDB client.
159176
*

src/DocumentManager.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
2323
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
2424
use Doctrine\ODM\MongoDB\Repository\ViewRepository;
25-
use Doctrine\ODM\MongoDB\Types\TypeRegistry;
2625
use Doctrine\Persistence\Mapping\ProxyClassNameResolver;
2726
use Doctrine\Persistence\ObjectManager;
2827
use Doctrine\Persistence\ObjectRepository;
@@ -145,7 +144,7 @@ class DocumentManager implements ObjectManager
145144
* Creates a new Document that operates on the given Mongo connection
146145
* and uses the given Configuration.
147146
*/
148-
protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null, private ?TypeRegistry $typeRegistry = null)
147+
protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null)
149148
{
150149
$this->config = $config ?: new Configuration();
151150
$this->eventManager = $eventManager ?: new EventManager();
@@ -204,7 +203,6 @@ public function resolveClassName(string $className): string
204203
default => new StaticProxyFactory($this),
205204
};
206205
$this->repositoryFactory = $this->config->getRepositoryFactory();
207-
$this->typeRegistry ??= TypeRegistry::getSharedInstance();
208206
}
209207

210208
/**
@@ -219,9 +217,9 @@ public function getProxyFactory(): ProxyFactory
219217
* Creates a new Document that operates on the given Mongo connection
220218
* and uses the given Configuration.
221219
*/
222-
public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null, ?TypeRegistry $registry = null): DocumentManager
220+
public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null): DocumentManager
223221
{
224-
return new static($client, $config, $eventManager, $registry);
222+
return new static($client, $config, $eventManager);
225223
}
226224

227225
/**
@@ -957,9 +955,4 @@ public function getClassNameForAssociation(array $mapping, $data): string
957955

958956
return $mapping['targetDocument'];
959957
}
960-
961-
public function getTypes(): TypeRegistry
962-
{
963-
return $this->typeRegistry;
964-
}
965958
}

src/Hydrator/HydratorFactory.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
use function rename;
2828
use function rtrim;
2929
use function sprintf;
30+
use function str_contains;
3031
use function str_replace;
3132
use function substr;
33+
use function trigger_deprecation;
3234
use function uniqid;
35+
use function var_export;
3336

3437
use const DIRECTORY_SEPARATOR;
3538
use const PHP_VERSION_ID;
@@ -195,7 +198,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
195198
,
196199
$mapping['name'],
197200
$mapping['fieldName'],
198-
$this->dm->getTypes()->get($mapping['type'])->closureToPHP(),
201+
$this->closureToPHP($class, $mapping['fieldName']),
199202
);
200203
} elseif (! isset($mapping['association'])) {
201204
$code .= sprintf(
@@ -218,7 +221,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
218221
,
219222
$mapping['name'],
220223
$mapping['fieldName'],
221-
$this->dm->getTypes()->get($mapping['type'])->closureToPHP(),
224+
$this->closureToPHP($class, $mapping['fieldName']),
222225
);
223226
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) {
224227
$code .= sprintf(
@@ -475,4 +478,19 @@ public function hydrate(object $document, array $data, array $hints = []): array
475478

476479
return $data;
477480
}
481+
482+
private function closureToPHP(ClassMetadata $class, string $fieldName): string
483+
{
484+
$code = $class->getFieldType($fieldName)->closureToPHP();
485+
486+
if (str_contains($code, '$typeIdentifier')) {
487+
trigger_deprecation('doctrine/mongodb-odm', '2.16', 'Using $typeIdentifier in Type::closureToPHP() is deprecated and will be removed in Doctrine ODM 3.0');
488+
}
489+
490+
return str_replace(
491+
'$fieldName',
492+
var_export($fieldName, true),
493+
$code,
494+
);
495+
}
478496
}

src/Mapping/ClassMetadata.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Doctrine\ODM\MongoDB\Mapping\PropertyAccessors\PropertyAccessorFactory;
2121
use Doctrine\ODM\MongoDB\Proxy\InternalProxy;
2222
use Doctrine\ODM\MongoDB\Types\Incrementable;
23+
use Doctrine\ODM\MongoDB\Types\InvalidTypeException;
2324
use Doctrine\ODM\MongoDB\Types\Type;
2425
use Doctrine\ODM\MongoDB\Types\TypeRegistry;
2526
use Doctrine\ODM\MongoDB\Types\Versionable;
@@ -869,7 +870,7 @@
869870
/** @var class-string|null */
870871
private ?string $rootClass;
871872

872-
private TypeRegistry $types;
873+
private TypeRegistry $typeRegistry;
873874

874875
/**
875876
* Initializes a new ClassMetadata instance that will hold the object-document mapping
@@ -935,18 +936,17 @@ public static function getReferenceFieldName(string $storeAs, string $pathPrefix
935936
*/
936937
public function setTypeRegistry(TypeRegistry $types): void
937938
{
938-
$this->types = $types;
939+
$this->typeRegistry = $types;
939940
}
940941

941942
private function getTypeRegistry(): TypeRegistry
942943
{
943-
if (! isset($this->types)) {
944-
/* @see Type::getRegistry() */
945-
$this->types = TypeRegistry::getSharedInstance();
946-
trigger_deprecation('doctrine/mongodb-odm', '2.16', 'Using ClassMetadata without a TypeRegistry is deprecated. Inject the TypeRegistry instance from the DocumentManager via ClassMetadata::setTypeRegistry($dm->getTypes()).');
944+
if (! isset($this->typeRegistry)) {
945+
$this->typeRegistry = TypeRegistry::getSharedInstance();
946+
trigger_deprecation('doctrine/mongodb-odm', '2.16', 'Using ClassMetadata without a TypeRegistry is deprecated. Inject the TypeRegistry instance from the DocumentManager via $classMetadata->setTypeRegistry($configuration->getTypeRegistry()).');
947947
}
948948

949-
return $this->types;
949+
return $this->typeRegistry;
950950
}
951951

952952
public function getReflectionClass(): ReflectionClass
@@ -1943,9 +1943,7 @@ public function setIdGenerator(IdGenerator $generator): void
19431943
*/
19441944
public function getPHPIdentifierValue($id)
19451945
{
1946-
$idType = $this->fieldMappings[$this->identifier]['type'];
1947-
1948-
return $this->getTypeRegistry()->get($idType)->convertToPHPValue($id);
1946+
return $this->getFieldType($this->identifier)->convertToPHPValue($id);
19491947
}
19501948

19511949
/**
@@ -1957,9 +1955,7 @@ public function getPHPIdentifierValue($id)
19571955
*/
19581956
public function getDatabaseIdentifierValue($id)
19591957
{
1960-
$idType = $this->fieldMappings[$this->identifier]['type'];
1961-
1962-
return $this->getTypeRegistry()->get($idType)->convertToDatabaseValue($id);
1958+
return $this->getFieldType($this->identifier)->convertToDatabaseValue($id);
19631959
}
19641960

19651961
/**
@@ -2061,6 +2057,19 @@ public function getFieldMapping(string $fieldName): array
20612057
return $this->fieldMappings[$fieldName];
20622058
}
20632059

2060+
public function getFieldType(string $fieldName): Type
2061+
{
2062+
if (! isset($this->fieldMappings[$fieldName]['type'])) {
2063+
return $this->getTypeRegistry()->get(Type::RAW);
2064+
}
2065+
2066+
try {
2067+
return $this->getTypeRegistry()->get($this->fieldMappings[$fieldName]['type']);
2068+
} catch (InvalidTypeException) {
2069+
throw MappingException::invalidTypeForField($this->name, $fieldName, $this->fieldMappings[$fieldName]['type']);
2070+
}
2071+
}
2072+
20642073
/**
20652074
* Gets mappings of fields holding embedded document(s).
20662075
*

src/Mapping/ClassMetadataFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function getDriver(): MappingDriver
119119

120120
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
121121
{
122-
$class->setTypeRegistry($this->dm->getTypes());
122+
$class->setTypeRegistry($this->config->getTypeRegistry());
123123

124124
if (PHP_VERSION_ID < 80400) {
125125
return;
@@ -136,7 +136,7 @@ protected function wakeupReflection(ClassMetadataInterface $class, ReflectionSer
136136

137137
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
138138
{
139-
$class->setTypeRegistry($this->dm->getTypes());
139+
$class->setTypeRegistry($this->config->getTypeRegistry());
140140
}
141141

142142
protected function isEntity(ClassMetadataInterface $class): bool

src/Mapping/MappingException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static function typeNotFound(string $name): self
2727
return new self(sprintf('Type to be overwritten %s does not exist.', $name));
2828
}
2929

30+
public static function invalidTypeForField(string $className, string $fieldName, string $type): self
31+
{
32+
return new self(sprintf("Type '%s' is not valid for field '%s' in class '%s'.", $type, $fieldName, $className));
33+
}
34+
3035
public static function typeRequirementsNotFulfilled(string $className, string $fieldName, string $type, string $reason): self
3136
{
3237
return new self(sprintf("Can not use '%s' type for field '%s' in class '%s' as its requirements are not met: %s.", $fieldName, $className, $type, $reason));

0 commit comments

Comments
 (0)