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\Driver; |
21
|
|
|
|
22
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
23
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
24
|
|
|
use Doctrine\Common\Annotations\Reader; |
25
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
26
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver; |
27
|
|
|
use Doctrine\ODM\MongoDB\Events; |
28
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
29
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata; |
30
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo; |
31
|
|
|
use Doctrine\ODM\MongoDB\Mapping\MappingException; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations. |
35
|
|
|
* |
36
|
|
|
* @since 1.0 |
37
|
|
|
*/ |
38
|
|
|
class AnnotationDriver extends AbstractAnnotationDriver |
39
|
|
|
{ |
40
|
|
|
protected $entityAnnotationClasses = array( |
41
|
|
|
ODM\Document::class => 1, |
42
|
|
|
ODM\MappedSuperclass::class => 2, |
43
|
|
|
ODM\EmbeddedDocument::class => 3, |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Registers annotation classes to the common registry. |
48
|
|
|
* |
49
|
|
|
* This method should be called when bootstrapping your application. |
50
|
|
|
*/ |
51
|
|
|
public static function registerAnnotationClasses() |
52
|
|
|
{ |
53
|
|
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotations/DoctrineAnnotations.php'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
867 |
|
public function loadMetadataForClass($className, ClassMetadata $class) |
60
|
|
|
{ |
61
|
|
|
/** @var $class ClassMetadataInfo */ |
62
|
867 |
|
$reflClass = $class->getReflectionClass(); |
63
|
|
|
|
64
|
867 |
|
$classAnnotations = $this->reader->getClassAnnotations($reflClass); |
65
|
|
|
|
66
|
867 |
|
$documentAnnots = array(); |
67
|
867 |
|
foreach ($classAnnotations as $annot) { |
68
|
865 |
|
$classAnnotations[get_class($annot)] = $annot; |
69
|
|
|
|
70
|
865 |
|
foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
71
|
865 |
|
if ($annot instanceof $annotClass) { |
72
|
865 |
|
$documentAnnots[$i] = $annot; |
73
|
865 |
|
continue 2; |
74
|
|
|
} |
75
|
559 |
|
} |
76
|
|
|
|
77
|
|
|
// non-document class annotations |
78
|
379 |
|
if ($annot instanceof ODM\AbstractIndex) { |
79
|
13 |
|
$this->addIndex($class, $annot); |
80
|
13 |
|
} |
81
|
379 |
|
if ($annot instanceof ODM\Indexes) { |
82
|
63 |
|
foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
83
|
63 |
|
$this->addIndex($class, $index); |
84
|
63 |
|
} |
85
|
379 |
|
} elseif ($annot instanceof ODM\InheritanceType) { |
86
|
289 |
|
$class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
87
|
370 |
|
} elseif ($annot instanceof ODM\DiscriminatorField) { |
88
|
|
|
// $fieldName property is deprecated, but fall back for BC |
89
|
112 |
|
if (isset($annot->value)) { |
90
|
54 |
|
$class->setDiscriminatorField($annot->value); |
|
|
|
|
91
|
112 |
|
} elseif (isset($annot->name)) { |
92
|
|
|
$class->setDiscriminatorField($annot->name); |
93
|
109 |
|
} elseif (isset($annot->fieldName)) { |
|
|
|
|
94
|
109 |
|
$class->setDiscriminatorField($annot->fieldName); |
|
|
|
|
95
|
109 |
|
} |
96
|
368 |
|
} elseif ($annot instanceof ODM\DiscriminatorMap) { |
97
|
112 |
|
$class->setDiscriminatorMap($annot->value); |
|
|
|
|
98
|
367 |
|
} elseif ($annot instanceof ODM\DiscriminatorValue) { |
99
|
|
|
$class->setDiscriminatorValue($annot->value); |
|
|
|
|
100
|
314 |
|
} elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
101
|
57 |
|
$class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
102
|
314 |
|
} elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
103
|
57 |
|
$class->setDefaultDiscriminatorValue($annot->value); |
|
|
|
|
104
|
57 |
|
} |
105
|
|
|
|
106
|
867 |
|
} |
107
|
|
|
|
108
|
867 |
|
if ( ! $documentAnnots) { |
|
|
|
|
109
|
3 |
|
throw MappingException::classIsNotAValidDocument($className); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// find the winning document annotation |
113
|
865 |
|
ksort($documentAnnots); |
114
|
865 |
|
$documentAnnot = reset($documentAnnots); |
115
|
|
|
|
116
|
865 |
|
if ($documentAnnot instanceof ODM\MappedSuperclass) { |
117
|
287 |
|
$class->isMappedSuperclass = true; |
118
|
865 |
|
} elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
119
|
287 |
|
$class->isEmbeddedDocument = true; |
120
|
287 |
|
} |
121
|
865 |
|
if (isset($documentAnnot->db)) { |
122
|
1 |
|
$class->setDatabase($documentAnnot->db); |
123
|
1 |
|
} |
124
|
865 |
|
if (isset($documentAnnot->collection)) { |
125
|
339 |
|
$class->setCollection($documentAnnot->collection); |
126
|
339 |
|
} |
127
|
865 |
|
if (isset($documentAnnot->repositoryClass) && !$class->isEmbeddedDocument) { |
128
|
65 |
|
$class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
129
|
65 |
|
} |
130
|
865 |
|
if (isset($documentAnnot->writeConcern)) { |
131
|
11 |
|
$class->setWriteConcern($documentAnnot->writeConcern); |
132
|
11 |
|
} |
133
|
865 |
|
if (isset($documentAnnot->indexes)) { |
134
|
864 |
|
foreach ($documentAnnot->indexes as $index) { |
135
|
|
|
$this->addIndex($class, $index); |
136
|
864 |
|
} |
137
|
864 |
|
} |
138
|
865 |
|
if (isset($documentAnnot->requireIndexes)) { |
139
|
857 |
|
$class->setRequireIndexes($documentAnnot->requireIndexes); |
140
|
857 |
|
} |
141
|
865 |
|
if (isset($documentAnnot->slaveOkay)) { |
142
|
1 |
|
$class->setSlaveOkay($documentAnnot->slaveOkay); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
865 |
|
foreach ($reflClass->getProperties() as $property) { |
146
|
864 |
|
if (($class->isMappedSuperclass && ! $property->isPrivate()) |
147
|
|
|
|| |
148
|
864 |
|
($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
149
|
324 |
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
863 |
|
$indexes = array(); |
153
|
863 |
|
$mapping = array('fieldName' => $property->getName()); |
154
|
863 |
|
$fieldAnnot = null; |
155
|
|
|
|
156
|
863 |
|
foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
157
|
863 |
|
if ($annot instanceof ODM\AbstractField) { |
158
|
863 |
|
$fieldAnnot = $annot; |
159
|
863 |
|
} |
160
|
863 |
|
if ($annot instanceof ODM\AbstractIndex) { |
161
|
185 |
|
$indexes[] = $annot; |
162
|
185 |
|
} |
163
|
863 |
|
if ($annot instanceof ODM\Indexes) { |
164
|
|
|
foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
165
|
|
|
$indexes[] = $index; |
166
|
|
|
} |
167
|
863 |
|
} elseif ($annot instanceof ODM\AlsoLoad) { |
168
|
13 |
|
$mapping['alsoLoadFields'] = (array) $annot->value; |
169
|
863 |
|
} elseif ($annot instanceof ODM\Version) { |
170
|
97 |
|
$mapping['version'] = true; |
171
|
863 |
|
} elseif ($annot instanceof ODM\Lock) { |
172
|
24 |
|
$mapping['lock'] = true; |
173
|
24 |
|
} |
174
|
863 |
|
} |
175
|
|
|
|
176
|
863 |
|
if ($fieldAnnot) { |
177
|
864 |
|
$mapping = array_replace($mapping, (array) $fieldAnnot); |
178
|
863 |
|
$class->mapField($mapping); |
179
|
863 |
|
} |
180
|
|
|
|
181
|
863 |
|
if ($indexes) { |
|
|
|
|
182
|
185 |
|
foreach ($indexes as $index) { |
183
|
186 |
|
$name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
184
|
185 |
|
$keys = array($name => $index->order ?: 'asc'); |
185
|
185 |
|
$this->addIndex($class, $index, $keys); |
186
|
185 |
|
} |
187
|
185 |
|
} |
188
|
865 |
|
} |
189
|
|
|
|
190
|
|
|
// Set shard key after all fields to ensure we mapped all its keys |
191
|
863 |
|
if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
192
|
71 |
|
$this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
193
|
70 |
|
} |
194
|
|
|
|
195
|
|
|
/** @var $method \ReflectionMethod */ |
196
|
862 |
|
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
197
|
|
|
/* Filter for the declaring class only. Callbacks from parent |
198
|
|
|
* classes will already be registered. |
199
|
|
|
*/ |
200
|
594 |
|
if ($method->getDeclaringClass()->name !== $reflClass->name) { |
201
|
298 |
|
continue; |
202
|
|
|
} |
203
|
|
|
|
204
|
594 |
|
foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
205
|
292 |
|
if ($annot instanceof ODM\AlsoLoad) { |
206
|
13 |
|
$class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
|
|
|
207
|
13 |
|
} |
208
|
|
|
|
209
|
292 |
|
if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
210
|
72 |
|
continue; |
211
|
|
|
} |
212
|
|
|
|
213
|
273 |
|
if ($annot instanceof ODM\PrePersist) { |
214
|
252 |
|
$class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
|
|
|
215
|
273 |
|
} elseif ($annot instanceof ODM\PostPersist) { |
216
|
11 |
|
$class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
|
|
|
217
|
86 |
|
} elseif ($annot instanceof ODM\PreUpdate) { |
218
|
15 |
|
$class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
|
|
|
219
|
85 |
|
} elseif ($annot instanceof ODM\PostUpdate) { |
220
|
67 |
|
$class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
|
|
|
221
|
80 |
|
} elseif ($annot instanceof ODM\PreRemove) { |
222
|
72 |
|
$class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
|
|
|
223
|
74 |
|
} elseif ($annot instanceof ODM\PostRemove) { |
224
|
70 |
|
$class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
|
|
|
225
|
74 |
|
} elseif ($annot instanceof ODM\PreLoad) { |
226
|
71 |
|
$class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
|
|
|
227
|
74 |
|
} elseif ($annot instanceof ODM\PostLoad) { |
228
|
72 |
|
$class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
|
|
|
229
|
73 |
|
} elseif ($annot instanceof ODM\PreFlush) { |
230
|
11 |
|
$class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
|
|
|
231
|
11 |
|
} |
232
|
594 |
|
} |
233
|
862 |
|
} |
234
|
862 |
|
} |
235
|
|
|
|
236
|
207 |
|
private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
237
|
|
|
{ |
238
|
207 |
|
$keys = array_merge($keys, $index->keys); |
239
|
207 |
|
$options = array(); |
240
|
207 |
|
$allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); |
241
|
207 |
|
foreach ($allowed as $name) { |
242
|
207 |
|
if (isset($index->$name)) { |
243
|
207 |
|
$options[$name] = $index->$name; |
244
|
207 |
|
} |
245
|
207 |
|
} |
246
|
207 |
|
if (! empty($index->partialFilterExpression)) { |
247
|
2 |
|
$options['partialFilterExpression'] = $index->partialFilterExpression; |
248
|
2 |
|
} |
249
|
207 |
|
$options = array_merge($options, $index->options); |
250
|
207 |
|
$class->addIndex($keys, $options); |
251
|
207 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param ClassMetadataInfo $class |
255
|
|
|
* @param ODM\ShardKey $shardKey |
256
|
|
|
* |
257
|
|
|
* @throws MappingException |
258
|
|
|
*/ |
259
|
71 |
|
private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
260
|
|
|
{ |
261
|
71 |
|
$options = array(); |
262
|
71 |
|
$allowed = array('unique', 'numInitialChunks'); |
263
|
71 |
|
foreach ($allowed as $name) { |
264
|
71 |
|
if (isset($shardKey->$name)) { |
265
|
1 |
|
$options[$name] = $shardKey->$name; |
266
|
1 |
|
} |
267
|
71 |
|
} |
268
|
|
|
|
269
|
71 |
|
$class->setShardKey($shardKey->keys, $options); |
270
|
70 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Factory method for the Annotation Driver |
274
|
|
|
* |
275
|
|
|
* @param array|string $paths |
276
|
|
|
* @param Reader $reader |
277
|
|
|
* @return AnnotationDriver |
278
|
|
|
*/ |
279
|
1087 |
|
public static function create($paths = array(), Reader $reader = null) |
280
|
|
|
{ |
281
|
1087 |
|
if ($reader === null) { |
282
|
1087 |
|
$reader = new AnnotationReader(); |
283
|
1087 |
|
} |
284
|
1087 |
|
return new self($reader, $paths); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.