NodeService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace think\admin\service;
  16. use think\admin\Service;
  17. /**
  18. * 应用节点服务管理
  19. * Class NodeService
  20. * @package think\admin\service
  21. */
  22. class NodeService extends Service
  23. {
  24. /**
  25. * 驼峰转下划线规则
  26. * @param string $name
  27. * @return string
  28. */
  29. public function nameTolower(string $name): string
  30. {
  31. $dots = [];
  32. foreach (explode('.', strtr($name, '/', '.')) as $dot) {
  33. $dots[] = trim(preg_replace("/[A-Z]/", "_\\0", $dot), '_');
  34. }
  35. return strtolower(join('.', $dots));
  36. }
  37. /**
  38. * 获取当前节点内容
  39. * @param string $type
  40. * @return string
  41. */
  42. public function getCurrent(string $type = ''): string
  43. {
  44. $space = $this->app->getNamespace();
  45. $prefix = strtolower($this->app->http->getName());
  46. if (preg_match("|\\\\addons\\\\{$prefix}$|", $space)) {
  47. $prefix = "addons-{$prefix}";
  48. }
  49. // 获取应用前缀节点
  50. if ($type === 'module') return $prefix;
  51. // 获取控制器前缀节点
  52. $middle = $this->nameTolower($this->app->request->controller());
  53. if ($type === 'controller') return $prefix . '/' . $middle;
  54. // 获取完整的权限节点
  55. return $prefix . '/' . $middle . '/' . strtolower($this->app->request->action());
  56. }
  57. /**
  58. * 检查并完整节点内容
  59. * @param null|string $node
  60. * @return string
  61. */
  62. public function fullnode(?string $node = ''): string
  63. {
  64. if (empty($node)) {
  65. return $this->getCurrent();
  66. }
  67. switch (count($attrs = explode('/', $node))) {
  68. case 2:
  69. return $this->getCurrent('module') . '/' . strtolower($node);
  70. case 1:
  71. return $this->getCurrent('controller') . '/' . strtolower($node);
  72. }
  73. $attrs[1] = $this->nameTolower($attrs[1]);
  74. return strtolower(join('/', $attrs));
  75. }
  76. /**
  77. * 获取应用列表
  78. * @param array $data
  79. * @return array
  80. */
  81. public function getModules(array $data = []): array
  82. {
  83. $path = $this->app->getBasePath();
  84. foreach (scandir($path) as $item) if ($item[0] !== '.') {
  85. if (is_dir(realpath($path . $item))) $data[] = $item;
  86. }
  87. return $data;
  88. }
  89. /**
  90. * 获取所有控制器入口
  91. * @param boolean $force
  92. * @return array
  93. * @throws \ReflectionException
  94. */
  95. public function getMethods(bool $force = false): array
  96. {
  97. static $data = [];
  98. if (empty($force)) {
  99. if (count($data) > 0) return $data;
  100. $data = $this->app->cache->get('SystemAuthNode', []);
  101. if (count($data) > 0) return $data;
  102. } else {
  103. $data = [];
  104. }
  105. /*! 排除内置方法,禁止访问内置方法 */
  106. $ignores = get_class_methods('\think\admin\Controller');
  107. /*! 扫描所有代码控制器节点,更新节点缓存 */
  108. foreach ($this->scanDirectory($this->app->getBasePath()) as $file) {
  109. $name = substr($file, strlen(strtr($this->app->getRootPath(), '\\', '/')) - 1);
  110. if (preg_match("|^([\w/]+)/(\w+)/controller/(.+)\.php$|i", $name, $matches)) {
  111. [, $namespace, $appname, $classname] = $matches;
  112. $addons = preg_match('|/addons$|', $namespace) ? 'addons-' : '';
  113. $class = new \ReflectionClass(strtr("{$namespace}/{$appname}/controller/{$classname}", '/', '\\'));
  114. $prefix = strtolower(strtr("{$addons}{$appname}/{$this->nameTolower($classname)}", '\\', '/'));
  115. $data[$prefix] = $this->_parseComment($class->getDocComment() ?: '', $classname);
  116. foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  117. if (in_array($metname = $method->getName(), $ignores)) continue;
  118. $data[strtolower("{$prefix}/{$metname}")] = $this->_parseComment($method->getDocComment() ?: '', $metname);
  119. }
  120. }
  121. }
  122. $this->app->cache->set('SystemAuthNode', $data);
  123. return $data;
  124. }
  125. /**
  126. * 解析硬节点属性
  127. * @param string $comment 备注内容
  128. * @param string $default 默认标题
  129. * @return array
  130. */
  131. private function _parseComment(string $comment, string $default = ''): array
  132. {
  133. $text = strtr($comment, "\n", ' ');
  134. $title = preg_replace('/^\/\*\s*\*\s*\*\s*(.*?)\s*\*.*?$/', '$1', $text);
  135. if (in_array(substr($title, 0, 5), ['@auth', '@menu', '@logi'])) $title = $default;
  136. return [
  137. 'title' => $title ?: $default,
  138. 'isauth' => intval(preg_match('/@auth\s*true/i', $text)),
  139. 'ismenu' => intval(preg_match('/@menu\s*true/i', $text)),
  140. 'islogin' => intval(preg_match('/@login\s*true/i', $text)),
  141. ];
  142. }
  143. /**
  144. * 获取所有PHP文件列表
  145. * @param string $path 扫描目录
  146. * @param array $data 额外数据
  147. * @param null|string $ext 文件后缀
  148. * @return array
  149. */
  150. public function scanDirectory(string $path, array $data = [], $ext = 'php'): array
  151. {
  152. if (file_exists($path)) {
  153. if (is_file($path)) {
  154. $data[] = strtr($path, '\\', '/');
  155. } elseif (is_dir($path)) foreach (scandir($path) as $item) if ($item[0] !== '.') {
  156. $real = rtrim($path, '\\/') . DIRECTORY_SEPARATOR . $item;
  157. if (is_readable($real)) if (is_dir($real)) {
  158. $data = $this->scanDirectory($real, $data, $ext);
  159. } elseif (is_file($real) && (is_null($ext) || pathinfo($real, 4) === $ext)) {
  160. $data[] = strtr($real, '\\', '/');
  161. }
  162. }
  163. }
  164. return $data;
  165. }
  166. }