public function loadMetadataForClass(\ReflectionClass $class) { $classMetadata = new ClassMetadata($class->getName()); foreach ($this->reader->getClassAnnotations($class) as $annotation) { if ($annotation instanceof Type) { $classMetadata->setType($annotation->value, $annotation->options); } else { if ($annotation instanceof EventSubscribers) { $classMetadata->setEventSubscribers($annotation->getValues()); } else { if ($annotation instanceof ModelTransformers) { $classMetadata->setModelTransformers($annotation->getValues()); } else { if ($annotation instanceof ViewTransformers) { $classMetadata->setViewTransformers($annotation->getValues()); } } } } } foreach ($class->getMethods() as $reflectionMethod) { foreach ($this->reader->getMethodAnnotations($reflectionMethod) as $annotation) { if ($annotation instanceof EventListener) { $methodMetadata = new MethodMetadata($class->getName(), $reflectionMethod->getName()); $methodMetadata->addEventListener($annotation->event, $annotation->priority); $classMetadata->addMethodMetadata($methodMetadata); } } } foreach ($class->getProperties() as $reflectionProperty) { foreach ($this->reader->getPropertyAnnotations($reflectionProperty) as $annotation) { if ($annotation instanceof Field) { $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName()); $propertyMetadata->setField($annotation->type, $annotation->options); $classMetadata->addPropertyMetadata($propertyMetadata); } } } return $classMetadata; }
protected function loadMetadataFromFile(\ReflectionClass $class, $file) { $classMetadata = new ClassMetadata($class->getName()); $config = Yaml::parse(file_get_contents($file)); $defaults = array('name' => null, 'options' => array(), 'fields' => array(), 'event_listeners' => array(), 'event_subscribers' => array(), 'model_transformers' => array(), 'view_transformers' => array()); $config = array_merge($defaults, $config); $classMetadata->setType($config['name'], $config['options']); unset($config['name'], $config['options']); $classMetadata->setEventSubscribers($config['event_subscribers']); $classMetadata->setModelTransformers($config['model_transformers']); $classMetadata->setViewTransformers($config['view_transformers']); unset($config['event_subscribers'], $config['model_transformers'], $config['view_transformers']); foreach ($class->getMethods() as $reflectionMethod) { $name = $reflectionMethod->getName(); if (array_key_exists($name, $config['event_listeners'] ?: array())) { $method = $config['event_listeners'][$name]; $methodMetadata = new MethodMetadata($class->getName(), $reflectionMethod->getName()); $methodMetadata->addEventListener($method['event'], $method['priority']); $classMetadata->addMethodMetadata($methodMetadata); } } unset($config['event_listeners']); $fieldDefaults = array('type' => null, 'options' => array()); foreach ($class->getProperties() as $reflectionProperty) { $name = $reflectionProperty->getName(); if (array_key_exists($name, $config['fields'] ?: array())) { $field = array_merge($fieldDefaults, $config['fields'][$name] ?: array()); $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName()); $propertyMetadata->setField($field['type'], $field['options']); $classMetadata->addPropertyMetadata($propertyMetadata); } } unset($config['fields']); foreach ($config as $key => $value) { throw new \RuntimeException(sprintf('Unknown configuration "%s" for class %s in %s.', $key, $class->getName(), realpath($file))); } return $classMetadata; }