Node.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\service\auth;
  3. use Doctrine\Common\Annotations\AnnotationException;
  4. use Doctrine\Common\Annotations\AnnotationReader;
  5. use Doctrine\Common\Annotations\AnnotationRegistry;
  6. use Doctrine\Common\Annotations\DocParser;
  7. use app\admin\service\annotation\ControllerAnnotation;
  8. use app\admin\service\annotation\NodeAnnotation;
  9. use app\admin\service\tool\CommonTool;
  10. use ReflectionException;
  11. /**
  12. * 节点处理类
  13. * Class Node
  14. * @package EasyAdmin\auth
  15. */
  16. class Node
  17. {
  18. /**
  19. * @var string 当前文件夹
  20. */
  21. protected string $basePath;
  22. /**
  23. * @var string 命名空间前缀
  24. */
  25. protected string $baseNamespace;
  26. /**
  27. * 构造方法
  28. * Node constructor.
  29. * @param string $basePath 读取的文件夹
  30. * @param string $baseNamespace 读取的命名空间前缀
  31. */
  32. public function __construct(string $basePath, string $baseNamespace)
  33. {
  34. $this->basePath = $basePath;
  35. $this->baseNamespace = $baseNamespace;
  36. return $this;
  37. }
  38. /**
  39. * 获取所有节点
  40. * @return array
  41. * @throws AnnotationException
  42. * @throws ReflectionException
  43. */
  44. public function getNodeList(): array
  45. {
  46. list($nodeList, $controllerList) = [[], $this->getControllerList()];
  47. if (!empty($controllerList)) {
  48. AnnotationRegistry::loadAnnotationClass('class_exists');
  49. $parser = new DocParser();
  50. $parser->setIgnoreNotImportedAnnotations(true);
  51. $reader = new AnnotationReader($parser);
  52. foreach ($controllerList as $controllerFormat => $controller) {
  53. // 获取类和方法的注释信息
  54. $reflectionClass = new \ReflectionClass($controller);
  55. $methods = $reflectionClass->getMethods();
  56. $actionList = [];
  57. // 遍历读取所有方法的注释的参数信息
  58. foreach ($methods as $method) {
  59. // 忽略掉不需要的节点
  60. $property = $reflectionClass->getProperty('ignoreNode');
  61. $propertyAttributes = $property->getAttributes(NodeAnnotation::class);
  62. if (!empty($propertyAttributes[0])) {
  63. $propertyAttribute = $propertyAttributes[0]->newInstance();
  64. if (in_array($method->name, $propertyAttribute->ignore)) continue;
  65. }
  66. $attributes = $reflectionClass->getMethod($method->name)->getAttributes(NodeAnnotation::class);
  67. foreach ($attributes as $attribute) {
  68. $annotation = $attribute->newInstance();
  69. if (!empty($annotation->ignore)) if (strtolower($annotation->ignore) == 'node') continue;
  70. $actionList[] = [
  71. 'node' => $controllerFormat . '/' . $method->name,
  72. 'title' => $annotation->title ?? null,
  73. 'is_auth' => $annotation->auth ?? false,
  74. 'type' => 2,
  75. ];
  76. }
  77. }
  78. // 方法非空才读取控制器注解
  79. if (!empty($actionList)) {
  80. // 读取Controller的注解
  81. $attributes = $reflectionClass->getAttributes(ControllerAnnotation::class);
  82. foreach ($attributes as $attribute) {
  83. $controllerAnnotation = $attribute->newInstance();
  84. $nodeList[] = [
  85. 'node' => $controllerFormat,
  86. 'title' => $controllerAnnotation->title ?? null,
  87. 'is_auth' => $controllerAnnotation->auth ?? false,
  88. 'type' => 1,
  89. ];
  90. }
  91. $nodeList = array_merge($nodeList, $actionList);
  92. }
  93. }
  94. }
  95. return $nodeList;
  96. }
  97. /**
  98. * 获取所有控制器
  99. * @return array
  100. */
  101. public function getControllerList(): array
  102. {
  103. return $this->readControllerFiles($this->basePath);
  104. }
  105. /**
  106. * 遍历读取控制器文件
  107. * @param $path
  108. * @return array
  109. */
  110. protected function readControllerFiles($path): array
  111. {
  112. list($list, $temp_list, $dirExplode) = [[], scandir($path), explode($this->basePath, $path)];
  113. $middleDir = !empty($dirExplode[1]) ? str_replace('/', '\\', substr($dirExplode[1], 1)) . "\\" : '';
  114. foreach ($temp_list as $file) {
  115. // 排除根目录和没有开启注解的模块
  116. if ($file == ".." || $file == ".") {
  117. continue;
  118. }
  119. if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
  120. // 子文件夹,进行递归
  121. $childFiles = $this->readControllerFiles($path . DIRECTORY_SEPARATOR . $file);
  122. $list = array_merge($childFiles, $list);
  123. }else {
  124. // 判断是不是控制器
  125. $fileExplodeArray = explode('.', $file);
  126. if (count($fileExplodeArray) != 2 || end($fileExplodeArray) != 'php') {
  127. continue;
  128. }
  129. // 根目录下的文件
  130. $className = str_replace('.php', '', $file);
  131. $controllerFormat = str_replace('\\', '.', $middleDir) . CommonTool::humpToLine(lcfirst($className));
  132. $list[$controllerFormat] = "{$this->baseNamespace}\\{$middleDir}" . $className;
  133. }
  134. }
  135. return $list;
  136. }
  137. }