CountController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * User: bluey
  4. * Date: 2019/1/10
  5. * Time: 17:47
  6. */
  7. namespace App\Doc\Controllers;
  8. use App\Base\Controllers\Controller;
  9. use function GuzzleHttp\Psr7\str;
  10. class CountController extends Controller
  11. {
  12. /**
  13. * 获取文件列表
  14. * @param $dir
  15. * @return array
  16. */
  17. protected function file_list($dir)
  18. {
  19. $arr = [];
  20. $dir_handle = opendir($dir);
  21. if ($dir_handle) {
  22. while (($file = readdir($dir_handle)) !== false) {
  23. if ($file === '.' || $file === '..') {
  24. continue;
  25. }
  26. $tmp = realpath($dir . '/' . $file);
  27. if (is_dir($tmp)) {
  28. if (in_array($file, ['Doc', 'Facades', 'Traits', 'Api', 'Providers', 'Base', 'Console', 'Jobs'])) {
  29. continue;
  30. }
  31. $retArr = $this->file_list($tmp);
  32. if (!empty($retArr)) {
  33. $arr = array_merge($arr, $retArr);
  34. }
  35. } else {
  36. if (in_array($file, ['routes.php', 'README.MD', 'WebSocketHandler.php'])) {
  37. continue;
  38. }
  39. $arr[] = $tmp;
  40. }
  41. }
  42. closedir($dir_handle);
  43. }
  44. return $arr;
  45. }
  46. /**
  47. * 解析PHP文件
  48. * @param $file - 文件绝对路径
  49. * @return string
  50. */
  51. protected function parseDoc($file)
  52. {
  53. $doc = '';
  54. $content = file_get_contents($file);
  55. $tokens = token_get_all($content);
  56. $total = count($tokens);
  57. for ($i = 0; $i < $total; $i++) {
  58. if (is_string($tokens[$i])) {
  59. $doc .= $tokens[$i];
  60. } else {
  61. if ($tokens[$i][0] != T_COMMENT && $tokens[$i][0] != T_DOC_COMMENT) {
  62. $doc .= $tokens[$i][1];
  63. }
  64. }
  65. }
  66. return str_replace("\r\n\r\n", "\r\n", $doc);
  67. }
  68. /**
  69. * 统计文件列表
  70. */
  71. public function doc()
  72. {
  73. set_time_limit(0);
  74. $zhList = [];
  75. $dir = str_replace(lcfirst(__CLASS__) . ".php", "app\\", __FILE__);
  76. $fileList = $this->file_list($dir);
  77. $curFile = '';
  78. foreach ($fileList as $file) {
  79. $path = str_replace($dir, "", $file);
  80. if ($curFile != $file) {
  81. $zhList[] = '## ' . $path;
  82. }
  83. $content = $this->parseDoc($file);
  84. // 逐行解析数据
  85. $lines = explode("\r\n", $content);
  86. if (1 == count($lines)) {
  87. $lines = explode("\n", $content);
  88. }
  89. $zhList[] = '~~~';
  90. $bFind = false;
  91. foreach ($lines as $line) {
  92. $newline = trim($line);
  93. if ($newline == "") {
  94. continue;
  95. }
  96. if (strlen($newline) != mb_strlen($newline)) {
  97. if (false !== strpos($newline, 'ApiException') ||
  98. false !== strpos($newline, 'transL')) {
  99. continue;
  100. }
  101. $bFind = true;
  102. $zhList[] = $newline . "\n";
  103. }
  104. }
  105. if ($bFind) {
  106. $zhList[] = '~~~';
  107. } else {
  108. array_pop($zhList);
  109. array_pop($zhList);
  110. }
  111. }
  112. header("Content-type:application/octet-stream");
  113. header("Accept-Ranges:bytes");
  114. header("Content-Disposition:attachment;filename=多语言关联文件.md");
  115. header("Expires: 0");
  116. header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
  117. header("Pragma:public");
  118. echo implode("\n", $zhList);
  119. }
  120. }