|
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
|
|
|
ODM\QueryResultDocument::class => 4, |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Registers annotation classes to the common registry. |
|
49
|
|
|
* |
|
50
|
|
|
* This method should be called when bootstrapping your application. |
|
51
|
|
|
* |
|
52
|
|
|
* @deprecated method was deprecated in 1.2 and will be removed in 2.0. Register class loader through AnnotationRegistry::registerLoader instead. |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function registerAnnotationClasses() |
|
55
|
|
|
{ |
|
56
|
|
|
@trigger_error( |
|
|
|
|
|
|
57
|
|
|
sprintf('%s is deprecated - register class loader through AnnotationRegistry::registerLoader instead.', __METHOD__), |
|
58
|
|
|
E_USER_DEPRECATED |
|
59
|
|
|
); |
|
60
|
|
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotations/DoctrineAnnotations.php'); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
936 |
|
public function loadMetadataForClass($className, ClassMetadata $class) |
|
67
|
|
|
{ |
|
68
|
|
|
/** @var $class ClassMetadataInfo */ |
|
69
|
936 |
|
$reflClass = $class->getReflectionClass(); |
|
70
|
|
|
|
|
71
|
936 |
|
$classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
72
|
|
|
|
|
73
|
936 |
|
$documentAnnots = array(); |
|
74
|
936 |
|
foreach ($classAnnotations as $annot) { |
|
75
|
934 |
|
$classAnnotations[get_class($annot)] = $annot; |
|
76
|
|
|
|
|
77
|
934 |
|
foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
78
|
934 |
|
if ($annot instanceof $annotClass) { |
|
79
|
934 |
|
$documentAnnots[$i] = $annot; |
|
80
|
934 |
|
continue 2; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// non-document class annotations |
|
85
|
418 |
|
if ($annot instanceof ODM\AbstractIndex) { |
|
86
|
13 |
|
$this->addIndex($class, $annot); |
|
87
|
|
|
} |
|
88
|
418 |
|
if ($annot instanceof ODM\Indexes) { |
|
89
|
65 |
|
foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
90
|
65 |
|
$this->addIndex($class, $index); |
|
91
|
|
|
} |
|
92
|
407 |
|
} elseif ($annot instanceof ODM\InheritanceType) { |
|
93
|
322 |
|
$class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
94
|
405 |
|
} elseif ($annot instanceof ODM\DiscriminatorField) { |
|
95
|
|
|
// $fieldName property is deprecated, but fall back for BC |
|
96
|
121 |
|
if (isset($annot->value)) { |
|
97
|
121 |
|
$class->setDiscriminatorField($annot->value); |
|
|
|
|
|
|
98
|
|
|
} elseif (isset($annot->name)) { |
|
|
|
|
|
|
99
|
|
|
@trigger_error( |
|
|
|
|
|
|
100
|
|
|
sprintf('Specifying discriminator\'s name through "name" is deprecated - use @DiscriminatorField("%s") instead', $annot->name), |
|
|
|
|
|
|
101
|
|
|
E_USER_DEPRECATED |
|
102
|
|
|
); |
|
103
|
|
|
$class->setDiscriminatorField($annot->name); |
|
|
|
|
|
|
104
|
|
|
} elseif (isset($annot->fieldName)) { |
|
|
|
|
|
|
105
|
|
|
@trigger_error( |
|
|
|
|
|
|
106
|
|
|
sprintf('Specifying discriminator\'s name through "name" is deprecated - use @DiscriminatorField("%s") instead', $annot->fieldName), |
|
|
|
|
|
|
107
|
|
|
E_USER_DEPRECATED |
|
108
|
|
|
); |
|
109
|
121 |
|
$class->setDiscriminatorField($annot->fieldName); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
404 |
|
} elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
112
|
121 |
|
$class->setDiscriminatorMap($annot->value); |
|
|
|
|
|
|
113
|
342 |
|
} elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
114
|
|
|
$class->setDiscriminatorValue($annot->value); |
|
|
|
|
|
|
115
|
342 |
|
} elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
116
|
57 |
|
$class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
117
|
339 |
|
} elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
118
|
57 |
|
$class->setDefaultDiscriminatorValue($annot->value); |
|
|
|
|
|
|
119
|
334 |
|
} elseif ($annot instanceof ODM\ReadPreference) { |
|
120
|
418 |
|
$class->setReadPreference($annot->value, $annot->tags); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
936 |
|
if ( ! $documentAnnots) { |
|
|
|
|
|
|
126
|
3 |
|
throw MappingException::classIsNotAValidDocument($className); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// find the winning document annotation |
|
130
|
934 |
|
ksort($documentAnnots); |
|
131
|
934 |
|
$documentAnnot = reset($documentAnnots); |
|
132
|
|
|
|
|
133
|
934 |
|
if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
134
|
312 |
|
$class->isMappedSuperclass = true; |
|
135
|
933 |
|
} elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
136
|
298 |
|
$class->isEmbeddedDocument = true; |
|
137
|
926 |
|
} elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
138
|
52 |
|
$class->isQueryResultDocument = true; |
|
139
|
|
|
} |
|
140
|
934 |
|
if (isset($documentAnnot->db)) { |
|
141
|
1 |
|
$class->setDatabase($documentAnnot->db); |
|
142
|
|
|
} |
|
143
|
934 |
|
if (isset($documentAnnot->collection)) { |
|
144
|
370 |
|
$class->setCollection($documentAnnot->collection); |
|
145
|
|
|
} |
|
146
|
934 |
|
if (isset($documentAnnot->repositoryClass)) { |
|
147
|
69 |
|
$class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
148
|
|
|
} |
|
149
|
934 |
|
if (isset($documentAnnot->writeConcern)) { |
|
150
|
11 |
|
$class->setWriteConcern($documentAnnot->writeConcern); |
|
151
|
|
|
} |
|
152
|
934 |
|
if (isset($documentAnnot->indexes)) { |
|
153
|
933 |
|
foreach ($documentAnnot->indexes as $index) { |
|
154
|
|
|
$this->addIndex($class, $index); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
934 |
|
if (isset($documentAnnot->requireIndexes)) { |
|
158
|
926 |
|
$class->setRequireIndexes($documentAnnot->requireIndexes); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
934 |
|
if (isset($documentAnnot->slaveOkay)) { |
|
161
|
1 |
|
$class->setSlaveOkay($documentAnnot->slaveOkay); |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
934 |
|
if (! empty($documentAnnot->readOnly)) { |
|
164
|
6 |
|
$class->markReadOnly(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
934 |
|
foreach ($reflClass->getProperties() as $property) { |
|
168
|
933 |
|
if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
169
|
|
|
|| |
|
170
|
933 |
|
($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
171
|
356 |
|
continue; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
932 |
|
$indexes = array(); |
|
175
|
932 |
|
$mapping = array('fieldName' => $property->getName()); |
|
176
|
932 |
|
$fieldAnnot = null; |
|
177
|
|
|
|
|
178
|
932 |
|
foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
179
|
932 |
|
if ($annot instanceof ODM\AbstractField) { |
|
180
|
932 |
|
$fieldAnnot = $annot; |
|
181
|
932 |
|
if ($annot->isDeprecated()) { |
|
182
|
|
|
@trigger_error($annot->getDeprecationMessage(), E_USER_DEPRECATED); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
932 |
|
if ($annot instanceof ODM\AbstractIndex) { |
|
186
|
201 |
|
$indexes[] = $annot; |
|
187
|
|
|
} |
|
188
|
932 |
|
if ($annot instanceof ODM\Indexes) { |
|
189
|
|
|
foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
|
190
|
|
|
$indexes[] = $index; |
|
191
|
|
|
} |
|
192
|
932 |
|
} elseif ($annot instanceof ODM\AlsoLoad) { |
|
193
|
15 |
|
$mapping['alsoLoadFields'] = (array) $annot->value; |
|
194
|
932 |
|
} elseif ($annot instanceof ODM\Version) { |
|
195
|
99 |
|
$mapping['version'] = true; |
|
196
|
932 |
|
} elseif ($annot instanceof ODM\Lock) { |
|
197
|
932 |
|
$mapping['lock'] = true; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
932 |
|
if ($fieldAnnot) { |
|
202
|
932 |
|
$mapping = array_replace($mapping, (array) $fieldAnnot); |
|
203
|
932 |
|
$class->mapField($mapping); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
932 |
|
if ($indexes) { |
|
|
|
|
|
|
207
|
201 |
|
foreach ($indexes as $index) { |
|
208
|
201 |
|
$name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
209
|
201 |
|
$keys = array($name => $index->order ?: 'asc'); |
|
210
|
932 |
|
$this->addIndex($class, $index, $keys); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
// Set shard key after all fields to ensure we mapped all its keys |
|
216
|
932 |
|
if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
217
|
72 |
|
$this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** @var $method \ReflectionMethod */ |
|
221
|
931 |
|
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
222
|
|
|
/* Filter for the declaring class only. Callbacks from parent |
|
223
|
|
|
* classes will already be registered. |
|
224
|
|
|
*/ |
|
225
|
650 |
|
if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
226
|
323 |
|
continue; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
650 |
|
foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
230
|
316 |
|
if ($annot instanceof ODM\AlsoLoad) { |
|
231
|
13 |
|
$class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
316 |
|
if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
235
|
72 |
|
continue; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
297 |
|
if ($annot instanceof ODM\PrePersist) { |
|
239
|
276 |
|
$class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
|
|
|
|
|
240
|
86 |
|
} elseif ($annot instanceof ODM\PostPersist) { |
|
241
|
11 |
|
$class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
|
|
|
|
|
242
|
85 |
|
} elseif ($annot instanceof ODM\PreUpdate) { |
|
243
|
15 |
|
$class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
|
|
|
|
|
244
|
80 |
|
} elseif ($annot instanceof ODM\PostUpdate) { |
|
245
|
67 |
|
$class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
|
|
|
|
|
246
|
74 |
|
} elseif ($annot instanceof ODM\PreRemove) { |
|
247
|
72 |
|
$class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
|
|
|
|
|
248
|
74 |
|
} elseif ($annot instanceof ODM\PostRemove) { |
|
249
|
70 |
|
$class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
|
|
|
|
|
250
|
74 |
|
} elseif ($annot instanceof ODM\PreLoad) { |
|
251
|
71 |
|
$class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
|
|
|
|
|
252
|
73 |
|
} elseif ($annot instanceof ODM\PostLoad) { |
|
253
|
72 |
|
$class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
|
|
|
|
|
254
|
11 |
|
} elseif ($annot instanceof ODM\PreFlush) { |
|
255
|
650 |
|
$class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
931 |
|
} |
|
260
|
|
|
|
|
261
|
225 |
|
private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
262
|
|
|
{ |
|
263
|
225 |
|
$keys = array_merge($keys, $index->keys); |
|
264
|
225 |
|
$options = array(); |
|
265
|
225 |
|
$allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); |
|
266
|
225 |
|
foreach ($allowed as $name) { |
|
267
|
225 |
|
if (isset($index->$name)) { |
|
268
|
225 |
|
$options[$name] = $index->$name; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
225 |
|
if (! empty($index->partialFilterExpression)) { |
|
272
|
3 |
|
$options['partialFilterExpression'] = $index->partialFilterExpression; |
|
273
|
|
|
} |
|
274
|
225 |
|
$options = array_merge($options, $index->options); |
|
275
|
225 |
|
$class->addIndex($keys, $options); |
|
276
|
225 |
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param ClassMetadataInfo $class |
|
280
|
|
|
* @param ODM\ShardKey $shardKey |
|
281
|
|
|
* |
|
282
|
|
|
* @throws MappingException |
|
283
|
|
|
*/ |
|
284
|
72 |
|
private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
285
|
|
|
{ |
|
286
|
72 |
|
$options = array(); |
|
287
|
72 |
|
$allowed = array('unique', 'numInitialChunks'); |
|
288
|
72 |
|
foreach ($allowed as $name) { |
|
289
|
72 |
|
if (isset($shardKey->$name)) { |
|
290
|
72 |
|
$options[$name] = $shardKey->$name; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
72 |
|
$class->setShardKey($shardKey->keys, $options); |
|
295
|
71 |
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Factory method for the Annotation Driver |
|
299
|
|
|
* |
|
300
|
|
|
* @param array|string $paths |
|
301
|
|
|
* @param Reader $reader |
|
302
|
|
|
* @return AnnotationDriver |
|
303
|
|
|
*/ |
|
304
|
1177 |
|
public static function create($paths = array(), Reader $reader = null) |
|
305
|
|
|
{ |
|
306
|
1177 |
|
if ($reader === null) { |
|
307
|
1177 |
|
$reader = new AnnotationReader(); |
|
308
|
|
|
} |
|
309
|
1177 |
|
return new self($reader, $paths); |
|
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: