protected function generateMap($class, $basePath) { $reflection = Utilities::reflectionClassFromObjectOrClass($class); if (isset($reflection)) { $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); } else { $methods = array(); } /** @var ReflectionMethod $method */ foreach ($methods as $method) { $doc = $method->getDocComment(); $all_http_verbs_joined_by_pipes = implode('|', HttpMethods::getAllMethods()); if (preg_match_all('/@url[ \\t]+(' . $all_http_verbs_joined_by_pipes . ')[ \\t]+\\/?(\\S*)/s', $doc, $matches, PREG_SET_ORDER)) { $params = $method->getParameters(); foreach ($matches as $match) { $httpMethod = $match[1]; $url = $this->root . $basePath . $match[2]; // quick fix for running on windows $url = str_replace('\\', '/', $url); if ($url && $url[0] == '/') { $url = substr($url, 1); } // end quick fix if ($url && $url[strlen($url) - 1] == '/') { $url = substr($url, 0, -1); } $call = array($class, $method->getName()); $args = array(); foreach ($params as $param) { // The order of the parameters is essential, there is no name-matching // inserting the name is just for easier debuging try { $args[$param->getName()] = $param->getDefaultValue(); } catch (ReflectionException $e) { // If the method has no default parameter set the value to null $args[$param->getName()] = null; } } $call[] = $args; $call[] = null; $call[] = DocBlockParser::getDocKeys($method); $this->map[$httpMethod][$url] = $call; } } } }
public function testReflectionClassFromObjectOrClassWithClassName() { $result = Utilities::reflectionClassFromObjectOrClass('stdClass'); $this->assertInstanceOf('ReflectionClass', $result); $this->assertEquals('stdClass', $result->getName()); }
/** * @param object $obj Class object * @param string $method Class method * @param string $type_hint_class Class name of the type hint * @return array Position of parameters of type $type_hint_class */ public static function getPositionsOfParameterWithTypeHint($obj, $method, $type_hint_class) { $reflection_class = Utilities::reflectionClassFromObjectOrClass($obj); $reflection_method = $reflection_class->getMethod($method); $reflection_parameters = $reflection_method->getParameters(); $positions = array(); /** @var ReflectionParameter $parameter */ foreach ($reflection_parameters as $parameter) { $type_hint = ''; if (isset($parameter->getClass()->name)) { $type_hint = $parameter->getClass()->name; } if ($type_hint == $type_hint_class) { $positions[$parameter->name] = $parameter->getPosition(); } } return $positions; }