|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\ODM\CouchDB\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ODM\CouchDB\DocumentManager, |
|
6
|
|
|
Doctrine\ODM\CouchDB\CouchDBException, |
|
7
|
|
|
Doctrine\ODM\CouchDB\Mapping\ClassMetadata, |
|
8
|
|
|
Doctrine\Common\Persistence\Mapping\Driver\MappingDriver, |
|
9
|
|
|
Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface, |
|
10
|
|
|
Doctrine\Common\Persistence\Mapping\ReflectionService, |
|
11
|
|
|
Doctrine\Common\Persistence\Mapping\RuntimeReflectionService, |
|
12
|
|
|
Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the |
|
16
|
|
|
* metadata mapping information of a class which describes how a class should be mapped |
|
17
|
|
|
* to a document database. |
|
18
|
|
|
|
|
19
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
20
|
|
|
* @link www.doctrine-project.com |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
23
|
|
|
* @author Lukas Kahwe Smith <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ClassMetadataFactory extends AbstractClassMetadataFactory |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var DocumentManager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $dm; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The used metadata driver. |
|
34
|
|
|
* |
|
35
|
|
|
* @var MappingDriver |
|
36
|
|
|
*/ |
|
37
|
|
|
private $driver; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates a new factory instance that uses the given DocumentManager instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param DocumentManager $dm The DocumentManager instance |
|
43
|
|
|
* @throws \RuntimeException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(DocumentManager $dm) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->dm = $dm; |
|
48
|
|
|
$config = $this->dm->getConfiguration(); |
|
49
|
|
|
$this->setCacheDriver($config->getMetadataCacheImpl()); |
|
50
|
|
|
$this->driver = $config->getMetadataDriverImpl(); |
|
51
|
|
|
if (!$this->driver) { |
|
52
|
|
|
throw new \RuntimeException('No metadata driver was configured.'); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents) |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var $parent ClassMetaData */ |
|
62
|
|
|
if ($parent) { |
|
63
|
|
|
$this->addAssociationsMapping($class, $parent); |
|
64
|
|
|
$this->addFieldMapping($class, $parent); |
|
65
|
|
|
$this->addIndexes($class, $parent); |
|
|
|
|
|
|
66
|
|
|
$parent->deriveChildMetadata($class); |
|
|
|
|
|
|
67
|
|
|
$class->setParentClasses($nonSuperclassParents); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($this->getDriver()) { |
|
71
|
|
|
$this->getDriver()->loadMetadataForClass($class->getName(), $class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->validateMapping($class); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check for any possible shortcomings in the class: |
|
79
|
|
|
* |
|
80
|
|
|
* The class must have an identifier field unless it's an embedded document or mapped superclass. |
|
81
|
|
|
*/ |
|
82
|
|
|
private function validateMapping(ClassMetadataInterface $class) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$class->identifier && !$class->isEmbeddedDocument && !$class->isMappedSuperclass) { |
|
|
|
|
|
|
85
|
|
|
throw new MappingException("An identifier (@Id) field is required in {$class->getName()}."); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function addFieldMapping(ClassMetadataInterface $class, ClassMetadataInterface $parent) |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($parent->reflFields as $name => $field) { |
|
|
|
|
|
|
92
|
|
|
$class->reflFields[$name] = $field; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
foreach ($parent->fieldMappings as $name => $field) { |
|
|
|
|
|
|
96
|
|
|
$class->fieldMappings[$name] = $field; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
foreach ($parent->jsonNames as $name => $field) { |
|
|
|
|
|
|
100
|
|
|
$class->jsonNames[$name] = $field; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($parent->identifier) { |
|
|
|
|
|
|
104
|
|
|
$class->setIdentifier($parent->identifier); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function addIndexes(ClassMetadata $class, ClassMetadata $parent) |
|
109
|
|
|
{ |
|
110
|
|
|
$class->indexes = $parent->indexes; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* |
|
115
|
|
|
* @param ClassMetadataInterface $class |
|
116
|
|
|
* @param ClassMetadataInterface $parent |
|
117
|
|
|
*/ |
|
118
|
|
|
private function addAssociationsMapping(ClassMetadataInterface $class, ClassMetadataInterface $parent) |
|
119
|
|
|
{ |
|
120
|
|
|
foreach ($parent->associationsMappings as $name => $field) { |
|
|
|
|
|
|
121
|
|
|
$class->associationsMappings[$name] = $field; |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->dm->getConfiguration()->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Forces the factory to load the metadata of all classes known to the underlying |
|
135
|
|
|
* mapping driver. |
|
136
|
|
|
* |
|
137
|
|
|
* @return array The ClassMetadata instances of all mapped classes. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getAllMetadata() |
|
140
|
|
|
{ |
|
141
|
|
|
$metadata = array(); |
|
142
|
|
|
foreach ($this->driver->getAllClassNames() as $className) { |
|
143
|
|
|
$metadata[] = $this->getMetadataFor($className); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $metadata; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Gets the class metadata descriptor for a class. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $className The name of the class. |
|
153
|
|
|
* @return ClassMetadata |
|
154
|
|
|
* @throws MappingException |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getMetadataFor($className) |
|
157
|
|
|
{ |
|
158
|
|
|
$metadata = parent::getMetadataFor($className); |
|
159
|
|
|
|
|
160
|
|
|
if ($metadata) { |
|
161
|
|
|
return $metadata; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
throw MappingException::classNotMapped($className); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Loads the metadata of the class in question and all it's ancestors whose metadata |
|
169
|
|
|
* is still not loaded. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $className The name of the class for which the metadata should get loaded. |
|
172
|
|
|
* @return array |
|
173
|
|
|
* @throws MappingException |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function loadMetadata($className) |
|
176
|
|
|
{ |
|
177
|
|
|
if (class_exists($className)) { |
|
178
|
|
|
return parent::loadMetadata($className); |
|
179
|
|
|
} |
|
180
|
|
|
throw MappingException::classNotFound($className); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Creates a new ClassMetadata instance for the given class name. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $className |
|
187
|
|
|
* @return ClassMetadata |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function newClassMetadataInstance($className) |
|
190
|
|
|
{ |
|
191
|
|
|
return new ClassMetadata($className); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* {@inheritdoc} |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function getDriver() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->driver; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* {@inheritdoc} |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function initialize() |
|
206
|
|
|
{ |
|
207
|
|
|
$this->initialized = true; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* {@inheritdoc} |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
214
|
|
|
{ |
|
215
|
|
|
$class->initializeReflection($reflService); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* {@inheritdoc} |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
222
|
|
|
{ |
|
223
|
|
|
$class->wakeupReflection($reflService); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* {@inheritDoc} |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function isEntity(ClassMetadataInterface $class) |
|
230
|
|
|
{ |
|
231
|
|
|
return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false; |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.