/**
  * @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;
 }
 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;
 }
Esempio n. 3
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;
 }
Esempio n. 4
0
 /**
  * @param string $className
  *
  * @return ClassHierarchyMetadata|MergeableClassMetadata|null
  */
 public function getMetadataForClass($className)
 {
     if (isset($this->loadedMetadata[$className])) {
         return $this->filterNullMetadata($this->loadedMetadata[$className]);
     }
     $metadata = null;
     /** @var \ReflectionClass $class */
     foreach ($this->getClassHierarchy($className) as $class) {
         if ($class->isAbstract()) {
             continue;
         }
         if (isset($this->loadedClassMetadata[$name = $class->getName()])) {
             if (null !== ($classMetadata = $this->filterNullMetadata($this->loadedClassMetadata[$name]))) {
                 $this->addClassMetadata($metadata, $classMetadata);
             }
             continue;
         }
         // check the cache
         if (null !== $this->cache) {
             /** @var ClassMetadata $classMetadata */
             if (($classMetadata = $this->cache->loadClassMetadataFromCache($class)) instanceof NullMetadata) {
                 $this->loadedClassMetadata[$name] = $classMetadata;
                 continue;
             }
             if (null !== $classMetadata) {
                 if (!$classMetadata instanceof ClassMetadata) {
                     throw new \LogicException(sprintf('The cache must return instances of ClassMetadata, but got %s.', var_export($classMetadata, true)));
                 }
                 if ($this->debug && !$classMetadata->isFresh()) {
                     $this->cache->evictClassMetadataFromCache($classMetadata->reflection);
                 } else {
                     $this->loadedClassMetadata[$name] = $classMetadata;
                     $this->addClassMetadata($metadata, $classMetadata);
                     continue;
                 }
             }
         }
         // load from source
         if (null !== ($classMetadata = $this->driver->loadMetadataForClass($class))) {
             $this->loadedClassMetadata[$name] = $classMetadata;
             $this->addClassMetadata($metadata, $classMetadata);
             if (null !== $this->cache) {
                 $this->cache->putClassMetadataInCache($classMetadata);
             }
             continue;
         }
         if (null !== $this->cache && !$this->debug) {
             $this->cache->putClassMetadataInCache(new NullMetadata($class->getName()));
         }
     }
     if (null === $metadata) {
         $metadata = new NullMetadata($className);
     }
     return $this->filterNullMetadata($this->loadedMetadata[$className] = $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);
 }