This commit is contained in:
wangjinlei
2022-04-06 18:02:49 +08:00
parent e34f87de36
commit c1885928ff
262 changed files with 18633 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
<?php
/**
* ==================================================================
* 文 件 名: ParseComment.php
* 概 要: 注释解析
* 作 者: IT小强
* 创建时间: 2018/6/5 10:38
* 修改时间:
* copyright (c) 2016 - 2018 mail@xqitw.cn
* ==================================================================
*/
namespace itxq\apidoc\lib;
/**
* 注释解析
* Class ParseComment
* @package itxq\apidoc\lib
*/
class ParseComment
{
/**
* @var array - 注释解析后的数组
*/
private $commentParams = [];
/**
* 将注释按行解析并以数组格式返回
* @param $comment - 原始注释字符串
* @return bool|array
*/
public function parseCommentToArray($comment) {
$comments = [];
if (empty($comment)) {
return $comments;
}
// 获取注释
if (preg_match('#^/\*\*(.*)\*/#s', $comment, $matches) === false) {
return $comments;
}
$matches = trim($matches[1]);
// 按行分割注释
if (preg_match_all('#^\s*\*(.*)#m', $matches, $lines) === false) {
return $comments;
}
$comments = $lines[1];
// 去除无用的注释
foreach ($comments as $k => $v) {
$comments[$k] = $v = trim($v);
if (strpos($v, '@') !== 0) {
continue;
}
$_parse = $this->_parseCommentLine($v);
if (!$_parse) {
continue;
}
$_type = $_parse['type'];
$_content = isset($_parse['content']) ? $_parse['content'] : '';
if (in_array($_type, ['param', 'code', 'return'])) {
if (!isset($this->commentParams[$_type])) {
$this->commentParams[$_type] = [];
}
unset($_parse['type']);
$this->commentParams[$_type][] = $_parse;
} else {
$this->commentParams[$_type] = $_content;
}
}
return $this->commentParams;
}
/**
* 解析注释中的参数
* @param $line - 注释行
* @return bool|array - 解析后的数组解析失败返回false
*/
private function _parseCommentLine($line) {
$line = explode(' ', $line);
$line[0] = substr($line[0], 1);
$class = new ParseLine();
$action = 'parseLine' . Tools::underlineToHump($line[0]);
if (!method_exists($class, $action)) {
$action = 'parseLineTitle';
}
return $class->$action($line);
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* ==================================================================
* 文 件 名: ParseLine.php
* 概 要: 按行解析注释参数
* 作 者: IT小强
* 创建时间: 2018/6/5 10:34
* 修改时间:
* copyright (c) 2016 - 2018 mail@xqitw.cn
* ==================================================================
*/
namespace itxq\apidoc\lib;
/**
* 按行解析注释参数
* Class ParseLine
* @package itxq\apidoc\lib
*/
class ParseLine
{
/**
* 解析 title|url
* @param $line
* @return array
*/
public function parseLineTitle($line) {
return ['type' => isset($line[0]) ? $line[0] : '', 'content' => isset($line[1]) ? $line[1] : ''];
}
/**
* 解析 param
* @param $line
* @return array
*/
public function parseLineParam($line) {
return [
'type' => isset($line[0]) ? $line[0] : '',
'param_type' => isset($line[1]) ? $line[1] : '',
'param_name' => isset($line[2]) ? $line[2] : '',
'param_title' => isset($line[3]) ? $line[3] : '',
'param_default' => isset($line[4]) ? $line[4] : '',
'param_require' => isset($line[5]) ? $line[5] : '',
];
}
/**
* 解析 code
* @param $line
* @return array
*/
public function parseLineCode($line) {
return [
'type' => isset($line[0]) ? $line[0] : '',
'code' => isset($line[1]) ? $line[1] : '',
'content' => isset($line[2]) ? $line[2] : '',
];
}
/**
* 解析 return
* @param $line
* @return array
*/
public function parseLineReturn($line) {
return [
'type' => isset($line[0]) ? $line[0] : '',
'return_type' => isset($line[1]) ? $line[1] : '',
'return_name' => isset($line[2]) ? $line[2] : '',
'return_title' => isset($line[3]) ? $line[3] : '',
];
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* ==================================================================
* 文 件 名: Tools.php
* 概 要:
* 作 者: IT小强
* 创建时间: 2018/6/6 8:47
* 修改时间:
* copyright (c) 2016 - 2018 mail@xqitw.cn
* ==================================================================
*/
namespace itxq\apidoc\lib;
/**
* 工具类
* Class Tools
* @package itxq\apidoc\lib
*/
class Tools
{
/**
* 下划线命名转驼峰命名
* @param $str - 下划线命名字符串
* @param $isFirst - 是否为大驼峰(即首字母也大写)
* @return mixed
*/
public static function underlineToHump($str, $isFirst = false) {
$str = preg_replace_callback('/([\-\_]+([a-z]{1}))/i', function ($matches) {
return strtoupper($matches[2]);
}, $str);
if ($isFirst) {
$str = ucfirst($str);
}
return $str;
}
/**
* 驼峰命名转下划线命名
* @param $str
* @return mixed
*/
public static function humpToUnderline($str) {
$str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
return '_' . strtolower($matches[0]);
}, $str);
$str = preg_replace('/^\_/', '', $str);
return $str;
}
/**
* 获取数组、对象下标对应值,不存在时返回指定的默认值
* @param string|integer $name - 下标(键名)
* @param array|object $data - 原始数组/对象
* @param mixed $default - 指定默认值
* @return mixed
*/
public static function getSubValue($name, $data, $default = '') {
if (is_object($data)) {
$value = isset($data->$name) ? $data->$name : $default;
} else if (is_array($data)) {
$value = isset($data[$name]) ? $data[$name] : $default;
} else {
$value = $default;
}
return $value;
}
/**
* 文件下载
* @param string - $docHtml - API文档HTML内容
*/
public static function downloadFile($docHtml) {
set_time_limit(0);
//下载文件需要用到的头
header('Content-type: application/octet-stream');
header('Accept-Ranges: bytes');
header('Content-Disposition: attachment; filename=api-doc_' . date('Y-m-d') . '.html');
echo $docHtml;
exit();
}
}