|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\ODM\CouchDB\Mapping\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader, |
|
6
|
|
|
Doctrine\Common\Annotations\AnnotationRegistry, |
|
7
|
|
|
Doctrine\Common\Annotations\Reader, |
|
8
|
|
|
Doctrine\Common\Persistence\Mapping\ClassMetadata, |
|
9
|
|
|
Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver, |
|
10
|
|
|
Doctrine\ODM\CouchDB\Mapping\Annotations as ODM, |
|
11
|
|
|
Doctrine\ODM\CouchDB\Mapping\MappingException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations. |
|
15
|
|
|
* |
|
16
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
17
|
|
|
* @link www.doctrine-project.org |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
* @author Jonathan H. Wage <[email protected]> |
|
20
|
|
|
* @author Roman Borschel <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class AnnotationDriver extends AbstractAnnotationDriver |
|
23
|
|
|
{ |
|
24
|
|
|
protected $entityAnnotationClasses = array( |
|
25
|
|
|
'Doctrine\ODM\CouchDB\Mapping\Annotations\Document' => true, |
|
26
|
|
|
'Doctrine\ODM\CouchDB\Mapping\Annotations\EmbeddedDocument' => true, |
|
27
|
|
|
'Doctrine\ODM\CouchDB\Mapping\Annotations\MappedSuperclass' => true, |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function loadMetadataForClass($className, ClassMetadata $class) |
|
34
|
|
|
{ |
|
35
|
|
|
$reflClass = $class->getReflectionClass(); |
|
36
|
|
|
|
|
37
|
|
|
$isValidDocument = false; |
|
38
|
|
|
$classAnnotations = $this->reader->getClassAnnotations($reflClass); |
|
39
|
|
|
|
|
40
|
|
|
foreach ($classAnnotations AS $classAnnotation) { |
|
41
|
|
|
if ($classAnnotation instanceof ODM\Document) { |
|
42
|
|
|
if ($classAnnotation->indexed) { |
|
43
|
|
|
$class->indexed = true; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
$class->setCustomRepositoryClass($classAnnotation->repositoryClass); |
|
46
|
|
|
$isValidDocument = true; |
|
47
|
|
|
} elseif ($classAnnotation instanceof ODM\EmbeddedDocument) { |
|
48
|
|
|
$class->isEmbeddedDocument = true; |
|
|
|
|
|
|
49
|
|
|
$isValidDocument = true; |
|
50
|
|
|
} else if ($classAnnotation instanceof ODM\MappedSuperclass) { |
|
51
|
|
|
$class->isMappedSuperclass = true; |
|
|
|
|
|
|
52
|
|
|
$isValidDocument = true; |
|
53
|
|
|
} else if ($classAnnotation instanceof ODM\Index) { |
|
54
|
|
|
$class->indexed = true; |
|
|
|
|
|
|
55
|
|
|
} else if ($classAnnotation instanceof ODM\InheritanceRoot) { |
|
56
|
|
|
$class->markInheritanceRoot(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ( ! $isValidDocument) { |
|
61
|
|
|
throw MappingException::classIsNotAValidDocument($className); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
foreach ($reflClass->getProperties() as $property) { |
|
65
|
|
|
if ($class->isInheritedAssociation($property->name) || $class->isInheritedField($property->name)) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$mapping = array(); |
|
70
|
|
|
$mapping['fieldName'] = $property->name; |
|
71
|
|
|
|
|
72
|
|
|
if ($this->reader->getPropertyAnnotation($property, 'Doctrine\ODM\CouchDB\Mapping\Annotations\Index')) { |
|
73
|
|
|
$mapping['indexed'] = true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) { |
|
77
|
|
|
if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Field) { |
|
78
|
|
|
if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Version) { |
|
79
|
|
|
$mapping['isVersionField'] = true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$mapping = array_merge($mapping, (array) $fieldAnnot); |
|
83
|
|
|
unset($mapping['value']); |
|
84
|
|
|
$class->mapField($mapping); |
|
85
|
|
|
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceOne) { |
|
86
|
|
|
$mapping = array_merge($mapping, (array) $fieldAnnot); |
|
87
|
|
|
$mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); |
|
88
|
|
|
unset($mapping['value']); |
|
89
|
|
|
$class->mapManyToOne($mapping); |
|
90
|
|
|
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceMany) { |
|
91
|
|
|
$mapping = array_merge($mapping, (array) $fieldAnnot); |
|
92
|
|
|
$mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade); |
|
93
|
|
|
unset($mapping['value']); |
|
94
|
|
|
$class->mapManyToMany($mapping); |
|
95
|
|
|
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Attachments) { |
|
96
|
|
|
$class->mapAttachments($mapping['fieldName']); |
|
97
|
|
|
} else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedOne || $fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedMany) { |
|
98
|
|
|
$mapping = array_merge($mapping, (array) $fieldAnnot); |
|
99
|
|
|
unset($mapping['value']); |
|
100
|
|
|
$class->mapEmbedded($mapping); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Gathers a list of cascade options found in the given cascade element. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $cascadeList cascade list |
|
110
|
|
|
* @return integer a bitmask of cascade options. |
|
111
|
|
|
* @throws MappingException |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
private function getCascadeMode(array $cascadeList) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$cascade = 0; |
|
116
|
|
|
foreach ($cascadeList as $cascadeMode) { |
|
117
|
|
|
$constantName = 'Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode); |
|
118
|
|
|
if (!defined($constantName)) { |
|
119
|
|
|
throw new MappingException("Cascade mode '$cascadeMode' not supported."); |
|
120
|
|
|
} |
|
121
|
|
|
$cascade |= constant($constantName); |
|
122
|
|
|
} |
|
123
|
|
|
return $cascade; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: