public function __construct() { $class_name = get_class($this); $reflector = new ReflectionClass($class_name); foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if (!$method->isConstructor() && !$method->isStatic() && !$method->isAbstract()) { // getting the annotation information for this method $method_reflector = new ReflectionAnnotatedMethod($class_name, $method->name); if ($method_reflector->hasAnnotation('AmfClassMapping')) { $as_class_name = $method_reflector->getAnnotation('AmfClassMapping')->name; $this->method_info[$method->name]['class_mapping'] = $as_class_name; } if ($method_reflector->hasAnnotation('AmfIgnore')) { $this->method_info[$method->name]['ignore'] = 1; } if ($method_reflector->hasAnnotation('AmfReturnType')) { $type = $method_reflector->getAnnotation('AmfReturnType')->value; // At the moment we can use ByteArray and ArrayCollection as // types if ($type == 'ArrayCollection' || $type == 'ByteArray') { $this->method_info[$method->name]['return_type'] = $type; } else { throw new Exception('AmfReturnType can only be one of the following values: ' . 'ByteArray, ArrayCollection'); } } } } }
/** * This method uses reflection to see if the given class uses annotations * to define a request handler. It returns a string that contains the * serialized Resource. * * @param string $file * @return string */ private function analyseClass($file) { //double check we have included addendum require_once dirname(__FILE__) . "/../util/addendum/annotations.php"; $class = pathinfo($file, PATHINFO_FILENAME); try { $reflection = new ReflectionClass($class); } catch (Exception $ex) { return; } $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); $string = ""; foreach ($methods as $method) { $annotation = new ReflectionAnnotatedMethod($method->class, $method->name); //if it is a request handler if ($annotation->hasAnnotation("RequestName")) { $requestName = $annotation->getAnnotation("RequestName")->value; $requestType = ''; } else { if ($annotation->hasAnnotation("GET")) { $requestName = $annotation->getAnnotation("GET")->value; $requestType = Request::GET; } else { if ($annotation->hasAnnotation("POST")) { $requestName = $annotation->getAnnotation("POST")->value; $requestType = Request::POST; } else { if ($annotation->hasAnnotation("DELETE")) { $requestName = $annotation->getAnnotation("DELETE")->value; $requestType = Request::DELETE; } else { if ($annotation->hasAnnotation("PUT")) { $requestName = $annotation->getAnnotation("PUT")->value; $requestType = Request::PUT; } else { continue; } } } } } $mappedParams = $annotation->getAnnotation("RequestParams")->value == null ? array() : $annotation->getAnnotation("RequestParams")->value; $cacheLength = $annotation->getAnnotation("CacheLength")->value == null ? false : $annotation->getAnnotation("CacheLength")->value; $authenticator = $annotation->getAnnotation("RequestAuthenticator")->value == null ? null : $annotation->getAnnotation("RequestAuthenticator")->value; $viewType = $annotation->getAnnotation("ViewType")->value == null ? Registry::get("DEFAULT_VIEW") : $annotation->getAnnotation("ViewType")->value; $viewTemplate = $annotation->getAnnotation("ViewTemplate")->value == null ? null : $annotation->getAnnotation("ViewTemplate")->value; $customParams = array(); foreach ($annotation->getAllAnnotations("CustomParam") as $custom) { $customParams[$custom->name] = $custom->value; } $resource = new Resource($requestName, $requestType, $method->class, $method->name, $mappedParams, $authenticator, $cacheLength, $viewType, $viewTemplate, null, $customParams); $string .= "Dispatcher::addResource(unserialize('" . serialize($resource) . "'));\n"; } return $string; }
public function testReflectionAnnotatedMethod() { $reflection = new ReflectionAnnotatedMethod('Example', 'exampleMethod'); $this->assertTrue($reflection->hasAnnotation('FirstAnnotation')); $this->assertFalse($reflection->hasAnnotation('NonExistentAnnotation')); $this->assertIsA($reflection->getAnnotation('FirstAnnotation'), 'FirstAnnotation'); $this->assertFalse($reflection->getAnnotation('NonExistentAnnotation')); $annotations = $reflection->getAnnotations(); $this->assertEqual(count($annotations), 1); $this->assertIsA($annotations[0], 'FirstAnnotation'); $this->assertIsA($reflection->getDeclaringClass(), 'ReflectionAnnotatedClass'); }
public function getEventTypeByMethod($obj, $methodName) { $method = new ReflectionAnnotatedMethod($obj, $methodName); if ($method->hasAnnotation('Event')) { return strtolower($method->getAnnotation('Event')->value); } else { return null; } }
public static function initAnnotatedObjectFromDBRow($row, $class_name) { require_once $class_name . ".php"; $reflect = new ReflectionClass($class_name); $methods = $reflect->getMethods(); $instance = $reflect->newInstanceArgs(); foreach ($methods as $value) { $method = new ReflectionAnnotatedMethod($class_name, $value->name); if ($method->hasAnnotation('DBAttribute')) { $attrib = $method->getAnnotation('DBAttribute'); if ($attrib->type == 'set') { $colName = $attrib->ColName; ///echo "initAnnotatedObjectFromDBRow: \$row[" . $colName . "]=" . $row[$colName] . " calling " . $method->getName() . "</br>"; $method->invoke($instance, $row[$colName]); } } } echo "initAnnotatedObjectFromDBRow: read object" . $instance . "</br>"; return $instance; }
/** * Check whether particular method is annotated. * @param string $method Tested method. */ private function hasAnnotation($method, $annotation) { $reflection = new ReflectionAnnotatedMethod($this, $method); return $reflection->hasAnnotation($annotation); }