NodeService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. namespace think\admin\service;
  15. use think\admin\Service;
  16. /**
  17. * 应用节点服务管理
  18. * Class NodeService
  19. * @package think\admin\service
  20. */
  21. class NodeService extends Service
  22. {
  23. /**
  24. * 驼峰转下划线规则
  25. * @param string $name
  26. * @return string
  27. */
  28. public function nameTolower($name)
  29. {
  30. $dots = [];
  31. foreach (explode('.', strtr($name, '/', '.')) as $dot) {
  32. $dots[] = trim(preg_replace("/[A-Z]/", "_\\0", $dot), '_');
  33. }
  34. return strtolower(join('.', $dots));
  35. }
  36. /**
  37. * 获取当前节点内容
  38. * @param string $type
  39. * @return string
  40. */
  41. public function getCurrent($type = '')
  42. {
  43. $prefix = $this->app->getNamespace();
  44. $middle = '\\' . $this->nameTolower($this->app->request->controller());
  45. $suffix = ($type === 'controller') ? '' : ('\\' . $this->app->request->action());
  46. return strtr(substr($prefix, stripos($prefix, '\\') + 1) . $middle . $suffix, '\\', '/');
  47. }
  48. /**
  49. * 检查并完整节点内容
  50. * @param string $node
  51. * @return string
  52. */
  53. public function fullnode($node)
  54. {
  55. if (empty($node)) return $this->getCurrent();
  56. if (count($attrs = explode('/', $node)) === 1) {
  57. return $this->getCurrent('controller') . '/' . $node;
  58. } else {
  59. $attrs[1] = $this->nameTolower($attrs[1]);
  60. return join('/', $attrs);
  61. }
  62. }
  63. /**
  64. * 获取应用列表
  65. * @param array $data
  66. * @return array
  67. */
  68. public function getModules($data = [])
  69. {
  70. $path = $this->app->getBasePath();
  71. foreach (scandir($path) as $item) if ($item[0] !== '.') {
  72. if (is_dir(realpath($path . $item))) $data[] = $item;
  73. }
  74. return $data;
  75. }
  76. /**
  77. * 获取所有控制器入口
  78. * @param boolean $force
  79. * @return array
  80. * @throws \ReflectionException
  81. */
  82. public function getMethods($force = false)
  83. {
  84. static $data = [];
  85. if (empty($force)) {
  86. if (count($data) > 0) return $data;
  87. $data = $this->app->cache->get('SystemAuthNode', []);
  88. if (count($data) > 0) return $data;
  89. } else {
  90. $data = [];
  91. }
  92. $ignores = get_class_methods('\think\admin\Controller');
  93. foreach ($this->scanDirectory($this->app->getBasePath()) as $file) {
  94. if (preg_match("|/(\w+)/(\w+)/controller/(.+)\.php$|i", $file, $matches)) {
  95. [, $namespace, $appname, $classname] = $matches;
  96. $prefix = strtr("{$appname}/{$this->nameTolower($classname)}", '\\', '/');
  97. $reflection = new \ReflectionClass(strtr("{$namespace}/{$appname}/controller/{$classname}", '/', '\\'));
  98. $data[$prefix] = $this->_parseComment($reflection->getDocComment(), $classname);
  99. foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  100. if (in_array($metname = $method->getName(), $ignores)) continue;
  101. $data["{$prefix}/{$metname}"] = $this->_parseComment($method->getDocComment(), $metname);
  102. }
  103. }
  104. }
  105. $data = array_change_key_case($data, CASE_LOWER);
  106. $this->app->cache->set('SystemAuthNode', $data);
  107. return $data;
  108. }
  109. /**
  110. * 解析硬节点属性
  111. * @param string $comment 备注内容
  112. * @param string $default 默认标题
  113. * @return array
  114. */
  115. private function _parseComment($comment, $default = '')
  116. {
  117. $text = strtr($comment, "\n", ' ');
  118. $title = preg_replace('/^\/\*\s*\*\s*\*\s*(.*?)\s*\*.*?$/', '$1', $text);
  119. if (in_array(substr($title, 0, 5), ['@auth', '@menu', '@logi'])) $title = $default;
  120. return [
  121. 'title' => $title ?: $default,
  122. 'isauth' => intval(preg_match('/@auth\s*true/i', $text)),
  123. 'ismenu' => intval(preg_match('/@menu\s*true/i', $text)),
  124. 'islogin' => intval(preg_match('/@login\s*true/i', $text)),
  125. ];
  126. }
  127. /**
  128. * 获取所有PHP文件列表
  129. * @param string $path 扫描目录
  130. * @param array $data 额外数据
  131. * @param string $ext 文件后缀
  132. * @return array
  133. */
  134. public function scanDirectory($path, $data = [], $ext = 'php')
  135. {
  136. if (file_exists($path) && is_dir($path)) foreach (scandir($path) as $item) if ($item[0] !== '.') {
  137. $realpath = rtrim($path, '\\/') . DIRECTORY_SEPARATOR . $item;
  138. if (is_readable($realpath)) if (is_dir($realpath)) {
  139. $data = $this->scanDirectory($realpath, $data, $ext);
  140. } elseif (is_file($realpath) && (is_null($ext) || pathinfo($realpath, 4) === $ext)) {
  141. $data[] = strtr($realpath, '\\', '/');
  142. }
  143. }
  144. return $data;
  145. }
  146. }