/** * 获取元信息 * @param object|class $inst * @param boolean $record_doc 是否加载注释文本, 如果是 * @param array $select, 只取选中的几个 * @return array */ static function get($inst, $record_doc = false, $select = null) { $reflection = new \ReflectionClass($inst); $reader = new AnnotationReader($reflection); $info = array(); if ($record_doc) { if (false !== ($doc = $reflection->getDocComment())) { $info['doc'] = $doc; } } if ($select !== null) { $select = array_flip($select); } foreach ($reader->getClassAnnotations($reflection, $record_doc) as $id => $ann) { if ($select !== null && !array_key_exists($id, $select)) { continue; } $ann = $ann[0]; //可能有多个重名的, 只取第一个 $info[$id] = $ann; } foreach ($reflection->getMethods() as $method) { foreach ($reader->getMethodAnnotations($method, $record_doc) as $id => $ann) { if ($select !== null && !array_key_exists($id, $select)) { continue; } $ann = $ann[0]; //可能有多个重名的, 只取第一个 $info += array($id => array()); $info[$id][$method->getName()] = $ann; } } foreach ($reflection->getProperties() as $property) { foreach ($reader->getPropertyAnnotations($property, $record_doc) as $id => $ann) { if ($select !== null && !array_key_exists($id, $select)) { continue; } $ann = $ann[0]; //可能有多个重名的, 只取第一个 $info += array($id => array()); $info[$id][$property->getName()] = $ann; } } return $info; }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadata $class) { $reflClass = $class->getReflectionClass(); $classAnnotations = $this->reader->getClassAnnotations($reflClass); $isValidDocument = false; foreach ($classAnnotations as $classAnnotation) { if ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Document) { if ($classAnnotation->indexed) { $class->indexed = true; } $class->setCustomRepositoryClass($classAnnotation->repositoryClass); $isValidDocument = true; } elseif ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbeddedDocument) { $class->isEmbeddedDocument = true; $isValidDocument = true; } else { if ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\MappedSuperclass) { $class->isMappedSuperclass = true; $isValidDocument = true; } else { if ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Index) { $class->indexed = true; } } } } if (!$isValidDocument) { throw MappingException::classIsNotAValidDocument($className); } foreach ($reflClass->getProperties() as $property) { $isField = false; $mapping = array(); $mapping['fieldName'] = $property->getName(); foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Field) { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Version) { $mapping['isVersionField'] = true; } $mapping = array_merge($mapping, (array) $fieldAnnot); unset($mapping['value']); $isField = true; } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Index) { $mapping['indexed'] = true; } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceOne) { $cascade = 0; foreach ($fieldAnnot->cascade as $cascadeMode) { $cascade += constant('Doctrine\\ODM\\CouchDB\\Mapping\\ClassMetadata::CASCADE_' . strtoupper($cascadeMode)); } $fieldAnnot->cascade = $cascade; $mapping = array_merge($mapping, (array) $fieldAnnot); unset($mapping['value']); $class->mapManyToOne($mapping); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceMany) { $cascade = 0; foreach ($fieldAnnot->cascade as $cascadeMode) { $cascade += constant('Doctrine\\ODM\\CouchDB\\Mapping\\ClassMetadata::CASCADE_' . strtoupper($cascadeMode)); } $fieldAnnot->cascade = $cascade; $mapping = array_merge($mapping, (array) $fieldAnnot); unset($mapping['value']); $class->mapManyToMany($mapping); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Attachments) { $class->mapAttachments($mapping['fieldName']); } else { if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedOne || $fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedMany) { $mapping = array_merge($mapping, (array) $fieldAnnot); unset($mapping['value']); $class->mapEmbedded($mapping); } } } } } } } if ($isField) { $class->mapField($mapping); } } }