Menu.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. class Menu extends Command
  13. {
  14. protected $model = null;
  15. protected function configure()
  16. {
  17. $this
  18. ->setName('menu')
  19. ->addOption('controller', 'c', Option::VALUE_REQUIRED, 'controller name,use \'all-controller\' when build all menu', null)
  20. ->setDescription('Build auth menu from controller');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $this->model = new AuthRule();
  25. $adminPath = dirname(__DIR__) . DS;
  26. //控制器名
  27. $controller = $input->getOption('controller') ? : '';
  28. if (!$controller)
  29. {
  30. $output->error("please input controller name");
  31. return;
  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('/', $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 . '/' . $value))
  71. {
  72. $result[$value] = $this->scandir($dir . '/' . $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. //反射机制调用类的注释和方法名
  120. $reflector = new ReflectionClass("\\app\\admin\\controller\\" . implode("\\", $controllerArr) . (Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : ''));
  121. //只匹配公共的方法
  122. $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
  123. $classComment = $reflector->getDocComment();
  124. //忽略的类
  125. if (stripos($classComment, "@internal") !== FALSE)
  126. {
  127. return;
  128. }
  129. preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
  130. $controllerIcon = 'fa fa-circle-o';
  131. $controllerRemark = '';
  132. //判断注释中是否设置了icon值
  133. if (isset($annotations[1]))
  134. {
  135. foreach ($annotations[1] as $tag)
  136. {
  137. if (stripos($tag, '@icon') !== FALSE)
  138. {
  139. $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
  140. }
  141. if (stripos($tag, '@remark') !== FALSE)
  142. {
  143. $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
  144. }
  145. }
  146. }
  147. //过滤掉其它字符
  148. $controllerTitle = trim(preg_replace(array('/^\/\*\*(.*)[\n\r\t]/', '/[\s]+\*\//', '/\*\s@(.*)/', '/[\s|\*]+/'), '', $classComment));
  149. //先定入菜单的数据
  150. $pid = 0;
  151. $name = "/admin";
  152. foreach (explode('/', $controller) as $k => $v)
  153. {
  154. $name .= '/' . strtolower($v);
  155. $title = (!isset($controllerArr[$k + 1]) ? $controllerTitle : ucfirst($v));
  156. $icon = (!isset($controllerArr[$k + 1]) ? $controllerIcon : 'fa fa-list');
  157. $remark = (!isset($controllerArr[$k + 1]) ? $controllerRemark : '');
  158. $title = $title ? $title : ucfirst($v);
  159. $rulemodel = $this->model->get(['name' => $name]);
  160. if (!$rulemodel)
  161. {
  162. $this->model
  163. ->data(['pid' => $pid, 'name' => $name, 'title' => $title, 'icon' => $icon, 'remark' => $remark, 'ismenu' => 1, 'status' => 'normal'])
  164. ->isUpdate(false)
  165. ->save();
  166. $pid = $this->model->id;
  167. }
  168. else
  169. {
  170. $pid = $rulemodel->id;
  171. }
  172. }
  173. $ruleArr = [];
  174. foreach ($methods as $m => $n)
  175. {
  176. //过滤特殊的类
  177. if (substr($n->name, 0, 2) == '__' || $n->name == '_initialize')
  178. {
  179. continue;
  180. }
  181. //只匹配符合的方法
  182. if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo))
  183. {
  184. unset($methods[$m]);
  185. continue;
  186. }
  187. $comment = $reflector->getMethod($n->name)->getDocComment();
  188. //忽略的方法
  189. if (stripos($comment, "@internal") !== FALSE)
  190. {
  191. continue;
  192. }
  193. //过滤掉其它字符
  194. $comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/', '/[\s]+\*\//', '/\*\s@(.*)/', '/[\s|\*]+/'), '', $comment);
  195. $ruleArr[] = array('pid' => $pid, 'name' => $name . "/" . strtolower($n->name), 'icon' => 'fa fa-circle-o', 'title' => $comment ? $comment : $n->name, 'ismenu' => 0, 'status' => 'normal');
  196. }
  197. $this->model->saveAll($ruleArr);
  198. }
  199. }