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\ORM\Mapping\Driver; |
||
21 | |||
22 | use Doctrine\Common\Collections\Criteria; |
||
23 | use SimpleXMLElement; |
||
24 | use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; |
||
25 | use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder; |
||
26 | use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
||
27 | use Doctrine\ORM\Mapping\MappingException; |
||
28 | use Doctrine\ORM\Mapping\ClassMetadata as Metadata; |
||
29 | |||
30 | /** |
||
31 | * XmlDriver is a metadata driver that enables mapping through XML files. |
||
32 | * |
||
33 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
||
34 | * @link www.doctrine-project.org |
||
35 | * @since 2.0 |
||
36 | * @author Benjamin Eberlei <[email protected]> |
||
37 | * @author Guilherme Blanco <[email protected]> |
||
38 | * @author Jonathan H. Wage <[email protected]> |
||
39 | * @author Roman Borschel <[email protected]> |
||
40 | */ |
||
41 | class XmlDriver extends FileDriver |
||
42 | { |
||
43 | const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | 46 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
49 | { |
||
50 | 46 | parent::__construct($locator, $fileExtension); |
|
51 | 46 | } |
|
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | 41 | public function loadMetadataForClass($className, ClassMetadata $metadata) |
|
57 | { |
||
58 | /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */ |
||
59 | /* @var $xmlRoot SimpleXMLElement */ |
||
60 | 41 | $xmlRoot = $this->getElement($className); |
|
61 | |||
62 | 39 | if ($xmlRoot->getName() == 'entity') { |
|
63 | 38 | if (isset($xmlRoot['repository-class'])) { |
|
64 | $metadata->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
||
65 | } |
||
66 | 38 | if (isset($xmlRoot['read-only']) && $this->evaluateBoolean($xmlRoot['read-only'])) { |
|
67 | 38 | $metadata->markReadOnly(); |
|
68 | } |
||
69 | 8 | } else if ($xmlRoot->getName() == 'mapped-superclass') { |
|
70 | 5 | $metadata->setCustomRepositoryClass( |
|
71 | 5 | isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
72 | ); |
||
73 | 5 | $metadata->isMappedSuperclass = true; |
|
74 | 3 | } else if ($xmlRoot->getName() == 'embeddable') { |
|
75 | 3 | $metadata->isEmbeddedClass = true; |
|
76 | } else { |
||
77 | throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); |
||
78 | } |
||
79 | |||
80 | // Evaluate <entity...> attributes |
||
81 | 39 | $primaryTable = []; |
|
82 | |||
83 | 39 | if (isset($xmlRoot['table'])) { |
|
84 | 16 | $primaryTable['name'] = (string) $xmlRoot['table']; |
|
85 | } |
||
86 | |||
87 | 39 | if (isset($xmlRoot['schema'])) { |
|
88 | 1 | $primaryTable['schema'] = (string) $xmlRoot['schema']; |
|
89 | } |
||
90 | |||
91 | 39 | $metadata->setPrimaryTable($primaryTable); |
|
92 | |||
93 | // Evaluate second level cache |
||
94 | 39 | if (isset($xmlRoot->cache)) { |
|
95 | 2 | $metadata->enableCache($this->cacheToArray($xmlRoot->cache)); |
|
96 | } |
||
97 | |||
98 | // Evaluate named queries |
||
99 | 39 | if (isset($xmlRoot->{'named-queries'})) { |
|
100 | 4 | foreach ($xmlRoot->{'named-queries'}->{'named-query'} as $namedQueryElement) { |
|
101 | 4 | $metadata->addNamedQuery( |
|
102 | [ |
||
103 | 4 | 'name' => (string) $namedQueryElement['name'], |
|
104 | 4 | 'query' => (string) $namedQueryElement['query'] |
|
105 | ] |
||
106 | ); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // Evaluate native named queries |
||
111 | 39 | if (isset($xmlRoot->{'named-native-queries'})) { |
|
112 | 3 | foreach ($xmlRoot->{'named-native-queries'}->{'named-native-query'} as $nativeQueryElement) { |
|
113 | 3 | $metadata->addNamedNativeQuery( |
|
114 | [ |
||
115 | 3 | 'name' => isset($nativeQueryElement['name']) ? (string) $nativeQueryElement['name'] : null, |
|
116 | 3 | 'query' => isset($nativeQueryElement->query) ? (string) $nativeQueryElement->query : null, |
|
117 | 3 | 'resultClass' => isset($nativeQueryElement['result-class']) ? (string) $nativeQueryElement['result-class'] : null, |
|
118 | 3 | 'resultSetMapping' => isset($nativeQueryElement['result-set-mapping']) ? (string) $nativeQueryElement['result-set-mapping'] : null, |
|
119 | ] |
||
120 | ); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | // Evaluate sql result set mapping |
||
125 | 39 | if (isset($xmlRoot->{'sql-result-set-mappings'})) { |
|
126 | 3 | foreach ($xmlRoot->{'sql-result-set-mappings'}->{'sql-result-set-mapping'} as $rsmElement) { |
|
127 | 3 | $entities = []; |
|
128 | 3 | $columns = []; |
|
129 | 3 | foreach ($rsmElement as $entityElement) { |
|
130 | //<entity-result/> |
||
131 | 3 | if (isset($entityElement['entity-class'])) { |
|
132 | $entityResult = [ |
||
133 | 3 | 'fields' => [], |
|
134 | 3 | 'entityClass' => (string) $entityElement['entity-class'], |
|
135 | 3 | 'discriminatorColumn' => isset($entityElement['discriminator-column']) ? (string) $entityElement['discriminator-column'] : null, |
|
136 | ]; |
||
137 | |||
138 | 3 | foreach ($entityElement as $fieldElement) { |
|
139 | 3 | $entityResult['fields'][] = [ |
|
140 | 3 | 'name' => isset($fieldElement['name']) ? (string) $fieldElement['name'] : null, |
|
141 | 3 | 'column' => isset($fieldElement['column']) ? (string) $fieldElement['column'] : null, |
|
142 | ]; |
||
143 | } |
||
144 | |||
145 | 3 | $entities[] = $entityResult; |
|
146 | } |
||
147 | |||
148 | //<column-result/> |
||
149 | 3 | if (isset($entityElement['name'])) { |
|
150 | 3 | $columns[] = [ |
|
151 | 3 | 'name' => (string) $entityElement['name'], |
|
152 | ]; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | 3 | $metadata->addSqlResultSetMapping( |
|
157 | [ |
||
158 | 3 | 'name' => (string) $rsmElement['name'], |
|
159 | 3 | 'entities' => $entities, |
|
160 | 3 | 'columns' => $columns |
|
161 | ] |
||
162 | ); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | 39 | if (isset($xmlRoot['inheritance-type'])) { |
|
167 | 10 | $inheritanceType = (string) $xmlRoot['inheritance-type']; |
|
168 | 10 | $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType)); |
|
169 | |||
170 | 10 | if ($metadata->inheritanceType != Metadata::INHERITANCE_TYPE_NONE) { |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
171 | // Evaluate <discriminator-column...> |
||
172 | 10 | if (isset($xmlRoot->{'discriminator-column'})) { |
|
173 | 7 | $discrColumn = $xmlRoot->{'discriminator-column'}; |
|
174 | 7 | $metadata->setDiscriminatorColumn( |
|
175 | [ |
||
176 | 7 | 'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null, |
|
177 | 7 | 'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string', |
|
178 | 7 | 'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255, |
|
179 | 7 | 'columnDefinition' => isset($discrColumn['column-definition']) ? (string) $discrColumn['column-definition'] : null |
|
180 | ] |
||
181 | ); |
||
182 | } else { |
||
183 | 6 | $metadata->setDiscriminatorColumn(['name' => 'dtype', 'type' => 'string', 'length' => 255]); |
|
184 | } |
||
185 | |||
186 | // Evaluate <discriminator-map...> |
||
187 | 10 | if (isset($xmlRoot->{'discriminator-map'})) { |
|
188 | 10 | $map = []; |
|
189 | 10 | foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) { |
|
190 | 10 | $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
|
191 | } |
||
192 | 10 | $metadata->setDiscriminatorMap($map); |
|
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | |||
198 | // Evaluate <change-tracking-policy...> |
||
199 | 39 | if (isset($xmlRoot['change-tracking-policy'])) { |
|
200 | $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' |
||
201 | . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
||
202 | } |
||
203 | |||
204 | // Evaluate <indexes...> |
||
205 | 39 | if (isset($xmlRoot->indexes)) { |
|
206 | 4 | $metadata->table['indexes'] = []; |
|
207 | 4 | foreach ($xmlRoot->indexes->index as $indexXml) { |
|
208 | 4 | $index = ['columns' => explode(',', (string) $indexXml['columns'])]; |
|
209 | |||
210 | 4 | if (isset($indexXml['flags'])) { |
|
211 | 1 | $index['flags'] = explode(',', (string) $indexXml['flags']); |
|
212 | } |
||
213 | |||
214 | 4 | if (isset($indexXml->options)) { |
|
215 | 1 | $index['options'] = $this->_parseOptions($indexXml->options->children()); |
|
216 | } |
||
217 | |||
218 | 4 | if (isset($indexXml['name'])) { |
|
219 | 3 | $metadata->table['indexes'][(string) $indexXml['name']] = $index; |
|
220 | } else { |
||
221 | 4 | $metadata->table['indexes'][] = $index; |
|
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // Evaluate <unique-constraints..> |
||
227 | 39 | if (isset($xmlRoot->{'unique-constraints'})) { |
|
228 | 3 | $metadata->table['uniqueConstraints'] = []; |
|
229 | 3 | foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $uniqueXml) { |
|
230 | 3 | $unique = ['columns' => explode(',', (string) $uniqueXml['columns'])]; |
|
231 | |||
232 | 3 | if (isset($uniqueXml->options)) { |
|
233 | 3 | $unique['options'] = $this->_parseOptions($uniqueXml->options->children()); |
|
234 | } |
||
235 | |||
236 | 3 | if (isset($uniqueXml['name'])) { |
|
237 | 3 | $metadata->table['uniqueConstraints'][(string) $uniqueXml['name']] = $unique; |
|
238 | } else { |
||
239 | 3 | $metadata->table['uniqueConstraints'][] = $unique; |
|
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | 39 | if (isset($xmlRoot->options)) { |
|
245 | 5 | $metadata->table['options'] = $this->_parseOptions($xmlRoot->options->children()); |
|
246 | } |
||
247 | |||
248 | // The mapping assignment is done in 2 times as a bug might occurs on some php/xml lib versions |
||
249 | // The internal SimpleXmlIterator get resetted, to this generate a duplicate field exception |
||
250 | 39 | $mappings = []; |
|
251 | // Evaluate <field ...> mappings |
||
252 | 39 | if (isset($xmlRoot->field)) { |
|
253 | 24 | foreach ($xmlRoot->field as $fieldMapping) { |
|
254 | 24 | $mapping = $this->columnToArray($fieldMapping); |
|
255 | |||
256 | 24 | if (isset($mapping['version'])) { |
|
257 | 3 | $metadata->setVersionMapping($mapping); |
|
258 | 3 | unset($mapping['version']); |
|
259 | } |
||
260 | |||
261 | 24 | $metadata->mapField($mapping); |
|
262 | } |
||
263 | } |
||
264 | |||
265 | 39 | if (isset($xmlRoot->embedded)) { |
|
266 | 3 | foreach ($xmlRoot->embedded as $embeddedMapping) { |
|
267 | 3 | $columnPrefix = isset($embeddedMapping['column-prefix']) |
|
268 | 3 | ? (string) $embeddedMapping['column-prefix'] |
|
269 | 3 | : null; |
|
270 | |||
271 | 3 | $useColumnPrefix = isset($embeddedMapping['use-column-prefix']) |
|
272 | 2 | ? $this->evaluateBoolean($embeddedMapping['use-column-prefix']) |
|
273 | 3 | : true; |
|
274 | |||
275 | $mapping = [ |
||
276 | 3 | 'fieldName' => (string) $embeddedMapping['name'], |
|
277 | 3 | 'class' => (string) $embeddedMapping['class'], |
|
278 | 3 | 'columnPrefix' => $useColumnPrefix ? $columnPrefix : false |
|
279 | ]; |
||
280 | |||
281 | 3 | $metadata->mapEmbedded($mapping); |
|
282 | } |
||
283 | } |
||
284 | |||
285 | 39 | foreach ($mappings as $mapping) { |
|
286 | if (isset($mapping['version'])) { |
||
287 | $metadata->setVersionMapping($mapping); |
||
288 | } |
||
289 | |||
290 | $metadata->mapField($mapping); |
||
291 | } |
||
292 | |||
293 | // Evaluate <id ...> mappings |
||
294 | 39 | $associationIds = []; |
|
295 | 39 | foreach ($xmlRoot->id as $idElement) { |
|
296 | 34 | if (isset($idElement['association-key']) && $this->evaluateBoolean($idElement['association-key'])) { |
|
297 | 2 | $associationIds[(string) $idElement['name']] = true; |
|
298 | 2 | continue; |
|
299 | } |
||
300 | |||
301 | $mapping = [ |
||
302 | 33 | 'id' => true, |
|
303 | 33 | 'fieldName' => (string) $idElement['name'] |
|
304 | ]; |
||
305 | |||
306 | 33 | if (isset($idElement['type'])) { |
|
307 | 21 | $mapping['type'] = (string) $idElement['type']; |
|
308 | } |
||
309 | |||
310 | 33 | if (isset($idElement['length'])) { |
|
311 | 3 | $mapping['length'] = (string) $idElement['length']; |
|
312 | } |
||
313 | |||
314 | 33 | if (isset($idElement['column'])) { |
|
315 | 24 | $mapping['columnName'] = (string) $idElement['column']; |
|
316 | } |
||
317 | |||
318 | 33 | if (isset($idElement['column-definition'])) { |
|
319 | 1 | $mapping['columnDefinition'] = (string) $idElement['column-definition']; |
|
320 | } |
||
321 | |||
322 | 33 | if (isset($idElement->options)) { |
|
323 | 3 | $mapping['options'] = $this->_parseOptions($idElement->options->children()); |
|
324 | } |
||
325 | |||
326 | 33 | $metadata->mapField($mapping); |
|
327 | |||
328 | 33 | if (isset($idElement->generator)) { |
|
329 | 32 | $strategy = isset($idElement->generator['strategy']) ? |
|
330 | 32 | (string) $idElement->generator['strategy'] : 'AUTO'; |
|
331 | 32 | $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' |
|
332 | 32 | . $strategy)); |
|
333 | } |
||
334 | |||
335 | // Check for SequenceGenerator/TableGenerator definition |
||
336 | 33 | if (isset($idElement->{'sequence-generator'})) { |
|
337 | 3 | $seqGenerator = $idElement->{'sequence-generator'}; |
|
338 | 3 | $metadata->setSequenceGeneratorDefinition( |
|
339 | [ |
||
340 | 3 | 'sequenceName' => (string) $seqGenerator['sequence-name'], |
|
341 | 3 | 'allocationSize' => (string) $seqGenerator['allocation-size'], |
|
342 | 3 | 'initialValue' => (string) $seqGenerator['initial-value'] |
|
343 | ] |
||
344 | ); |
||
345 | 30 | } else if (isset($idElement->{'custom-id-generator'})) { |
|
346 | 2 | $customGenerator = $idElement->{'custom-id-generator'}; |
|
347 | 2 | $metadata->setCustomGeneratorDefinition( |
|
348 | [ |
||
349 | 2 | 'class' => (string) $customGenerator['class'] |
|
350 | ] |
||
351 | ); |
||
352 | 28 | } else if (isset($idElement->{'table-generator'})) { |
|
353 | 33 | throw MappingException::tableIdGeneratorNotImplemented($className); |
|
354 | } |
||
355 | } |
||
356 | |||
357 | // Evaluate <one-to-one ...> mappings |
||
358 | 39 | if (isset($xmlRoot->{'one-to-one'})) { |
|
359 | 8 | foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) { |
|
360 | $mapping = [ |
||
361 | 8 | 'fieldName' => (string) $oneToOneElement['field'], |
|
362 | 8 | 'targetEntity' => (string) $oneToOneElement['target-entity'] |
|
363 | ]; |
||
364 | |||
365 | 8 | if (isset($associationIds[$mapping['fieldName']])) { |
|
366 | $mapping['id'] = true; |
||
367 | } |
||
368 | |||
369 | 8 | if (isset($oneToOneElement['fetch'])) { |
|
370 | 2 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string) $oneToOneElement['fetch']); |
|
371 | } |
||
372 | |||
373 | 8 | if (isset($oneToOneElement['mapped-by'])) { |
|
374 | 2 | $mapping['mappedBy'] = (string) $oneToOneElement['mapped-by']; |
|
375 | } else { |
||
376 | 8 | if (isset($oneToOneElement['inversed-by'])) { |
|
377 | 8 | $mapping['inversedBy'] = (string) $oneToOneElement['inversed-by']; |
|
378 | } |
||
379 | 8 | $joinColumns = []; |
|
380 | |||
381 | 8 | if (isset($oneToOneElement->{'join-column'})) { |
|
382 | 7 | $joinColumns[] = $this->joinColumnToArray($oneToOneElement->{'join-column'}); |
|
383 | 1 | } else if (isset($oneToOneElement->{'join-columns'})) { |
|
384 | 1 | foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
|
385 | 1 | $joinColumns[] = $this->joinColumnToArray($joinColumnElement); |
|
386 | } |
||
387 | } |
||
388 | |||
389 | 8 | $mapping['joinColumns'] = $joinColumns; |
|
390 | } |
||
391 | |||
392 | 8 | if (isset($oneToOneElement->cascade)) { |
|
393 | 6 | $mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade); |
|
394 | } |
||
395 | |||
396 | 8 | if (isset($oneToOneElement['orphan-removal'])) { |
|
397 | 3 | $mapping['orphanRemoval'] = $this->evaluateBoolean($oneToOneElement['orphan-removal']); |
|
398 | } |
||
399 | |||
400 | // Evaluate second level cache |
||
401 | 8 | if (isset($oneToOneElement->cache)) { |
|
402 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement->cache)); |
||
403 | } |
||
404 | |||
405 | 8 | $metadata->mapOneToOne($mapping); |
|
406 | } |
||
407 | } |
||
408 | |||
409 | // Evaluate <one-to-many ...> mappings |
||
410 | 39 | if (isset($xmlRoot->{'one-to-many'})) { |
|
411 | 9 | foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) { |
|
412 | $mapping = [ |
||
413 | 9 | 'fieldName' => (string) $oneToManyElement['field'], |
|
414 | 9 | 'targetEntity' => (string) $oneToManyElement['target-entity'], |
|
415 | 9 | 'mappedBy' => (string) $oneToManyElement['mapped-by'] |
|
416 | ]; |
||
417 | |||
418 | 9 | if (isset($oneToManyElement['fetch'])) { |
|
419 | 2 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string) $oneToManyElement['fetch']); |
|
420 | } |
||
421 | |||
422 | 9 | if (isset($oneToManyElement->cascade)) { |
|
423 | 6 | $mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade); |
|
424 | } |
||
425 | |||
426 | 9 | if (isset($oneToManyElement['orphan-removal'])) { |
|
427 | 6 | $mapping['orphanRemoval'] = $this->evaluateBoolean($oneToManyElement['orphan-removal']); |
|
428 | } |
||
429 | |||
430 | 9 | if (isset($oneToManyElement->{'order-by'})) { |
|
431 | 7 | $orderBy = []; |
|
432 | 7 | foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} as $orderByField) { |
|
433 | 7 | $orderBy[(string) $orderByField['name']] = isset($orderByField['direction']) |
|
434 | 6 | ? (string) $orderByField['direction'] |
|
435 | 7 | : Criteria::ASC |
|
436 | ; |
||
437 | } |
||
438 | 7 | $mapping['orderBy'] = $orderBy; |
|
439 | } |
||
440 | |||
441 | 9 | if (isset($oneToManyElement['index-by'])) { |
|
442 | 3 | $mapping['indexBy'] = (string) $oneToManyElement['index-by']; |
|
443 | 6 | } else if (isset($oneToManyElement->{'index-by'})) { |
|
444 | throw new \InvalidArgumentException("<index-by /> is not a valid tag"); |
||
445 | } |
||
446 | |||
447 | // Evaluate second level cache |
||
448 | 9 | if (isset($oneToManyElement->cache)) { |
|
449 | 1 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement->cache)); |
|
450 | } |
||
451 | |||
452 | 9 | $metadata->mapOneToMany($mapping); |
|
453 | } |
||
454 | } |
||
455 | |||
456 | // Evaluate <many-to-one ...> mappings |
||
457 | 39 | if (isset($xmlRoot->{'many-to-one'})) { |
|
458 | 8 | foreach ($xmlRoot->{'many-to-one'} as $manyToOneElement) { |
|
459 | $mapping = [ |
||
460 | 8 | 'fieldName' => (string) $manyToOneElement['field'], |
|
461 | 8 | 'targetEntity' => (string) $manyToOneElement['target-entity'] |
|
462 | ]; |
||
463 | |||
464 | 8 | if (isset($associationIds[$mapping['fieldName']])) { |
|
465 | 2 | $mapping['id'] = true; |
|
466 | } |
||
467 | |||
468 | 8 | if (isset($manyToOneElement['fetch'])) { |
|
469 | 1 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string) $manyToOneElement['fetch']); |
|
470 | } |
||
471 | |||
472 | 8 | if (isset($manyToOneElement['inversed-by'])) { |
|
473 | 1 | $mapping['inversedBy'] = (string) $manyToOneElement['inversed-by']; |
|
474 | } |
||
475 | |||
476 | 8 | $joinColumns = []; |
|
477 | |||
478 | 8 | if (isset($manyToOneElement->{'join-column'})) { |
|
479 | 5 | $joinColumns[] = $this->joinColumnToArray($manyToOneElement->{'join-column'}); |
|
480 | 3 | } else if (isset($manyToOneElement->{'join-columns'})) { |
|
481 | 2 | foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
|
482 | 2 | $joinColumns[] = $this->joinColumnToArray($joinColumnElement); |
|
483 | } |
||
484 | } |
||
485 | |||
486 | 8 | $mapping['joinColumns'] = $joinColumns; |
|
487 | |||
488 | 8 | if (isset($manyToOneElement->cascade)) { |
|
489 | 2 | $mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade); |
|
490 | } |
||
491 | |||
492 | // Evaluate second level cache |
||
493 | 8 | if (isset($manyToOneElement->cache)) { |
|
494 | 1 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement->cache)); |
|
495 | } |
||
496 | |||
497 | 8 | $metadata->mapManyToOne($mapping); |
|
498 | |||
499 | } |
||
500 | } |
||
501 | |||
502 | // Evaluate <many-to-many ...> mappings |
||
503 | 38 | if (isset($xmlRoot->{'many-to-many'})) { |
|
504 | 13 | foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) { |
|
505 | $mapping = [ |
||
506 | 13 | 'fieldName' => (string) $manyToManyElement['field'], |
|
507 | 13 | 'targetEntity' => (string) $manyToManyElement['target-entity'] |
|
508 | ]; |
||
509 | |||
510 | 13 | if (isset($manyToManyElement['fetch'])) { |
|
511 | 3 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string) $manyToManyElement['fetch']); |
|
512 | } |
||
513 | |||
514 | 13 | if (isset($manyToManyElement['orphan-removal'])) { |
|
515 | $mapping['orphanRemoval'] = $this->evaluateBoolean($manyToManyElement['orphan-removal']); |
||
516 | } |
||
517 | |||
518 | 13 | if (isset($manyToManyElement['mapped-by'])) { |
|
519 | 3 | $mapping['mappedBy'] = (string) $manyToManyElement['mapped-by']; |
|
520 | 10 | } else if (isset($manyToManyElement->{'join-table'})) { |
|
521 | 8 | if (isset($manyToManyElement['inversed-by'])) { |
|
522 | 2 | $mapping['inversedBy'] = (string) $manyToManyElement['inversed-by']; |
|
523 | } |
||
524 | |||
525 | 8 | $joinTableElement = $manyToManyElement->{'join-table'}; |
|
526 | $joinTable = [ |
||
527 | 8 | 'name' => (string) $joinTableElement['name'] |
|
528 | ]; |
||
529 | |||
530 | 8 | if (isset($joinTableElement['schema'])) { |
|
531 | $joinTable['schema'] = (string) $joinTableElement['schema']; |
||
532 | } |
||
533 | |||
534 | 8 | foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
|
535 | 8 | $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
536 | } |
||
537 | |||
538 | 8 | foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) { |
|
539 | 8 | $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
540 | } |
||
541 | |||
542 | 8 | $mapping['joinTable'] = $joinTable; |
|
543 | } |
||
544 | |||
545 | 13 | if (isset($manyToManyElement->cascade)) { |
|
546 | 8 | $mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade); |
|
547 | } |
||
548 | |||
549 | 13 | if (isset($manyToManyElement->{'order-by'})) { |
|
550 | 1 | $orderBy = []; |
|
551 | 1 | foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} as $orderByField) { |
|
552 | 1 | $orderBy[(string) $orderByField['name']] = isset($orderByField['direction']) |
|
553 | ? (string) $orderByField['direction'] |
||
554 | 1 | : Criteria::ASC; |
|
555 | } |
||
556 | 1 | $mapping['orderBy'] = $orderBy; |
|
557 | } |
||
558 | |||
559 | 13 | if (isset($manyToManyElement['index-by'])) { |
|
560 | $mapping['indexBy'] = (string) $manyToManyElement['index-by']; |
||
561 | 13 | } else if (isset($manyToManyElement->{'index-by'})) { |
|
562 | throw new \InvalidArgumentException("<index-by /> is not a valid tag"); |
||
563 | } |
||
564 | |||
565 | // Evaluate second level cache |
||
566 | 13 | if (isset($manyToManyElement->cache)) { |
|
567 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement->cache)); |
||
568 | } |
||
569 | |||
570 | 13 | $metadata->mapManyToMany($mapping); |
|
571 | } |
||
572 | } |
||
573 | |||
574 | // Evaluate association-overrides |
||
575 | 38 | if (isset($xmlRoot->{'attribute-overrides'})) { |
|
576 | 2 | foreach ($xmlRoot->{'attribute-overrides'}->{'attribute-override'} as $overrideElement) { |
|
577 | 2 | $fieldName = (string) $overrideElement['name']; |
|
578 | 2 | foreach ($overrideElement->field as $field) { |
|
579 | 2 | $mapping = $this->columnToArray($field); |
|
580 | 2 | $mapping['fieldName'] = $fieldName; |
|
581 | 2 | $metadata->setAttributeOverride($fieldName, $mapping); |
|
582 | } |
||
583 | } |
||
584 | } |
||
585 | |||
586 | // Evaluate association-overrides |
||
587 | 38 | if (isset($xmlRoot->{'association-overrides'})) { |
|
588 | 4 | foreach ($xmlRoot->{'association-overrides'}->{'association-override'} as $overrideElement) { |
|
589 | 4 | $fieldName = (string) $overrideElement['name']; |
|
590 | 4 | $override = []; |
|
591 | |||
592 | // Check for join-columns |
||
593 | 4 | if (isset($overrideElement->{'join-columns'})) { |
|
594 | 2 | $joinColumns = []; |
|
595 | 2 | foreach ($overrideElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
|
596 | 2 | $joinColumns[] = $this->joinColumnToArray($joinColumnElement); |
|
597 | } |
||
598 | 2 | $override['joinColumns'] = $joinColumns; |
|
599 | } |
||
600 | |||
601 | // Check for join-table |
||
602 | 4 | if ($overrideElement->{'join-table'}) { |
|
603 | 2 | $joinTable = null; |
|
604 | 2 | $joinTableElement = $overrideElement->{'join-table'}; |
|
605 | |||
606 | $joinTable = [ |
||
607 | 2 | 'name' => (string) $joinTableElement['name'], |
|
608 | 2 | 'schema' => (string) $joinTableElement['schema'] |
|
609 | ]; |
||
610 | |||
611 | 2 | if (isset($joinTableElement->{'join-columns'})) { |
|
612 | 2 | foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) { |
|
613 | 2 | $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
614 | } |
||
615 | } |
||
616 | |||
617 | 2 | if (isset($joinTableElement->{'inverse-join-columns'})) { |
|
618 | 2 | foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) { |
|
619 | 2 | $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
620 | } |
||
621 | } |
||
622 | |||
623 | 2 | $override['joinTable'] = $joinTable; |
|
624 | } |
||
625 | |||
626 | // Check for inversed-by |
||
627 | 4 | if (isset($overrideElement->{'inversed-by'})) { |
|
628 | 1 | $override['inversedBy'] = (string) $overrideElement->{'inversed-by'}['name']; |
|
629 | } |
||
630 | |||
631 | // Check for `fetch` |
||
632 | 4 | if (isset($overrideElement['fetch'])) { |
|
633 | 1 | $override['fetch'] = constant(Metadata::class . '::FETCH_' . (string) $overrideElement['fetch']); |
|
634 | } |
||
635 | |||
636 | 4 | $metadata->setAssociationOverride($fieldName, $override); |
|
637 | } |
||
638 | } |
||
639 | |||
640 | // Evaluate <lifecycle-callbacks...> |
||
641 | 38 | if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
642 | 5 | foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
643 | 5 | $metadata->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ORM\Events::' . (string) $lifecycleCallback['type'])); |
|
644 | } |
||
645 | } |
||
646 | |||
647 | // Evaluate entity listener |
||
648 | 38 | if (isset($xmlRoot->{'entity-listeners'})) { |
|
649 | 6 | foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) { |
|
650 | 6 | $className = (string) $listenerElement['class']; |
|
651 | // Evaluate the listener using naming convention. |
||
652 | 6 | if ($listenerElement->count() === 0) { |
|
653 | 2 | EntityListenerBuilder::bindEntityListener($metadata, $className); |
|
654 | |||
655 | 2 | continue; |
|
656 | } |
||
657 | |||
658 | 4 | foreach ($listenerElement as $callbackElement) { |
|
659 | 4 | $eventName = (string) $callbackElement['type']; |
|
660 | 4 | $methodName = (string) $callbackElement['method']; |
|
661 | |||
662 | 4 | $metadata->addEntityListener($eventName, $className, $methodName); |
|
663 | } |
||
664 | } |
||
665 | } |
||
666 | 38 | } |
|
667 | |||
668 | /** |
||
669 | * Parses (nested) option elements. |
||
670 | * |
||
671 | * @param SimpleXMLElement $options The XML element. |
||
672 | * |
||
673 | * @return array The options array. |
||
674 | */ |
||
675 | 6 | private function _parseOptions(SimpleXMLElement $options) |
|
676 | { |
||
677 | 6 | $array = []; |
|
678 | |||
679 | /* @var $option SimpleXMLElement */ |
||
680 | 6 | foreach ($options as $option) { |
|
681 | 6 | if ($option->count()) { |
|
682 | 5 | $value = $this->_parseOptions($option->children()); |
|
683 | } else { |
||
684 | 6 | $value = (string) $option; |
|
685 | } |
||
686 | |||
687 | 6 | $attributes = $option->attributes(); |
|
688 | |||
689 | 6 | if (isset($attributes->name)) { |
|
690 | 6 | $nameAttribute = (string) $attributes->name; |
|
691 | 6 | $array[$nameAttribute] = in_array($nameAttribute, ['unsigned', 'fixed']) |
|
692 | 5 | ? $this->evaluateBoolean($value) |
|
693 | 6 | : $value; |
|
694 | } else { |
||
695 | 6 | $array[] = $value; |
|
696 | } |
||
697 | } |
||
698 | |||
699 | 6 | return $array; |
|
700 | } |
||
701 | |||
702 | /** |
||
703 | * Constructs a joinColumn mapping array based on the information |
||
704 | * found in the given SimpleXMLElement. |
||
705 | * |
||
706 | * @param SimpleXMLElement $joinColumnElement The XML element. |
||
707 | * |
||
708 | * @return array The mapping array. |
||
709 | */ |
||
710 | 14 | private function joinColumnToArray(SimpleXMLElement $joinColumnElement) |
|
711 | { |
||
712 | $joinColumn = [ |
||
713 | 14 | 'name' => (string) $joinColumnElement['name'], |
|
714 | 14 | 'referencedColumnName' => (string) $joinColumnElement['referenced-column-name'] |
|
715 | ]; |
||
716 | |||
717 | 14 | if (isset($joinColumnElement['unique'])) { |
|
718 | 4 | $joinColumn['unique'] = $this->evaluateBoolean($joinColumnElement['unique']); |
|
719 | } |
||
720 | |||
721 | 14 | if (isset($joinColumnElement['nullable'])) { |
|
722 | 6 | $joinColumn['nullable'] = $this->evaluateBoolean($joinColumnElement['nullable']); |
|
723 | } |
||
724 | |||
725 | 14 | if (isset($joinColumnElement['on-delete'])) { |
|
726 | 6 | $joinColumn['onDelete'] = (string) $joinColumnElement['on-delete']; |
|
727 | } |
||
728 | |||
729 | 14 | if (isset($joinColumnElement['column-definition'])) { |
|
730 | 5 | $joinColumn['columnDefinition'] = (string) $joinColumnElement['column-definition']; |
|
731 | } |
||
732 | |||
733 | 14 | return $joinColumn; |
|
734 | } |
||
735 | |||
736 | /** |
||
737 | * Parses the given field as array. |
||
738 | * |
||
739 | * @param SimpleXMLElement $fieldMapping |
||
740 | * |
||
741 | * @return array |
||
742 | */ |
||
743 | 24 | private function columnToArray(SimpleXMLElement $fieldMapping) |
|
744 | { |
||
745 | $mapping = [ |
||
746 | 24 | 'fieldName' => (string) $fieldMapping['name'], |
|
747 | ]; |
||
748 | |||
749 | 24 | if (isset($fieldMapping['type'])) { |
|
750 | 20 | $mapping['type'] = (string) $fieldMapping['type']; |
|
751 | } |
||
752 | |||
753 | 24 | if (isset($fieldMapping['column'])) { |
|
754 | 16 | $mapping['columnName'] = (string) $fieldMapping['column']; |
|
755 | } |
||
756 | |||
757 | 24 | if (isset($fieldMapping['length'])) { |
|
758 | 11 | $mapping['length'] = (int) $fieldMapping['length']; |
|
759 | } |
||
760 | |||
761 | 24 | if (isset($fieldMapping['precision'])) { |
|
762 | 1 | $mapping['precision'] = (int) $fieldMapping['precision']; |
|
763 | } |
||
764 | |||
765 | 24 | if (isset($fieldMapping['scale'])) { |
|
766 | 1 | $mapping['scale'] = (int) $fieldMapping['scale']; |
|
767 | } |
||
768 | |||
769 | 24 | if (isset($fieldMapping['unique'])) { |
|
770 | 10 | $mapping['unique'] = $this->evaluateBoolean($fieldMapping['unique']); |
|
771 | } |
||
772 | |||
773 | 24 | if (isset($fieldMapping['nullable'])) { |
|
774 | 9 | $mapping['nullable'] = $this->evaluateBoolean($fieldMapping['nullable']); |
|
775 | } |
||
776 | |||
777 | 24 | if (isset($fieldMapping['version']) && $fieldMapping['version']) { |
|
778 | 3 | $mapping['version'] = $this->evaluateBoolean($fieldMapping['version']); |
|
779 | } |
||
780 | |||
781 | 24 | if (isset($fieldMapping['column-definition'])) { |
|
782 | 6 | $mapping['columnDefinition'] = (string) $fieldMapping['column-definition']; |
|
783 | } |
||
784 | |||
785 | 24 | if (isset($fieldMapping->options)) { |
|
786 | 5 | $mapping['options'] = $this->_parseOptions($fieldMapping->options->children()); |
|
787 | } |
||
788 | |||
789 | 24 | return $mapping; |
|
790 | } |
||
791 | |||
792 | /** |
||
793 | * Parse / Normalize the cache configuration |
||
794 | * |
||
795 | * @param SimpleXMLElement $cacheMapping |
||
796 | * |
||
797 | * @return array |
||
798 | */ |
||
799 | 2 | private function cacheToArray(SimpleXMLElement $cacheMapping) |
|
800 | { |
||
801 | 2 | $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null; |
|
802 | 2 | $usage = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null; |
|
803 | |||
804 | 2 | if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) { |
|
805 | throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage)); |
||
806 | } |
||
807 | |||
808 | 2 | if ($usage) { |
|
809 | 2 | $usage = constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage); |
|
810 | } |
||
811 | |||
812 | return [ |
||
813 | 2 | 'usage' => $usage, |
|
814 | 2 | 'region' => $region, |
|
815 | ]; |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * Gathers a list of cascade options found in the given cascade element. |
||
820 | * |
||
821 | * @param SimpleXMLElement $cascadeElement The cascade element. |
||
822 | * |
||
823 | * @return array The list of cascade options. |
||
824 | */ |
||
825 | 8 | private function _getCascadeMappings(SimpleXMLElement $cascadeElement) |
|
826 | { |
||
827 | 8 | $cascades = []; |
|
828 | /* @var $action SimpleXmlElement */ |
||
829 | 8 | foreach ($cascadeElement->children() as $action) { |
|
830 | // According to the JPA specifications, XML uses "cascade-persist" |
||
831 | // instead of "persist". Here, both variations |
||
832 | // are supported because both YAML and Annotation use "persist" |
||
833 | // and we want to make sure that this driver doesn't need to know |
||
834 | // anything about the supported cascading actions |
||
835 | 8 | $cascades[] = str_replace('cascade-', '', $action->getName()); |
|
836 | } |
||
837 | |||
838 | 8 | return $cascades; |
|
839 | } |
||
840 | |||
841 | /** |
||
842 | * {@inheritDoc} |
||
843 | */ |
||
844 | 41 | protected function loadMappingFile($file) |
|
845 | { |
||
846 | 41 | $result = []; |
|
847 | // Note: we do not use `simplexml_load_file()` because of https://bugs.php.net/bug.php?id=62577 |
||
848 | 41 | $xmlElement = simplexml_load_string(file_get_contents($file)); |
|
849 | |||
850 | 41 | if (isset($xmlElement->entity)) { |
|
851 | 39 | foreach ($xmlElement->entity as $entityElement) { |
|
852 | 39 | $entityName = (string) $entityElement['name']; |
|
853 | 39 | $result[$entityName] = $entityElement; |
|
854 | } |
||
855 | 9 | } else if (isset($xmlElement->{'mapped-superclass'})) { |
|
856 | 5 | foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) { |
|
857 | 5 | $className = (string) $mappedSuperClass['name']; |
|
858 | 5 | $result[$className] = $mappedSuperClass; |
|
859 | } |
||
860 | 4 | } else if (isset($xmlElement->embeddable)) { |
|
861 | 3 | foreach ($xmlElement->embeddable as $embeddableElement) { |
|
862 | 3 | $embeddableName = (string) $embeddableElement['name']; |
|
863 | 3 | $result[$embeddableName] = $embeddableElement; |
|
864 | } |
||
865 | } |
||
866 | |||
867 | 41 | return $result; |
|
868 | } |
||
869 | |||
870 | /** |
||
871 | * @param mixed $element |
||
872 | * |
||
873 | * @return bool |
||
874 | */ |
||
875 | 14 | protected function evaluateBoolean($element) |
|
876 | { |
||
877 | 14 | $flag = (string) $element; |
|
878 | |||
879 | 14 | return ($flag == "true" || $flag == "1"); |
|
880 | } |
||
881 | } |
||
882 |