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 LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\ORM\Mapping\Driver; |
---|
21 | |
---|
22 | use Doctrine\Common\Cache\ArrayCache, |
---|
23 | Doctrine\Common\Annotations\AnnotationReader, |
---|
24 | Doctrine\Common\Annotations\AnnotationRegistry, |
---|
25 | Doctrine\ORM\Mapping\ClassMetadataInfo, |
---|
26 | Doctrine\ORM\Mapping\MappingException; |
---|
27 | |
---|
28 | /** |
---|
29 | * The AnnotationDriver reads the mapping metadata from docblock annotations. |
---|
30 | * |
---|
31 | * @since 2.0 |
---|
32 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
33 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
---|
34 | * @author Jonathan H. Wage <jonwage@gmail.com> |
---|
35 | * @author Roman Borschel <roman@code-factory.org> |
---|
36 | */ |
---|
37 | class AnnotationDriver implements Driver |
---|
38 | { |
---|
39 | /** |
---|
40 | * The AnnotationReader. |
---|
41 | * |
---|
42 | * @var AnnotationReader |
---|
43 | */ |
---|
44 | protected $_reader; |
---|
45 | |
---|
46 | /** |
---|
47 | * The paths where to look for mapping files. |
---|
48 | * |
---|
49 | * @var array |
---|
50 | */ |
---|
51 | protected $_paths = array(); |
---|
52 | |
---|
53 | /** |
---|
54 | * The file extension of mapping documents. |
---|
55 | * |
---|
56 | * @var string |
---|
57 | */ |
---|
58 | protected $_fileExtension = '.php'; |
---|
59 | |
---|
60 | /** |
---|
61 | * @param array |
---|
62 | */ |
---|
63 | protected $_classNames; |
---|
64 | |
---|
65 | /** |
---|
66 | * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading |
---|
67 | * docblock annotations. |
---|
68 | * |
---|
69 | * @param AnnotationReader $reader The AnnotationReader to use, duck-typed. |
---|
70 | * @param string|array $paths One or multiple paths where mapping classes can be found. |
---|
71 | */ |
---|
72 | public function __construct($reader, $paths = null) |
---|
73 | { |
---|
74 | $this->_reader = $reader; |
---|
75 | if ($paths) { |
---|
76 | $this->addPaths((array) $paths); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Append lookup paths to metadata driver. |
---|
82 | * |
---|
83 | * @param array $paths |
---|
84 | */ |
---|
85 | public function addPaths(array $paths) |
---|
86 | { |
---|
87 | $this->_paths = array_unique(array_merge($this->_paths, $paths)); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Retrieve the defined metadata lookup paths. |
---|
92 | * |
---|
93 | * @return array |
---|
94 | */ |
---|
95 | public function getPaths() |
---|
96 | { |
---|
97 | return $this->_paths; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Retrieve the current annotation reader |
---|
102 | * |
---|
103 | * @return AnnotationReader |
---|
104 | */ |
---|
105 | public function getReader() |
---|
106 | { |
---|
107 | return $this->_reader; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Get the file extension used to look for mapping files under |
---|
112 | * |
---|
113 | * @return void |
---|
114 | */ |
---|
115 | public function getFileExtension() |
---|
116 | { |
---|
117 | return $this->_fileExtension; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Set the file extension used to look for mapping files under |
---|
122 | * |
---|
123 | * @param string $fileExtension The file extension to set |
---|
124 | * @return void |
---|
125 | */ |
---|
126 | public function setFileExtension($fileExtension) |
---|
127 | { |
---|
128 | $this->_fileExtension = $fileExtension; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * {@inheritdoc} |
---|
133 | */ |
---|
134 | public function loadMetadataForClass($className, ClassMetadataInfo $metadata) |
---|
135 | { |
---|
136 | $class = $metadata->getReflectionClass(); |
---|
137 | if (!$class) { |
---|
138 | // this happens when running annotation driver in combination with |
---|
139 | // static reflection services. This is not the nicest fix |
---|
140 | $class = new \ReflectionClass($metadata->name); |
---|
141 | } |
---|
142 | |
---|
143 | $classAnnotations = $this->_reader->getClassAnnotations($class); |
---|
144 | |
---|
145 | if ($classAnnotations) { |
---|
146 | foreach ($classAnnotations as $key => $annot) { |
---|
147 | if ( ! is_numeric($key)) { |
---|
148 | continue; |
---|
149 | } |
---|
150 | |
---|
151 | $classAnnotations[get_class($annot)] = $annot; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | // Evaluate Entity annotation |
---|
156 | if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) { |
---|
157 | $entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity']; |
---|
158 | if ($entityAnnot->repositoryClass !== null) { |
---|
159 | $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass); |
---|
160 | } |
---|
161 | if ($entityAnnot->readOnly) { |
---|
162 | $metadata->markReadOnly(); |
---|
163 | } |
---|
164 | } else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) { |
---|
165 | $mappedSuperclassAnnot = $classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']; |
---|
166 | $metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass); |
---|
167 | $metadata->isMappedSuperclass = true; |
---|
168 | } else { |
---|
169 | throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); |
---|
170 | } |
---|
171 | |
---|
172 | // Evaluate Table annotation |
---|
173 | if (isset($classAnnotations['Doctrine\ORM\Mapping\Table'])) { |
---|
174 | $tableAnnot = $classAnnotations['Doctrine\ORM\Mapping\Table']; |
---|
175 | $primaryTable = array( |
---|
176 | 'name' => $tableAnnot->name, |
---|
177 | 'schema' => $tableAnnot->schema |
---|
178 | ); |
---|
179 | |
---|
180 | if ($tableAnnot->indexes !== null) { |
---|
181 | foreach ($tableAnnot->indexes as $indexAnnot) { |
---|
182 | $index = array('columns' => $indexAnnot->columns); |
---|
183 | |
---|
184 | if ( ! empty($indexAnnot->name)) { |
---|
185 | $primaryTable['indexes'][$indexAnnot->name] = $index; |
---|
186 | } else { |
---|
187 | $primaryTable['indexes'][] = $index; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | if ($tableAnnot->uniqueConstraints !== null) { |
---|
193 | foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) { |
---|
194 | $uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns); |
---|
195 | |
---|
196 | if ( ! empty($uniqueConstraintAnnot->name)) { |
---|
197 | $primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint; |
---|
198 | } else { |
---|
199 | $primaryTable['uniqueConstraints'][] = $uniqueConstraint; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | $metadata->setPrimaryTable($primaryTable); |
---|
205 | } |
---|
206 | |
---|
207 | // Evaluate NamedQueries annotation |
---|
208 | if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedQueries'])) { |
---|
209 | $namedQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedQueries']; |
---|
210 | |
---|
211 | if (!is_array($namedQueriesAnnot->value)) { |
---|
212 | throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations."); |
---|
213 | } |
---|
214 | |
---|
215 | foreach ($namedQueriesAnnot->value as $namedQuery) { |
---|
216 | if (!($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) { |
---|
217 | throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations."); |
---|
218 | } |
---|
219 | $metadata->addNamedQuery(array( |
---|
220 | 'name' => $namedQuery->name, |
---|
221 | 'query' => $namedQuery->query |
---|
222 | )); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | // Evaluate InheritanceType annotation |
---|
227 | if (isset($classAnnotations['Doctrine\ORM\Mapping\InheritanceType'])) { |
---|
228 | $inheritanceTypeAnnot = $classAnnotations['Doctrine\ORM\Mapping\InheritanceType']; |
---|
229 | $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value)); |
---|
230 | |
---|
231 | if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) { |
---|
232 | // Evaluate DiscriminatorColumn annotation |
---|
233 | if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) { |
---|
234 | $discrColumnAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn']; |
---|
235 | $metadata->setDiscriminatorColumn(array( |
---|
236 | 'name' => $discrColumnAnnot->name, |
---|
237 | 'type' => $discrColumnAnnot->type, |
---|
238 | 'length' => $discrColumnAnnot->length |
---|
239 | )); |
---|
240 | } else { |
---|
241 | $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255)); |
---|
242 | } |
---|
243 | |
---|
244 | // Evaluate DiscriminatorMap annotation |
---|
245 | if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'])) { |
---|
246 | $discrMapAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap']; |
---|
247 | $metadata->setDiscriminatorMap($discrMapAnnot->value); |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | // Evaluate DoctrineChangeTrackingPolicy annotation |
---|
254 | if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) { |
---|
255 | $changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy']; |
---|
256 | $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value)); |
---|
257 | } |
---|
258 | |
---|
259 | // Evaluate annotations on properties/fields |
---|
260 | foreach ($class->getProperties() as $property) { |
---|
261 | if ($metadata->isMappedSuperclass && ! $property->isPrivate() |
---|
262 | || |
---|
263 | $metadata->isInheritedField($property->name) |
---|
264 | || |
---|
265 | $metadata->isInheritedAssociation($property->name)) { |
---|
266 | continue; |
---|
267 | } |
---|
268 | |
---|
269 | $mapping = array(); |
---|
270 | $mapping['fieldName'] = $property->getName(); |
---|
271 | |
---|
272 | // Check for JoinColummn/JoinColumns annotations |
---|
273 | $joinColumns = array(); |
---|
274 | |
---|
275 | if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) { |
---|
276 | $joinColumns[] = array( |
---|
277 | 'name' => $joinColumnAnnot->name, |
---|
278 | 'referencedColumnName' => $joinColumnAnnot->referencedColumnName, |
---|
279 | 'unique' => $joinColumnAnnot->unique, |
---|
280 | 'nullable' => $joinColumnAnnot->nullable, |
---|
281 | 'onDelete' => $joinColumnAnnot->onDelete, |
---|
282 | 'columnDefinition' => $joinColumnAnnot->columnDefinition, |
---|
283 | ); |
---|
284 | } else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) { |
---|
285 | foreach ($joinColumnsAnnot->value as $joinColumn) { |
---|
286 | $joinColumns[] = array( |
---|
287 | 'name' => $joinColumn->name, |
---|
288 | 'referencedColumnName' => $joinColumn->referencedColumnName, |
---|
289 | 'unique' => $joinColumn->unique, |
---|
290 | 'nullable' => $joinColumn->nullable, |
---|
291 | 'onDelete' => $joinColumn->onDelete, |
---|
292 | 'columnDefinition' => $joinColumn->columnDefinition, |
---|
293 | ); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | // Field can only be annotated with one of: |
---|
298 | // @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany |
---|
299 | if ($columnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) { |
---|
300 | if ($columnAnnot->type == null) { |
---|
301 | throw MappingException::propertyTypeIsRequired($className, $property->getName()); |
---|
302 | } |
---|
303 | |
---|
304 | $mapping['type'] = $columnAnnot->type; |
---|
305 | $mapping['length'] = $columnAnnot->length; |
---|
306 | $mapping['precision'] = $columnAnnot->precision; |
---|
307 | $mapping['scale'] = $columnAnnot->scale; |
---|
308 | $mapping['nullable'] = $columnAnnot->nullable; |
---|
309 | $mapping['unique'] = $columnAnnot->unique; |
---|
310 | if ($columnAnnot->options) { |
---|
311 | $mapping['options'] = $columnAnnot->options; |
---|
312 | } |
---|
313 | |
---|
314 | if (isset($columnAnnot->name)) { |
---|
315 | $mapping['columnName'] = $columnAnnot->name; |
---|
316 | } |
---|
317 | |
---|
318 | if (isset($columnAnnot->columnDefinition)) { |
---|
319 | $mapping['columnDefinition'] = $columnAnnot->columnDefinition; |
---|
320 | } |
---|
321 | |
---|
322 | if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) { |
---|
323 | $mapping['id'] = true; |
---|
324 | } |
---|
325 | |
---|
326 | if ($generatedValueAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) { |
---|
327 | $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy)); |
---|
328 | } |
---|
329 | |
---|
330 | if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) { |
---|
331 | $metadata->setVersionMapping($mapping); |
---|
332 | } |
---|
333 | |
---|
334 | $metadata->mapField($mapping); |
---|
335 | |
---|
336 | // Check for SequenceGenerator/TableGenerator definition |
---|
337 | if ($seqGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\SequenceGenerator')) { |
---|
338 | $metadata->setSequenceGeneratorDefinition(array( |
---|
339 | 'sequenceName' => $seqGeneratorAnnot->sequenceName, |
---|
340 | 'allocationSize' => $seqGeneratorAnnot->allocationSize, |
---|
341 | 'initialValue' => $seqGeneratorAnnot->initialValue |
---|
342 | )); |
---|
343 | } else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) { |
---|
344 | throw MappingException::tableIdGeneratorNotImplemented($className); |
---|
345 | } |
---|
346 | } else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) { |
---|
347 | if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) { |
---|
348 | $mapping['id'] = true; |
---|
349 | } |
---|
350 | |
---|
351 | $mapping['targetEntity'] = $oneToOneAnnot->targetEntity; |
---|
352 | $mapping['joinColumns'] = $joinColumns; |
---|
353 | $mapping['mappedBy'] = $oneToOneAnnot->mappedBy; |
---|
354 | $mapping['inversedBy'] = $oneToOneAnnot->inversedBy; |
---|
355 | $mapping['cascade'] = $oneToOneAnnot->cascade; |
---|
356 | $mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval; |
---|
357 | $mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch); |
---|
358 | $metadata->mapOneToOne($mapping); |
---|
359 | } else if ($oneToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToMany')) { |
---|
360 | $mapping['mappedBy'] = $oneToManyAnnot->mappedBy; |
---|
361 | $mapping['targetEntity'] = $oneToManyAnnot->targetEntity; |
---|
362 | $mapping['cascade'] = $oneToManyAnnot->cascade; |
---|
363 | $mapping['indexBy'] = $oneToManyAnnot->indexBy; |
---|
364 | $mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval; |
---|
365 | $mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch); |
---|
366 | |
---|
367 | if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) { |
---|
368 | $mapping['orderBy'] = $orderByAnnot->value; |
---|
369 | } |
---|
370 | |
---|
371 | $metadata->mapOneToMany($mapping); |
---|
372 | } else if ($manyToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToOne')) { |
---|
373 | if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) { |
---|
374 | $mapping['id'] = true; |
---|
375 | } |
---|
376 | |
---|
377 | $mapping['joinColumns'] = $joinColumns; |
---|
378 | $mapping['cascade'] = $manyToOneAnnot->cascade; |
---|
379 | $mapping['inversedBy'] = $manyToOneAnnot->inversedBy; |
---|
380 | $mapping['targetEntity'] = $manyToOneAnnot->targetEntity; |
---|
381 | $mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch); |
---|
382 | $metadata->mapManyToOne($mapping); |
---|
383 | } else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) { |
---|
384 | $joinTable = array(); |
---|
385 | |
---|
386 | if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) { |
---|
387 | $joinTable = array( |
---|
388 | 'name' => $joinTableAnnot->name, |
---|
389 | 'schema' => $joinTableAnnot->schema |
---|
390 | ); |
---|
391 | |
---|
392 | foreach ($joinTableAnnot->joinColumns as $joinColumn) { |
---|
393 | $joinTable['joinColumns'][] = array( |
---|
394 | 'name' => $joinColumn->name, |
---|
395 | 'referencedColumnName' => $joinColumn->referencedColumnName, |
---|
396 | 'unique' => $joinColumn->unique, |
---|
397 | 'nullable' => $joinColumn->nullable, |
---|
398 | 'onDelete' => $joinColumn->onDelete, |
---|
399 | 'columnDefinition' => $joinColumn->columnDefinition, |
---|
400 | ); |
---|
401 | } |
---|
402 | |
---|
403 | foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) { |
---|
404 | $joinTable['inverseJoinColumns'][] = array( |
---|
405 | 'name' => $joinColumn->name, |
---|
406 | 'referencedColumnName' => $joinColumn->referencedColumnName, |
---|
407 | 'unique' => $joinColumn->unique, |
---|
408 | 'nullable' => $joinColumn->nullable, |
---|
409 | 'onDelete' => $joinColumn->onDelete, |
---|
410 | 'columnDefinition' => $joinColumn->columnDefinition, |
---|
411 | ); |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | $mapping['joinTable'] = $joinTable; |
---|
416 | $mapping['targetEntity'] = $manyToManyAnnot->targetEntity; |
---|
417 | $mapping['mappedBy'] = $manyToManyAnnot->mappedBy; |
---|
418 | $mapping['inversedBy'] = $manyToManyAnnot->inversedBy; |
---|
419 | $mapping['cascade'] = $manyToManyAnnot->cascade; |
---|
420 | $mapping['indexBy'] = $manyToManyAnnot->indexBy; |
---|
421 | $mapping['orphanRemoval'] = $manyToManyAnnot->orphanRemoval; |
---|
422 | $mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch); |
---|
423 | |
---|
424 | if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) { |
---|
425 | $mapping['orderBy'] = $orderByAnnot->value; |
---|
426 | } |
---|
427 | |
---|
428 | $metadata->mapManyToMany($mapping); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | // Evaluate @HasLifecycleCallbacks annotation |
---|
433 | if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) { |
---|
434 | foreach ($class->getMethods() as $method) { |
---|
435 | // filter for the declaring class only, callbacks from parents will already be registered. |
---|
436 | if ($method->isPublic() && $method->getDeclaringClass()->getName() == $class->name) { |
---|
437 | $annotations = $this->_reader->getMethodAnnotations($method); |
---|
438 | |
---|
439 | if ($annotations) { |
---|
440 | foreach ($annotations as $key => $annot) { |
---|
441 | if ( ! is_numeric($key)) { |
---|
442 | continue; |
---|
443 | } |
---|
444 | $annotations[get_class($annot)] = $annot; |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) { |
---|
449 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist); |
---|
450 | } |
---|
451 | |
---|
452 | if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) { |
---|
453 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist); |
---|
454 | } |
---|
455 | |
---|
456 | if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) { |
---|
457 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate); |
---|
458 | } |
---|
459 | |
---|
460 | if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) { |
---|
461 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate); |
---|
462 | } |
---|
463 | |
---|
464 | if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) { |
---|
465 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove); |
---|
466 | } |
---|
467 | |
---|
468 | if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) { |
---|
469 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove); |
---|
470 | } |
---|
471 | |
---|
472 | if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) { |
---|
473 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad); |
---|
474 | } |
---|
475 | |
---|
476 | if (isset($annotations['Doctrine\ORM\Mapping\PreFlush'])) { |
---|
477 | $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preFlush); |
---|
478 | } |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | /** |
---|
485 | * Whether the class with the specified name is transient. Only non-transient |
---|
486 | * classes, that is entities and mapped superclasses, should have their metadata loaded. |
---|
487 | * A class is non-transient if it is annotated with either @Entity or |
---|
488 | * @MappedSuperclass in the class doc block. |
---|
489 | * |
---|
490 | * @param string $className |
---|
491 | * @return boolean |
---|
492 | */ |
---|
493 | public function isTransient($className) |
---|
494 | { |
---|
495 | $classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className)); |
---|
496 | |
---|
497 | if ($classAnnotations && is_numeric(key($classAnnotations))) { |
---|
498 | foreach ($classAnnotations as $annot) { |
---|
499 | if ($annot instanceof \Doctrine\ORM\Mapping\Entity) { |
---|
500 | return false; |
---|
501 | } |
---|
502 | if ($annot instanceof \Doctrine\ORM\Mapping\MappedSuperclass) { |
---|
503 | return false; |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | return true; |
---|
508 | } |
---|
509 | |
---|
510 | return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) && |
---|
511 | ! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']); |
---|
512 | } |
---|
513 | |
---|
514 | /** |
---|
515 | * {@inheritDoc} |
---|
516 | */ |
---|
517 | public function getAllClassNames() |
---|
518 | { |
---|
519 | if ($this->_classNames !== null) { |
---|
520 | return $this->_classNames; |
---|
521 | } |
---|
522 | |
---|
523 | if (!$this->_paths) { |
---|
524 | throw MappingException::pathRequired(); |
---|
525 | } |
---|
526 | |
---|
527 | $classes = array(); |
---|
528 | $includedFiles = array(); |
---|
529 | |
---|
530 | foreach ($this->_paths as $path) { |
---|
531 | if ( ! is_dir($path)) { |
---|
532 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
---|
533 | } |
---|
534 | |
---|
535 | $iterator = new \RegexIterator( |
---|
536 | new \RecursiveIteratorIterator( |
---|
537 | new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS), |
---|
538 | \RecursiveIteratorIterator::LEAVES_ONLY |
---|
539 | ), |
---|
540 | '/^.+' . str_replace('.', '\.', $this->_fileExtension) . '$/i', |
---|
541 | \RecursiveRegexIterator::GET_MATCH |
---|
542 | ); |
---|
543 | |
---|
544 | foreach ($iterator as $file) { |
---|
545 | $sourceFile = realpath($file[0]); |
---|
546 | |
---|
547 | require_once $sourceFile; |
---|
548 | |
---|
549 | $includedFiles[] = $sourceFile; |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | $declared = get_declared_classes(); |
---|
554 | |
---|
555 | foreach ($declared as $className) { |
---|
556 | $rc = new \ReflectionClass($className); |
---|
557 | $sourceFile = $rc->getFileName(); |
---|
558 | if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) { |
---|
559 | $classes[] = $className; |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | $this->_classNames = $classes; |
---|
564 | |
---|
565 | return $classes; |
---|
566 | } |
---|
567 | |
---|
568 | /** |
---|
569 | * Attempts to resolve the fetch mode. |
---|
570 | * |
---|
571 | * @param string $className The class name |
---|
572 | * @param string $fetchMode The fetch mode |
---|
573 | * @return integer The fetch mode as defined in ClassMetadata |
---|
574 | * @throws MappingException If the fetch mode is not valid |
---|
575 | */ |
---|
576 | private function getFetchMode($className, $fetchMode) |
---|
577 | { |
---|
578 | if(!defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) { |
---|
579 | throw MappingException::invalidFetchMode($className, $fetchMode); |
---|
580 | } |
---|
581 | |
---|
582 | return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode); |
---|
583 | } |
---|
584 | /** |
---|
585 | * Factory method for the Annotation Driver |
---|
586 | * |
---|
587 | * @param array|string $paths |
---|
588 | * @param AnnotationReader $reader |
---|
589 | * @return AnnotationDriver |
---|
590 | */ |
---|
591 | static public function create($paths = array(), AnnotationReader $reader = null) |
---|
592 | { |
---|
593 | if ($reader == null) { |
---|
594 | $reader = new AnnotationReader(); |
---|
595 | $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); |
---|
596 | } |
---|
597 | return new self($reader, $paths); |
---|
598 | } |
---|
599 | } |
---|