From 9e5b2dfdc9f30ab8aa207416dd92631aa48a6542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IT=E5=B0=8F=E5=BC=BAxqitw=2Ecn?= <360237521@qq.com> Date: Thu, 7 Jun 2018 13:42:54 +0800 Subject: [PATCH] =?UTF-8?q?BootstrapAPI=E6=96=87=E6=A1=A3=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapApiDoc.php | 356 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 src/BootstrapApiDoc.php diff --git a/src/BootstrapApiDoc.php b/src/BootstrapApiDoc.php new file mode 100644 index 0000000..8c13927 --- /dev/null +++ b/src/BootstrapApiDoc.php @@ -0,0 +1,356 @@ + + ::-webkit-scrollbar {width: 5px;} + .navbar-collapse.collapse.show::-webkit-scrollbar {width: 0; height: 0;background-color: rgba(255, 255, 255, 0);} + ::-webkit-scrollbar-track {background-color: rgba(255, 255, 255, 0.2);-webkit-border-radius: 2em;-moz-border-radius: 2em;border-radius: 2em;} + ::-webkit-scrollbar-thumb {background-color: rgba(0, 0, 0, 0.8);-webkit-border-radius: 2em;-moz-border-radius: 2em;border-radius: 2em;} + ::-webkit-scrollbar-button {-webkit-border-radius: 2em;-moz-border-radius: 2em;border-radius: 2em;height: 0;background-color: rgba(0, 0, 0, 0.9);} + ::-webkit-scrollbar-corner {background-color: rgba(0, 0, 0, 0.9);} + #list-tab-left-nav{display: none;} + .doc-content{margin-top: 75px;} + .class-item .class-title {text-indent: 0.6em;border-left: 5px solid lightseagreen;font-size: 24px;margin: 15px 0;} + .action-item .action-title {text-indent: 0.6em;border-left: 3px solid #F0AD4E;font-size: 20px;margin: 8px 0;} + .table-item {background-color:#FFFFFF;padding-top: 10px;margin-bottom:10px;border: solid 1px #ccc;border-radius: 5px;} + .list-group-item-sub{padding: .5rem 1.25rem;} + '; + + /** + * @var string - 自定义JS + */ + private $customJs = ''; + + /** + * Bootstrap 构造函数. + * @param array $config - 配置信息 + */ + public function __construct($config) { + parent::__construct($config); + // bootstrapJs文件路径 + $this->bootstrapJs = Tools::getSubValue('bootstrap_js', $config, $this->bootstrapJs); + // jQueryJs文件路径 + $this->jQueryJs = Tools::getSubValue('jquery_js', $config, $this->jQueryJs); + // 自定义js + $this->customJs .= Tools::getSubValue('custom_js', $config, ''); + // bootstrapCSS文件路径 + $this->bootstrapCss = Tools::getSubValue('bootstrap_css', $config, $this->bootstrapCss); + // 自定义CSS + $this->customCss .= Tools::getSubValue('custom_css', $config, ''); + // 合并CSS + $this->_getCss(); + // 合并JS + $this->_getJs(); + } + + /** + * 输出HTML + * @param int $type - 方法过滤,默认只获取 public类型 方法 + * ReflectionMethod::IS_STATIC + * ReflectionMethod::IS_PUBLIC + * ReflectionMethod::IS_PROTECTED + * ReflectionMethod::IS_PRIVATE + * ReflectionMethod::IS_ABSTRACT + * ReflectionMethod::IS_FINAL + * @return string + */ + public function getHtml($type = \ReflectionMethod::IS_PUBLIC) { + $data = $this->getApiDoc($type); + $html = << + + + + + + + + API文档 By Api-Doc-PHP + {$this->customCss} + + +
+ +
+
{$this->_getDocList($data)}
+
+
+
+ 版权的撒打算 +
+
+
+ {$this->customJs} + + +EXT; + + if (isset($_GET['download']) && $_GET['download'] === 'api_doc_php') { + Tools::downloadFile($html); + return true; + } + return $html; + } + + /** + * 解析return 并生成HTML + * @param array $data + * @return string + */ + private function _getReturnData($data = []) { + $html = ''; + if (!is_array($data) || count($data) < 1) { + return $html; + } + $html .= '

返回参数

'; + $html .= ''; + foreach ($data as $v) { + $html .= ' + + + + '; + } + $html .= '
参数类型描述
' . Tools::getSubValue('return_name', $v, '') . '' . Tools::getSubValue('return_type', $v, '') . '' . Tools::getSubValue('return_title', $v, '') . '
'; + return $html; + } + + /** + * 解析param 并生成HTML + * @param array $data + * @return string + */ + private function _getParamData($data = []) { + $html = ''; + if (!is_array($data) || count($data) < 1) { + return $html; + } + $html .= '

请求参数

'; + $html .= ''; + foreach ($data as $v) { + $html .= ' + + + + + + '; + } + $html .= '
参数类型描述默认值是否必须
' . Tools::getSubValue('param_name', $v, '') . '' . Tools::getSubValue('param_type', $v, '') . '' . Tools::getSubValue('param_title', $v, '') . '' . Tools::getSubValue('param_default', $v, '无默认值') . '' . Tools::getSubValue('param_require', $v, '非必须') . '
'; + return $html; + } + + /** + * 解析code 并生成HTML + * @param array $data + * @return string + */ + private function _getCodeData($data = []) { + $html = ''; + if (!is_array($data) || count($data) < 1) { + return $html; + } + $html .= '

状态码说明

'; + $html .= ''; + foreach ($data as $v) { + $html .= ' + + + '; + } + $html .= '
状态码描述
' . Tools::getSubValue('code', $v, '') . '' . Tools::getSubValue('content', $v, '暂无说明') . '
'; + return $html; + } + + /** + * 获取指定接口操作下的文档信息 + * @param $className - 类名 + * @param $actionName - 操作名 + * @param $actionItem - 接口数据 + * @return string + */ + private function _getActionItem($className, $actionName, $actionItem) { + $html = << +

API - {$actionItem['title']}

+

请求方式: + {$actionItem['method']} +

+

请求地址:{$actionItem['url']}

+ {$this->_getParamData(Tools::getSubValue('param', $actionItem, []))} + {$this->_getReturnData(Tools::getSubValue('return', $actionItem, []))} + {$this->_getCodeData(Tools::getSubValue('code', $actionItem, []))} + +EXT; + return $html; + } + + /** + * 获取指定API类的文档HTML + * @param $className - 类名称 + * @param $classItem - 类数据 + * @return string + */ + private function _getClassItem($className, $classItem) { + $title = Tools::getSubValue('title', $classItem, '未命名'); + $actionHtml = ''; + if (isset($classItem['action']) && is_array($classItem['action']) && count($classItem['action']) >= 1) { + foreach ($classItem['action'] as $actionName => $actionItem) { + $actionHtml .= $this->_getActionItem($className, $actionName, $actionItem); + } + } + $html = << +

{$title}

+
{$actionHtml}
+ +EXT; + return $html; + } + + /** + * 获取API文档HTML + * @param array $data - 文档数据 + * @return string + */ + private function _getDocList($data) { + $html = ''; + if (count($data) < 1) { + return $html; + } + $html .= '
'; + foreach ($data as $className => $classItem) { + $html .= $this->_getClassItem($className, $classItem); + } + $html .= '
'; + return $html; + } + + /** + * 获取顶部导航HTML + * @param $data -API文档数据 + * @return string + */ + private function _getTopNavList($data) { + $html = ''; + return $html; + } + + /** + * 获取文档CSS + * @return string + */ + private function _getCss() { + $path = realpath($this->bootstrapCss); + if (!$path || !is_file($path)) { + return $this->customCss; + } + $bootstrapCss = file_get_contents($path); + if (empty($bootstrapCss)) { + return $this->customCss; + } + $this->customCss = '' . $this->customCss; + // $this->customCss = ' ' . $this->customCss; + return $this->customCss; + } + + /** + * 获取文档JS + * @return string + */ + private function _getJs() { + // $js = ''; + // $js .= ''; + // $this->customJs = $js . $this->customJs; + // return $this->customJs; + $bootstrapJs = realpath($this->bootstrapJs); + $jQueryJs = realpath($this->jQueryJs); + if (!$bootstrapJs || !$jQueryJs || !is_file($bootstrapJs) || !is_file($jQueryJs)) { + $this->customJs = ''; + return $this->customCss; + } + $bootstrapJs = file_get_contents($bootstrapJs); + $jQueryJs = file_get_contents($jQueryJs); + if (empty($bootstrapJs) || empty($jQueryJs)) { + $this->customJs = ''; + return $this->customJs; + } + $js = '' . ''; + $this->customJs = $js . $this->customJs; + return $this->customJs; + } +} \ No newline at end of file