Skip to content

Commit 9e09862

Browse files
committed
增加生成离线文档
1 parent 7f04d79 commit 9e09862

File tree

10 files changed

+95
-23
lines changed

10 files changed

+95
-23
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ composer create-project phpyii/php-apidoc:dev-master
2727

2828
3、src/app下为接口项目
2929

30+
31+
生成离线文档
32+
------------
33+
34+
php ./public/index.php
35+
36+
脚本执行完毕!离线文档保存路径为:/path/to/apidoc/public/docs
37+
3038
效果图
3139
------------
3240
<img src="https://camo.githubusercontent.com/25c9525feb1c3a6357f235a7cf136d3aea3bf60d/687474703a2f2f63646e372e6f6b61796170692e636f6d2f7965737965736170695f32303230303331303232353935325f64333139636331393761333166386633353232613832363433626633316436302e706e67" alt="" data-canonical-src="http://cdn7.okayapi.com/yesyesapi_20200310225952_d319cc197a31f8f3522a82643bf31d60.png" style="max-width:100%;">

public/index.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@
4141
$listTpl = API_ROOT . '/src/view/docs/api_list_tpl.php';
4242

4343
if (substr(PHP_SAPI, 0, 3) == 'cli') {
44+
$theme = 'fold';
45+
if(count($argv) > 1){
46+
$theme = $argv[1] == 'fold' ? 'fold' : 'expand';
47+
}
4448
// 生成离线文档
45-
echo '暂未实现';exit;
46-
//$apiHtml = new \PhalApi\Helper\ApiStaticCreate($projectName, 'fold', $detailTpl);
47-
//$apiHtml->render($listTpl);
49+
$apiHtml = new core\ApiStaticCreate($projectName, $theme, $detailTpl);
50+
$apiHtml->render($listTpl);
4851
} else if (!empty($_GET['detail'])) {
4952
checkViewCode();
5053

src/app/api2/Ip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Ip extends \core\Api {
1616

1717
public function getRules() {
1818
return array(
19-
'/service/getIpInfo2' => array(
19+
'getIp' => array(
2020
'title' => 'ip查询接口',
2121
'desc' => 'ip查询接口描述',
2222
'url' => '/service/getIpInfo2.php',

src/app/api2/Test.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,4 @@ public function getRules() {
4747
);
4848
}
4949

50-
// /**
51-
// * 测试
52-
// * @desc 测试接口
53-
// * @api_url api/test
54-
// * @return int user_id 新账号的ID
55-
// */
56-
// public function test() {
57-
// return array('user_id' => 111);
58-
// }
5950
}

src/app/api2/Test2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class Test2 extends \core\Api {
1010

1111
public function getRules() {
1212
return array(
13-
'/project/livedetaillist' => array(
13+
'livedetaillist' => array(
1414
'method' => 'post',
15-
//'url' => '/test',
15+
'url' => '/project/livedetaillist',
1616
'title' => '测试接口',
1717
'desc' => '测试接口描述',
1818
//'exceptions' => array('401'=> '异常了'),

src/core/ApiList.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public function render($tplPath = NULL) {
122122
}
123123
}
124124
// 主题风格,fold = 折叠,expand = 展开
125-
$theme = isset($_GET['type']) ? $_GET['type'] : 'fold';
125+
//if (empty($theme) || substr(PHP_SAPI, 0, 3) != 'cli') {
126+
$theme = isset($_GET['type']) ? $_GET['type'] : 'fold';
127+
//}
126128
if (!in_array($theme, array('fold', 'expand'))) {
127129
$theme = 'fold';
128130
}

src/core/ApiStaticCreate.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
namespace core;
10+
11+
/**
12+
* Description of ApiStaticCreate
13+
*
14+
* @author 最初的梦想
15+
*/
16+
class ApiStaticCreate extends ApiList {
17+
18+
protected $webRoot = '';
19+
protected $theme = '';
20+
protected $detailTplPath = NULL;
21+
22+
public function __construct($projectName, $theme = 'fold', $detailTplPath = NULL) {
23+
parent::__construct($projectName);
24+
$this->theme = $theme;
25+
if (!empty($detailTplPath)) {
26+
$this->detailTplPath = $detailTplPath;
27+
}
28+
}
29+
30+
public function render($tplPath = NULL) {
31+
$theme = $this->theme;
32+
$trace = debug_backtrace();
33+
$listFilePath = $trace[0]['file'];
34+
$this->webRoot = substr($listFilePath, 0, strrpos($listFilePath, D_S));
35+
ob_start();
36+
// 运行模式
37+
parent::render($tplPath);
38+
$string = ob_get_clean();
39+
\core\saveHtml($this->webRoot, 'index', $string);
40+
$str = "
41+
脚本执行完毕!离线文档保存路径为:
42+
";
43+
$str .= $this->webRoot;
44+
echo $str . D_S . 'docs', PHP_EOL, PHP_EOL;
45+
}
46+
47+
public function makeApiServiceLink($service, $theme = '') {
48+
ob_start();
49+
// 换一种更优雅的方式
50+
\PhalApi\DI()->request = new \PhalApi\Request(array('service' => $service));
51+
$apiDesc = new ApiDesc($this->projectName);
52+
$apiDesc->render($this->detailTplPath);
53+
54+
$string = ob_get_clean();
55+
\core\saveHtml($this->webRoot, $service, $string);
56+
$link = $service . '.html';
57+
return $link;
58+
}
59+
60+
public function getUri() {
61+
return '';
62+
}
63+
64+
public function makeThemeButton($theme) {
65+
return '';
66+
}
67+
68+
}

src/view/docs/api_desc_tpl.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<link rel="stylesheet" href="{$semanticPath}components/label.min.css">
2828
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
2929
30-
<script src="/static/jquery.min.js"></script>
31-
<script src="/static/jquery.cookie.min.js"></script>
30+
<script src="../static/jquery.min.js"></script>
31+
<script src="../static/jquery.cookie.min.js"></script>
3232
</head>
3333
3434
<body>
@@ -344,8 +344,8 @@
344344
</div>
345345
346346
<!-- 代码高亮 -->
347-
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/default.min.css">
348-
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
347+
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/highlight.js/9.13.1/styles/default.min.css">
348+
<script src="https://cdn.bootcdn.net/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
349349
<script>hljs.initHighlightingOnLoad();</script>
350350
351351
EOT;

src/view/docs/api_list_tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<link rel="stylesheet" href="<?php echo $semanticPath; ?>semantic.min.css">
1616
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
1717

18-
<script src="/static/jquery.min.js"></script>
18+
<script src="../static/jquery.min.js"></script>
1919
<script src="<?php echo $semanticPath; ?>semantic.min.js"></script>
2020
<meta name="robots" content="none"/>
2121
</head>

src/view/docs/check_view_code.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<link rel="stylesheet" href="<?php echo $semanticPath; ?>components/label.min.css">
2929
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
3030

31-
<script src="/static/jquery.min.js"></script>
32-
<script src="/static/jquery.cookie.min.js"></script>
31+
<script src="../static/jquery.min.js"></script>
32+
<script src="../static/jquery.cookie.min.js"></script>
3333

3434

3535
<style type="text/css">

0 commit comments

Comments
 (0)