public function getRawAnnotation($s) { $reader = new AnnotationReader(); if (is_string($s)) { if (class_exists($s)) { return $reader->getClass($s); } elseif (function_exists($s)) { return $reader->getFunction($s); } } elseif (is_array($s) && is_callable($s)) { if (method_exists($s[0], $s[1])) { return $reader->getMethod($s[0], $s[1]); } elseif (is_callable([$s[0], 'isCallable'])) { $func = $s[0]->isCallable($s[1]); if ($func) { if (is_object($func)) { return $this->getRawAnnotation([$func, '__invoke']); } else { return $this->getRawAnnotation($func); } } } } if (is_a($s, 'Closure')) { return $reader->getFunction($s); } return null; }
public function buildActionArrays() { if (count($this->arrControllers) > 0) { $this->acl['admin'] = 'Modul Administrare'; foreach ($this->arrControllers as $strModule => $arrController) { foreach ($arrController as $strController) { $fileName = APPPATH . 'controllers/' . $strModule . '/' . $strController; require_once $fileName; if (file_exists($fileName)) { $strClassName = explode(".php", $fileName); $class = basename($strClassName[0]); $reflector = new ReflectionClass($class); $methods = array_filter($reflector->getMethods(ReflectionMethod::IS_PUBLIC), function ($prop) use($reflector) { return $prop->getDeclaringClass()->getName() == $reflector->getName(); }); foreach ($methods as $method) { $annotationReader = new AnnotationReader($class, $method->name); $annotations = $annotationReader->getParameter("AclResource"); if ($annotations) { if ($strModule) { $this->acl[$strModule . '/' . $class . '/' . $method->name] = $annotations; } else { $this->acl[$class . '/' . $method->name] = $annotations; } } } } else { } } } } //modules return $this->acl; }
/** * Map using the @MapTo() annotations found in the object * @param object $modelOrClass A class or object to map from * @param object $entityOrClass A class or object to map to */ public function mapFromModel($modelOrClass, $entityOrClass) { $class = new \ReflectionClass($modelOrClass); $annotationName = 'Rezonant\\MapperBundle\\Annotations\\MapTo'; $transformationAnnotationName = 'Rezonant\\MapperBundle\\Annotations\\Transformation'; $excludeAnnotationName = 'Rezonant\\MapperBundle\\Annotations\\Exclude'; $map = new MapBuilder(); $destinationClass = new \ReflectionClass($entityOrClass); foreach ($class->getProperties() as $property) { $annotation = $this->annotationReader->getPropertyAnnotation($property, $annotationName); $transformationAnnotation = $this->annotationReader->getPropertyAnnotation($property, $transformationAnnotationName); $excludeAnnotation = $this->annotationReader->getPropertyAnnotation($property, $excludeAnnotationName); // Resolve the type of this property for submapping later. $subSourceType = $this->reflector->getTypeFromProperty($property); $subDestType = null; $fieldValue = null; $submap = null; $destinationField = null; $exclude = false; // Use the annoted destination field, or assume that the mapping // is 1-to-1. if ($annotation) { $destinationField = $annotation->value; } else { if ($destinationClass->hasProperty($property->name)) { $destinationField = $property->name; } } //Get transformation if there is one $transformation = $this->getTransformationFromAnnotation($transformationAnnotation); if ($excludeAnnotation) { $exclude = true; } // Resolve the destination field's type for generating a submap. if ($destinationField) { $destReference = new Reference($destinationField, $destinationClass); $subDestTypes = $destReference->getTypes(); $subDestType = $subDestTypes[count($subDestTypes) - 1]; if ($subSourceType && $subDestType) { $submap = $this->mapFromModel($subSourceType, $subDestType); } $map->field(new Reference($property->name, $class), $destReference, $submap, $transformation, $exclude); } } return $map->build(); }
/** * 获取元信息 * @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; }
/** * Whether the class with the specified name is transient. Only non-transient * classes, that is entities and mapped superclasses, should have their metadata loaded. * A class is non-transient if it is annotated with either @Entity or * @MappedSuperclass in the class doc block. * * @param string $className * @return boolean */ public function isTransient($className) { $classAnnotations = $this->reader->getClassAnnotations(new \ReflectionClass($className)); foreach ($classAnnotations as $classAnnotation) { if ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Document) { return false; } else { if ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\MappedSuperclass) { return false; } else { if ($classAnnotation instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbeddedDocument) { return false; } } } } return true; }