/** * 从注释解析生成文档 * */ public static function parse() { $result = []; $config = Config::load('api', Cml::getApplicationDir('app_controller_path') ? true : false); foreach ($config['version'] as $version => $apiList) { isset($result[$version]) || ($result[$version] = []); foreach ($apiList as $model => $api) { $pos = strrpos($api, '\\'); $controller = substr($api, 0, $pos); $action = substr($api, $pos + 1); if (class_exists($controller) === false) { continue; } $annotationParams = self::getAnnotationParams($controller, $action); empty($annotationParams) || ($result[$version][$model] = $annotationParams); } } foreach ($result as $key => $val) { if (count($val) < 1) { unset($result[$key]); } } $systemCode = Cml::requireFile(__DIR__ . DIRECTORY_SEPARATOR . 'resource' . DIRECTORY_SEPARATOR . 'code.php'); Cml::requireFile(__DIR__ . DIRECTORY_SEPARATOR . 'resource' . DIRECTORY_SEPARATOR . 'doc.html', ['config' => $config, 'result' => $result, 'systemCode' => $systemCode]); }
/** * 从注释解析生成文档 * */ public static function parse() { $result = array(); $config = Config::load('api', false); foreach ($config['version'] as $version => $apiList) { isset($result[$version]) || ($result[$version] = array()); foreach ($apiList as $model => $api) { $pos = strrpos($api, '\\'); $controller = substr($api, 0, $pos); $action = substr($api, $pos + 1); $reflection = new \ReflectionClass($controller); $res = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($res as $method) { if ($method->name == $action) { $annotation = $method->getDocComment(); if (strpos($annotation, '@doc') !== false) { $result[$version][$model] = array(); //$result[$version][$model]['all'] = $annotation; //描述 preg_match('/@desc([^\\n]+)/', $annotation, $desc); $result[$version][$model]['desc'] = isset($desc[1]) ? $desc[1] : ''; //参数 preg_match_all('/@param([^\\n]+)/', $annotation, $params); foreach ($params[1] as $key => $val) { $tmp = explode(' ', preg_replace('/\\s(\\s+)/', ' ', trim($val))); isset($tmp[3]) || ($tmp[3] = 'N'); $tmp[1] = substr($tmp[1], 1); $result[$version][$model]['params'][] = $tmp; } //请求示例 preg_match('/@req(.+?)(\\*\\s*?@|\\*\\/)/s', $annotation, $reqEg); $result[$version][$model]['req'] = isset($reqEg[1]) ? $reqEg[1] : ''; //请求成功示例 preg_match('/@success(.+?)(\\*\\s*?@|\\*\\/)/s', $annotation, $success); $result[$version][$model]['success'] = isset($success[1]) ? $success[1] : ''; //请求失败示例 preg_match('/@error(.+?)(\\*\\s*?@|\\*\\/)/s', $annotation, $error); $result[$version][$model]['error'] = isset($error[1]) ? $error[1] : ''; } } } } } $systemCode = (require __DIR__ . DIRECTORY_SEPARATOR . 'resource' . DIRECTORY_SEPARATOR . 'code.php'); require __DIR__ . DIRECTORY_SEPARATOR . 'resource' . DIRECTORY_SEPARATOR . 'doc.html'; }