Menu.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\admin\command;
  3. use app\admin\model\AuthRule;
  4. use ReflectionClass;
  5. use ReflectionMethod;
  6. use think\Cache;
  7. use think\Config;
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\input\Option;
  11. use think\console\Output;
  12. use think\Exception;
  13. class Menu extends Command
  14. {
  15. protected $model = null;
  16. protected function configure()
  17. {
  18. $this
  19. ->setName('menu')
  20. ->addOption('controller', 'c', Option::VALUE_REQUIRED, 'controller name,use \'all-controller\' when build all menu', null)
  21. ->setDescription('Build auth menu from controller');
  22. }
  23. protected function execute(Input $input, Output $output)
  24. {
  25. $this->model = new AuthRule();
  26. $adminPath = dirname(__DIR__) . DS;
  27. //控制器名
  28. $controller = $input->getOption('controller') ?: '';
  29. if (!$controller)
  30. {
  31. throw new Exception("please input controller name");
  32. }
  33. if ($controller != 'all-controller')
  34. {
  35. $controllerArr = explode('/', $controller);
  36. end($controllerArr);
  37. $key = key($controllerArr);
  38. $controllerArr[$key] = ucfirst($controllerArr[$key]);
  39. $adminPath = dirname(__DIR__) . DS . 'controller' . DS . implode(DS, $controllerArr) . '.php';
  40. if (!is_file($adminPath))
  41. {
  42. $output->error("controller not found");
  43. return;
  44. }
  45. $this->importRule($controller);
  46. }
  47. else
  48. {
  49. $this->model->destroy([]);
  50. $controllerDir = $adminPath . 'controller' . DS;
  51. // 扫描新的节点信息并导入
  52. $treelist = $this->import($this->scandir($controllerDir));
  53. }
  54. Cache::rm("__menu__");
  55. $output->info("Build Successed!");
  56. }
  57. /**
  58. * 递归扫描文件夹
  59. * @param string $dir
  60. * @return array
  61. */
  62. public function scandir($dir)
  63. {
  64. $result = [];
  65. $cdir = scandir($dir);
  66. foreach ($cdir as $value)
  67. {
  68. if (!in_array($value, array(".", "..")))
  69. {
  70. if (is_dir($dir . DS . $value))
  71. {
  72. $result[$value] = $this->scandir($dir . DS . $value);
  73. }
  74. else
  75. {
  76. $result[] = $value;
  77. }
  78. }
  79. }
  80. return $result;
  81. }
  82. /**
  83. * 导入规则节点
  84. * @param array $dirarr
  85. * @param array $parentdir
  86. * @return array
  87. */
  88. public function import($dirarr, $parentdir = [])
  89. {
  90. $menuarr = [];
  91. foreach ($dirarr as $k => $v)
  92. {
  93. if (is_array($v))
  94. {
  95. //当前是文件夹
  96. $nowparentdir = array_merge($parentdir, [$k]);
  97. $this->import($v, $nowparentdir);
  98. }
  99. else
  100. {
  101. //只匹配PHP文件
  102. if (!preg_match('/^(\w+)\.php$/', $v, $matchone))
  103. {
  104. continue;
  105. }
  106. //导入文件
  107. $controller = ($parentdir ? implode('/', $parentdir) . '/' : '') . $matchone[1];
  108. $this->importRule($controller);
  109. }
  110. }
  111. return $menuarr;
  112. }
  113. protected function importRule($controller)
  114. {
  115. $controllerArr = explode('/', $controller);
  116. end($controllerArr);
  117. $key = key($controllerArr);
  118. $controllerArr[$key] = ucfirst($controllerArr[$key]);
  119. $classSuffix = Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
  120. $className = "\\app\\admin\\controller\\" . implode("\\", $controllerArr) . $classSuffix;
  121. $pathArr = $controllerArr;
  122. array_unshift($pathArr, '', 'application', 'admin', 'controller');
  123. $classFile = ROOT_PATH . implode(DS, $pathArr) . $classSuffix . ".php";
  124. $classContent = file_get_contents($classFile);
  125. $uniqueName = uniqid("FastAdmin") . $classSuffix;
  126. $classContent = str_replace("class " . $controllerArr[$key] . $classSuffix . " ", 'class ' . $uniqueName . ' ', $classContent);
  127. $classContent = preg_replace("/namespace\s(.*);/", 'namespace ' . __NAMESPACE__ . ";", $classContent);
  128. //临时的类文件
  129. $tempClassFile = __DIR__ . DS . $uniqueName . ".php";
  130. file_put_contents($tempClassFile, $classContent);
  131. $className = "\\app\\admin\\command\\" . $uniqueName;
  132. //反射机制调用类的注释和方法名
  133. $reflector = new ReflectionClass($className);
  134. if (isset($tempClassFile))
  135. {
  136. //删除临时文件
  137. @unlink($tempClassFile);
  138. }
  139. //只匹配公共的方法
  140. $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
  141. $classComment = $reflector->getDocComment();
  142. //忽略的类
  143. if (stripos($classComment, "@internal") !== FALSE)
  144. {
  145. return;
  146. }
  147. preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
  148. $controllerIcon = 'fa fa-circle-o';
  149. $controllerRemark = '';
  150. //判断注释中是否设置了icon值
  151. if (isset($annotations[1]))
  152. {
  153. foreach ($annotations[1] as $tag)
  154. {
  155. if (stripos($tag, '@icon') !== FALSE)
  156. {
  157. $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
  158. }
  159. if (stripos($tag, '@remark') !== FALSE)
  160. {
  161. $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
  162. }
  163. }
  164. }
  165. //过滤掉其它字符
  166. $controllerTitle = trim(preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $classComment));
  167. //导入中文语言包
  168. \think\Lang::load(dirname(__DIR__) . DS . 'lang/zh-cn.php');
  169. //先定入菜单的数据
  170. $pid = 0;
  171. $name = "/admin";
  172. foreach (explode('/', $controller) as $k => $v)
  173. {
  174. $name .= '/' . strtolower($v);
  175. $title = (!isset($controllerArr[$k + 1]) ? $controllerTitle : '');
  176. $icon = (!isset($controllerArr[$k + 1]) ? $controllerIcon : 'fa fa-list');
  177. $remark = (!isset($controllerArr[$k + 1]) ? $controllerRemark : '');
  178. $title = $title ? $title : __(ucfirst($v) . ' manager');
  179. $rulemodel = $this->model->get(['name' => $name]);
  180. if (!$rulemodel)
  181. {
  182. $this->model
  183. ->data(['pid' => $pid, 'name' => $name, 'title' => $title, 'icon' => $icon, 'remark' => $remark, 'ismenu' => 1, 'status' => 'normal'])
  184. ->isUpdate(false)
  185. ->save();
  186. $pid = $this->model->id;
  187. }
  188. else
  189. {
  190. $pid = $rulemodel->id;
  191. }
  192. }
  193. $ruleArr = [];
  194. foreach ($methods as $m => $n)
  195. {
  196. //过滤特殊的类
  197. if (substr($n->name, 0, 2) == '__' || $n->name == '_initialize')
  198. {
  199. continue;
  200. }
  201. //只匹配符合的方法
  202. if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo))
  203. {
  204. unset($methods[$m]);
  205. continue;
  206. }
  207. $comment = $reflector->getMethod($n->name)->getDocComment();
  208. //忽略的方法
  209. if (stripos($comment, "@internal") !== FALSE)
  210. {
  211. continue;
  212. }
  213. //过滤掉其它字符
  214. $comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/', '/[\s]+\*\//', '/\*\s@(.*)/', '/[\s|\*]+/'), '', $comment);
  215. $ruleArr[] = array('pid' => $pid, 'name' => $name . "/" . strtolower($n->name), 'icon' => 'fa fa-circle-o', 'title' => $comment ? $comment : $n->name, 'ismenu' => 0, 'status' => 'normal');
  216. }
  217. $this->model->saveAll($ruleArr);
  218. }
  219. }