Failed Conditions
Pull Request — 2.7 (#7901)
by Luís
06:54
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 3.0 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 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
                ];
356
                $metadata->mapEmbedded($mapping);
357
            }
358
        }
359
360
        // Evaluate oneToOne relationships
361 64
        if (isset($element['oneToOne'])) {
362 15
            foreach ($element['oneToOne'] as $name => $oneToOneElement) {
363
                $mapping = [
364 15
                    'fieldName' => $name,
365 15
                    'targetEntity' => $oneToOneElement['targetEntity']
366
                ];
367
368 15
                if (isset($associationIds[$mapping['fieldName']])) {
369
                    $mapping['id'] = true;
370
                }
371
372 15
                if (isset($oneToOneElement['fetch'])) {
373 3
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToOneElement['fetch']);
374
                }
375
376 15
                if (isset($oneToOneElement['mappedBy'])) {
377 3
                    $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
378
                } else {
379 14
                    if (isset($oneToOneElement['inversedBy'])) {
380 14
                        $mapping['inversedBy'] = $oneToOneElement['inversedBy'];
381
                    }
382
383 14
                    $joinColumns = [];
384
385 14
                    if (isset($oneToOneElement['joinColumn'])) {
386 13
                        $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']);
387 1
                    } else if (isset($oneToOneElement['joinColumns'])) {
388 1
                        foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
389 1
                            if ( ! isset($joinColumnElement['name'])) {
390 1
                                $joinColumnElement['name'] = $joinColumnName;
391
                            }
392
393 1
                            $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
394
                        }
395
                    }
396
397 14
                    $mapping['joinColumns'] = $joinColumns;
398
                }
399
400 15
                if (isset($oneToOneElement['cascade'])) {
401 11
                    $mapping['cascade'] = $oneToOneElement['cascade'];
402
                }
403
404 15
                if (isset($oneToOneElement['orphanRemoval'])) {
405 5
                    $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval'];
406
                }
407
408
                // Evaluate second level cache
409 15
                if (isset($oneToOneElement['cache'])) {
410
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache']));
411
                }
412
413 15
                $metadata->mapOneToOne($mapping);
414
            }
415
        }
416
417
        // Evaluate oneToMany relationships
418 64
        if (isset($element['oneToMany'])) {
419 10
            foreach ($element['oneToMany'] as $name => $oneToManyElement) {
420
                $mapping = [
421 10
                    'fieldName' => $name,
422 10
                    'targetEntity' => $oneToManyElement['targetEntity'],
423 10
                    'mappedBy' => $oneToManyElement['mappedBy']
424
                ];
425
426 10
                if (isset($oneToManyElement['fetch'])) {
427 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToManyElement['fetch']);
428
                }
429
430 10
                if (isset($oneToManyElement['cascade'])) {
431 8
                    $mapping['cascade'] = $oneToManyElement['cascade'];
432
                }
433
434 10
                if (isset($oneToManyElement['orphanRemoval'])) {
435 8
                    $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval'];
436
                }
437
438 10
                if (isset($oneToManyElement['orderBy'])) {
439 10
                    $mapping['orderBy'] = $oneToManyElement['orderBy'];
440
                }
441
442 10
                if (isset($oneToManyElement['indexBy'])) {
443
                    $mapping['indexBy'] = $oneToManyElement['indexBy'];
444
                }
445
446
447
                // Evaluate second level cache
448 10
                if (isset($oneToManyElement['cache'])) {
449 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache']));
450
                }
451
452 10
                $metadata->mapOneToMany($mapping);
453
            }
454
        }
455
456
        // Evaluate manyToOne relationships
457 64
        if (isset($element['manyToOne'])) {
458 10
            foreach ($element['manyToOne'] as $name => $manyToOneElement) {
459
                $mapping = [
460 10
                    'fieldName' => $name,
461 10
                    'targetEntity' => $manyToOneElement['targetEntity']
462
                ];
463
464 10
                if (isset($associationIds[$mapping['fieldName']])) {
465
                    $mapping['id'] = true;
466
                }
467
468 10
                if (isset($manyToOneElement['fetch'])) {
469 1
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToOneElement['fetch']);
470
                }
471
472 10
                if (isset($manyToOneElement['inversedBy'])) {
473 2
                    $mapping['inversedBy'] = $manyToOneElement['inversedBy'];
474
                }
475
476 10
                $joinColumns = [];
477
478 10
                if (isset($manyToOneElement['joinColumn'])) {
479 4
                    $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']);
480 6
                } else if (isset($manyToOneElement['joinColumns'])) {
481 3
                    foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
482 3
                        if ( ! isset($joinColumnElement['name'])) {
483 3
                            $joinColumnElement['name'] = $joinColumnName;
484
                        }
485
486 3
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
487
                    }
488
                }
489
490 10
                $mapping['joinColumns'] = $joinColumns;
491
492 10
                if (isset($manyToOneElement['cascade'])) {
493 5
                    $mapping['cascade'] = $manyToOneElement['cascade'];
494
                }
495
496
                // Evaluate second level cache
497 10
                if (isset($manyToOneElement['cache'])) {
498 2
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache']));
499
                }
500
501 10
                $metadata->mapManyToOne($mapping);
502
            }
503
        }
504
505
        // Evaluate manyToMany relationships
506 64
        if (isset($element['manyToMany'])) {
507 21
            foreach ($element['manyToMany'] as $name => $manyToManyElement) {
508
                $mapping = [
509 21
                    'fieldName' => $name,
510 21
                    'targetEntity' => $manyToManyElement['targetEntity']
511
                ];
512
513 21
                if (isset($manyToManyElement['fetch'])) {
514 2
                    $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToManyElement['fetch']);
515
                }
516
517 21
                if (isset($manyToManyElement['mappedBy'])) {
518 2
                    $mapping['mappedBy'] = $manyToManyElement['mappedBy'];
519 19
                } else if (isset($manyToManyElement['joinTable'])) {
520
521 15
                    $joinTableElement = $manyToManyElement['joinTable'];
522
                    $joinTable = [
523 15
                        'name' => $joinTableElement['name']
524
                    ];
525
526 15
                    if (isset($joinTableElement['schema'])) {
527
                        $joinTable['schema'] = $joinTableElement['schema'];
528
                    }
529
530 15
                    if (isset($joinTableElement['joinColumns'])) {
531 15
                        foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) {
532 15
                            if ( ! isset($joinColumnElement['name'])) {
533 14
                                $joinColumnElement['name'] = $joinColumnName;
534
                            }
535 15
                            $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
536
                        }
537
                    }
538
539 15
                    if (isset($joinTableElement['inverseJoinColumns'])) {
540 15
                        foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) {
541 15
                            if ( ! isset($joinColumnElement['name'])) {
542 14
                                $joinColumnElement['name'] = $joinColumnName;
543
                            }
544 15
                            $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
545
                        }
546
                    }
547
548 15
                    $mapping['joinTable'] = $joinTable;
549
                }
550
551 21
                if (isset($manyToManyElement['inversedBy'])) {
552 2
                    $mapping['inversedBy'] = $manyToManyElement['inversedBy'];
553
                }
554
555 21
                if (isset($manyToManyElement['cascade'])) {
556 14
                    $mapping['cascade'] = $manyToManyElement['cascade'];
557
                }
558
559 21
                if (isset($manyToManyElement['orderBy'])) {
560
                    $mapping['orderBy'] = $manyToManyElement['orderBy'];
561
                }
562
563 21
                if (isset($manyToManyElement['indexBy'])) {
564
                    $mapping['indexBy'] = $manyToManyElement['indexBy'];
565
                }
566
567 21
                if (isset($manyToManyElement['orphanRemoval'])) {
568
                    $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval'];
569
                }
570
571
                // Evaluate second level cache
572 21
                if (isset($manyToManyElement['cache'])) {
573
                    $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache']));
574
                }
575
576 21
                $metadata->mapManyToMany($mapping);
577
            }
578
        }
579
580
        // Evaluate associationOverride
581 64
        if (isset($element['associationOverride']) && is_array($element['associationOverride'])) {
582
583 8
            foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) {
584 8
                $override   = [];
585
586
                // Check for joinColumn
587 8
                if (isset($associationOverrideElement['joinColumn'])) {
588 4
                    $joinColumns = [];
589 4
                    foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) {
590 4
                        if ( ! isset($joinColumnElement['name'])) {
591
                            $joinColumnElement['name'] = $name;
592
                        }
593 4
                        $joinColumns[] = $this->joinColumnToArray($joinColumnElement);
594
                    }
595 4
                    $override['joinColumns'] = $joinColumns;
596
                }
597
598
                // Check for joinTable
599 8
                if (isset($associationOverrideElement['joinTable'])) {
600
601 4
                    $joinTableElement   = $associationOverrideElement['joinTable'];
602
                    $joinTable          =  [
603 4
                        'name' => $joinTableElement['name']
604
                    ];
605
606 4
                    if (isset($joinTableElement['schema'])) {
607
                        $joinTable['schema'] = $joinTableElement['schema'];
608
                    }
609
610 4
                    foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
611 4
                        if ( ! isset($joinColumnElement['name'])) {
612 4
                            $joinColumnElement['name'] = $name;
613
                        }
614
615 4
                        $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
616
                    }
617
618 4
                    foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
619 4
                        if ( ! isset($joinColumnElement['name'])) {
620 4
                            $joinColumnElement['name'] = $name;
621
                        }
622
623 4
                        $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
624
                    }
625
626 4
                    $override['joinTable'] = $joinTable;
627
                }
628
629
                // Check for inversedBy
630 8
                if (isset($associationOverrideElement['inversedBy'])) {
631 2
                    $override['inversedBy'] = (string) $associationOverrideElement['inversedBy'];
632
                }
633
634
                // Check for `fetch`
635 8
                if (isset($associationOverrideElement['fetch'])) {
636 2
                    $override['fetch'] = constant(Metadata::class . '::FETCH_' . $associationOverrideElement['fetch']);
637
                }
638
639 8
                $metadata->setAssociationOverride($fieldName, $override);
640
            }
641
        }
642
643
        // Evaluate associationOverride
644 64
        if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) {
645
646 4
            foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) {
647 4
                $mapping = $this->columnToArray($fieldName, $attributeOverrideElement);
648 4
                $metadata->setAttributeOverride($fieldName, $mapping);
649
            }
650
        }
651
652
        // Evaluate lifeCycleCallbacks
653 64
        if (isset($element['lifecycleCallbacks'])) {
654 9
            foreach ($element['lifecycleCallbacks'] as $type => $methods) {
655 8
                foreach ($methods as $method) {
656 8
                    $metadata->addLifecycleCallback($method, constant('Doctrine\ORM\Events::' . $type));
657
                }
658
            }
659
        }
660
661
        // Evaluate entityListeners
662 64
        if (isset($element['entityListeners'])) {
663 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...
664
                // Evaluate the listener using naming convention.
665 10
                if (empty($entityListener)) {
666 4
                    EntityListenerBuilder::bindEntityListener($metadata, $className);
667
668 4
                    continue;
669
                }
670
671 6
                foreach ($entityListener as $eventName => $callbackElement) {
672 6
                    foreach ($callbackElement as $methodName) {
673 6
                        $metadata->addEntityListener($eventName, $className, $methodName);
674
                    }
675
                }
676
            }
677
        }
678 64
    }
679
680
    /**
681
     * Constructs a joinColumn mapping array based on the information
682
     * found in the given join column element.
683
     *
684
     * @param array $joinColumnElement The array join column element.
685
     *
686
     * @return array The mapping array.
687
     */
688 21
    private function joinColumnToArray($joinColumnElement)
689
    {
690 21
        $joinColumn = [];
691 21
        if (isset($joinColumnElement['referencedColumnName'])) {
692 21
            $joinColumn['referencedColumnName'] = (string) $joinColumnElement['referencedColumnName'];
693
        }
694
695 21
        if (isset($joinColumnElement['name'])) {
696 17
            $joinColumn['name'] = (string) $joinColumnElement['name'];
697
        }
698
699 21
        if (isset($joinColumnElement['fieldName'])) {
700
            $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName'];
701
        }
702
703 21
        if (isset($joinColumnElement['unique'])) {
704 8
            $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
705
        }
706
707 21
        if (isset($joinColumnElement['nullable'])) {
708 10
            $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
709
        }
710
711 21
        if (isset($joinColumnElement['onDelete'])) {
712 8
            $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
713
        }
714
715 21
        if (isset($joinColumnElement['columnDefinition'])) {
716 8
            $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
717
        }
718
719 21
        return $joinColumn;
720
    }
721
722
    /**
723
     * Parses the given column as array.
724
     *
725
     * @param string $fieldName
726
     * @param array  $column
727
     *
728
     * @return  array
729
     */
730 45
    private function columnToArray($fieldName, $column)
731
    {
732
        $mapping = [
733 45
            'fieldName' => $fieldName
734
        ];
735
736 45
        if (isset($column['type'])) {
737 39
            $params = explode('(', $column['type']);
738
739 39
            $column['type']  = $params[0];
740 39
            $mapping['type'] = $column['type'];
741
742 39
            if (isset($params[1])) {
743 4
                $column['length'] = (integer) substr($params[1], 0, strlen($params[1]) - 1);
744
            }
745
        }
746
747 45
        if (isset($column['column'])) {
748 15
            $mapping['columnName'] = $column['column'];
749
        }
750
751 45
        if (isset($column['length'])) {
752 27
            $mapping['length'] = $column['length'];
753
        }
754
755 45
        if (isset($column['precision'])) {
756
            $mapping['precision'] = $column['precision'];
757
        }
758
759 45
        if (isset($column['scale'])) {
760
            $mapping['scale'] = $column['scale'];
761
        }
762
763 45
        if (isset($column['unique'])) {
764 17
            $mapping['unique'] = (bool) $column['unique'];
765
        }
766
767 45
        if (isset($column['options'])) {
768 8
            $mapping['options'] = $column['options'];
769
        }
770
771 45
        if (isset($column['nullable'])) {
772 14
            $mapping['nullable'] = $column['nullable'];
773
        }
774
775 45
        if (isset($column['version']) && $column['version']) {
776 6
            $mapping['version'] = $column['version'];
777
        }
778
779 45
        if (isset($column['columnDefinition'])) {
780 10
            $mapping['columnDefinition'] = $column['columnDefinition'];
781
        }
782
783 45
        return $mapping;
784
    }
785
786
    /**
787
     * Parse / Normalize the cache configuration
788
     *
789
     * @param array $cacheMapping
790
     *
791
     * @return array
792
     */
793 2
    private function cacheToArray($cacheMapping)
794
    {
795 2
        $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null;
796 2
        $usage  = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null;
797
798 2
        if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) {
799
            throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage));
800
        }
801
802 2
        if ($usage) {
803 2
            $usage = constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage);
804
        }
805
806
        return [
807 2
            'usage'  => $usage,
808 2
            'region' => $region,
809
        ];
810
    }
811
812
    /**
813
     * {@inheritDoc}
814
     */
815 68
    protected function loadMappingFile($file)
816
    {
817 68
        return Yaml::parse(file_get_contents($file));
818
    }
819
}
820