DocService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Doc\Services;
  3. use Illuminate\Support\Facades\Log;
  4. class DocService
  5. {
  6. private $data = [];
  7. /**
  8. * 获取文档分组菜单
  9. * @return array
  10. */
  11. public function group()
  12. {
  13. return include base_path('config/doc.php');
  14. }
  15. /**
  16. * 获取模块接口详情
  17. * @param $data
  18. * @return array
  19. */
  20. public function detail($data)
  21. {
  22. if (!isset($data['module'])) {
  23. return [];
  24. }
  25. $module = $data['module'];
  26. $content = file_get_contents(storage_path('doc/' . $module . '.json'));
  27. $content = json_decode($content, true);
  28. return $content;
  29. }
  30. /**
  31. * 根据路径生成文档
  32. * @param $path
  33. */
  34. public function make($path)
  35. {
  36. $this->delete();
  37. $files = $this->getAllFiles($path);
  38. foreach ($files as $file) {
  39. if (strpos($file, '.php') == false) {
  40. continue;
  41. }
  42. if (strpos($file, 'routes.php') != false) {
  43. continue;
  44. }
  45. if (strpos($file, 'helpers.php') != false) {
  46. continue;
  47. }
  48. if (strpos($file, 'Controller.php') == false) {
  49. continue;
  50. }
  51. $class = $this->getClassByPath($file);
  52. $annotation = new AnnotationService($class);
  53. $document = $annotation->getAllDocComment();
  54. if (empty($document)) {
  55. continue;
  56. }
  57. if ($document) {
  58. $this->addData($document);
  59. }
  60. }
  61. $this->writeToFile(base_path('public').'/doc.json', $this->data);
  62. echo 'success'.PHP_EOL;
  63. }
  64. /**
  65. * 根据路径获取类名
  66. * @param $path
  67. * @return string
  68. */
  69. private function getClassByPath($path)
  70. {
  71. $basePath = base_path('app');
  72. $path = str_replace($basePath, 'App', $path);
  73. $path = str_replace("/", "\\", $path);
  74. $path = str_replace('.php', '', $path);
  75. return $path;
  76. }
  77. /**
  78. * 删除存储目录下所有生成的文件
  79. */
  80. private function delete()
  81. {
  82. $files = $this->getAllFiles(storage_path('doc'));
  83. foreach ($files as $file) {
  84. @unlink($file);
  85. }
  86. }
  87. /**
  88. * 遍历获取目录下的所有文件
  89. * @param $path
  90. * @param $files
  91. */
  92. private function getFiles($path, &$files)
  93. {
  94. if (is_dir($path)) {
  95. $dp = dir($path);
  96. while ($file = $dp->read()) {
  97. if ($file != "." && $file != "..") {
  98. // AudiencePool/Scene/Websocket/Workflow
  99. $this->getFiles($path . "/" . $file, $files);
  100. }
  101. }
  102. $dp->close();
  103. }
  104. if (is_file($path)) {
  105. $files[] = $path;
  106. }
  107. }
  108. /**
  109. * 递归获取目录下所有的文件 包含子目录
  110. * @param $dir
  111. * @return array
  112. */
  113. public function getAllFiles($dir)
  114. {
  115. $files = array();
  116. $this->getFiles($dir, $files);
  117. return $files;
  118. }
  119. /**
  120. * 写入文档
  121. * @param $document
  122. */
  123. private function write($document)
  124. {
  125. foreach ($document as $key=>$item) {
  126. $file = storage_path('doc/'.$key.'.json');
  127. if (!file_exists($file)) {
  128. $this->writeToFile($file);
  129. }
  130. $content = json_decode(file_get_contents($file), true);
  131. foreach ($item as $value) {
  132. $content[] = $value;
  133. }
  134. $this->writeToFile($file, $content);
  135. }
  136. }
  137. /**
  138. * 写入内容到文件
  139. * @param $file
  140. * @param array $content
  141. */
  142. private function writeToFile($file, $content=[])
  143. {
  144. $content = array_values($content);
  145. foreach ($content as &$item) {
  146. if (isset($item['list'])) {
  147. $item['list'] = array_values($item['list']);
  148. }
  149. }
  150. if (strpos($file, '.json')) {
  151. $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  152. } else {
  153. $content = "export default ".json_encode($content, JSON_UNESCAPED_UNICODE);
  154. }
  155. $file = fopen($file, 'w');
  156. fwrite($file, $content);
  157. fclose($file);
  158. }
  159. /**
  160. * 加入数据
  161. * @param $document
  162. */
  163. private function addData($document)
  164. {
  165. foreach ($document as $index=>$item) {
  166. if (isset($this->data[$index])) {
  167. foreach ($item['list'] as $list) {
  168. $this->data[$index]['list'][] = $list;
  169. }
  170. } else {
  171. $this->data[$index] = $document[$index];
  172. }
  173. }
  174. }
  175. }