44 * User: Jay
55 * Date: 2018/5/2
66 */
7-
87namespace PHPZlc \Document \DocumentBundle \Command ;
98
10-
119use App \Document \Config ;
1210use Doctrine \Common \Annotations \AnnotationReader ;
1311use Doctrine \DBAL \Connection ;
14- use MongoDB \Driver \Command ;
1512use PHPZlc \Document \Document ;
1613use Symfony \Component \Console \Input \InputInterface ;
1714use Symfony \Component \Console \Output \OutputInterface ;
15+ use Symfony \Component \Filesystem \Filesystem ;
1816use Symfony \Component \Routing \Matcher \RedirectableUrlMatcher ;
17+ use ZanySoft \Zip \Zip ;
1918
2019class GenerateDocumentCommand extends Base
2120{
@@ -24,10 +23,16 @@ class GenerateDocumentCommand extends Base
2423 */
2524 private $ connection ;
2625
26+ /**
27+ * @var Filesystem;
28+ */
29+ private $ fileSystem ;
30+
2731 public function __construct (Connection $ connection = null )
2832 {
2933 parent ::__construct ();
3034 $ this ->connection = $ connection ;
35+ $ this ->fileSystem = new Filesystem ();
3136 }
3237
3338 private $ vars = array (
@@ -58,18 +63,18 @@ public function execute(InputInterface $input, OutputInterface $output)
5863 $ this ->globalConfig ();
5964
6065 //TODO 接口数据
61- foreach ($ this ->getDocumentClassArray ($ this ->getRootPath () . ' / src/ Document ' ) as $ document ) {
66+ foreach ($ this ->getDocumentClassArray ($ this ->getRootPath () . DIRECTORY_SEPARATOR . ' src ' . DIRECTORY_SEPARATOR . ' Document ' ) as $ document ) {
6267 $ this ->reader ($ document );
6368 }
64-
69+
6570 $ this ->actionsArrange ();
6671
6772 //TODO 代码生成
6873
6974 //>1 目录资源重置
70- exec ( ' rm -rf ' . $ this ->rootApiDir ());
71- mkdir ($ this ->rootApiDir ());
72- exec ( ' cp -rf ' . __DIR__ . '/../Resources/ Default/ApiDoc/* ' . $ this ->rootApiDir () . ' / ' );
75+ $ this -> fileSystem -> remove ( $ this ->rootApiDir ());
76+ $ this -> fileSystem -> mkdir ($ this ->rootApiDir ());
77+ $ this -> fileSystem -> mirror ( dirname ( __DIR__ ) . DIRECTORY_SEPARATOR . ' Resources ' . DIRECTORY_SEPARATOR . 'Default ' . DIRECTORY_SEPARATOR . ' ApiDoc ' , $ this ->rootApiDir () . DIRECTORY_SEPARATOR );
7378
7479 //>2 生成静态页面
7580 $ this ->generateIndexFile ();
@@ -80,7 +85,8 @@ public function execute(InputInterface $input, OutputInterface $output)
8085 $ this ->generateDebugFile ();
8186
8287 //>3 生成打包件
83- exec ('cd ' . $ this ->rootApiDir () .'; zip -r ' . $ this ->rootApiDir () . '/ ' . $ this ->jsonToArray ($ this ->global )['title ' ] . 'API文档.zip . ' );
88+ $ zip = Zip::create ($ this ->rootApiDir () . DIRECTORY_SEPARATOR . $ this ->jsonToArray ($ this ->global )['title ' ] . 'API文档.zip ' );
89+ $ zip ->add ($ this ->rootApiDir ());
8490
8591 $ this ->io ->success ('生成成功 ' );
8692
@@ -123,7 +129,7 @@ private function globalConfig()
123129
124130 private function rootApiDir ()
125131 {
126- return $ this ->getRootPath () . ' / public/ apidoc ' ;
132+ return $ this ->getRootPath () . DIRECTORY_SEPARATOR . ' public ' . DIRECTORY_SEPARATOR . ' apidoc ' ;
127133 }
128134
129135 /**
@@ -138,11 +144,11 @@ private function getDocumentClassArray($dir_name)
138144 $ return_array = [];
139145 if (!empty ($ arr )) {
140146 foreach ($ arr as $ value ) {
141- if (is_file ($ dir_name . ' / ' . $ value ) && strpos ($ value , 'Document ' ) !== false ){
142- $ return_array [] = str_replace (' / ' ,'\\' , str_replace ($ this ->getRootPath () . ' / src/ ' , '' , 'App/ ' . $ dir_name .' / ' . rtrim ($ value , '.php ' )));
143- }elseif (is_dir ($ dir_name . ' / ' . $ value )){
147+ if (is_file ($ dir_name . DIRECTORY_SEPARATOR . $ value ) && strpos ($ value , 'Document ' ) !== false ){
148+ $ return_array [] = str_replace (DIRECTORY_SEPARATOR ,'\\' , str_replace ($ this ->getRootPath () . DIRECTORY_SEPARATOR . ' src ' . DIRECTORY_SEPARATOR , '' , 'App ' . DIRECTORY_SEPARATOR . $ dir_name . DIRECTORY_SEPARATOR . rtrim ($ value , '.php ' )));
149+ }elseif (is_dir ($ dir_name . DIRECTORY_SEPARATOR . $ value )){
144150 if (!in_array ($ value , ['. ' , '.. ' ])) {
145- $ return_array = array_merge ($ return_array , $ this ->getDocumentClassArray ($ dir_name . ' / ' . $ value ));
151+ $ return_array = array_merge ($ return_array , $ this ->getDocumentClassArray ($ dir_name . DIRECTORY_SEPARATOR . $ value ));
146152 }
147153 }
148154 }
@@ -164,8 +170,8 @@ function reader($document)
164170 $ class = new $ document ();
165171
166172 if ($ class instanceof Document){
167- foreach ($ reflClass ->getMethods () as $ action ){
168- if (strpos ($ action ->getName (), 'Action ' ) !== false && strpos ($ action ->__toString (), str_replace ('App/ ' , '' , str_replace ('\\' , ' / ' , $ document ))) !== false ){
173+ foreach ($ reflClass ->getMethods () as $ action ) {
174+ if (strpos ($ action ->getName (), 'Action ' ) !== false && strpos ($ action ->__toString (), str_replace ('App ' . DIRECTORY_SEPARATOR , '' , str_replace ('\\' , DIRECTORY_SEPARATOR , $ document ))) !== false ){
169175 $ method = $ action ->getName ();
170176 $ class ->$ method ();
171177 }
0 commit comments