Parser.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\doc;
  3. class Parser
  4. {
  5. /**
  6. * 解析类
  7. * @param $object
  8. *
  9. * @return array
  10. */
  11. public function parse_class($object)
  12. {
  13. $list = $this->parseCommentArray($this->comment2Array($object));
  14. return $list;
  15. }
  16. /**
  17. * @param \ReflectionClass $object
  18. *
  19. * @return array|bool
  20. */
  21. public function parse_action($object)
  22. {
  23. $comment = $this->parseCommentArray($this->comment2Array($object));
  24. if (!isset($comment['url']) || !$comment['url']) {
  25. $comment['url'] = $this->buildUrl($object);
  26. }
  27. if (!isset($comment['method']) || !$comment['method']) {
  28. $comment['method'] = 'GET';
  29. }
  30. $comment['href'] = "{$object->class}::{$object->name}";
  31. return $comment;
  32. }
  33. /**
  34. * @title 生成请求地址
  35. * @param \ReflectionClass $object
  36. * @return mixed
  37. */
  38. private function buildUrl($object)
  39. {
  40. $_arr = explode('\\', $object->class);
  41. array_shift($_arr);
  42. $a = array_shift($_arr);
  43. array_shift($_arr);
  44. $url = $a.DS.implode('.',$_arr).DS.$object->name;
  45. $url = \think\facade\Route::buildUrl($url)->domain(true)->suffix(false)->build();
  46. return $url;
  47. }
  48. /**
  49. * 注释字符串转数组
  50. *
  51. * @param string $comment
  52. *
  53. * @return array
  54. */
  55. private function comment2Array($comment = '')
  56. {
  57. // 多空格转换成单空格
  58. $comment = str_replace(':', ' ', $comment);
  59. $comment = preg_replace('/[ ]+/', ' ', $comment);
  60. preg_match_all('/[\*+][\s+]?@(.*?)[\n|\r]/is', $comment, $matches);
  61. $arr = [];
  62. foreach ($matches[1] as $key => $match) {
  63. $arr[$key] = explode(' ', $match);
  64. }
  65. return $arr;
  66. }
  67. /**
  68. * 解析注释数组
  69. *
  70. * @param array $array
  71. *
  72. * @return array
  73. */
  74. private function parseCommentArray(array $array = [])
  75. {
  76. $newArr = [];
  77. foreach ($array as $item) {
  78. $item[0] = trim(strtolower($item[0]),":");
  79. switch (trim(strtolower($item[0]),":")) {
  80. case 'title':
  81. case 'desc':
  82. case 'version':
  83. case 'author':
  84. default:
  85. $newArr[$item[0]] = isset($item[1]) ? $item[1] : '-';
  86. break;
  87. case 'url':
  88. @eval('$newArr["url"]=(' . $item[1] . ');');
  89. break;
  90. case 'icon':
  91. //默认使用font-awesome图标
  92. $first = isset($item[1])&&isset($item[2])?$item[1]:"fa";
  93. $second = isset($item[2])?$item[2]:(isset($item[1])?$item[1]:"fa-leaf");
  94. $newArr[$item[0]] = $first." ".$second;
  95. break;
  96. case 'param':
  97. case 'return':
  98. $newArr[$item[0]][] = [
  99. 'type' => trim($item[1],"\{\\\}"),
  100. 'name' => isset($item[2])?trim($item[2],"\{\\\}"):'',
  101. 'default' => isset($item[3]) ? trim($item[3],"\{\\\}") : '-',
  102. 'desc' => isset($item[4]) ? trim($item[4],"\{\\\}") : '-'
  103. ];
  104. break;
  105. }
  106. }
  107. if(empty($newArr['icon'])){
  108. $newArr['icon'] = "fa fa-leaf";
  109. }
  110. return $newArr;
  111. }
  112. }