使用php提取方法注释
1.注释格式
<?php /** * @method 发送邮件 * @url email/send?token=xxx * @http POST * @param token string [必填] 调用接口凭证 (post|get) * @param ema_type enum [必填] 发送速度:'普通','紧急','延时' * @param ema_from enum [必填] 来源:'B2C','主站','客户网','CRM' * @param ema_from_email string [可选] 发送人邮箱 * @param ema_from_name string [可选] 发送人 * @param ema_to_email string [必填] 接收人手机号码,多个逗号分隔 * @param ema_title string [必填] 标题 * @param ema_text string [必填] 内容 * @param ema_style string [可选] 信息类型 例如:邮箱验证,取回密码.... * @param ema_expire_time int [可选] 过期时间戳,超过此时间即使失败也不再发送 * @author soul \r\n * @copyright 2017/4/13 \r\n * @return {"status":false,"data":'失败原因',"code":0} \r\n */ public function sendEmail() { //xxxx }
2.代码格式
<?php public function test() { $reflection = new \ReflectionClass ( 'apps\v10\controller\search' ); $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $docData = []; //遍历所有的方法 foreach ($methods as $method) { //获取方法的注释 $doc = $method->getDocComment(); $doc = ltrim($doc,'/**'); $doc = rtrim($doc,'*/'); if (!empty($doc)) { $doc = explode("\n",$doc); $data = []; foreach ($doc as $value) { //获取接口名 $strNum = strpos($value, '* @method'); if ($strNum !== false){ $data['api_name'] = trim(str_replace('* @method','',$value)); continue; } //获取请求地址 $strNum = strpos($value, '* @url'); if ($strNum !== false){ $data['url'] = trim(str_replace('* @url','',$value)); continue; } //获取请求类型 $strNum = strpos($value, '* @http'); if ($strNum !== false){ $data['http'] = trim(str_replace('* @http','',$value)); continue; } //获取字段相关信息 $strNum = strpos($value, '* @param'); if ($strNum !== false){ //获取字段名 $params = trim(str_replace('* @param','',$value)); $lineNum = strpos($params, ' '); $field_name = trim(mb_substr($params,0,$lineNum,'UTF-8')); //获取字段类型 $params = trim(str_replace($field_name,'', $params)); $lineNum = strpos($params, ' '); $field_type = trim(mb_substr($params,0,$lineNum,'UTF-8')); //获取字段是否必填 $params = trim(str_replace($field_type,'', $params)); $lineNum = strpos($params, '[必填]'); if ($lineNum !== false) { $non_empty = trim(substr($params,0,8)); } else { $non_empty = '[可选]'; } //获取字段描述 $params = trim(str_replace($non_empty,'', $params)); $data['params'][] = [ 'field_name' => $field_name, 'field_type' => $field_type, 'non-empty' => $non_empty, 'field_desc' => $params ]; //获取返回值 $strNum = strpos($value, '* @return '); if ($strNum !== false){ $data['return'] = trim(str_replace('* @return','',$value)); continue; } //获取作者 $strNum = strpos($value, '* @author'); if ($strNum !== false){ $data['author'] = trim(str_replace('* @author','',$value)); continue; } //获取最后修改时间 $strNum = strpos($value, '* @copyright'); if ($strNum !== false){ $data['copyright'] = trim(str_replace('* @copyright','',$value)); continue; } } } $docData[] = $data; } } }