Skip to content

Commit af66eaa

Browse files
author
jay
committed
fixed
1 parent 0e5e939 commit af66eaa

File tree

20 files changed

+57
-200
lines changed

20 files changed

+57
-200
lines changed

.gitignore

100755100644
File mode changed.

Entrance/DocumentBundle/Command/GenerateDocumentConfigCommand.php

Lines changed: 0 additions & 124 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# 安装
22
composer require phpzlc/document-bundle
3+
4+
# 生成文档
5+
php bin/console phpzlc:generate:document
File renamed without changes.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Date: 2018/4/20
66
*/
77

8-
namespace PHPZlc\Document\Entrance\DocumentBundle\Command;
8+
namespace PHPZlc\Document\DocumentBundle\Command;
99

1010

1111
use Symfony\Component\Console\Command\Command;
@@ -20,15 +20,15 @@ abstract class Base extends Command
2020
*/
2121
protected $io;
2222

23-
protected $command_pre = 'unicorn:';
23+
protected $command_pre = 'phpzlc:';
2424

25-
protected $description_pre = '[Unicorn] ';
25+
protected $description_pre = '[phpzlc] ';
2626

2727
protected $document_config = 'document.config.php';
2828

2929
protected function getRootPath()
3030
{
31-
return dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
31+
return dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))));
3232
}
3333

3434
protected function interact(InputInterface $input, OutputInterface $output)

Entrance/DocumentBundle/Command/GenerateDocumentCommand.php renamed to src/DocumentBundle/Command/GenerateDocumentCommand.php

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@
55
* Date: 2018/5/2
66
*/
77

8-
namespace PHPZlc\Document\Entrance\DocumentBundle\Command;
8+
namespace PHPZlc\Document\DocumentBundle\Command;
99

1010

11+
use App\Document\Config;
1112
use Doctrine\Common\Annotations\AnnotationReader;
13+
use Doctrine\DBAL\Connection;
14+
use MongoDB\Driver\Command;
1215
use PHPZlc\Document\Document;
1316
use Symfony\Component\Console\Input\InputInterface;
1417
use Symfony\Component\Console\Output\OutputInterface;
1518
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher;
1619

1720
class GenerateDocumentCommand extends Base
1821
{
22+
/**
23+
* @var Connection|null
24+
*/
25+
private $connection;
26+
27+
public function __construct(Connection $connection = null)
28+
{
29+
parent::__construct();
30+
$this->connection = $connection;
31+
}
32+
1933
private $vars = array(
2034
'$' => '$'
2135
);
@@ -32,46 +46,7 @@ public function configure()
3246
{
3347
$this
3448
->setName($this->command_pre . 'generate:document')
35-
->setDescription($this->description_pre . '生成API文档')
36-
->setHelp(<<<EOF
37-
<info>%command.name%</info> 生成API文档
38-
<comment>[注意]在生成文档之前需先运行生成API配置文件:</comment>
39-
<info>{$this->command_pre}generate:document:config</info>
40-
<comment>API写在哪?</comment>
41-
<info>1:建立src/*Bundle/Document目录</info>
42-
<info>2:在建立好的目录中建立*Document.php类文件</info>
43-
<info>3:在类文件的中建立名为*Action的方法,在其之上书写注释</info>
44-
<comment>目录结构示例</comment>
45-
<info>src
46-
----BaseBundle
47-
--------Document
48-
------------IndexDocument.php</info>
49-
<comment>代码示例</comment>
50-
<info>
51-
<?php
52-
namespace BaseBundle\Document;
53-
54-
use PHPZlc\Document\Document;
55-
56-
class IndexDocument extends Document
57-
{
58-
/**
59-
* @throws \Exception
60-
*/
61-
function indexAction()
62-
{
63-
{$this->vars['$']}this->add()
64-
->setTitle('测试')
65-
->setUrl('www.***.com/api/beta')
66-
->setMethod('post')
67-
->generate();
68-
}
69-
}
70-
71-
?>
72-
</info>
73-
EOF
74-
);
49+
->setDescription($this->description_pre . '生成API文档');
7550
;
7651
}
7752

@@ -83,17 +58,18 @@ public function execute(InputInterface $input, OutputInterface $output)
8358
$this->globalConfig();
8459

8560
//TODO 接口数据
86-
foreach ($this->getDocumentClassArray($this->getRootPath() . '/src/') as $document) {
61+
foreach ($this->getDocumentClassArray($this->getRootPath() . '/src/Document') as $document) {
8762
$this->reader($document);
8863
}
64+
8965
$this->actionsArrange();
9066

9167
//TODO 代码生成
9268

9369
//>1 目录资源重置
9470
exec('rm -rf ' . $this->rootApiDir());
9571
mkdir($this->rootApiDir());
96-
exec('cp -rf ' . __DIR__ . '/../../../Resources/Default/ApiDoc/* ' . $this->rootApiDir() . '/');
72+
exec('cp -rf ' . __DIR__ . '/../Resources/Default/ApiDoc/* ' . $this->rootApiDir() . '/');
9773

9874
//>2 生成静态页面
9975
$this->generateIndexFile();
@@ -107,44 +83,49 @@ public function execute(InputInterface $input, OutputInterface $output)
10783
exec('cd ' . $this->rootApiDir() .'; zip -r ' . $this->rootApiDir() . '/' . $this->jsonToArray($this->global)['title'] . 'API文档.zip .');
10884

10985
$this->io->success('生成成功');
110-
return;
86+
87+
return \Symfony\Component\Console\Command\Command::SUCCESS;
11188
}
11289

11390
private function globalConfig()
11491
{
115-
$globalConfigPath = $this->getRootPath() . '/app/config/' . $this->document_config;
116-
117-
if(!is_file($globalConfigPath)){
118-
$this->io->error('配置文件不存在 请用--help得到帮助');
119-
exit;
92+
$global['title'] = Config::$title;
93+
$global['publishing'] = Config::$publishing;
94+
$global['domain'] = isset($_ENV['DOC_HOST']) ? $_ENV['DOC_HOST'] : '';
95+
$global['explain'] = Config::$explain;
96+
$global['note'] = Config::$note;
97+
$global['appendix'] = Config::$appendix;
98+
99+
foreach ($global as $key => $value){
100+
if(empty($value)){
101+
$global[$key] = '';
102+
}
120103
}
121104

122-
include_once $globalConfigPath;
105+
$database_url = isset($_ENV['DATABASE_URL']) ? $_ENV['DATABASE_URL'] : '';
123106

124-
$global['title'] = isset($title) ? $title : '';
125-
$global['publishing'] = isset($publishing) ? $publishing : '';
126-
$global['domain'] = isset($domain) ? $domain : '';
127-
$global['explain'] = isset($explain) ? $explain : '';
128-
$global['note'] = isset($note) ? $note : '';
129-
$global['appendix'] = isset($appendix) ? $appendix : '';
130-
131-
$this->config['database_host'] = isset($database_host) ? $database_host : '';
132-
$this->config['database_name'] = isset($database_name) ? $database_name : '';
133-
$this->config['database_user_name'] = isset($database_user_name) ? $database_user_name : '';
134-
$this->config['database_password'] = isset($database_password) ? $database_password : '';
107+
$this->config['database_host'] = $this->connection->getHost();
108+
$this->config['database_name'] = $this->connection->getDatabase();
109+
$this->config['database_user_name'] = $this->connection->getUsername();
110+
$this->config['database_password'] = $this->connection->getPassword();
135111

136112
if(empty($global['title'])) {
137113
$this->io->error('文档标题不能为空');
138114
exit;
139115
}
140116

117+
if(empty($global['domain'])) {
118+
$this->io->error('根地址不能为空');
119+
exit;
120+
}
121+
141122
$this->global = json_encode($global);
142123
}
143124

144125

145126
private function rootApiDir()
146127
{
147-
return $this->getRootPath() . '/web/apidoc';
128+
return $this->getRootPath() . '/public/apidoc';
148129
}
149130

150131
/**
@@ -159,14 +140,11 @@ private function getDocumentClassArray($dir_name)
159140
$return_array = [];
160141
if(!empty($arr)) {
161142
foreach ($arr as $value) {
162-
if(strpos($value, 'Bundle') !== false){
163-
$return_array = array_merge($return_array , $this->getDocumentClassArray($dir_name . '/' . $value));
164-
}
165-
if(strpos($value, 'Document') !== false ){
166-
if(is_dir($dir_name . '/' . $value)){
167-
$return_array = array_merge($return_array , $this->getDocumentClassArray($dir_name . '/' . $value));
168-
} else {
169-
$return_array[] = str_replace('/' ,'\\' , str_replace($this->getRootPath() . '/src/', '', $dir_name .'/'. rtrim($value, '.php')));
143+
if(is_file($dir_name . '/' . $value) && strpos($value, 'Document') !== false){
144+
$return_array[] = str_replace('/' ,'\\' , str_replace($this->getRootPath() . '/src/', '', 'App/'. $dir_name .'/'. rtrim($value, '.php')));
145+
}elseif(is_dir($dir_name . '/' . $value)){
146+
if(!in_array($value, ['.', '..'])) {
147+
$return_array = array_merge($return_array, $this->getDocumentClassArray($dir_name . '/' . $value));
170148
}
171149
}
172150
}
@@ -189,7 +167,7 @@ function reader($document)
189167

190168
if($class instanceof Document){
191169
foreach ($reflClass->getMethods() as $action){
192-
if(strpos($action->getName(), 'Action') !== false && strpos($action->__toString(), str_replace('\\', '/', $document)) !== false){
170+
if(strpos($action->getName(), 'Action') !== false && strpos($action->__toString(), str_replace('App/', '', str_replace('\\', '/', $document))) !== false){
193171
$method = $action->getName();
194172
$class->$method();
195173
}

Entrance/DocumentBundle/DocumentBundle.php renamed to src/DocumentBundle/DocumentBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace PHPZlc\Document\Entrance\DocumentBundle;
3+
namespace PHPZlc\Document\DocumentBundle;
44

55
use Symfony\Component\HttpKernel\Bundle\Bundle;
66

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)