/** * Creates a new operation with from a given annotated php method. * * @param ReflectionAnnotatedMethod $method An annotated php method * * @return ckWsdlOperation An operation, which's input corresponds to the parameters and which's output * corresponds to the return value of the given php method. */ public static function create(ReflectionAnnotatedMethod $method) { $name = $method->getAnnotation('WSMethod')->getName(); $result = new ckWsdlOperation(); $result->setName($name); $params = ckDocBlockParser::parseParameters($method->getDocComment()); $return = ckDocBlockParser::parseReturn($method->getDocComment()); $headers = $method->getAllAnnotations('WSHeader'); $result->input = new ckWsdlMessage($name . 'Request'); $result->output = new ckWsdlMessage($name . 'Response'); foreach ($headers as $header) { $type = ckXsdType::get($header->type); $type->setName($header->name); ckXsdType::set($header->name, $type); ckXsdType::set($header->type, null); $result->input->addPart(new ckWsdlPart($header->name, $type, true)); $result->output->addPart(new ckWsdlPart($header->name, $type, true)); } foreach ($params as $param) { $type = ckXsdType::get($param['type']); $result->input->addPart(new ckWsdlPart($param['name'], $type)); } if (!empty($return)) { $type = ckXsdType::get($return['type']); $result->output->addPart(new ckWsdlPart('result', $type)); } return $result; }
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'); } } } } }
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 ServiceMethod(&$reflectionClass, &$reflectionMethod, $httpMethods) { parent::__construct($reflectionMethod->class, $reflectionMethod->name); $this->reflectionClass = $reflectionClass; $this->requiresAuthentication = false; $this->requiredAccessLevel = 0; $this->contentType = 'text/plain'; $this->httpMethods = $httpMethods; // Annotations if ($this->hasAnnotation('Authenticated')) { $this->requiresAuthentication = true; } else { // Check if service class has global settings if ($this->reflectionClass->hasAnnotation('Authenticated')) { $this->requiresAuthentication = true; } } if ($this->hasAnnotation('AccessLevel')) { $annotation = $this->getAnnotation('AccessLevel'); $value = intval($annotation->value); if ($value) { $this->requiredAccessLevel = $value; } } else { // Check if service class has global settings if ($this->reflectionClass->hasAnnotation('AccessLevel')) { $annotation = $this->reflectionClass->getAnnotation('AccessLevel'); $value = intval($annotation->value); if ($value) { $this->requiredAccessLevel = $value; } } } if ($this->hasAnnotation('ContentType')) { $annotation = $this->getAnnotation('ContentType'); $value = $annotation->value; if ($value) { $this->contentType = $value; } } else { // Check if service class has global settings if ($this->reflectionClass->hasAnnotation('ContentType')) { $annotation = $this->reflectionClass->getAnnotation('ContentType'); $value = $annotation->value; if ($value) { $this->contentType = $value; } } } }
/** * 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 getEventTypeByMethod($obj, $methodName) { $method = new ReflectionAnnotatedMethod($obj, $methodName); if ($method->hasAnnotation('Event')) { return strtolower($method->getAnnotation('Event')->value); } else { return null; } }
/** * Checks if a given method has a WSMethod annotation and if it can be added to the webservice represented by this context. * * @param ReflectionAnnotatedMethod $method An annotated method to check * * @return bool True, if the check succeeds, false otherwise */ public function matchesContext(ReflectionAnnotatedMethod $method) { $hasAnnotation = ($annotation = $method->getAnnotation('WSMethod')) !== false; if ($hasAnnotation) { $webservices = $annotation->getWebservice(); $hasAnnotation = empty($webservices) && $this->default || array_search($this->name, $webservices) !== false; } return $hasAnnotation; }
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; }
<?php //Configurando a data e hora do servidor: date_default_timezone_set('America/Sao_Paulo'); //Criar um arquivo para definições mais tarde. define('WWW_ROOT', dirname(__FILE__)); define('DS', DIRECTORY_SEPARATOR); define('PATH', WWW_ROOT . DS); require_once PATH . 'annotations.php'; require_once PATH . 'AnnotationFirewall.php'; require_once PATH . 'Teste.php'; //Entrar com o nome da classe e o nome da método que será pego a anotação. $reflection = new ReflectionAnnotatedMethod('Teste', 'soma'); // by class name $annotation = $reflection->getAnnotation('AnnotationFirewall'); var_dump($annotation->visitante); // contains string "admin" var_dump($annotation->vendedor); // contains integer "2"
/** * 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); }
protected function doMethod(BeanDefinition $bean, \ReflectionAnnotatedMethod $method) { $listAnnotation = $method->getAllAnnotations(); foreach ($listAnnotation as $annotation) { if ($annotation instanceof annotations\PropertyAnnotation) { $annotation->doMethodProperty($bean, $method->name); } } }