These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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( |
||
0 ignored issues
–
show
|
|||
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 | 903 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
67 | { |
||
68 | /** @var $class ClassMetadataInfo */ |
||
69 | 903 | $reflClass = $class->getReflectionClass(); |
|
70 | |||
71 | 903 | $classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
72 | |||
73 | 903 | $documentAnnots = array(); |
|
74 | 903 | foreach ($classAnnotations as $annot) { |
|
75 | 901 | $classAnnotations[get_class($annot)] = $annot; |
|
76 | |||
77 | 901 | foreach ($this->entityAnnotationClasses as $annotClass => $i) { |
|
78 | 901 | if ($annot instanceof $annotClass) { |
|
79 | 901 | $documentAnnots[$i] = $annot; |
|
80 | 901 | continue 2; |
|
81 | } |
||
82 | } |
||
83 | |||
84 | // non-document class annotations |
||
85 | 396 | if ($annot instanceof ODM\AbstractIndex) { |
|
86 | 13 | $this->addIndex($class, $annot); |
|
87 | } |
||
88 | 396 | 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 | 385 | } elseif ($annot instanceof ODM\InheritanceType) { |
|
93 | 313 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_'.$annot->value)); |
|
94 | 383 | } 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)) { |
||
0 ignored issues
–
show
The property
Doctrine\ODM\MongoDB\Map...scriminatorField::$name has been deprecated with message: property was deprecated in 1.2 and will be removed in 2.0
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.
Loading history...
|
|||
99 | @trigger_error( |
||
0 ignored issues
–
show
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||
100 | sprintf('Specifying discriminator\'s name through "name" is deprecated - use @DiscriminatorField("%s") instead', $annot->name), |
||
0 ignored issues
–
show
The property
Doctrine\ODM\MongoDB\Map...scriminatorField::$name has been deprecated with message: property was deprecated in 1.2 and will be removed in 2.0
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.
Loading history...
|
|||
101 | E_USER_DEPRECATED |
||
102 | ); |
||
103 | $class->setDiscriminatorField($annot->name); |
||
104 | } elseif (isset($annot->fieldName)) { |
||
105 | @trigger_error( |
||
0 ignored issues
–
show
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||
106 | sprintf('Specifying discriminator\'s name through "name" is deprecated - use @DiscriminatorField("%s") instead', $annot->fieldName), |
||
0 ignored issues
–
show
The property
Doctrine\ODM\MongoDB\Map...inatorField::$fieldName has been deprecated with message: property was deprecated in 1.0.0-BETA10 and will be removed in 2.0
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.
Loading history...
|
|||
107 | E_USER_DEPRECATED |
||
108 | ); |
||
109 | $class->setDiscriminatorField($annot->fieldName); |
||
110 | } |
||
111 | 382 | } elseif ($annot instanceof ODM\DiscriminatorMap) { |
|
112 | 121 | $class->setDiscriminatorMap($annot->value); |
|
113 | 320 | } elseif ($annot instanceof ODM\DiscriminatorValue) { |
|
114 | $class->setDiscriminatorValue($annot->value); |
||
115 | 320 | } elseif ($annot instanceof ODM\ChangeTrackingPolicy) { |
|
116 | 57 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_'.$annot->value)); |
|
117 | 317 | } elseif ($annot instanceof ODM\DefaultDiscriminatorValue) { |
|
118 | 57 | $class->setDefaultDiscriminatorValue($annot->value); |
|
119 | } |
||
120 | |||
121 | } |
||
122 | |||
123 | 903 | if ( ! $documentAnnots) { |
|
124 | 3 | throw MappingException::classIsNotAValidDocument($className); |
|
125 | } |
||
126 | |||
127 | // find the winning document annotation |
||
128 | 901 | ksort($documentAnnots); |
|
129 | 901 | $documentAnnot = reset($documentAnnots); |
|
130 | |||
131 | 901 | if ($documentAnnot instanceof ODM\MappedSuperclass) { |
|
132 | 302 | $class->isMappedSuperclass = true; |
|
133 | 900 | } elseif ($documentAnnot instanceof ODM\EmbeddedDocument) { |
|
134 | 298 | $class->isEmbeddedDocument = true; |
|
135 | 893 | } elseif ($documentAnnot instanceof ODM\QueryResultDocument) { |
|
136 | 52 | $class->isQueryResultDocument = true; |
|
137 | } |
||
138 | 901 | if (isset($documentAnnot->db)) { |
|
139 | 1 | $class->setDatabase($documentAnnot->db); |
|
140 | } |
||
141 | 901 | if (isset($documentAnnot->collection)) { |
|
142 | 351 | $class->setCollection($documentAnnot->collection); |
|
143 | } |
||
144 | 901 | if (isset($documentAnnot->repositoryClass)) { |
|
145 | 69 | $class->setCustomRepositoryClass($documentAnnot->repositoryClass); |
|
146 | } |
||
147 | 901 | if (isset($documentAnnot->writeConcern)) { |
|
148 | 11 | $class->setWriteConcern($documentAnnot->writeConcern); |
|
149 | } |
||
150 | 901 | if (isset($documentAnnot->indexes)) { |
|
151 | 900 | foreach ($documentAnnot->indexes as $index) { |
|
152 | $this->addIndex($class, $index); |
||
153 | } |
||
154 | } |
||
155 | 901 | if (isset($documentAnnot->requireIndexes)) { |
|
156 | 893 | $class->setRequireIndexes($documentAnnot->requireIndexes); |
|
157 | } |
||
158 | 901 | if (isset($documentAnnot->slaveOkay)) { |
|
159 | 1 | $class->setSlaveOkay($documentAnnot->slaveOkay); |
|
0 ignored issues
–
show
The method
Doctrine\ODM\MongoDB\Map...ataInfo::setSlaveOkay() has been deprecated with message: in version 1.2 and will be removed in 2.0.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
160 | } |
||
161 | |||
162 | 901 | foreach ($reflClass->getProperties() as $property) { |
|
163 | 900 | if (($class->isMappedSuperclass && ! $property->isPrivate()) |
|
164 | || |
||
165 | 899 | ($class->isInheritedField($property->name) && $property->getDeclaringClass()->name !== $class->name)) { |
|
166 | 347 | continue; |
|
167 | } |
||
168 | |||
169 | 899 | $indexes = array(); |
|
170 | 899 | $mapping = array('fieldName' => $property->getName()); |
|
171 | 899 | $fieldAnnot = null; |
|
172 | |||
173 | 899 | foreach ($this->reader->getPropertyAnnotations($property) as $annot) { |
|
174 | 899 | if ($annot instanceof ODM\AbstractField) { |
|
175 | 899 | $fieldAnnot = $annot; |
|
176 | 899 | if ($annot->isDeprecated()) { |
|
177 | @trigger_error($annot->getDeprecationMessage(), E_USER_DEPRECATED); |
||
0 ignored issues
–
show
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||
178 | } |
||
179 | } |
||
180 | 899 | if ($annot instanceof ODM\AbstractIndex) { |
|
181 | 198 | $indexes[] = $annot; |
|
182 | } |
||
183 | 899 | if ($annot instanceof ODM\Indexes) { |
|
184 | foreach (is_array($annot->value) ? $annot->value : array($annot->value) as $index) { |
||
185 | $indexes[] = $index; |
||
186 | } |
||
187 | 899 | } elseif ($annot instanceof ODM\AlsoLoad) { |
|
188 | 15 | $mapping['alsoLoadFields'] = (array) $annot->value; |
|
189 | 899 | } elseif ($annot instanceof ODM\Version) { |
|
190 | 97 | $mapping['version'] = true; |
|
191 | 899 | } elseif ($annot instanceof ODM\Lock) { |
|
192 | 24 | $mapping['lock'] = true; |
|
193 | } |
||
194 | } |
||
195 | |||
196 | 899 | if ($fieldAnnot) { |
|
197 | 899 | $mapping = array_replace($mapping, (array) $fieldAnnot); |
|
198 | 899 | $class->mapField($mapping); |
|
199 | } |
||
200 | |||
201 | 899 | if ($indexes) { |
|
202 | 198 | foreach ($indexes as $index) { |
|
203 | 198 | $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; |
|
204 | 198 | $keys = array($name => $index->order ?: 'asc'); |
|
205 | 198 | $this->addIndex($class, $index, $keys); |
|
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | // Set shard key after all fields to ensure we mapped all its keys |
||
211 | 899 | if (isset($classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey'])) { |
|
212 | 62 | $this->setShardKey($class, $classAnnotations['Doctrine\ODM\MongoDB\Mapping\Annotations\ShardKey']); |
|
213 | } |
||
214 | |||
215 | /** @var $method \ReflectionMethod */ |
||
216 | 898 | foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
217 | /* Filter for the declaring class only. Callbacks from parent |
||
218 | * classes will already be registered. |
||
219 | */ |
||
220 | 634 | if ($method->getDeclaringClass()->name !== $reflClass->name) { |
|
221 | 314 | continue; |
|
222 | } |
||
223 | |||
224 | 634 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
225 | 307 | if ($annot instanceof ODM\AlsoLoad) { |
|
226 | 13 | $class->registerAlsoLoadMethod($method->getName(), $annot->value); |
|
227 | } |
||
228 | |||
229 | 307 | if ( ! isset($classAnnotations[ODM\HasLifecycleCallbacks::class])) { |
|
230 | 72 | continue; |
|
231 | } |
||
232 | |||
233 | 288 | if ($annot instanceof ODM\PrePersist) { |
|
234 | 267 | $class->addLifecycleCallback($method->getName(), Events::prePersist); |
|
235 | 86 | } elseif ($annot instanceof ODM\PostPersist) { |
|
236 | 11 | $class->addLifecycleCallback($method->getName(), Events::postPersist); |
|
237 | 85 | } elseif ($annot instanceof ODM\PreUpdate) { |
|
238 | 15 | $class->addLifecycleCallback($method->getName(), Events::preUpdate); |
|
239 | 80 | } elseif ($annot instanceof ODM\PostUpdate) { |
|
240 | 67 | $class->addLifecycleCallback($method->getName(), Events::postUpdate); |
|
241 | 74 | } elseif ($annot instanceof ODM\PreRemove) { |
|
242 | 72 | $class->addLifecycleCallback($method->getName(), Events::preRemove); |
|
243 | 74 | } elseif ($annot instanceof ODM\PostRemove) { |
|
244 | 70 | $class->addLifecycleCallback($method->getName(), Events::postRemove); |
|
245 | 74 | } elseif ($annot instanceof ODM\PreLoad) { |
|
246 | 71 | $class->addLifecycleCallback($method->getName(), Events::preLoad); |
|
247 | 73 | } elseif ($annot instanceof ODM\PostLoad) { |
|
248 | 72 | $class->addLifecycleCallback($method->getName(), Events::postLoad); |
|
249 | 11 | } elseif ($annot instanceof ODM\PreFlush) { |
|
250 | 634 | $class->addLifecycleCallback($method->getName(), Events::preFlush); |
|
251 | } |
||
252 | } |
||
253 | } |
||
254 | 898 | } |
|
255 | |||
256 | 222 | private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) |
|
257 | { |
||
258 | 222 | $keys = array_merge($keys, $index->keys); |
|
259 | 222 | $options = array(); |
|
260 | 222 | $allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); |
|
261 | 222 | foreach ($allowed as $name) { |
|
262 | 222 | if (isset($index->$name)) { |
|
263 | 222 | $options[$name] = $index->$name; |
|
264 | } |
||
265 | } |
||
266 | 222 | if (! empty($index->partialFilterExpression)) { |
|
267 | 3 | $options['partialFilterExpression'] = $index->partialFilterExpression; |
|
268 | } |
||
269 | 222 | $options = array_merge($options, $index->options); |
|
270 | 222 | $class->addIndex($keys, $options); |
|
271 | 222 | } |
|
272 | |||
273 | /** |
||
274 | * @param ClassMetadataInfo $class |
||
275 | * @param ODM\ShardKey $shardKey |
||
276 | * |
||
277 | * @throws MappingException |
||
278 | */ |
||
279 | 62 | private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) |
|
280 | { |
||
281 | 62 | $options = array(); |
|
282 | 62 | $allowed = array('unique', 'numInitialChunks'); |
|
283 | 62 | foreach ($allowed as $name) { |
|
284 | 62 | if (isset($shardKey->$name)) { |
|
285 | 1 | $options[$name] = $shardKey->$name; |
|
286 | } |
||
287 | } |
||
288 | |||
289 | 62 | $class->setShardKey($shardKey->keys, $options); |
|
290 | 61 | } |
|
291 | |||
292 | /** |
||
293 | * Factory method for the Annotation Driver |
||
294 | * |
||
295 | * @param array|string $paths |
||
296 | * @param Reader $reader |
||
297 | * @return AnnotationDriver |
||
298 | */ |
||
299 | 1139 | public static function create($paths = array(), Reader $reader = null) |
|
300 | { |
||
301 | 1139 | if ($reader === null) { |
|
302 | 1139 | $reader = new AnnotationReader(); |
|
303 | } |
||
304 | 1139 | return new self($reader, $paths); |
|
305 | } |
||
306 | } |
||
307 |
If you suppress an error, we recommend checking for the error condition explicitly: