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

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