vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php line 53

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