/** * If the method was called with the same class name (or an object of that * class) before, the same metadata instance is returned. * * If the factory was configured with a cache, this method will first look * for an existing metadata instance in the cache. If an existing instance * is found, it will be returned without further ado. * * Otherwise, a new metadata instance is created. If the factory was * configured with a loader, the metadata is passed to the * {@link LoaderInterface::loadClassMetadata()} method for further * configuration. At last, the new object is returned. * * @param string|object $value * @return ClassMetadata * @throws \InvalidArgumentException */ public function getMetadataFor($value) { $class = $this->getClass($value); if (!$class) { throw new \InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value))); } if (isset($this->loadedClasses[$class])) { return $this->loadedClasses[$class]; } if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) { return $this->loadedClasses[$class]; } if (!class_exists($class) && !interface_exists($class)) { throw new \InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $class)); } $metadata = new ClassMetadata($class); $reflClass = $metadata->getReflectionClass(); // Include groups from the parent class if ($parent = $reflClass->getParentClass()) { $metadata->mergeAttributesGroups($this->getMetadataFor($parent->name)); } // Include groups from all implemented interfaces foreach ($reflClass->getInterfaces() as $interface) { $metadata->mergeAttributesGroups($this->getMetadataFor($interface->name)); } if ($this->loader) { $this->loader->loadClassMetadata($metadata); } if ($this->cache) { $this->cache->save($class, $metadata); } return $this->loadedClasses[$class] = $metadata; }
public function testLoadClassMetadataAndMerge() { $loader = new AnnotationLoader(new AnnotationReader()); $metadata = new ClassMetadata('Symfony\\Component\\Serializer\\Tests\\Fixtures\\GroupDummy'); $parentMetadata = new ClassMetadata('Symfony\\Component\\Serializer\\Tests\\Fixtures\\GroupDummyParent'); $loader->loadClassMetadata($parentMetadata); $metadata->mergeAttributesGroups($parentMetadata); $loader->loadClassMetadata($metadata); $this->assertEquals(TestClassMetadataFactory::createClassMetadata(true), $metadata); }