forked from doctrine/mongodb-odm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassMetadataFactory.php
More file actions
384 lines (322 loc) · 13.4 KB
/
ClassMetadataFactory.php
File metadata and controls
384 lines (322 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB\Mapping;
use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\ConfigurationException;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
use Doctrine\ODM\MongoDB\Event\OnClassMetadataNotFoundEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Id\AlnumGenerator;
use Doctrine\ODM\MongoDB\Id\AutoGenerator;
use Doctrine\ODM\MongoDB\Id\IdGenerator;
use Doctrine\ODM\MongoDB\Id\IncrementGenerator;
use Doctrine\ODM\MongoDB\Id\UuidGenerator;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\ReflectionService;
use ReflectionException;
use function assert;
use function get_class;
use function get_class_methods;
use function in_array;
use function interface_exists;
use function trigger_deprecation;
use function ucfirst;
/**
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
* metadata mapping informations of a class which describes how a class should be mapped
* to a document database.
*
* @internal
*
* @template-extends AbstractClassMetadataFactory<ClassMetadata>
*/
final class ClassMetadataFactory extends AbstractClassMetadataFactory implements ClassMetadataFactoryInterface
{
/** @var string */
protected $cacheSalt = '$MONGODBODMCLASSMETADATA';
/** @var DocumentManager The DocumentManager instance */
private DocumentManager $dm;
/** @var Configuration The Configuration instance */
private Configuration $config;
/** @var MappingDriver The used metadata driver. */
private MappingDriver $driver;
/** @var EventManager The event manager instance */
private EventManager $evm;
public function setDocumentManager(DocumentManager $dm): void
{
$this->dm = $dm;
}
public function setConfiguration(Configuration $config): void
{
$this->config = $config;
}
/**
* Lazy initialization of this stuff, especially the metadata driver,
* since these are not needed at all when a metadata cache is active.
*/
protected function initialize(): void
{
$driver = $this->config->getMetadataDriverImpl();
if ($driver === null) {
throw ConfigurationException::noMetadataDriverConfigured();
}
$this->driver = $driver;
$this->evm = $this->dm->getEventManager();
$this->initialized = true;
}
/** @param string $className */
protected function onNotFoundMetadata($className)
{
if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
return null;
}
$eventArgs = new OnClassMetadataNotFoundEventArgs($className, $this->dm);
$this->evm->dispatchEvent(Events::onClassMetadataNotFound, $eventArgs);
return $eventArgs->getFoundMetadata();
}
/**
* @deprecated
*
* @param string $namespaceAlias
* @param string $simpleClassName
*/
protected function getFqcnFromAlias($namespaceAlias, $simpleClassName): string
{
return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName;
}
protected function getDriver(): MappingDriver
{
return $this->driver;
}
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
{
}
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
{
}
protected function isEntity(ClassMetadataInterface $class): bool
{
assert($class instanceof ClassMetadata);
return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument && ! $class->isView();
}
/** @param bool $rootEntityFound */
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = []): void
{
assert($class instanceof ClassMetadata);
if ($parent instanceof ClassMetadata) {
$class->setInheritanceType($parent->inheritanceType);
$class->setDiscriminatorField($parent->discriminatorField);
$class->setDiscriminatorMap($parent->discriminatorMap);
$class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue);
$class->setIdGeneratorType($parent->generatorType);
$this->addInheritedFields($class, $parent);
$this->addInheritedRelations($class, $parent);
if ($parent->isMappedSuperclass) {
// only inherit indexes if parent won't apply them itself
$this->addInheritedIndexes($class, $parent);
}
$this->setInheritedShardKey($class, $parent);
$class->setIdentifier($parent->identifier);
$class->setVersioned($parent->isVersioned);
$class->setVersionField($parent->versionField);
$class->setLifecycleCallbacks($parent->lifecycleCallbacks);
$class->setAlsoLoadMethods($parent->alsoLoadMethods);
$class->setChangeTrackingPolicy($parent->changeTrackingPolicy);
$class->setReadPreference($parent->readPreference, $parent->readPreferenceTags);
$class->setWriteConcern($parent->writeConcern);
if ($parent->isMappedSuperclass) {
$class->setCustomRepositoryClass($parent->customRepositoryClassName);
}
if ($parent->isFile) {
$class->isFile = true;
$class->setBucketName($parent->bucketName);
if ($parent->chunkSizeBytes !== null) {
$class->setChunkSizeBytes($parent->chunkSizeBytes);
}
}
}
// Invoke driver
try {
$this->driver->loadMetadataForClass($class->getName(), $class);
} catch (ReflectionException $e) {
throw MappingException::reflectionFailure($class->getName(), $e);
}
$this->validateIdentifier($class);
if ($parent instanceof ClassMetadata && $rootEntityFound && $parent->generatorType === $class->generatorType) {
if ($parent->generatorType) {
$class->setIdGeneratorType($parent->generatorType);
}
if ($parent->generatorOptions) {
$class->setIdGeneratorOptions($parent->generatorOptions);
}
if ($parent->idGenerator) {
$class->setIdGenerator($parent->idGenerator);
}
} else {
$this->completeIdGeneratorMapping($class);
}
if ($parent instanceof ClassMetadata && $parent->isInheritanceTypeSingleCollection()) {
$class->setDatabase($parent->getDatabase());
$class->setCollection($parent->getCollection());
}
$class->setParentClasses($nonSuperclassParents);
$this->evm->dispatchEvent(
Events::loadClassMetadata,
new LoadClassMetadataEventArgs($class, $this->dm),
);
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
if ($class->isChangeTrackingNotify()) {
trigger_deprecation(
'doctrine/mongodb-odm',
'2.4',
'NOTIFY tracking policy used in class "%s" is deprecated. Please use DEFERRED_EXPLICIT instead.',
$class->name,
);
}
}
/**
* Validates the identifier mapping.
*
* @throws MappingException
*/
protected function validateIdentifier(ClassMetadata $class): void
{
if (! $class->identifier && $this->isEntity($class)) {
throw MappingException::identifierRequired($class->name);
}
}
protected function newClassMetadataInstance($className): ClassMetadata
{
return new ClassMetadata($className);
}
private function completeIdGeneratorMapping(ClassMetadata $class): void
{
$idGenOptions = $class->generatorOptions;
switch ($class->generatorType) {
case ClassMetadata::GENERATOR_TYPE_AUTO:
$class->setIdGenerator(new AutoGenerator());
break;
case ClassMetadata::GENERATOR_TYPE_INCREMENT:
$incrementGenerator = new IncrementGenerator();
if (isset($idGenOptions['key'])) {
$incrementGenerator->setKey((string) $idGenOptions['key']);
}
if (isset($idGenOptions['collection'])) {
$incrementGenerator->setCollection((string) $idGenOptions['collection']);
}
if (isset($idGenOptions['startingId'])) {
$incrementGenerator->setStartingId((int) $idGenOptions['startingId']);
}
$class->setIdGenerator($incrementGenerator);
break;
case ClassMetadata::GENERATOR_TYPE_UUID:
$uuidGenerator = new UuidGenerator();
if (isset($idGenOptions['salt'])) {
$uuidGenerator->setSalt((string) $idGenOptions['salt']);
}
$class->setIdGenerator($uuidGenerator);
break;
case ClassMetadata::GENERATOR_TYPE_ALNUM:
$alnumGenerator = new AlnumGenerator();
if (isset($idGenOptions['pad'])) {
$alnumGenerator->setPad((int) $idGenOptions['pad']);
}
if (isset($idGenOptions['chars'])) {
$alnumGenerator->setChars((string) $idGenOptions['chars']);
} elseif (isset($idGenOptions['awkwardSafe'])) {
$alnumGenerator->setAwkwardSafeMode((bool) $idGenOptions['awkwardSafe']);
}
$class->setIdGenerator($alnumGenerator);
break;
case ClassMetadata::GENERATOR_TYPE_CUSTOM:
if (empty($idGenOptions['class'])) {
throw MappingException::missingIdGeneratorClass($class->name);
}
$customGenerator = new $idGenOptions['class']();
unset($idGenOptions['class']);
if (! $customGenerator instanceof IdGenerator) {
throw MappingException::classIsNotAValidGenerator(get_class($customGenerator));
}
$methods = get_class_methods($customGenerator);
foreach ($idGenOptions as $name => $value) {
$method = 'set' . ucfirst($name);
if (! in_array($method, $methods)) {
throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name);
}
$customGenerator->$method($value);
}
$class->setIdGenerator($customGenerator);
break;
case ClassMetadata::GENERATOR_TYPE_NONE:
break;
default:
throw new MappingException('Unknown generator type: ' . $class->generatorType);
}
}
/**
* Adds inherited fields to the subclass mapping.
*/
private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass): void
{
foreach ($parentClass->fieldMappings as $fieldName => $mapping) {
if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;
}
if (! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}
$subClass->addInheritedFieldMapping($mapping);
}
foreach ($parentClass->reflFields as $name => $field) {
$subClass->reflFields[$name] = $field;
}
}
/**
* Adds inherited association mappings to the subclass mapping.
*
* @throws MappingException
*/
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass): void
{
foreach ($parentClass->associationMappings as $field => $mapping) {
if ($parentClass->isMappedSuperclass) {
$mapping['sourceDocument'] = $subClass->name;
}
if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;
}
if (! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}
$subClass->addInheritedAssociationMapping($mapping);
}
}
/**
* Adds inherited indexes to the subclass mapping.
*/
private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass): void
{
foreach ($parentClass->indexes as $index) {
$subClass->addIndex($index['keys'], $index['options']);
}
}
/**
* Adds inherited shard key to the subclass mapping.
*/
private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass): void
{
if (! $parentClass->isSharded()) {
return;
}
$subClass->setShardKey(
$parentClass->shardKey['keys'],
$parentClass->shardKey['options'],
);
}
}
interface_exists(ClassMetadataInterface::class);
interface_exists(ReflectionService::class);