Failed Conditions
Pull Request — 2.6 (#7119)
by
unknown
09:21
created

YamlDriver::loadMappingFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Persistence\Mapping\ClassMetadata;
23
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
24
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
25
use Doctrine\ORM\Mapping\ClassMetadata as Metadata;
26
use Doctrine\ORM\Mapping\MappingException;
27
use Symfony\Component\Yaml\Yaml;
28
29
/**
30
 * The YamlDriver reads the mapping metadata from yaml schema files.
31
 *
32
 * @since 2.0
33
 * @author Benjamin Eberlei <[email protected]>
34
 * @author Guilherme Blanco <[email protected]>
35
 * @author Jonathan H. Wage <[email protected]>
36
 * @author Roman Borschel <[email protected]>
37
 */
38
class YamlDriver extends FileDriver
39
{
40
    const DEFAULT_FILE_EXTENSION = '.dcm.yml';
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 71
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
46
    {
47 71
        parent::__construct($locator, $fileExtension);
48 71
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 66
    public function loadMetadataForClass($className, ClassMetadata $metadata)
54
    {
55
        /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */
56 66
        $element = $this->getElement($className);
57
58 64
        if ($element['type'] == 'entity') {
59 62
            if (isset($element['repositoryClass'])) {
60
                $metadata->setCustomRepositoryClass($element['repositoryClass']);
61
            }
62 62
            if (isset($element['readOnly']) && $element['readOnly'] == true) {
63 62
                $metadata->markReadOnly();
64
            }
65 16
        } else if ($element['type'] == 'mappedSuperclass') {
66 14
            $metadata->setCustomRepositoryClass(
67 14
                $element['repositoryClass'] ?? null
68
            );
69 14
            $metadata->isMappedSuperclass = true;
70 2
        } else if ($element['type'] == 'embeddable') {
71
            $metadata->isEmbeddedClass = true;
72
        } else {
73 2
            throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
74
        }
75
76
        // Evaluate root level properties
77 62
        $primaryTable = [];
78
79 62
        if (isset($element['table'])) {
80 26
            $primaryTable['name'] = $element['table'];
81
        }
82
83 62
        if (isset($element['schema'])) {
84 2
            $primaryTable['schema'] = $element['schema'];
85
        }
86
87
        // Evaluate second level cache
88 62
        if (isset($element['cache'])) {
89 2
            $metadata->enableCache($this->cacheToArray($element['cache']));
90
        }
91
92 62
        $metadata->setPrimaryTable($primaryTable);
93
94
        // Evaluate named queries
95 62
        if (isset($element['namedQueries'])) {
96 8
            foreach ($element['namedQueries'] as $name => $queryMapping) {
97 8
                if (is_string($queryMapping)) {
98 8
                    $queryMapping = ['query' => $queryMapping];
99
                }
100
101 8
                if ( ! isset($queryMapping['name'])) {
102 8
                    $queryMapping['name'] = $name;
103
                }
104
105 8
                $metadata->addNamedQuery($queryMapping);
106
            }
107
        }
108
109
        // Evaluate named native queries
110 62
        if (isset($element['namedNativeQueries'])) {
111 6
            foreach ($element['namedNativeQueries'] as $name => $mappingElement) {
112 6
                if (!isset($mappingElement['name'])) {
113 6
                    $mappingElement['name'] = $name;
114
                }
115 6
                $metadata->addNamedNativeQuery(
116
                    [
117 6
                        'name'              => $mappingElement['name'],
118 6
                        'query'             => $mappingElement['query'] ?? null,
119 6
                        'resultClass'       => $mappingElement['resultClass'] ?? null,
120 6
                        'resultSetMapping'  => $mappingElement['resultSetMapping'] ?? null,
121
                    ]
122
                );
123
            }
124
        }
125
126
        // Evaluate sql result set mappings
127 62
        if (isset($element['sqlResultSetMappings'])) {
128 6
            foreach ($element['sqlResultSetMappings'] as $name => $resultSetMapping) {
129 6
                if (!isset($resultSetMapping['name'])) {
130 6
                    $resultSetMapping['name'] = $name;
131
                }
132
133 6
                $entities = [];
134 6
                $columns  = [];
135 6
                if (isset($resultSetMapping['entityResult'])) {
136 6
                    foreach ($resultSetMapping['entityResult'] as $entityResultElement) {
137
                        $entityResult = [
138 6
                            'fields'                => [],
139 6
                            'entityClass'           => $entityResultElement['entityClass'] ?? null,
140 6
                            'discriminatorColumn'   => $entityResultElement['discriminatorColumn'] ?? null,
141
                        ];
142
143 6
                        if (isset($entityResultElement['fieldResult'])) {
144 6
                            foreach ($entityResultElement['fieldResult'] as $fieldResultElement) {
145 6
                                $entityResult['fields'][] = [
146 6
                                    'name'      => $fieldResultElement['name'] ?? null,
147 6
                                    'column'    => $fieldResultElement['column'] ?? null,
148
                                ];
149
                            }
150
                        }
151
152 6
                        $entities[] = $entityResult;
153
                    }
154
                }
155
156
157 6
                if (isset($resultSetMapping['columnResult'])) {
158 6
                    foreach ($resultSetMapping['columnResult'] as $columnResultAnnot) {
159 6
                        $columns[] = [
160 6
                            'name' => $columnResultAnnot['name'] ?? null,
161
                        ];
162
                    }
163
                }
164
165 6
                $metadata->addSqlResultSetMapping(
166
                    [
167 6
                        'name'          => $resultSetMapping['name'],
168 6
                        'entities'      => $entities,
169 6
                        'columns'       => $columns
170
                    ]
171
                );
172
            }
173
        }
174
175 62
        if (isset($element['inheritanceType'])) {
176 18
            $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType'])));
177
178 18
            if ($metadata->inheritanceType != Metadata::INHERITANCE_TYPE_NONE) {
179
                // Evaluate discriminatorColumn
180 18
                if (isset($element['discriminatorColumn'])) {
181 12
                    $discrColumn = $element['discriminatorColumn'];
182 12
                    $metadata->setDiscriminatorColumn(
183
                        [
184 12
                            'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null,
185 12
                            'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string',
186 12
                            'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255,
187 12
                            'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null,
188 12
                            'strict' => isset($discrColumn['strict']) ? (bool) $discrColumn['strict'] : false
189
                        ]
190
                    );
191
                } else {
192 12
                    $metadata->setDiscriminatorColumn(['name' => 'dtype', 'type' => 'string', 'length' => 255]);
193
                }
194
195
                // Evaluate discriminatorMap
196 18
                if (isset($element['discriminatorMap'])) {
197 18
                    $metadata->setDiscriminatorMap($element['discriminatorMap']);
198
                }
199
            }
200
        }
201
202
203
        // Evaluate changeTrackingPolicy
204 62
        if (isset($element['changeTrackingPolicy'])) {
205
            $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
206
                . strtoupper($element['changeTrackingPolicy'])));
207
        }
208
209
        // Evaluate indexes
210 62
        if (isset($element['indexes'])) {
211 10
            foreach ($element['indexes'] as $name => $indexYml) {
212 10
                if ( ! isset($indexYml['name'])) {
213 10
                    $indexYml['name'] = $name;
214
                }
215
216 10
                if (is_string($indexYml['columns'])) {
217 10
                    $index = ['columns' => array_map('trim', explode(',', $indexYml['columns']))];
218
                } else {
219
                    $index = ['columns' => $indexYml['columns']];
220
                }
221
222 10
                if (isset($indexYml['flags'])) {
223 2
                    if (is_string($indexYml['flags'])) {
224 2
                        $index['flags'] = array_map('trim', explode(',', $indexYml['flags']));
225
                    } else {
226
                        $index['flags'] = $indexYml['flags'];
227
                    }
228
                }
229
230 10
                if (isset($indexYml['options'])) {
231 2
                    $index['options'] = $indexYml['options'];
232
                }
233
234 10
                $metadata->table['indexes'][$indexYml['name']] = $index;
235
            }
236
        }
237
238
        // Evaluate uniqueConstraints
239 62
        if (isset($element['uniqueConstraints'])) {
240 9
            foreach ($element['uniqueConstraints'] as $name => $uniqueYml) {
241 9
                if ( ! isset($uniqueYml['name'])) {
242 9
                    $uniqueYml['name'] = $name;
243
                }
244
245 9
                if (is_string($uniqueYml['columns'])) {
246 8
                    $unique = ['columns' => array_map('trim', explode(',', $uniqueYml['columns']))];
247
                } else {
248 1
                    $unique = ['columns' => $uniqueYml['columns']];
249
                }
250
251 9
                if (isset($uniqueYml['options'])) {
252 6
                    $unique['options'] = $uniqueYml['options'];
253
                }
254
255 9
                $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique;
256
            }
257
        }
258
259 62
        if (isset($element['options'])) {
260 8
            $metadata->table['options'] = $element['options'];
261
        }
262
263 62
        $associationIds = [];
264 62
        if (isset($element['id'])) {
265
            // Evaluate identifier settings
266 58
            foreach ($element['id'] as $name => $idElement) {
267 58
                if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) {
268
                    $associationIds[$name] = true;
269
                    continue;
270
                }
271
272
                $mapping = [
273 58
                    'id' => true,
274 58
                    'fieldName' => $name
275
                ];
276
277 58
                if (isset($idElement['type'])) {
278 38
                    $mapping['type'] = $idElement['type'];
279
                }
280
281 58
                if (isset($idElement['column'])) {
282 8
                    $mapping['columnName'] = $idElement['column'];
283
                }
284
285 58
                if (isset($idElement['length'])) {
286 6
                    $mapping['length'] = $idElement['length'];
287
                }
288
289 58
                if (isset($idElement['columnDefinition'])) {
290 2
                    $mapping['columnDefinition'] = $idElement['columnDefinition'];
291
                }
292
293 58
                if (isset($idElement['options'])) {
294 6
                    $mapping['options'] = $idElement['options'];
295
                }
296
297 58
                $metadata->mapField($mapping);
298
299 58
                if (isset($idElement['generator'])) {
300 55
                    $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
301 55
                        . strtoupper($idElement['generator']['strategy'])));
302
                }
303
                // Check for SequenceGenerator/TableGenerator definition
304 58
                if (isset($idElement['sequenceGenerator'])) {
305 6
                    $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']);
306 52
                } else if (isset($idElement['customIdGenerator'])) {
307 4
                    $customGenerator = $idElement['customIdGenerator'];
308 4
                    $metadata->setCustomGeneratorDefinition(
309
                        [
310 4
                            'class' => (string) $customGenerator['class']
311
                        ]
312
                    );
313 48
                } else if (isset($idElement['tableGenerator'])) {
314 58
                    throw MappingException::tableIdGeneratorNotImplemented($className);
315
                }
316
            }
317
        }
318
319
        // Evaluate fields
320 62
        if (isset($element['fields'])) {
321 43
            foreach ($element['fields'] as $name => $fieldMapping) {
322
323 43
                $mapping = $this->columnToArray($name, $fieldMapping);
324
325 43
                if (isset($fieldMapping['id'])) {
326
                    $mapping['id'] = true;
327
                    if (isset($fieldMapping['generator']['strategy'])) {
328
                        $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
329
                            . strtoupper($fieldMapping['generator']['strategy'])));
330
                    }
331
                }
332
333 43
                if (isset($mapping['version'])) {
334 6
                    $metadata->setVersionMapping($mapping);
335 6
                    unset($mapping['version']);
336
                }
337
338 43
                $metadata->mapField($mapping);
339
            }
340
        }
341
342 62
        if (isset($element['embedded'])) {
343
            foreach ($element['embedded'] as $name => $embeddedMapping) {
344
                $mapping = [
345
                    'fieldName' => $name,
346
                    'class' => $embeddedMapping['class'],
347
                    'columnPrefix' => $embeddedMapping['columnPrefix'] ?? null,
348
                ];
349
                $metadata->mapEmbedded($mapping);
350
            }
351
        }
352
353
        // Evaluate oneToOne relationships
354 62
        if (isset($element['oneToOne'])) {
355 15
            foreach ($element['oneToOne'] as $name => $oneToOneElement) {
356
                $mapping = [
357 15
                    'fieldName' => $name,
358 15
                    'targetEntity' => $oneToOneElement['targetEntity']
359
                ];
360
361 15
                if (isset($associationIds[$mapping['fieldName']])) {
362
                    $mapping['id'] = true;
363
                }
364
365 15
                if (isset($oneToOneElement['fetch'])) {
366 3
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToOneElement['fetch']);
367
                }
368
369 15
                if (isset($oneToOneElement['mappedBy'])) {
370 3
                    $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
371
                } else {
372 14
                    if (isset($oneToOneElement['inversedBy'])) {
373 14
                        $mapping['inversedBy'] = $oneToOneElement['inversedBy'];
374
                    }
375
376 14
                    $joinColumns = [];
377
378 14
                    if (isset($oneToOneElement['joinColumn'])) {
379 13
                        $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']);
380 1
                    } else if (isset($oneToOneElement['joinColumns'])) {
381 1
                        foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
382 1
                            if ( ! isset($joinColumnElement['name'])) {
383 1
                                $joinColumnElement['name'] = $joinColumnName;
384
                            }
385
386 1
                            $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
387
                        }
388
                    }
389
390 14
                    $mapping['joinColumns'] = $joinColumns;
391
                }
392
393 15
                if (isset($oneToOneElement['cascade'])) {
394 11
                    $mapping['cascade'] = $oneToOneElement['cascade'];
395
                }
396
397 15
                if (isset($oneToOneElement['orphanRemoval'])) {
398 5
                    $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval'];
399
                }
400
401
                // Evaluate second level cache
402 15
                if (isset($oneToOneElement['cache'])) {
403
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache']));
404
                }
405
406 15
                $metadata->mapOneToOne($mapping);
407
            }
408
        }
409
410
        // Evaluate oneToMany relationships
411 62
        if (isset($element['oneToMany'])) {
412 10
            foreach ($element['oneToMany'] as $name => $oneToManyElement) {
413
                $mapping = [
414 10
                    'fieldName' => $name,
415 10
                    'targetEntity' => $oneToManyElement['targetEntity'],
416 10
                    'mappedBy' => $oneToManyElement['mappedBy']
417
                ];
418
419 10
                if (isset($oneToManyElement['fetch'])) {
420 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToManyElement['fetch']);
421
                }
422
423 10
                if (isset($oneToManyElement['cascade'])) {
424 8
                    $mapping['cascade'] = $oneToManyElement['cascade'];
425
                }
426
427 10
                if (isset($oneToManyElement['orphanRemoval'])) {
428 8
                    $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval'];
429
                }
430
431 10
                if (isset($oneToManyElement['orderBy'])) {
432 10
                    $mapping['orderBy'] = $oneToManyElement['orderBy'];
433
                }
434
435 10
                if (isset($oneToManyElement['indexBy'])) {
436
                    $mapping['indexBy'] = $oneToManyElement['indexBy'];
437
                }
438
439
440
                // Evaluate second level cache
441 10
                if (isset($oneToManyElement['cache'])) {
442 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache']));
443
                }
444
445 10
                $metadata->mapOneToMany($mapping);
446
            }
447
        }
448
449
        // Evaluate manyToOne relationships
450 62
        if (isset($element['manyToOne'])) {
451 10
            foreach ($element['manyToOne'] as $name => $manyToOneElement) {
452
                $mapping = [
453 10
                    'fieldName' => $name,
454 10
                    'targetEntity' => $manyToOneElement['targetEntity']
455
                ];
456
457 10
                if (isset($associationIds[$mapping['fieldName']])) {
458
                    $mapping['id'] = true;
459
                }
460
461 10
                if (isset($manyToOneElement['fetch'])) {
462 1
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToOneElement['fetch']);
463
                }
464
465 10
                if (isset($manyToOneElement['inversedBy'])) {
466 2
                    $mapping['inversedBy'] = $manyToOneElement['inversedBy'];
467
                }
468
469 10
                $joinColumns = [];
470
471 10
                if (isset($manyToOneElement['joinColumn'])) {
472 4
                    $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']);
473 6
                } else if (isset($manyToOneElement['joinColumns'])) {
474 3
                    foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
475 3
                        if ( ! isset($joinColumnElement['name'])) {
476 3
                            $joinColumnElement['name'] = $joinColumnName;
477
                        }
478
479 3
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
480
                    }
481
                }
482
483 10
                $mapping['joinColumns'] = $joinColumns;
484
485 10
                if (isset($manyToOneElement['cascade'])) {
486 5
                    $mapping['cascade'] = $manyToOneElement['cascade'];
487
                }
488
489
                // Evaluate second level cache
490 10
                if (isset($manyToOneElement['cache'])) {
491 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache']));
492
                }
493
494 10
                $metadata->mapManyToOne($mapping);
495
            }
496
        }
497
498
        // Evaluate manyToMany relationships
499 62
        if (isset($element['manyToMany'])) {
500 21
            foreach ($element['manyToMany'] as $name => $manyToManyElement) {
501
                $mapping = [
502 21
                    'fieldName' => $name,
503 21
                    'targetEntity' => $manyToManyElement['targetEntity']
504
                ];
505
506 21
                if (isset($manyToManyElement['fetch'])) {
507 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToManyElement['fetch']);
508
                }
509
510 21
                if (isset($manyToManyElement['mappedBy'])) {
511 2
                    $mapping['mappedBy'] = $manyToManyElement['mappedBy'];
512 19
                } else if (isset($manyToManyElement['joinTable'])) {
513
514 15
                    $joinTableElement = $manyToManyElement['joinTable'];
515
                    $joinTable = [
516 15
                        'name' => $joinTableElement['name']
517
                    ];
518
519 15
                    if (isset($joinTableElement['schema'])) {
520
                        $joinTable['schema'] = $joinTableElement['schema'];
521
                    }
522
523 15
                    if (isset($joinTableElement['joinColumns'])) {
524 15
                        foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
525 15
                            if ( ! isset($joinColumnElement['name'])) {
526 14
                                $joinColumnElement['name'] = $joinColumnName;
527
                            }
528 15
                            $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
529
                        }
530
                    }
531
532 15
                    if (isset($joinTableElement['inverseJoinColumns'])) {
533 15
                        foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) {
534 15
                            if ( ! isset($joinColumnElement['name'])) {
535 14
                                $joinColumnElement['name'] = $joinColumnName;
536
                            }
537 15
                            $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
538
                        }
539
                    }
540
541 15
                    $mapping['joinTable'] = $joinTable;
542
                }
543
544 21
                if (isset($manyToManyElement['inversedBy'])) {
545 2
                    $mapping['inversedBy'] = $manyToManyElement['inversedBy'];
546
                }
547
548 21
                if (isset($manyToManyElement['cascade'])) {
549 14
                    $mapping['cascade'] = $manyToManyElement['cascade'];
550
                }
551
552 21
                if (isset($manyToManyElement['orderBy'])) {
553
                    $mapping['orderBy'] = $manyToManyElement['orderBy'];
554
                }
555
556 21
                if (isset($manyToManyElement['indexBy'])) {
557
                    $mapping['indexBy'] = $manyToManyElement['indexBy'];
558
                }
559
560 21
                if (isset($manyToManyElement['orphanRemoval'])) {
561
                    $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval'];
562
                }
563
564
                // Evaluate second level cache
565 21
                if (isset($manyToManyElement['cache'])) {
566
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache']));
567
                }
568
569 21
                $metadata->mapManyToMany($mapping);
570
            }
571
        }
572
573
        // Evaluate associationOverride
574 62
        if (isset($element['associationOverride']) && is_array($element['associationOverride'])) {
575
576 8
            foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) {
577 8
                $override   = [];
578
579
                // Check for joinColumn
580 8
                if (isset($associationOverrideElement['joinColumn'])) {
581 4
                    $joinColumns = [];
582 4
                    foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) {
583 4
                        if ( ! isset($joinColumnElement['name'])) {
584
                            $joinColumnElement['name'] = $name;
585
                        }
586 4
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
587
                    }
588 4
                    $override['joinColumns'] = $joinColumns;
589
                }
590
591
                // Check for joinTable
592 8
                if (isset($associationOverrideElement['joinTable'])) {
593
594 4
                    $joinTableElement   = $associationOverrideElement['joinTable'];
595
                    $joinTable          =  [
596 4
                        'name' => $joinTableElement['name']
597
                    ];
598
599 4
                    if (isset($joinTableElement['schema'])) {
600
                        $joinTable['schema'] = $joinTableElement['schema'];
601
                    }
602
603 4
                    foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
604 4
                        if ( ! isset($joinColumnElement['name'])) {
605 4
                            $joinColumnElement['name'] = $name;
606
                        }
607
608 4
                        $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
609
                    }
610
611 4
                    foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
612 4
                        if ( ! isset($joinColumnElement['name'])) {
613 4
                            $joinColumnElement['name'] = $name;
614
                        }
615
616 4
                        $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
617
                    }
618
619 4
                    $override['joinTable'] = $joinTable;
620
                }
621
622
                // Check for inversedBy
623 8
                if (isset($associationOverrideElement['inversedBy'])) {
624 2
                    $override['inversedBy'] = (string) $associationOverrideElement['inversedBy'];
625
                }
626
627
                // Check for `fetch`
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
628 8
                if (isset($associationOverrideElement['fetch'])) {
629 2
                    $override['fetch'] = constant(Metadata::class . '::FETCH_' . $associationOverrideElement['fetch']);
630
                }
631
632 8
                $metadata->setAssociationOverride($fieldName, $override);
633
            }
634
        }
635
636
        // Evaluate associationOverride
637 62
        if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) {
638
639 4
            foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) {
640 4
                $mapping = $this->columnToArray($fieldName, $attributeOverrideElement);
641 4
                $metadata->setAttributeOverride($fieldName, $mapping);
642
            }
643
        }
644
645
        // Evaluate lifeCycleCallbacks
646 62
        if (isset($element['lifecycleCallbacks'])) {
647 9
            foreach ($element['lifecycleCallbacks'] as $type => $methods) {
648 8
                foreach ($methods as $method) {
649 8
                    $metadata->addLifecycleCallback($method, constant('Doctrine\ORM\Events::' . $type));
650
                }
651
            }
652
        }
653
654
        // Evaluate entityListeners
655 62
        if (isset($element['entityListeners'])) {
656 10
            foreach ($element['entityListeners'] as $className => $entityListener) {
0 ignored issues
show
introduced by
$className is overwriting one of the parameters of this function.
Loading history...
657
                // Evaluate the listener using naming convention.
658 10
                if (empty($entityListener)) {
659 4
                    EntityListenerBuilder::bindEntityListener($metadata, $className);
660
661 4
                    continue;
662
                }
663
664 6
                foreach ($entityListener as $eventName => $callbackElement) {
665 6
                    foreach ($callbackElement as $methodName) {
666 6
                        $metadata->addEntityListener($eventName, $className, $methodName);
667
                    }
668
                }
669
            }
670
        }
671 62
    }
672
673
    /**
674
     * Constructs a joinColumn mapping array based on the information
675
     * found in the given join column element.
676
     *
677
     * @param array $joinColumnElement The array join column element.
678
     *
679
     * @return array The mapping array.
680
     */
681 21
    private function joinColumnToArray($joinColumnElement)
682
    {
683 21
        $joinColumn = [];
684 21
        if (isset($joinColumnElement['referencedColumnName'])) {
685 21
            $joinColumn['referencedColumnName'] = (string) $joinColumnElement['referencedColumnName'];
686
        }
687
688 21
        if (isset($joinColumnElement['name'])) {
689 17
            $joinColumn['name'] = (string) $joinColumnElement['name'];
690
        }
691
692 21
        if (isset($joinColumnElement['fieldName'])) {
693
            $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName'];
694
        }
695
696 21
        if (isset($joinColumnElement['unique'])) {
697 8
            $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
698
        }
699
700 21
        if (isset($joinColumnElement['nullable'])) {
701 10
            $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
702
        }
703
704 21
        if (isset($joinColumnElement['onDelete'])) {
705 8
            $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
706
        }
707
708 21
        if (isset($joinColumnElement['columnDefinition'])) {
709 8
            $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
710
        }
711
712 21
        return $joinColumn;
713
    }
714
715
    /**
716
     * Parses the given column as array.
717
     *
718
     * @param string $fieldName
719
     * @param array  $column
720
     *
721
     * @return  array
722
     */
723 43
    private function columnToArray($fieldName, $column)
724
    {
725
        $mapping = [
726 43
            'fieldName' => $fieldName
727
        ];
728
729 43
        if (isset($column['type'])) {
730 37
            $params = explode('(', $column['type']);
731
732 37
            $column['type']  = $params[0];
733 37
            $mapping['type'] = $column['type'];
734
735 37
            if (isset($params[1])) {
736 2
                $column['length'] = (integer) substr($params[1], 0, strlen($params[1]) - 1);
737
            }
738
        }
739
740 43
        if (isset($column['column'])) {
741 15
            $mapping['columnName'] = $column['column'];
742
        }
743
744 43
        if (isset($column['length'])) {
745 25
            $mapping['length'] = $column['length'];
746
        }
747
748 43
        if (isset($column['precision'])) {
749
            $mapping['precision'] = $column['precision'];
750
        }
751
752 43
        if (isset($column['scale'])) {
753
            $mapping['scale'] = $column['scale'];
754
        }
755
756 43
        if (isset($column['unique'])) {
757 17
            $mapping['unique'] = (bool) $column['unique'];
758
        }
759
760 43
        if (isset($column['options'])) {
761 8
            $mapping['options'] = $column['options'];
762
        }
763
764 43
        if (isset($column['nullable'])) {
765 14
            $mapping['nullable'] = $column['nullable'];
766
        }
767
768 43
        if (isset($column['version']) && $column['version']) {
769 6
            $mapping['version'] = $column['version'];
770
        }
771
772 43
        if (isset($column['columnDefinition'])) {
773 10
            $mapping['columnDefinition'] = $column['columnDefinition'];
774
        }
775
776 43
        return $mapping;
777
    }
778
779
    /**
780
     * Parse / Normalize the cache configuration
781
     *
782
     * @param array $cacheMapping
783
     *
784
     * @return array
785
     */
786 2
    private function cacheToArray($cacheMapping)
787
    {
788 2
        $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null;
789 2
        $usage  = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null;
790
791 2
        if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) {
792
            throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage));
793
        }
794
795 2
        if ($usage) {
796 2
            $usage = constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage);
797
        }
798
799
        return [
800 2
            'usage'  => $usage,
801 2
            'region' => $region,
802
        ];
803
    }
804
805
    /**
806
     * {@inheritDoc}
807
     */
808 66
    protected function loadMappingFile($file)
809
    {
810 66
        return Yaml::parse(file_get_contents($file));
811
    }
812
}
813