|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Mapping; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory; |
|
23
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface; |
|
24
|
|
|
use Doctrine\Common\Persistence\Mapping\ReflectionService; |
|
25
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
|
26
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
27
|
|
|
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs; |
|
28
|
|
|
use Doctrine\ODM\MongoDB\Events; |
|
29
|
|
|
use Doctrine\ODM\MongoDB\Id\AbstractIdGenerator; |
|
30
|
|
|
use Doctrine\ODM\MongoDB\Id\AlnumGenerator; |
|
31
|
|
|
use Doctrine\ODM\MongoDB\Id\AutoGenerator; |
|
32
|
|
|
use Doctrine\ODM\MongoDB\Id\IncrementGenerator; |
|
33
|
|
|
use Doctrine\ODM\MongoDB\Id\UuidGenerator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the |
|
37
|
|
|
* metadata mapping informations of a class which describes how a class should be mapped |
|
38
|
|
|
* to a document database. |
|
39
|
|
|
* |
|
40
|
|
|
* @since 1.0 |
|
41
|
|
|
*/ |
|
42
|
|
|
class ClassMetadataFactory extends AbstractClassMetadataFactory |
|
43
|
|
|
{ |
|
44
|
|
|
protected $cacheSalt = "\$MONGODBODMCLASSMETADATA"; |
|
45
|
|
|
|
|
46
|
|
|
/** @var DocumentManager The DocumentManager instance */ |
|
47
|
|
|
private $dm; |
|
48
|
|
|
|
|
49
|
|
|
/** @var Configuration The Configuration instance */ |
|
50
|
|
|
private $config; |
|
51
|
|
|
|
|
52
|
|
|
/** @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver The used metadata driver. */ |
|
53
|
|
|
private $driver; |
|
54
|
|
|
|
|
55
|
|
|
/** @var \Doctrine\Common\EventManager The event manager instance */ |
|
56
|
|
|
private $evm; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Sets the DocumentManager instance for this class. |
|
60
|
|
|
* |
|
61
|
|
|
* @param DocumentManager $dm The DocumentManager instance |
|
62
|
|
|
*/ |
|
63
|
1179 |
|
public function setDocumentManager(DocumentManager $dm) |
|
64
|
|
|
{ |
|
65
|
1179 |
|
$this->dm = $dm; |
|
66
|
1179 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sets the Configuration instance |
|
70
|
|
|
* |
|
71
|
|
|
* @param Configuration $config |
|
72
|
|
|
*/ |
|
73
|
1179 |
|
public function setConfiguration(Configuration $config) |
|
74
|
|
|
{ |
|
75
|
1179 |
|
$this->config = $config; |
|
76
|
1179 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Lazy initialization of this stuff, especially the metadata driver, |
|
80
|
|
|
* since these are not needed at all when a metadata cache is active. |
|
81
|
|
|
*/ |
|
82
|
933 |
|
protected function initialize() |
|
83
|
|
|
{ |
|
84
|
933 |
|
$this->driver = $this->config->getMetadataDriverImpl(); |
|
85
|
933 |
|
$this->evm = $this->dm->getEventManager(); |
|
86
|
933 |
|
$this->initialized = true; |
|
87
|
933 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritDoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritDoc} |
|
99
|
|
|
*/ |
|
100
|
379 |
|
protected function getDriver() |
|
101
|
|
|
{ |
|
102
|
379 |
|
return $this->driver; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritDoc} |
|
107
|
|
|
*/ |
|
108
|
929 |
|
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
109
|
|
|
{ |
|
110
|
929 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritDoc} |
|
114
|
|
|
*/ |
|
115
|
933 |
|
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
116
|
|
|
{ |
|
117
|
933 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritDoc} |
|
121
|
|
|
*/ |
|
122
|
929 |
|
protected function isEntity(ClassMetadataInterface $class) |
|
123
|
|
|
{ |
|
124
|
929 |
|
return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritDoc} |
|
129
|
|
|
*/ |
|
130
|
933 |
|
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
131
|
|
|
{ |
|
132
|
|
|
/** @var $class ClassMetadata */ |
|
133
|
|
|
/** @var $parent ClassMetadata */ |
|
134
|
933 |
|
if ($parent) { |
|
135
|
377 |
|
$class->setInheritanceType($parent->inheritanceType); |
|
136
|
377 |
|
$class->setDiscriminatorField($parent->discriminatorField); |
|
137
|
377 |
|
$class->setDiscriminatorMap($parent->discriminatorMap); |
|
138
|
377 |
|
$class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
|
139
|
377 |
|
$class->setIdGeneratorType($parent->generatorType); |
|
140
|
377 |
|
$this->addInheritedFields($class, $parent); |
|
|
|
|
|
|
141
|
377 |
|
$this->addInheritedRelations($class, $parent); |
|
|
|
|
|
|
142
|
377 |
|
$this->addInheritedIndexes($class, $parent); |
|
|
|
|
|
|
143
|
377 |
|
$this->setInheritedShardKey($class, $parent); |
|
|
|
|
|
|
144
|
377 |
|
$class->setIdentifier($parent->identifier); |
|
145
|
377 |
|
$class->setVersioned($parent->isVersioned); |
|
146
|
377 |
|
$class->setVersionField($parent->versionField); |
|
147
|
377 |
|
$class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
148
|
377 |
|
$class->setAlsoLoadMethods($parent->alsoLoadMethods); |
|
149
|
377 |
|
$class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
150
|
377 |
|
$class->setReadPreference($parent->readPreference, $parent->readPreferenceTags); |
|
151
|
377 |
|
$class->setWriteConcern($parent->writeConcern); |
|
152
|
377 |
|
$class->setFile($parent->getFile()); |
|
153
|
377 |
|
if ($parent->isMappedSuperclass) { |
|
154
|
311 |
|
$class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// Invoke driver |
|
159
|
|
|
try { |
|
160
|
933 |
|
$this->driver->loadMetadataForClass($class->getName(), $class); |
|
161
|
6 |
|
} catch (\ReflectionException $e) { |
|
162
|
|
|
throw MappingException::reflectionFailure($class->getName(), $e); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
929 |
|
$this->validateIdentifier($class); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
929 |
|
if ($parent && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
|
|
|
|
|
168
|
111 |
|
if ($parent->generatorType) { |
|
169
|
111 |
|
$class->setIdGeneratorType($parent->generatorType); |
|
170
|
|
|
} |
|
171
|
111 |
|
if ($parent->generatorOptions) { |
|
|
|
|
|
|
172
|
|
|
$class->setIdGeneratorOptions($parent->generatorOptions); |
|
173
|
|
|
} |
|
174
|
111 |
|
if ($parent->idGenerator) { |
|
175
|
111 |
|
$class->setIdGenerator($parent->idGenerator); |
|
176
|
|
|
} |
|
177
|
|
|
} else { |
|
178
|
929 |
|
$this->completeIdGeneratorMapping($class); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
929 |
|
if ($parent && $parent->isInheritanceTypeSingleCollection()) { |
|
182
|
96 |
|
$class->setDatabase($parent->getDatabase()); |
|
183
|
96 |
|
$class->setCollection($parent->getCollection()); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
929 |
|
$class->setParentClasses($nonSuperclassParents); |
|
187
|
|
|
|
|
188
|
929 |
View Code Duplication |
if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
|
|
|
|
|
|
189
|
2 |
|
$eventArgs = new LoadClassMetadataEventArgs($class, $this->dm); |
|
190
|
2 |
|
$this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
191
|
|
|
} |
|
192
|
929 |
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Validates the identifier mapping. |
|
196
|
|
|
* |
|
197
|
|
|
* @param ClassMetadata $class |
|
198
|
|
|
* @throws MappingException |
|
199
|
|
|
*/ |
|
200
|
929 |
|
protected function validateIdentifier($class) |
|
201
|
|
|
{ |
|
202
|
929 |
|
if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument) { |
|
203
|
|
|
throw MappingException::identifierRequired($class->name); |
|
204
|
|
|
} |
|
205
|
929 |
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Creates a new ClassMetadata instance for the given class name. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $className |
|
211
|
|
|
* @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
|
212
|
|
|
*/ |
|
213
|
933 |
|
protected function newClassMetadataInstance($className) |
|
214
|
|
|
{ |
|
215
|
933 |
|
return new ClassMetadata($className); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
929 |
|
private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
219
|
|
|
{ |
|
220
|
929 |
|
$idGenOptions = $class->generatorOptions; |
|
221
|
929 |
|
switch ($class->generatorType) { |
|
222
|
929 |
|
case ClassMetadata::GENERATOR_TYPE_AUTO: |
|
223
|
859 |
|
$class->setIdGenerator(new AutoGenerator()); |
|
224
|
859 |
|
break; |
|
225
|
154 |
|
case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
|
226
|
8 |
|
$incrementGenerator = new IncrementGenerator(); |
|
227
|
8 |
|
if (isset($idGenOptions['key'])) { |
|
228
|
|
|
$incrementGenerator->setKey($idGenOptions['key']); |
|
229
|
|
|
} |
|
230
|
8 |
|
if (isset($idGenOptions['collection'])) { |
|
231
|
|
|
$incrementGenerator->setCollection($idGenOptions['collection']); |
|
232
|
|
|
} |
|
233
|
8 |
|
$class->setIdGenerator($incrementGenerator); |
|
234
|
8 |
|
break; |
|
235
|
146 |
|
case ClassMetadata::GENERATOR_TYPE_UUID: |
|
236
|
4 |
|
$uuidGenerator = new UuidGenerator(); |
|
237
|
4 |
|
isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); |
|
238
|
4 |
|
$class->setIdGenerator($uuidGenerator); |
|
239
|
4 |
|
break; |
|
240
|
142 |
|
case ClassMetadata::GENERATOR_TYPE_ALNUM: |
|
241
|
1 |
|
$alnumGenerator = new AlnumGenerator(); |
|
242
|
1 |
|
if (isset($idGenOptions['pad'])) { |
|
243
|
|
|
$alnumGenerator->setPad($idGenOptions['pad']); |
|
244
|
|
|
} |
|
245
|
1 |
|
if (isset($idGenOptions['chars'])) { |
|
246
|
1 |
|
$alnumGenerator->setChars($idGenOptions['chars']); |
|
247
|
|
|
} elseif (isset($idGenOptions['awkwardSafe'])) { |
|
248
|
|
|
$alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
1 |
|
$class->setIdGenerator($alnumGenerator); |
|
252
|
1 |
|
break; |
|
253
|
141 |
|
case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
|
254
|
|
|
if (empty($idGenOptions['class'])) { |
|
255
|
|
|
throw MappingException::missingIdGeneratorClass($class->name); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$customGenerator = new $idGenOptions['class']; |
|
259
|
|
|
unset($idGenOptions['class']); |
|
260
|
|
|
if ( ! $customGenerator instanceof AbstractIdGenerator) { |
|
261
|
|
|
throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$methods = get_class_methods($customGenerator); |
|
265
|
|
|
foreach ($idGenOptions as $name => $value) { |
|
266
|
|
|
$method = 'set' . ucfirst($name); |
|
267
|
|
|
if ( ! in_array($method, $methods)) { |
|
268
|
|
|
throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
$customGenerator->$method($value); |
|
272
|
|
|
} |
|
273
|
|
|
$class->setIdGenerator($customGenerator); |
|
274
|
|
|
break; |
|
275
|
141 |
|
case ClassMetadata::GENERATOR_TYPE_NONE; |
|
276
|
141 |
|
break; |
|
277
|
|
|
default: |
|
278
|
|
|
throw new MappingException('Unknown generator type: ' . $class->generatorType); |
|
279
|
|
|
} |
|
280
|
929 |
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Adds inherited fields to the subclass mapping. |
|
284
|
|
|
* |
|
285
|
|
|
* @param ClassMetadata $subClass |
|
286
|
|
|
* @param ClassMetadata $parentClass |
|
287
|
|
|
*/ |
|
288
|
377 |
|
private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
289
|
|
|
{ |
|
290
|
377 |
|
foreach ($parentClass->fieldMappings as $fieldName => $mapping) { |
|
291
|
130 |
View Code Duplication |
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
|
|
|
|
|
|
292
|
123 |
|
$mapping['inherited'] = $parentClass->name; |
|
293
|
|
|
} |
|
294
|
130 |
|
if ( ! isset($mapping['declared'])) { |
|
295
|
130 |
|
$mapping['declared'] = $parentClass->name; |
|
296
|
|
|
} |
|
297
|
130 |
|
$subClass->addInheritedFieldMapping($mapping); |
|
298
|
|
|
} |
|
299
|
377 |
|
foreach ($parentClass->reflFields as $name => $field) { |
|
300
|
130 |
|
$subClass->reflFields[$name] = $field; |
|
301
|
|
|
} |
|
302
|
377 |
|
} |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Adds inherited association mappings to the subclass mapping. |
|
307
|
|
|
* |
|
308
|
|
|
* @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $subClass |
|
309
|
|
|
* @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentClass |
|
310
|
|
|
* |
|
311
|
|
|
* @return void |
|
312
|
|
|
* |
|
313
|
|
|
* @throws MappingException |
|
314
|
|
|
*/ |
|
315
|
377 |
|
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
316
|
|
|
{ |
|
317
|
377 |
|
foreach ($parentClass->associationMappings as $field => $mapping) { |
|
318
|
78 |
|
if ($parentClass->isMappedSuperclass) { |
|
319
|
4 |
|
$mapping['sourceDocument'] = $subClass->name; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
78 |
View Code Duplication |
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
|
|
|
|
|
|
323
|
74 |
|
$mapping['inherited'] = $parentClass->name; |
|
324
|
|
|
} |
|
325
|
78 |
|
if ( ! isset($mapping['declared'])) { |
|
326
|
78 |
|
$mapping['declared'] = $parentClass->name; |
|
327
|
|
|
} |
|
328
|
78 |
|
$subClass->addInheritedAssociationMapping($mapping); |
|
329
|
|
|
} |
|
330
|
377 |
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Adds inherited indexes to the subclass mapping. |
|
334
|
|
|
* |
|
335
|
|
|
* @param ClassMetadata $subClass |
|
336
|
|
|
* @param ClassMetadata $parentClass |
|
337
|
|
|
*/ |
|
338
|
377 |
|
private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
339
|
|
|
{ |
|
340
|
377 |
|
foreach ($parentClass->indexes as $index) { |
|
341
|
53 |
|
$subClass->addIndex($index['keys'], $index['options']); |
|
342
|
|
|
} |
|
343
|
377 |
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Adds inherited shard key to the subclass mapping. |
|
347
|
|
|
* |
|
348
|
|
|
* @param ClassMetadata $subClass |
|
349
|
|
|
* @param ClassMetadata $parentClass |
|
350
|
|
|
*/ |
|
351
|
377 |
|
private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
352
|
|
|
{ |
|
353
|
377 |
|
if ($parentClass->isSharded()) { |
|
354
|
5 |
|
$subClass->setShardKey( |
|
355
|
5 |
|
$parentClass->shardKey['keys'], |
|
356
|
5 |
|
$parentClass->shardKey['options'] |
|
357
|
|
|
); |
|
358
|
|
|
} |
|
359
|
377 |
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: