Passed
Pull Request — 2.8.x (#8022)
by Benjamin
08:51
created

YamlDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
use function trigger_error;
29
30
/**
31
 * The YamlDriver reads the mapping metadata from yaml schema files.
32
 *
33
 * @since 2.0
34
 * @author Benjamin Eberlei <[email protected]>
35
 * @author Guilherme Blanco <[email protected]>
36
 * @author Jonathan H. Wage <[email protected]>
37
 * @author Roman Borschel <[email protected]>
38
 *
39
 * @deprecated 2.7 This class is being removed from the ORM and won't have any replacement
40
 */
41
class YamlDriver extends FileDriver
42
{
43
    const DEFAULT_FILE_EXTENSION = '.dcm.yml';
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 73
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
49
    {
50 73
        @trigger_error(
51 73
            'YAML mapping driver is deprecated and will be removed in Doctrine ORM 3.0, please migrate to annotation or XML driver.',
52 73
            E_USER_DEPRECATED
53
        );
54
55 73
        parent::__construct($locator, $fileExtension);
56 73
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 68
    public function loadMetadataForClass($className, ClassMetadata $metadata)
62
    {
63
        /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */
64 68
        $element = $this->getElement($className);
65
66 66
        if ($element['type'] == 'entity') {
67 64
            if (isset($element['repositoryClass'])) {
68
                $metadata->setCustomRepositoryClass($element['repositoryClass']);
69
            }
70 64
            if (isset($element['readOnly']) && $element['readOnly'] == true) {
71 64
                $metadata->markReadOnly();
72
            }
73 16
        } else if ($element['type'] == 'mappedSuperclass') {
74 14
            $metadata->setCustomRepositoryClass(
75 14
                $element['repositoryClass'] ?? null
76
            );
77 14
            $metadata->isMappedSuperclass = true;
78 2
        } else if ($element['type'] == 'embeddable') {
79
            $metadata->isEmbeddedClass = true;
80
        } else {
81 2
            throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
82
        }
83
84
        // Evaluate root level properties
85 64
        $primaryTable = [];
86
87 64
        if (isset($element['table'])) {
88 26
            $primaryTable['name'] = $element['table'];
89
        }
90
91 64
        if (isset($element['schema'])) {
92 2
            $primaryTable['schema'] = $element['schema'];
93
        }
94
95
        // Evaluate second level cache
96 64
        if (isset($element['cache'])) {
97 2
            $metadata->enableCache($this->cacheToArray($element['cache']));
98
        }
99
100 64
        $metadata->setPrimaryTable($primaryTable);
101
102
        // Evaluate named queries
103 64
        if (isset($element['namedQueries'])) {
104 8
            foreach ($element['namedQueries'] as $name => $queryMapping) {
105 8
                if (is_string($queryMapping)) {
106 8
                    $queryMapping = ['query' => $queryMapping];
107
                }
108
109 8
                if ( ! isset($queryMapping['name'])) {
110 8
                    $queryMapping['name'] = $name;
111
                }
112
113 8
                $metadata->addNamedQuery($queryMapping);
114
            }
115
        }
116
117
        // Evaluate named native queries
118 64
        if (isset($element['namedNativeQueries'])) {
119 6
            foreach ($element['namedNativeQueries'] as $name => $mappingElement) {
120 6
                if (!isset($mappingElement['name'])) {
121 6
                    $mappingElement['name'] = $name;
122
                }
123 6
                $metadata->addNamedNativeQuery(
124
                    [
125 6
                        'name'              => $mappingElement['name'],
126 6
                        'query'             => $mappingElement['query'] ?? null,
127 6
                        'resultClass'       => $mappingElement['resultClass'] ?? null,
128 6
                        'resultSetMapping'  => $mappingElement['resultSetMapping'] ?? null,
129
                    ]
130
                );
131
            }
132
        }
133
134
        // Evaluate sql result set mappings
135 64
        if (isset($element['sqlResultSetMappings'])) {
136 6
            foreach ($element['sqlResultSetMappings'] as $name => $resultSetMapping) {
137 6
                if (!isset($resultSetMapping['name'])) {
138 6
                    $resultSetMapping['name'] = $name;
139
                }
140
141 6
                $entities = [];
142 6
                $columns  = [];
143 6
                if (isset($resultSetMapping['entityResult'])) {
144 6
                    foreach ($resultSetMapping['entityResult'] as $entityResultElement) {
145
                        $entityResult = [
146 6
                            'fields'                => [],
147 6
                            'entityClass'           => $entityResultElement['entityClass'] ?? null,
148 6
                            'discriminatorColumn'   => $entityResultElement['discriminatorColumn'] ?? null,
149
                        ];
150
151 6
                        if (isset($entityResultElement['fieldResult'])) {
152 6
                            foreach ($entityResultElement['fieldResult'] as $fieldResultElement) {
153 6
                                $entityResult['fields'][] = [
154 6
                                    'name'      => $fieldResultElement['name'] ?? null,
155 6
                                    'column'    => $fieldResultElement['column'] ?? null,
156
                                ];
157
                            }
158
                        }
159
160 6
                        $entities[] = $entityResult;
161
                    }
162
                }
163
164
165 6
                if (isset($resultSetMapping['columnResult'])) {
166 6
                    foreach ($resultSetMapping['columnResult'] as $columnResultAnnot) {
167 6
                        $columns[] = [
168 6
                            'name' => $columnResultAnnot['name'] ?? null,
169
                        ];
170
                    }
171
                }
172
173 6
                $metadata->addSqlResultSetMapping(
174
                    [
175 6
                        'name'          => $resultSetMapping['name'],
176 6
                        'entities'      => $entities,
177 6
                        'columns'       => $columns
178
                    ]
179
                );
180
            }
181
        }
182
183 64
        if (isset($element['inheritanceType'])) {
184 18
            $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType'])));
185
186 18
            if ($metadata->inheritanceType != Metadata::INHERITANCE_TYPE_NONE) {
187
                // Evaluate discriminatorColumn
188 18
                if (isset($element['discriminatorColumn'])) {
189 12
                    $discrColumn = $element['discriminatorColumn'];
190 12
                    $metadata->setDiscriminatorColumn(
191
                        [
192 12
                            'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null,
193 12
                            'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string',
194 12
                            'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255,
195 12
                            'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null
196
                        ]
197
                    );
198
                } else {
199 12
                    $metadata->setDiscriminatorColumn(['name' => 'dtype', 'type' => 'string', 'length' => 255]);
200
                }
201
202
                // Evaluate discriminatorMap
203 18
                if (isset($element['discriminatorMap'])) {
204 18
                    $metadata->setDiscriminatorMap($element['discriminatorMap']);
205
                }
206
            }
207
        }
208
209
210
        // Evaluate changeTrackingPolicy
211 64
        if (isset($element['changeTrackingPolicy'])) {
212
            $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
213
                . strtoupper($element['changeTrackingPolicy'])));
214
        }
215
216
        // Evaluate indexes
217 64
        if (isset($element['indexes'])) {
218 12
            foreach ($element['indexes'] as $name => $indexYml) {
219 12
                if ( ! isset($indexYml['name'])) {
220 12
                    $indexYml['name'] = $name;
221
                }
222
223 12
                if (is_string($indexYml['columns'])) {
224 12
                    $index = ['columns' => array_map('trim', explode(',', $indexYml['columns']))];
225
                } else {
226
                    $index = ['columns' => $indexYml['columns']];
227
                }
228
229 12
                if (isset($indexYml['flags'])) {
230 2
                    if (is_string($indexYml['flags'])) {
231 2
                        $index['flags'] = array_map('trim', explode(',', $indexYml['flags']));
232
                    } else {
233
                        $index['flags'] = $indexYml['flags'];
234
                    }
235
                }
236
237 12
                if (isset($indexYml['options'])) {
238 2
                    $index['options'] = $indexYml['options'];
239
                }
240
241 12
                $metadata->table['indexes'][$indexYml['name']] = $index;
242
            }
243
        }
244
245
        // Evaluate uniqueConstraints
246 64
        if (isset($element['uniqueConstraints'])) {
247 11
            foreach ($element['uniqueConstraints'] as $name => $uniqueYml) {
248 11
                if ( ! isset($uniqueYml['name'])) {
249 11
                    $uniqueYml['name'] = $name;
250
                }
251
252 11
                if (is_string($uniqueYml['columns'])) {
253 10
                    $unique = ['columns' => array_map('trim', explode(',', $uniqueYml['columns']))];
254
                } else {
255 1
                    $unique = ['columns' => $uniqueYml['columns']];
256
                }
257
258 11
                if (isset($uniqueYml['options'])) {
259 6
                    $unique['options'] = $uniqueYml['options'];
260
                }
261
262 11
                $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique;
263
            }
264
        }
265
266 64
        if (isset($element['options'])) {
267 8
            $metadata->table['options'] = $element['options'];
268
        }
269
270 64
        $associationIds = [];
271 64
        if (isset($element['id'])) {
272
            // Evaluate identifier settings
273 60
            foreach ($element['id'] as $name => $idElement) {
274 60
                if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) {
275
                    $associationIds[$name] = true;
276
                    continue;
277
                }
278
279
                $mapping = [
280 60
                    'id' => true,
281 60
                    'fieldName' => $name
282
                ];
283
284 60
                if (isset($idElement['type'])) {
285 38
                    $mapping['type'] = $idElement['type'];
286
                }
287
288 60
                if (isset($idElement['column'])) {
289 8
                    $mapping['columnName'] = $idElement['column'];
290
                }
291
292 60
                if (isset($idElement['length'])) {
293 6
                    $mapping['length'] = $idElement['length'];
294
                }
295
296 60
                if (isset($idElement['columnDefinition'])) {
297 2
                    $mapping['columnDefinition'] = $idElement['columnDefinition'];
298
                }
299
300 60
                if (isset($idElement['options'])) {
301 6
                    $mapping['options'] = $idElement['options'];
302
                }
303
304 60
                $metadata->mapField($mapping);
305
306 60
                if (isset($idElement['generator'])) {
307 55
                    $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
308 55
                        . strtoupper($idElement['generator']['strategy'])));
309
                }
310
                // Check for SequenceGenerator/TableGenerator definition
311 60
                if (isset($idElement['sequenceGenerator'])) {
312 6
                    $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']);
313 54
                } else if (isset($idElement['customIdGenerator'])) {
314 4
                    $customGenerator = $idElement['customIdGenerator'];
315 4
                    $metadata->setCustomGeneratorDefinition(
316
                        [
317 4
                            'class' => (string) $customGenerator['class']
318
                        ]
319
                    );
320 50
                } else if (isset($idElement['tableGenerator'])) {
321 60
                    throw MappingException::tableIdGeneratorNotImplemented($className);
322
                }
323
            }
324
        }
325
326
        // Evaluate fields
327 64
        if (isset($element['fields'])) {
328 45
            foreach ($element['fields'] as $name => $fieldMapping) {
329
330 45
                $mapping = $this->columnToArray($name, $fieldMapping);
331
332 45
                if (isset($fieldMapping['id'])) {
333
                    $mapping['id'] = true;
334
                    if (isset($fieldMapping['generator']['strategy'])) {
335
                        $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
336
                            . strtoupper($fieldMapping['generator']['strategy'])));
337
                    }
338
                }
339
340 45
                if (isset($mapping['version'])) {
341 6
                    $metadata->setVersionMapping($mapping);
342 6
                    unset($mapping['version']);
343
                }
344
345 45
                $metadata->mapField($mapping);
346
            }
347
        }
348
349 64
        if (isset($element['embedded'])) {
350
            foreach ($element['embedded'] as $name => $embeddedMapping) {
351
                $mapping = [
352
                    'fieldName' => $name,
353
                    'class' => $embeddedMapping['class'],
354
                    'columnPrefix' => $embeddedMapping['columnPrefix'] ?? null,
355
                    'nullable' => $embeddedMapping['nullable'] ?? null,
356
                ];
357
                $metadata->mapEmbedded($mapping);
358
            }
359
        }
