Node.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace library;
  14. /**
  15. * 代码节点分析器
  16. *
  17. * @author Anyon <zoujingli@qq.com>
  18. * @date 2017/02/23 14:46
  19. */
  20. class Node {
  21. /**
  22. * 获取节点列表
  23. * @param string $path
  24. * @param array $nodes
  25. * @return array
  26. */
  27. static public function getNodeTree($path, $nodes = []) {
  28. foreach (self::getFilePaths($path) as $vo) {
  29. if (stripos($vo, DS . 'controller' . DS) === false) {
  30. continue;
  31. }
  32. $_tmp = explode(DS, $vo);
  33. $controllerName = rtrim(array_pop($_tmp), '.php');
  34. array_pop($_tmp);
  35. $moduleName = array_pop($_tmp);
  36. $className = config('app_namespace') . "\\{$moduleName}\\controller\\{$controllerName}";
  37. if (!class_exists($className)) {
  38. continue;
  39. }
  40. foreach (get_class_methods($className) as $actionName) {
  41. if ($actionName[0] !== '_') {
  42. $nodes[] = strtolower("{$moduleName}/{$controllerName}/{$actionName}");
  43. }
  44. }
  45. }
  46. return $nodes;
  47. }
  48. /**
  49. * 获取所有PHP文件
  50. * @param string $path
  51. * @param array $data
  52. * @param string $ext
  53. * @return array
  54. */
  55. static public function getFilePaths($path, $data = [], $ext = 'php') {
  56. foreach (scandir($path) as $dir) {
  57. if ($dir[0] === '.') {
  58. continue;
  59. }
  60. $tmp = realpath($path . DS . $dir);
  61. if ($tmp && (is_dir($tmp) || pathinfo($tmp, PATHINFO_EXTENSION) === $ext)) {
  62. is_dir($tmp) ? $data = array_merge($data, self::getFilePaths($tmp)) : $data[] = $tmp;
  63. }
  64. }
  65. return $data;
  66. }
  67. }