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\ClassMetadataInfo; |
30
|
|
|
use Doctrine\ODM\MongoDB\Mapping\MappingException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations. |
34
|
|
|
* |
35
|
|
|
* @since 1.0 |
36
|
|
|
* @author Jonathan H. Wage <[email protected]> |
37
|
|
|
* @author Roman Borschel <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class AnnotationDriver extends AbstractAnnotationDriver |
40
|
|
|
{ |
41
|
|
|
protected $entityAnnotationClasses = array( |
42
|
|
|
'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\Document' => 1, |
43
|
|
|
'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\MappedSuperclass' => 2, |
44
|
|
|
'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\EmbeddedDocument' => 3, |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Registers annotation classes to the common registry. |
49
|
|
|
* |
50
|
|
|
* This method should be called when bootstrapping your application. |
51
|
|
|
*/ |
52
|
|
|
public static function registerAnnotationClasses() |
53
|
|
|
{ |
54
|
|
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotations/DoctrineAnnotations.php'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
766 |
|
public function loadMetadataForClass($className, ClassMetadata $class) |
61
|
|
|
{ |
62
|
|
|
/** @var $class ClassMetadataInfo */ |
63
|
766 |
|
$reflClass = $class->getReflectionClass(); |
64
|
|
|
|
65
|
766 |
|
$classAnnotations = $this->reader->getClassAnnotations($reflClass); |
66
|
|
|
|
67
|
766 |
|
$documentAnnots = array(); |
68
|
766 |
|
foreach ($classAnnotations as $annot) { |
69
|
764 |
|
$classAnnotations[get_class($annot)] = $annot; |
70
|
|
|
|
71
|
764 |
|
foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
72
|
764 |
|
if ($annot instanceof $annotClass) { |
73
|
764 |
|
$documentAnnots[$i] = $annot; |
74
|
764 |
|
continue 2; |
75
|
|
|
} |
76
|
502 |
|
} |
77
|
|
|
|
78
|
|
|
// non-document class annotations |
79
|
321 |
|
if ($annot instanceof ODM\AbstractIndex) { |
80
|
13 |
|
$this->addIndex($class, $annot); |
81
|
13 |
|
} |
82
|
321 |
|
if ($annot instanceof ODM\Indexes) { |
83
|
37 |
|
foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
84
|
37 |
|
$this->addIndex($class, $index); |
85
|
37 |
|
} |
86
|
321 |
|
} elseif ($annot instanceof ODM\InheritanceType) { |
87
|
242 |
|
$class->setInheritanceType(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::INHERITANCE_TYPE_'.$annot->value)); |
88
|
303 |
|
} elseif ($annot instanceof ODM\DiscriminatorField) { |
89
|
|
|
// $fieldName property is deprecated, but fall back for BC |
90
|
75 |
|
if (isset($annot->value)) { |
91
|
19 |
|
$class->setDiscriminatorField($annot->value); |
|
|
|
|
92
|
75 |
|
} elseif (isset($annot->name)) { |
93
|
|
|
$class->setDiscriminatorField($annot->name); |
94
|
72 |
|
} elseif (isset($annot->fieldName)) { |
|
|
|
|
95
|
72 |
|
$class->setDiscriminatorField($annot->fieldName); |
|
|
|
|
96
|
72 |
|
} |
97
|
301 |
|
} elseif ($annot instanceof ODM\DiscriminatorMap) { |
98
|
75 |
|
$class->setDiscriminatorMap($annot->value); |
|
|
|
|
99
|
300 |
|
} elseif ($annot instanceof ODM\DiscriminatorValue) { |
100
|
|
|
$class->setDiscriminatorValue($annot->value); |
|
|
|
|
101
|
249 |
|
} elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
102
|
21 |
|
$class->setChangeTrackingPolicy(constant('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata::CHANGETRACKING_'.$annot->value)); |
103
|
249 |
|
} elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
104
|
22 |
|
$class->setDefaultDiscriminatorValue($annot->value); |
|
|
|
|
105
|
22 |
|
} |
106
|
|
|
|
107
|
766 |
|
} |
108
|
|
|
|
109
|
766 |
|
if ( ! $documentAnnots) { |
|
|
|
|
110
|
2 |
|
throw MappingException::classIsNotAValidDocument($className); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// find the winning document annotation |
114
|
764 |
|
ksort($documentAnnots); |
115
|
764 |
|
$documentAnnot = reset($documentAnnots); |
116
|
|
|
|
117
|
764 |
|
if ($documentAnnot instanceof ODM\MappedSuperclass) { |
118
|
241 |
|
$class->isMappedSuperclass = true; |
119
|
764 |
|
} elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
120
|
399 |
|
$class->isEmbeddedDocument = true; |
121
|
399 |
|
} |
122
|
764 |
|
if (isset($documentAnnot->db)) { |
123
|
1 |
|
$class->setDatabase($documentAnnot->db); |
124
|
1 |
|
} |
125
|
764 |
|
if (isset($documentAnnot->collection)) { |
126
|
280 |
|
$class->setCollection($documentAnnot->collection); |
127
|
280 |
|
} |
128
|
764 |
|
if (isset($documentAnnot->repositoryClass) && !$class->isEmbeddedDocument) { |
129
|
31 |
|
$class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
130
|
31 |
|
} |
131
|
764 |
|
if (isset($documentAnnot->indexes)) { |
132
|
763 |
|
foreach ($documentAnnot->indexes as $index) { |
133
|
|
|
$this->addIndex($class, $index); |
134
|
763 |
|
} |
135
|
763 |
|
} |
136
|
764 |
|
if (isset($documentAnnot->requireIndexes)) { |
137
|
757 |
|
$class->setRequireIndexes($documentAnnot->requireIndexes); |
138
|
757 |
|
} |
139
|
764 |
|
if (isset($documentAnnot->slaveOkay)) { |
140
|
1 |
|
$class->setSlaveOkay($documentAnnot->slaveOkay); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
764 |
|
foreach ($reflClass->getProperties() as $property) { |
144
|
763 |
|
if (($class->isMappedSuperclass && ! $property->isPrivate()) |
145
|
|
|
|| |
146
|
763 |
|
($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
147
|
273 |
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
762 |
|
$indexes = array(); |
151
|
762 |
|
$mapping = array('fieldName' => $property->getName()); |
152
|
762 |
|
$fieldAnnot = null; |
153
|
|
|
|
154
|
762 |
|
foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
155
|
762 |
|
if ($annot instanceof ODM\AbstractField) { |
156
|
762 |
|
$fieldAnnot = $annot; |
157
|
762 |
|
} |
158
|
762 |
|
if ($annot instanceof ODM\AbstractIndex) { |
159
|
149 |
|
$indexes[] = $annot; |
160
|
149 |
|
} |
161
|
762 |
|
if ($annot instanceof ODM\Indexes) { |
162
|
|
|
foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
163
|
|
|
$indexes[] = $index; |
164
|
|
|
} |
165
|
762 |
|
} elseif ($annot instanceof ODM\AlsoLoad) { |
166
|
13 |
|
$mapping['alsoLoadFields'] = (array) $annot->value; |
167
|
762 |
|
} elseif ($annot instanceof ODM\Version) { |
168
|
61 |
|
$mapping['version'] = true; |
169
|
762 |
|
} elseif ($annot instanceof ODM\Lock) { |
170
|
24 |
|
$mapping['lock'] = true; |
171
|
24 |
|
} |
172
|
762 |
|
} |
173
|
|
|
|
174
|
762 |
|
if ($fieldAnnot) { |
175
|
762 |
|
$mapping = array_replace($mapping, (array) $fieldAnnot); |
176
|
762 |
|
$class->mapField($mapping); |
177
|
762 |
|
} |
178
|
|
|
|
179
|
762 |
|
if ($indexes) { |
|
|
|
|
180
|
149 |
|
foreach ($indexes as $index) { |
181
|
149 |
|
$name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
182
|
149 |
|
$keys = array($name => $index->order ?: 'asc'); |
183
|
150 |
|
$this->addIndex($class, $index, $keys); |
184
|
149 |
|
} |
185
|
149 |
|
} |
186
|
764 |
|
} |
187
|
|
|
|
188
|
|
|
/** @var $method \ReflectionMethod */ |
189
|
762 |
|
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
190
|
|
|
/* Filter for the declaring class only. Callbacks from parent |
191
|
|
|
* classes will already be registered. |
192
|
|
|
*/ |
193
|
528 |
|
if ($method->getDeclaringClass()->name !== $reflClass->name) { |
194
|
257 |
|
continue; |
195
|
|
|
} |
196
|
|
|
|
197
|
528 |
|
foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
198
|
247 |
|
if ($annot instanceof ODM\AlsoLoad) { |
199
|
13 |
|
$class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
|
|
|
200
|
13 |
|
} |
201
|
|
|
|
202
|
247 |
|
if ( ! isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\HasLifecycleCallbacks'])) { |
203
|
37 |
|
continue; |
204
|
|
|
} |
205
|
|
|
|
206
|
228 |
|
if ($annot instanceof ODM\PrePersist) { |
207
|
207 |
|
$class->addLifecycleCallback($method->getName(), Events::prePersist); |
208
|
228 |
|
} elseif ($annot instanceof ODM\PostPersist) { |
209
|
11 |
|
$class->addLifecycleCallback($method->getName(), Events::postPersist); |
210
|
51 |
|
} elseif ($annot instanceof ODM\PreUpdate) { |
211
|
15 |
|
$class->addLifecycleCallback($method->getName(), Events::preUpdate); |
212
|
50 |
|
} elseif ($annot instanceof ODM\PostUpdate) { |
213
|
32 |
|
$class->addLifecycleCallback($method->getName(), Events::postUpdate); |
214
|
45 |
|
} elseif ($annot instanceof ODM\PreRemove) { |
215
|
37 |
|
$class->addLifecycleCallback($method->getName(), Events::preRemove); |
216
|
39 |
|
} elseif ($annot instanceof ODM\PostRemove) { |
217
|
35 |
|
$class->addLifecycleCallback($method->getName(), Events::postRemove); |
218
|
39 |
|
} elseif ($annot instanceof ODM\PreLoad) { |
219
|
36 |
|
$class->addLifecycleCallback($method->getName(), Events::preLoad); |
220
|
39 |
|
} elseif ($annot instanceof ODM\PostLoad) { |
221
|
37 |
|
$class->addLifecycleCallback($method->getName(), Events::postLoad); |
222
|
38 |
|
} elseif ($annot instanceof ODM\PreFlush) { |
223
|
11 |
|
$class->addLifecycleCallback($method->getName(), Events::preFlush); |
224
|
11 |
|
} |
225
|
528 |
|
} |
226
|
762 |
|
} |
227
|
762 |
|
} |
228
|
|
|
|
229
|
180 |
|
private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
230
|
|
|
{ |
231
|
180 |
|
$keys = array_merge($keys, $index->keys); |
232
|
180 |
|
$options = array(); |
233
|
180 |
|
$allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); |
234
|
180 |
|
foreach ($allowed as $name) { |
235
|
180 |
|
if (isset($index->$name)) { |
236
|
180 |
|
$options[$name] = $index->$name; |
237
|
180 |
|
} |
238
|
180 |
|
} |
239
|
180 |
|
$options = array_merge($options, $index->options); |
240
|
180 |
|
$class->addIndex($keys, $options); |
241
|
180 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Factory method for the Annotation Driver |
245
|
|
|
* |
246
|
|
|
* @param array|string $paths |
247
|
|
|
* @param Reader $reader |
248
|
|
|
* @return AnnotationDriver |
249
|
|
|
*/ |
250
|
946 |
|
public static function create($paths = array(), Reader $reader = null) |
251
|
|
|
{ |
252
|
946 |
|
if ($reader === null) { |
253
|
946 |
|
$reader = new AnnotationReader(); |
254
|
946 |
|
} |
255
|
946 |
|
return new self($reader, $paths); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
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.