Menu.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (version_compare(PHP_VERSION, '7.0.0', '<'))
  122. {
  123. $pathArr = $controllerArr;
  124. array_unshift($pathArr, '', 'application', 'admin', 'controller');
  125. $classFile = ROOT_PATH . implode(DS, $pathArr) . $classSuffix . ".php";
  126. $classContent = file_get_contents($classFile);
  127. $uniqueName = uniqid("FastAdmin") . $classSuffix;
  128. $classContent = str_replace("class " . $controllerArr[$key] . $classSuffix . " ", 'class ' . $uniqueName . ' ', $classContent);
  129. $classContent = preg_replace("/namespace\s(.*);/", 'namespace ' . __NAMESPACE__ . ";", $classContent);
  130. //临时的类文件
  131. $tempClassFile = __DIR__ . DS . $uniqueName . ".php";
  132. file_put_contents($tempClassFile, $classContent);
  133. $className = "\\app\\admin\\command\\" . $uniqueName;
  134. }
  135. //反射机制调用类的注释和方法名
  136. $reflector = new ReflectionClass($className);
  137. if (isset($tempClassFile))
  138. {
  139. //删除临时文件
  140. @unlink($tempClassFile);
  141. }
  142. //只匹配公共的方法
  143. $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
  144. $classComment = $reflector->getDocComment();
  145. //忽略的类
  146. if (stripos($classComment, "@internal") !== FALSE)
  147. {
  148. return;
  149. }
  150. preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
  151. $controllerIcon = 'fa fa-circle-o';
  152. $controllerRemark = '';
  153. //判断注释中是否设置了icon值
  154. if (isset($annotations[1]))
  155. {
  156. foreach ($annotations[1] as $tag)
  157. {
  158. if (stripos($tag, '@icon') !== FALSE)
  159. {
  160. $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
  161. }
  162. if (stripos($tag, '@remark') !== FALSE)
  163. {
  164. $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
  165. }
  166. }
  167. }
  168. //过滤掉其它字符
  169. $controllerTitle = trim(preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $classComment));
  170. //先定入菜单的数据
  171. $pid = 0;
  172. $name = "/admin";
  173. foreach (explode('/', $controller) as $k => $v)
  174. {
  175. $name .= '/' . strtolower($v);
  176. $title = (!isset($controllerArr[$k + 1]) ? $controllerTitle : ucfirst($v));
  177. $icon = (!isset($controllerArr[$k + 1]) ? $controllerIcon : 'fa fa-list');
  178. $remark = (!isset($controllerArr[$k + 1]) ? $controllerRemark : '');
  179. $title = $title ? $title : ucfirst($v);
  180. $rulemodel = $this->model->get(['name' => $name]);
  181. if (!$rulemodel)
  182. {
  183. $this->model
  184. ->data(['pid' => $pid, 'name' => $name, 'title' => $title, 'icon' => $icon, 'remark' => $remark, 'ismenu' => 1, 'status' => 'normal'])
  185. ->isUpdate(false)
  186. ->save();
  187. $pid = $this->model->id;
  188. }
  189. else
  190. {
  191. $pid = $rulemodel->id;
  192. }
  193. }
  194. $ruleArr = [];
  195. foreach ($methods as $m => $n)
  196. {
  197. //过滤特殊的类
  198. if (substr($n->name, 0, 2) == '__' || $n->name == '_initialize')
  199. {
  200. continue;
  201. }
  202. //只匹配符合的方法
  203. if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo))
  204. {
  205. unset($methods[$m]);
  206. continue;
  207. }
  208. $comment = $reflector->getMethod($n->name)->getDocComment();
  209. //忽略的方法
  210. if (stripos($comment, "@internal") !== FALSE)
  211. {
  212. continue;
  213. }
  214. //过滤掉其它字符
  215. $comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/', '/[\s]+\*\//', '/\*\s@(.*)/', '/[\s|\*]+/'), '', $comment);
  216. $ruleArr[] = array('pid' => $pid, 'name' => $name . "/" . strtolower($n->name), 'icon' => 'fa fa-circle-o', 'title' => $comment ? $comment : $n->name, 'ismenu' => 0, 'status' => 'normal');
  217. }
  218. $this->model->saveAll($ruleArr);
  219. }
  220. }