ApiDoc生成

This commit is contained in:
IT小强xqitw.cn 2018-06-05 11:30:36 +08:00
parent ff2b370498
commit 051cc4f34d

71
src/ApiDoc.php Normal file
View File

@ -0,0 +1,71 @@
<?php
/**
* ==================================================================
* : ApiDoc.php
* : ApiDoc生成
* : IT小强
* 创建时间: 2018/6/5 9:40
* 修改时间:
* copyright (c) 2016 - 2018 mail@xqitw.cn
* ==================================================================
*/
namespace itxq\apidoc;
use itxq\apidoc\lib\ParseComment;
/**
* ApiDoc生成
* Class ApiDoc
* @package itxq\apidoc
*/
class ApiDoc
{
/**
* 获取类的注释
* @param $class - 类名称(存在命名空间时要完整写入) eg: $class = 'itxq\\apidoc\\ApiDoc';
* @return array - 返回格式为数组(未获取到注释时返回空数组)
*/
public function getDocComment($class) {
try {
$reflection = new \ReflectionClass($class);
$classDocComment = $reflection->getDocComment();
} catch (\Exception $exception) {
return [];
}
$parse = new ParseComment();
return $parse->parseCommentToArray($classDocComment);
}
/**
* 获取指定类下方法的注释
* @param $class - 类名称(存在命名空间时要完整写入) eg: $class = 'itxq\\apidoc\\ApiDoc';
* @param $type - 方法过滤,默认只获取 public类型 方法
* ReflectionMethod::IS_STATIC
* ReflectionMethod::IS_PUBLIC
* ReflectionMethod::IS_PROTECTED
* ReflectionMethod::IS_PRIVATE
* ReflectionMethod::IS_ABSTRACT
* ReflectionMethod::IS_FINAL
* @return array - 返回格式为数组(未获取到注释时返回空数组)
*/
public function getActionComment($class, $type = \ReflectionMethod::IS_PUBLIC) {
try {
$reflection = new \ReflectionClass($class);
//只允许生成public方法
$method = $reflection->getMethods($type);
} catch (\Exception $exception) {
return [];
}
$comments = [];
$parse = new ParseComment();
foreach ($method as $action) {
try {
$comments[] = $parse->parseCommentToArray($action->getDocComment());
} catch (\Exception $exception) {
continue;
}
}
return $comments;
}
}