360
361
        // Evaluate oneToOne relationships
362 64
        if (isset($element['oneToOne'])) {
363 15
            foreach ($element['oneToOne'] as $name => $oneToOneElement) {
364
                $mapping = [
365 15
                    'fieldName' => $name,
366 15
                    'targetEntity' => $oneToOneElement['targetEntity']
367
                ];
368
369 15
                if (isset($associationIds[$mapping['fieldName']])) {
370
                    $mapping['id'] = true;
371
                }
372
373 15
                if (isset($oneToOneElement['fetch'])) {
374 3
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToOneElement['fetch']);
375
                }
376
377 15
                if (isset($oneToOneElement['mappedBy'])) {
378 3
                    $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
379
                } else {
380 14
                    if (isset($oneToOneElement['inversedBy'])) {
381 14
                        $mapping['inversedBy'] = $oneToOneElement['inversedBy'];
382
                    }
383
384 14
                    $joinColumns = [];
385
386 14
                    if (isset($oneToOneElement['joinColumn'])) {
387 13
                        $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']);
388 1
                    } else if (isset($oneToOneElement['joinColumns'])) {
389 1
                        foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
390 1
                            if ( ! isset($joinColumnElement['name'])) {
391 1
                                $joinColumnElement['name'] = $joinColumnName;
392
                            }
393
394 1
                            $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
395
                        }
396
                    }
397
398 14
                    $mapping['joinColumns'] = $joinColumns;
399
                }
400
401 15
                if (isset($oneToOneElement['cascade'])) {
402 11
                    $mapping['cascade'] = $oneToOneElement['cascade'];
403
                }
404
405 15
                if (isset($oneToOneElement['orphanRemoval'])) {
406 5
                    $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval'];
407
                }
408
409
                // Evaluate second level cache
410 15
                if (isset($oneToOneElement['cache'])) {
411
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache']));
412
                }
413
414 15
                $metadata->mapOneToOne($mapping);
415
            }
416
        }
417
418
        // Evaluate oneToMany relationships
419 64
        if (isset($element['oneToMany'])) {
420 10
            foreach ($element['oneToMany'] as $name => $oneToManyElement) {
421
                $mapping = [
422 10
                    'fieldName' => $name,
423 10
                    'targetEntity' => $oneToManyElement['targetEntity'],
424 10
                    'mappedBy' => $oneToManyElement['mappedBy']
425
                ];
426
427 10
                if (isset($oneToManyElement['fetch'])) {
428 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToManyElement['fetch']);
429
                }
430
431 10
                if (isset($oneToManyElement['cascade'])) {
432 8
                    $mapping['cascade'] = $oneToManyElement['cascade'];
433
                }
434
435 10
                if (isset($oneToManyElement['orphanRemoval'])) {
436 8
                    $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval'];
437
                }
438
439 10
                if (isset($oneToManyElement['orderBy'])) {
440 10
                    $mapping['orderBy'] = $oneToManyElement['orderBy'];
441
                }
442
443 10
                if (isset($oneToManyElement['indexBy'])) {
444
                    $mapping['indexBy'] = $oneToManyElement['indexBy'];
445
                }
446
447
448
                // Evaluate second level cache
449 10
                if (isset($oneToManyElement['cache'])) {
450 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache']));
451
                }
452
453 10
                $metadata->mapOneToMany($mapping);
454
            }
455
        }
456
457
        // Evaluate manyToOne relationships
458 64
        if (isset($element['manyToOne'])) {
459 10
            foreach ($element['manyToOne'] as $name => $manyToOneElement) {
460
                $mapping = [
461 10
                    'fieldName' => $name,
462 10
                    'targetEntity' => $manyToOneElement['targetEntity']
463
                ];
464
465 10
                if (isset($associationIds[$mapping['fieldName']])) {
466
                    $mapping['id'] = true;
467
                }
468
469 10
                if (isset($manyToOneElement['fetch'])) {
470 1
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToOneElement['fetch']);
471
                }
472
473 10
                if (isset($manyToOneElement['inversedBy'])) {
474 2
                    $mapping['inversedBy'] = $manyToOneElement['inversedBy'];
475
                }
476
477 10
                $joinColumns = [];
478
479 10
                if (isset($manyToOneElement['joinColumn'])) {
480 4
                    $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']);
481 6
                } else if (isset($manyToOneElement['joinColumns'])) {
482 3
                    foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
483 3
                        if ( ! isset($joinColumnElement['name'])) {
484 3
                            $joinColumnElement['name'] = $joinColumnName;
485
                        }
486
487 3
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
488
                    }
489
                }
490
491 10
                $mapping['joinColumns'] = $joinColumns;
492
493 10
                if (isset($manyToOneElement['cascade'])) {
494 5
                    $mapping['cascade'] = $manyToOneElement['cascade'];
495
                }
496
497
                // Evaluate second level cache
498 10
                if (isset($manyToOneElement['cache'])) {
499 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache']));
500
                }
501
502 10
                $metadata->mapManyToOne($mapping);
503
            }
504
        }
505
506
        // Evaluate manyToMany relationships
507 64
        if (isset($element['manyToMany'])) {
508 21
            foreach ($element['manyToMany'] as $name => $manyToManyElement) {
509
                $mapping = [
510 21
                    'fieldName' => $name,
511 21
                    'targetEntity' => $manyToManyElement['targetEntity']
512
                ];
513
514 21
                if (isset($manyToManyElement['fetch'])) {
515 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToManyElement['fetch']);
516
                }
517
518 21
                if (isset($manyToManyElement['mappedBy'])) {
519 2
                    $mapping['mappedBy'] = $manyToManyElement['mappedBy'];
520 19
                } else if (isset($manyToManyElement['joinTable'])) {
521
522 15
                    $joinTableElement = $manyToManyElement['joinTable'];
523
                    $joinTable = [
524 15
                        'name' => $joinTableElement['name']
525
                    ];
526
527 15
                    if (isset($joinTableElement['schema'])) {
528
                        $joinTable['schema'] = $joinTableElement['schema'];
529
                    }
530
531 15
                    if (isset($joinTableElement['joinColumns'])) {
532 15
                        foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
533 15
                            if ( ! isset($joinColumnElement['name'])) {
534 14
                                $joinColumnElement['name'] = $joinColumnName;
535
                            }
536 15
                            $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
537
                        }
538
                    }
539
540 15
                    if (isset($joinTableElement['inverseJoinColumns'])) {
541 15
                        foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) {
542 15
                            if ( ! isset($joinColumnElement['name'])) {
543 14
                                $joinColumnElement['name'] = $joinColumnName;
544
                            }
545 15
                            $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
546
                        }
547
                    }
548
549 15
                    $mapping['joinTable'] = $joinTable;
550
                }
551
552 21
                if (isset($manyToManyElement['inversedBy'])) {
553 2
                    $mapping['inversedBy'] = $manyToManyElement['inversedBy'];
554
                }
555
556 21
                if (isset($manyToManyElement['cascade'])) {
557 14
                    $mapping['cascade'] = $manyToManyElement['cascade'];
558
                }
559
560 21
                if (isset($manyToManyElement['orderBy'])) {
561
                    $mapping['orderBy'] = $manyToManyElement['orderBy'];
562
                }
563
564 21
                if (isset($manyToManyElement['indexBy'])) {
565
                    $mapping['indexBy'] = $manyToManyElement['indexBy'];
566
                }
567
568 21
                if (isset($manyToManyElement['orphanRemoval'])) {
569
                    $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval'];
570
                }
571
572
                // Evaluate second level cache
573 21
                if (isset($manyToManyElement['cache'])) {
574
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache']));
575
                }
576
577 21
                $metadata->mapManyToMany($mapping);
578
            }
579
        }
580
581
        // Evaluate associationOverride
582 64
        if (isset($element['associationOverride']) && is_array($element['associationOverride'])) {
583
584 8
            foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) {
585 8
                $override   = [];
586
587
                // Check for joinColumn
588 8
                if (isset($associationOverrideElement['joinColumn'])) {
589 4
                    $joinColumns = [];
590 4
                    foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) {
591 4
                        if ( ! isset($joinColumnElement['name'])) {
592
                            $joinColumnElement['name'] = $name;
593
                        }
594 4
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
595
                    }
596 4
                    $override['joinColumns'] = $joinColumns;
597
                }
598
599
                // Check for joinTable
600 8
                if (isset($associationOverrideElement['joinTable'])) {
601
602 4
                    $joinTableElement   = $associationOverrideElement['joinTable'];
603
                    $joinTable          =  [
604 4
                        'name' => $joinTableElement['name']
605
                    ];
606
607 4
                    if (isset($joinTableElement['schema'])) {
608
                        $joinTable['schema'] = $joinTableElement['schema'];
609
                    }
610
611 4
                    foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
612 4
                        if ( ! isset($joinColumnElement['name'])) {
613 4
                            $joinColumnElement['name'] = $name;
614
                        }
615
616 4
                        $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
617
                    }
618
619 4
                    foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
620 4
                        if ( ! isset($joinColumnElement['name'])) {
621 4
                            $joinColumnElement['name'] = $name;
622
                        }
623
624 4
                        $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
625
                    }
626
627 4
                    $override['joinTable'] = $joinTable;
628
                }
629
630
                // Check for inversedBy
631 8
                if (isset($associationOverrideElement['inversedBy'])) {
632 2
                    $override['inversedBy'] = (string) $associationOverrideElement['inversedBy'];
633
                }
634
635
                // Check for `fetch`
636 8
                if (isset($associationOverrideElement['fetch'])) {
637 2
                    $override['fetch'] = constant(Metadata::class . '::FETCH_' . $associationOverrideElement['fetch']);
638
                }
639
640 8
                $metadata->setAssociationOverride($fieldName, $override);
641
            }
642
        }
643
644
        // Evaluate associationOverride
645 64
        if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) {
646
647 4
            foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) {
648 4
                $mapping = $this->columnToArray($fieldName, $attributeOverrideElement);
649 4
                $metadata->setAttributeOverride($fieldName, $mapping);
650
            }
651
        }
652
653
        // Evaluate lifeCycleCallbacks
654 64
        if (isset($element['lifecycleCallbacks'])) {
655 9
            foreach ($element['lifecycleCallbacks'] as $type => $methods) {
656 8
                foreach ($methods as $method) {
657 8
                    $metadata->addLifecycleCallback($method, constant('Doctrine\ORM\Events::' . $type));
658
                }
659
            }
660
        }
661
662
        // Evaluate entityListeners
663 64
        if (isset($element['entityListeners'])) {
664 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...
665
                // Evaluate the listener using naming convention.
666 10
                if (empty($entityListener)) {
667 4
                    EntityListenerBuilder::bindEntityListener($metadata, $className);
668
669 4
                    continue;
670
                }
671
672 6
                foreach ($entityListener as $eventName => $callbackElement) {
673 6
                    foreach ($callbackElement as $methodName) {
674 6
                        $metadata->addEntityListener($eventName, $className, $methodName);
675
                    }
676
                }
677
            }
678
        }
679 64
    }
680
681
    /**
682
     * Constructs a joinColumn mapping array based on the information
683
     * found in the given join column element.
684
     *
685
     * @param array $joinColumnElement The array join column element.
686
     *
687
     * @return array The mapping array.
688
     */
689 21
    private function joinColumnToArray($joinColumnElement)
690
    {
691 21
        $joinColumn = [];
692 21
        if (isset($joinColumnElement['referencedColumnName'])) {
693 21
            $joinColumn['referencedColumnName'] = (string) $joinColumnElement['referencedColumnName'];
694
        }
695
696 21
        if (isset($joinColumnElement['name'])) {
697 17
            $joinColumn['name'] = (string) $joinColumnElement['name'];
698
        }
699
700 21
        if (isset($joinColumnElement['fieldName'])) {
701
            $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName'];
702
        }
703
704 21
        if (isset($joinColumnElement['unique'])) {
705 8
            $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
706
        }
707
708 21
        if (isset($joinColumnElement['nullable'])) {
709 10
            $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
710
        }
711
712 21
        if (isset($joinColumnElement['onDelete'])) {
713 8
            $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
714
        }
715
716 21
        if (isset($joinColumnElement['columnDefinition'])) {
717 8
            $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
718
        }
719
720 21
        return $joinColumn;
721
    }
722
723
    /**
724
     * Parses the given column as array.
725
     *
726
     * @param string $fieldName
727
     * @param array  $column
728
     *
729
     * @return  array
730
     */
731 45
    private function columnToArray($fieldName, $column)
732
    {
733
        $mapping = [
734 45
            'fieldName' => $fieldName
735
        ];
736
737 45
        if (isset($column['type'])) {
738 39
            $params = explode('(', $column['type']);
739
740 39
            $column['type']  = $params[0];
741 39
            $mapping['type'] = $column['type'];
742
743 39
            if (isset($params[1])) {
744 4
                $column['length'] = (integer) substr($params[1], 0, strlen($params[1]) - 1);
745
            }
746
        }
747
748 45
        if (isset($column['column'])) {
749 15
            $mapping['columnName'] = $column['column'];
750
        }
751
752 45
        if (isset($column['length'])) {
753 27
            $mapping['length'] = $column['length'];
754
        }
755
756 45
        if (isset($column['precision'])) {
757
            $mapping['precision'] = $column['precision'];
758
        }
759
760 45
        if (isset($column['scale'])) {
761
            $mapping['scale'] = $column['scale'];
762
        }
763
764 45
        if (isset($column['unique'])) {
765 17
            $mapping['unique'] = (bool) $column['unique'];
766
        }
767
768 45
        if (isset($column['options'])) {
769 8
            $mapping['options'] = $column['options'];
770
        }
771
772 45
        if (isset($column['nullable'])) {
773 14
            $mapping['nullable'] = $column['nullable'];
774
        }
775
776 45
        if (isset($column['version']) && $column['version']) {
777 6
            $mapping['version'] = $column['version'];
778
        }
779
780 45
        if (isset($column['columnDefinition'])) {
781 10
            $mapping['columnDefinition'] = $column['columnDefinition'];
782
        }
783
784 45
        return $mapping;
785
    }
786
787
    /**
788
     * Parse / Normalize the cache configuration
789
     *
790
     * @param array $cacheMapping
791
     *
792
     * @return array
793
     */
794 2
    private function cacheToArray($cacheMapping)
795
    {
796 2
        $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null;
797 2
        $usage  = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null;
798
799 2
        if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) {
800
            throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage));
801
        }
802
803 2
        if ($usage) {
804 2
            $usage = constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage);
805
        }
806
807
        return [
808 2
            'usage'  => $usage,
809 2
            'region' => $region,
810
        ];
811
    }
812
813
    /**
814
     * {@inheritDoc}
815
     */
816 68
    protected function loadMappingFile($file)
817
    {
818 68
        return Yaml::parse(file_get_contents($file));
819
    }
820
}
821