/**
  * @param \ReflectionClass $class
  *
  * @return \Metadata\ClassMetadata
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $metadata = $this->driver->loadMetadataForClass($class);
     foreach ($metadata->propertyMetadata as $key => $propertyMetadata) {
         $type = $propertyMetadata->type['name'];
         if (!$propertyMetadata->reflection) {
             continue;
         }
         /** @var PropertyMetadata $propertyMetadata */
         /** @var HandledType $annot */
         $annot = $this->reader->getPropertyAnnotation($propertyMetadata->reflection, HandledType::class);
         if (!$annot) {
             continue;
         }
         $isCollection = false;
         $collectionType = null;
         if (in_array($type, ['array', 'ArrayCollection'], true)) {
             $isCollection = true;
             $collectionType = $type;
             $type = $propertyMetadata->type['params'][0]['name'];
         }
         $handler = $annot->handler ?: 'Relation';
         $newType = sprintf('%s<%s>', $handler, $type);
         if ($isCollection) {
             $newType = sprintf('%s<%s<%s>>', $collectionType, $handler, $type);
         }
         $propertyMetadata->setType($newType);
     }
     return $metadata;
 }
Example #2
0
 /**
  * {@inheritDoc}
  */
 public function getAllClassNames()
 {
     if (!$this->driver instanceof AdvancedDriverInterface) {
         throw new \RuntimeException(sprintf('Driver "%s" must be an instance of "AdvancedDriverInterface".', get_class($this->driver)));
     }
     return $this->driver->getAllClassNames();
 }
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     /** @var $classMetadata ClassMetadata */
     $classMetadata = $this->delegate->loadMetadataForClass($class);
     // Abort if the given class is not a mapped entity
     if (!($doctrineMetadata = $this->tryLoadingDoctrineMetadata($class->name))) {
         return $classMetadata;
     }
     if ($doctrineMetadata instanceof ClassMetadataInfo) {
         if (empty($classMetadata->discriminatorMap) && !$classMetadata->discriminatorDisabled && !empty($doctrineMetadata->discriminatorMap) && $doctrineMetadata->isRootEntity()) {
             $classMetadata->setDiscriminator($doctrineMetadata->discriminatorColumn['name'], $doctrineMetadata->discriminatorMap);
         }
     }
     // We base our scan on the internal driver's property list so that we
     // respect any internal white/blacklisting like in the AnnotationDriver
     foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
         /** @var $propertyMetadata PropertyMetadata */
         // If the inner driver provides a type, don't guess anymore.
         if ($propertyMetadata->type) {
             continue;
         }
         $propertyName = $propertyMetadata->name;
         if ($doctrineMetadata->hasField($propertyName) && ($fieldType = $this->normalizeFieldType($doctrineMetadata->getTypeOfField($propertyName)))) {
             $propertyMetadata->setType($fieldType);
         } elseif ($doctrineMetadata->hasAssociation($propertyName)) {
             $targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName);
             if (null === ($targetMetadata = $this->tryLoadingDoctrineMetadata($targetEntity))) {
                 continue;
             }
             // For inheritance schemes, we cannot add any type as we would only add the super-type of the hierarchy.
             // On serialization, this would lead to only the supertype being serialized, and properties of subtypes
             // being ignored.
             if ($targetMetadata instanceof ClassMetadataInfo && !$targetMetadata->isInheritanceTypeNone()) {
                 continue;
             }
             if ($doctrineMetadata->isSingleValuedAssociation($propertyName)) {
                 $propertyMetadata->setType($targetEntity);
             } else {
                 $propertyMetadata->setType("ArrayCollection<{$targetEntity}>");
             }
         }
     }
     return $classMetadata;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $metadata = $this->delegate->loadMetadataForClass($class);
     $newMetadata = false;
     if (empty($this->extensions)) {
         return $metadata;
     }
     if (null === $metadata) {
         $metadata = new ClassMetadata($class->getName());
         $newMetadata = true;
     }
     foreach ($this->extensions as $extension) {
         $extension->decorate($metadata);
     }
     if ($newMetadata && count($metadata->getRelations()) < 1 && count($metadata->getRelationProviders()) < 1) {
         $metadata = null;
     }
     return $metadata;
 }
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     /** @var $classMetadata ClassMetadata */
     $classMetadata = $this->delegate->loadMetadataForClass($class);
     // Abort if the given class is not a mapped entity
     if (!($doctrineMetadata = $this->tryLoadingDoctrineMetadata($class->name))) {
         return $classMetadata;
     }
     $this->setDiscriminator($doctrineMetadata, $classMetadata);
     // We base our scan on the internal driver's property list so that we
     // respect any internal white/blacklisting like in the AnnotationDriver
     foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
         /** @var $propertyMetadata PropertyMetadata */
         // If the inner driver provides a type, don't guess anymore.
         if ($propertyMetadata->type) {
             continue;
         }
         if ($this->hideProperty($doctrineMetadata, $propertyMetadata)) {
             unset($classMetadata->propertyMetadata[$key]);
         }
         $this->setPropertyType($doctrineMetadata, $propertyMetadata);
     }
     return $classMetadata;
 }
 /**
  * Test LoadMetadataForClass
  */
 public function testLoadMetadataForClass()
 {
     $class = new \ReflectionClass(new FakeClassMetadata());
     $metadata = $this->driver->loadMetadataForClass($class);
     $this->assertMetadata($metadata);
 }