Doctrine\OXM\Mapping\Driver\AnnotationDriver::loadMetadataForClass PHP Method

loadMetadataForClass() public method

public loadMetadataForClass ( $className, ClassMetadataInfo $metadata )
$metadata Doctrine\OXM\Mapping\ClassMetadataInfo
    public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
    {
        $reflClass = $metadata->getReflectionClass();
        $classAnnotations = $this->reader->getClassAnnotations($reflClass);
        // Compatibility with Doctrine Common 3.x
        if ($classAnnotations && is_int(key($classAnnotations))) {
            foreach ($classAnnotations as $annot) {
                $classAnnotations[get_class($annot)] = $annot;
            }
        }
        // Evaluate XmlEntity Annotations
        if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlEntity'])) {
            $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlEntity'];
        } elseif (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlRootEntity'])) {
            $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlRootEntity'];
            $metadata->isRoot = true;
        } elseif (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlMappedSuperclass'])) {
            $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlMappedSuperclass'];
            $metadata->isMappedSuperclass = true;
        } else {
            throw MappingException::classIsNotAValidXmlEntity($className);
        }
        $metadata->setName($reflClass->getName());
        if (isset($entityAnnot->xml)) {
            $metadata->setXmlName($entityAnnot->xml);
        } else {
            $metadata->setXmlName(Inflector::xmlize($reflClass->getShortName()));
        }
        if (isset($entityAnnot->repositoryClass)) {
            $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
        }
        // Evaluate XmlChangeTrackingPolicy annotation
        if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlChangeTrackingPolicy'])) {
            $changeTrackingAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlChangeTrackingPolicy'];
            $metadata->setChangeTrackingPolicy(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
        }
        // Check for XmlNamespace/XmlNamespaces annotations
        $xmlNamespaces = array();
        if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespace'])) {
            $xmlNamespaceAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespace'];
            $xmlNamespaces[] = array('url' => $xmlNamespaceAnnot->url, 'prefix' => $xmlNamespaceAnnot->prefix);
        } else {
            if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespaces'])) {
                $xmlNamespaceAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespaces'];
                foreach ($xmlNamespaceAnnot->value as $xmlNamespace) {
                    $xmlNamespaces[] = array('url' => $xmlNamespace->url, 'prefix' => $xmlNamespace->prefix);
                }
            }
        }
        $metadata->setXmlNamespaces($xmlNamespaces);
        foreach ($reflClass->getProperties() as $property) {
            if ($metadata->isMappedSuperclass && !$property->isPrivate() || $metadata->isInheritedField($property->name)) {
                continue;
            }
            $mapping = array();
            $mapping['fieldName'] = $property->getName();
            if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlId')) {
                $mapping['id'] = true;
            }
            if ($generatedValueAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlGeneratedValue')) {
                $metadata->setIdGeneratorType(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
            }
            $referenceAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlReferences');
            if (isset($referenceAnnot->entityName)) {
                $mapping['references'] = $referenceAnnot->entityName;
            }
            // todo add Id Generator strategy support
            foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
                if ($fieldAnnot instanceof \Doctrine\OXM\Mapping\XmlField) {
                    if ($fieldAnnot->type == null) {
                        throw MappingException::propertyTypeIsRequired($className, $property->getName());
                    }
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
                    $metadata->mapField($mapping);
                }
            }
        }
        // Evaluate @HasLifecycleCallbacks annotations
        if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\HasLifecycleCallbacks'])) {
            foreach ($reflClass->getMethods() as $method) {
                // filter for the declaring class only, callbacks from parents will already be registered.
                if ($method->isPublic() && $method->getDeclaringClass()->getName() == $reflClass->name) {
                    $annotations = $this->reader->getMethodAnnotations($method);
                    // Compatibility with Doctrine Common 3.x
                    if ($annotations && is_int(key($annotations))) {
                        foreach ($annotations as $annot) {
                            $annotations[get_class($annot)] = $annot;
                        }
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PreMarshal'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preMarshal);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PostMarshal'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postMarshal);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PreUnmarshal'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preUnmarshal);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PostUnmarshal'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postUnmarshal);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PrePersist'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::prePersist);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PostPersist'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postPersist);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PreUpdate'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preUpdate);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PostUpdate'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postUpdate);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PreRemove'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preRemove);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PostRemove'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postRemove);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PreLoad'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preLoad);
                    }
                    if (isset($annotations['Doctrine\\OXM\\Mapping\\PostLoad'])) {
                        $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postLoad);
                    }
                }
            }
        }
    }