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
|
1089 |
|
public function setDocumentManager(DocumentManager $dm) |
64
|
|
|
{ |
65
|
1089 |
|
$this->dm = $dm; |
66
|
1089 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets the Configuration instance |
70
|
|
|
* |
71
|
|
|
* @param Configuration $config |
72
|
|
|
*/ |
73
|
1089 |
|
public function setConfiguration(Configuration $config) |
74
|
|
|
{ |
75
|
1089 |
|
$this->config = $config; |
76
|
1089 |
|
} |
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
|
864 |
|
protected function initialize() |
83
|
|
|
{ |
84
|
864 |
|
$this->driver = $this->config->getMetadataDriverImpl(); |
85
|
864 |
|
$this->evm = $this->dm->getEventManager(); |
86
|
864 |
|
$this->initialized = true; |
87
|
864 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
2 |
|
protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
93
|
|
|
{ |
94
|
|
|
return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
347 |
|
protected function getDriver() |
101
|
|
|
{ |
102
|
347 |
|
return $this->driver; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
860 |
|
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
109
|
|
|
{ |
110
|
860 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
864 |
|
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
116
|
|
|
{ |
117
|
864 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
860 |
|
protected function isEntity(ClassMetadataInterface $class) |
123
|
|
|
{ |
124
|
860 |
|
return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritDoc} |
129
|
|
|
*/ |
130
|
864 |
|
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
131
|
|
|
{ |
132
|
|
|
/** @var $class ClassMetadata */ |
133
|
|
|
/** @var $parent ClassMetadata */ |
134
|
864 |
|
if ($parent) { |
135
|
345 |
|
$class->setInheritanceType($parent->inheritanceType); |
136
|
345 |
|
$class->setDiscriminatorField($parent->discriminatorField); |
137
|
345 |
|
$class->setDiscriminatorMap($parent->discriminatorMap); |
138
|
345 |
|
$class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
139
|
345 |
|
$class->setIdGeneratorType($parent->generatorType); |
140
|
345 |
|
$this->addInheritedFields($class, $parent); |
|
|
|
|
141
|
345 |
|
$this->addInheritedRelations($class, $parent); |
|
|
|
|
142
|
345 |
|
$this->addInheritedIndexes($class, $parent); |
|
|
|
|
143
|
345 |
|
$this->setInheritedShardKey($class, $parent); |
|
|
|
|
144
|
345 |
|
$class->setIdentifier($parent->identifier); |
145
|
345 |
|
$class->setVersioned($parent->isVersioned); |
146
|
345 |
|
$class->setVersionField($parent->versionField); |
147
|
345 |
|
$class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
148
|
345 |
|
$class->setAlsoLoadMethods($parent->alsoLoadMethods); |
149
|
345 |
|
$class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
150
|
345 |
|
$class->setWriteConcern($parent->writeConcern); |
151
|
345 |
|
$class->setFile($parent->getFile()); |
152
|
345 |
|
if ($parent->isMappedSuperclass) { |
153
|
288 |
|
$class->setCustomRepositoryClass($parent->customRepositoryClassName); |
154
|
288 |
|
} |
155
|
345 |
|
} |
156
|
|
|
|
157
|
|
|
// Invoke driver |
158
|
|
|
try { |
159
|
864 |
|
$this->driver->loadMetadataForClass($class->getName(), $class); |
160
|
864 |
|
} catch (\ReflectionException $e) { |
161
|
|
|
throw MappingException::reflectionFailure($class->getName(), $e); |
162
|
|
|
} |
163
|
|
|
|
164
|
860 |
|
$this->validateIdentifier($class); |
|
|
|
|
165
|
|
|
|
166
|
860 |
|
if ($parent && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
|
|
|
167
|
102 |
|
if ($parent->generatorType) { |
168
|
102 |
|
$class->setIdGeneratorType($parent->generatorType); |
169
|
102 |
|
} |
170
|
102 |
|
if ($parent->generatorOptions) { |
|
|
|
|
171
|
|
|
$class->setIdGeneratorOptions($parent->generatorOptions); |
172
|
|
|
} |
173
|
102 |
|
if ($parent->idGenerator) { |
174
|
102 |
|
$class->setIdGenerator($parent->idGenerator); |
175
|
102 |
|
} |
176
|
102 |
|
} else { |
177
|
860 |
|
$this->completeIdGeneratorMapping($class); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
860 |
|
if ($parent && $parent->isInheritanceTypeSingleCollection()) { |
181
|
87 |
|
$class->setDatabase($parent->getDatabase()); |
182
|
87 |
|
$class->setCollection($parent->getCollection()); |
183
|
87 |
|
} |
184
|
|
|
|
185
|
860 |
|
$class->setParentClasses($nonSuperclassParents); |
186
|
|
|
|
187
|
860 |
View Code Duplication |
if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
|
|
|
|
188
|
2 |
|
$eventArgs = new LoadClassMetadataEventArgs($class, $this->dm); |
189
|
2 |
|
$this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
190
|
2 |
|
} |
191
|
860 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Validates the identifier mapping. |
195
|
|
|
* |
196
|
|
|
* @param ClassMetadata $class |
197
|
|
|
* @throws MappingException |
198
|
|
|
*/ |
199
|
860 |
|
protected function validateIdentifier($class) |
200
|
|
|
{ |
201
|
860 |
|
if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument) { |
202
|
|
|
throw MappingException::identifierRequired($class->name); |
203
|
|
|
} |
204
|
860 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Creates a new ClassMetadata instance for the given class name. |
208
|
|
|
* |
209
|
|
|
* @param string $className |
210
|
|
|
* @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
211
|
|
|
*/ |
212
|
864 |
|
protected function newClassMetadataInstance($className) |
213
|
|
|
{ |
214
|
864 |
|
return new ClassMetadata($className); |
215
|
|
|
} |
216
|
|
|
|
217
|
860 |
|
private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
218
|
|
|
{ |
219
|
860 |
|
$idGenOptions = $class->generatorOptions; |
220
|
860 |
|
switch ($class->generatorType) { |
221
|
860 |
|
case ClassMetadata::GENERATOR_TYPE_AUTO: |
222
|
795 |
|
$class->setIdGenerator(new AutoGenerator()); |
223
|
795 |
|
break; |
224
|
143 |
|
case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
225
|
6 |
|
$incrementGenerator = new IncrementGenerator(); |
226
|
6 |
|
if (isset($idGenOptions['key'])) { |
227
|
|
|
$incrementGenerator->setKey($idGenOptions['key']); |
228
|
|
|
} |
229
|
6 |
|
if (isset($idGenOptions['collection'])) { |
230
|
|
|
$incrementGenerator->setCollection($idGenOptions['collection']); |
231
|
|
|
} |
232
|
6 |
|
$class->setIdGenerator($incrementGenerator); |
233
|
6 |
|
break; |
234
|
137 |
|
case ClassMetadata::GENERATOR_TYPE_UUID: |
235
|
4 |
|
$uuidGenerator = new UuidGenerator(); |
236
|
4 |
|
isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); |
237
|
4 |
|
$class->setIdGenerator($uuidGenerator); |
238
|
4 |
|
break; |
239
|
133 |
|
case ClassMetadata::GENERATOR_TYPE_ALNUM: |
240
|
1 |
|
$alnumGenerator = new AlnumGenerator(); |
241
|
1 |
|
if (isset($idGenOptions['pad'])) { |
242
|
|
|
$alnumGenerator->setPad($idGenOptions['pad']); |
243
|
|
|
} |
244
|
1 |
|
if (isset($idGenOptions['chars'])) { |
245
|
1 |
|
$alnumGenerator->setChars($idGenOptions['chars']); |
246
|
1 |
|
} elseif (isset($idGenOptions['awkwardSafe'])) { |
247
|
|
|
$alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
$class->setIdGenerator($alnumGenerator); |
251
|
1 |
|
break; |
252
|
132 |
|
case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
253
|
|
|
if (empty($idGenOptions['class'])) { |
254
|
|
|
throw MappingException::missingIdGeneratorClass($class->name); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$customGenerator = new $idGenOptions['class']; |
258
|
|
|
unset($idGenOptions['class']); |
259
|
|
|
if ( ! $customGenerator instanceof AbstractIdGenerator) { |
260
|
|
|
throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$methods = get_class_methods($customGenerator); |
264
|
|
|
foreach ($idGenOptions as $name => $value) { |
265
|
|
|
$method = 'set' . ucfirst($name); |
266
|
|
|
if ( ! in_array($method, $methods)) { |
267
|
|
|
throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$customGenerator->$method($value); |
271
|
|
|
} |
272
|
|
|
$class->setIdGenerator($customGenerator); |
273
|
|
|
break; |
274
|
132 |
|
case ClassMetadata::GENERATOR_TYPE_NONE; |
275
|
132 |
|
break; |
276
|
|
|
default: |
277
|
|
|
throw new MappingException('Unknown generator type: ' . $class->generatorType); |
278
|
860 |
|
} |
279
|
860 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Adds inherited fields to the subclass mapping. |
283
|
|
|
* |
284
|
|
|
* @param ClassMetadata $subClass |
285
|
|
|
* @param ClassMetadata $parentClass |
286
|
|
|
*/ |
287
|
345 |
|
private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
288
|
|
|
{ |
289
|
345 |
|
foreach ($parentClass->fieldMappings as $fieldName => $mapping) { |
290
|
120 |
View Code Duplication |
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
|
|
|
|
291
|
114 |
|
$mapping['inherited'] = $parentClass->name; |
292
|
114 |
|
} |
293
|
120 |
|
if ( ! isset($mapping['declared'])) { |
294
|
120 |
|
$mapping['declared'] = $parentClass->name; |
295
|
120 |
|
} |
296
|
120 |
|
$subClass->addInheritedFieldMapping($mapping); |
297
|
345 |
|
} |
298
|
345 |
|
foreach ($parentClass->reflFields as $name => $field) { |
299
|
120 |
|
$subClass->reflFields[$name] = $field; |
300
|
345 |
|
} |
301
|
345 |
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Adds inherited association mappings to the subclass mapping. |
306
|
|
|
* |
307
|
|
|
* @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $subClass |
308
|
|
|
* @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentClass |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
* |
312
|
|
|
* @throws MappingException |
313
|
|
|
*/ |
314
|
345 |
|
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
315
|
|
|
{ |
316
|
345 |
|
foreach ($parentClass->associationMappings as $field => $mapping) { |
317
|
76 |
|
if ($parentClass->isMappedSuperclass) { |
318
|
3 |
|
$mapping['sourceDocument'] = $subClass->name; |
319
|
3 |
|
} |
320
|
|
|
|
321
|
76 |
View Code Duplication |
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
|
|
|
|
322
|
73 |
|
$mapping['inherited'] = $parentClass->name; |
323
|
73 |
|
} |
324
|
76 |
|
if ( ! isset($mapping['declared'])) { |
325
|
76 |
|
$mapping['declared'] = $parentClass->name; |
326
|
76 |
|
} |
327
|
76 |
|
$subClass->addInheritedAssociationMapping($mapping); |
328
|
345 |
|
} |
329
|
345 |
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Adds inherited indexes to the subclass mapping. |
333
|
|
|
* |
334
|
|
|
* @param ClassMetadata $subClass |
335
|
|
|
* @param ClassMetadata $parentClass |
336
|
|
|
*/ |
337
|
345 |
|
private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
338
|
|
|
{ |
339
|
345 |
|
foreach ($parentClass->indexes as $index) { |
340
|
53 |
|
$subClass->addIndex($index['keys'], $index['options']); |
341
|
345 |
|
} |
342
|
345 |
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Adds inherited shard key to the subclass mapping. |
346
|
|
|
* |
347
|
|
|
* @param ClassMetadata $subClass |
348
|
|
|
* @param ClassMetadata $parentClass |
349
|
|
|
*/ |
350
|
345 |
|
private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass) |
351
|
|
|
{ |
352
|
345 |
|
if ($parentClass->isSharded()) { |
353
|
5 |
|
$subClass->setShardKey( |
354
|
5 |
|
$parentClass->shardKey['keys'], |
355
|
5 |
|
$parentClass->shardKey['options'] |
356
|
5 |
|
); |
357
|
5 |
|
} |
358
|
345 |
|
} |
359
|
|
|
} |
360
|
|
|
|
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: