예제 #1
0
 /**
  * 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;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     $className = $reflClass->name;
     $loaded = false;
     foreach ($reflClass->getProperties() as $property) {
         if ($property->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
                 if ($groups instanceof Groups) {
                     foreach ($groups->getGroups() as $group) {
                         $metadata->addAttributeGroup($property->name, $group);
                     }
                 }
                 $loaded = true;
             }
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($method->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getMethodAnnotations($method) as $groups) {
                 if ($groups instanceof Groups) {
                     if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
                         foreach ($groups->getGroups() as $group) {
                             $metadata->addAttributeGroup(lcfirst($matches[2]), $group);
                         }
                     } else {
                         throw new \BadMethodCallException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get" or "is".', $className, $method->name));
                     }
                 }
                 $loaded = true;
             }
         }
     }
     return $loaded;
 }
예제 #3
0
 public static function createClassMetadata($withParent = false, $withInterface = false)
 {
     $expected = new ClassMetadata('Symfony\\Component\\Serializer\\Tests\\Fixtures\\GroupDummy');
     $foo = new AttributeMetadata('foo');
     $foo->addGroup('a');
     $expected->addAttributeMetadata($foo);
     $bar = new AttributeMetadata('bar');
     $bar->addGroup('b');
     $bar->addGroup('c');
     $expected->addAttributeMetadata($bar);
     $fooBar = new AttributeMetadata('fooBar');
     $fooBar->addGroup('a');
     $fooBar->addGroup('b');
     $expected->addAttributeMetadata($fooBar);
     $symfony = new AttributeMetadata('symfony');
     $expected->addAttributeMetadata($symfony);
     if ($withParent) {
         $kevin = new AttributeMetadata('kevin');
         $kevin->addGroup('a');
         $expected->addAttributeMetadata($kevin);
         $coopTilleuls = new AttributeMetadata('coopTilleuls');
         $coopTilleuls->addGroup('a');
         $coopTilleuls->addGroup('b');
         $expected->addAttributeMetadata($coopTilleuls);
     }
     if ($withInterface) {
         $symfony->addGroup('a');
     }
     // load reflection class so that the comparison passes
     $expected->getReflectionClass();
     return $expected;
 }
 public static function createClassMetadata($withParent = false, $withInterface = false)
 {
     $expected = new ClassMetadata('Symfony\\Component\\Serializer\\Tests\\Fixtures\\GroupDummy');
     if ($withParent) {
         $expected->addAttributeGroup('kevin', 'a');
         $expected->addAttributeGroup('coopTilleuls', 'a');
         $expected->addAttributeGroup('coopTilleuls', 'b');
     }
     if ($withInterface) {
         $expected->addAttributeGroup('symfony', 'a');
     }
     $expected->addAttributeGroup('foo', 'a');
     $expected->addAttributeGroup('bar', 'b');
     $expected->addAttributeGroup('bar', 'c');
     $expected->addAttributeGroup('fooBar', 'a');
     $expected->addAttributeGroup('fooBar', 'b');
     // load reflection class so that the comparison passes
     $expected->getReflectionClass();
     return $expected;
 }