Menu.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 | Option::VALUE_IS_ARRAY, 'controller name,use \'all-controller\' when build all menu', null)
  21. ->addOption('delete', 'd', Option::VALUE_OPTIONAL, 'delete the specified menu', '')
  22. ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force delete menu,without tips', null)
  23. ->addOption('equal', 'e', Option::VALUE_OPTIONAL, 'the controller must be equal', null)
  24. ->setDescription('Build auth menu from controller');
  25. //要执行的controller必须一样,不适用模糊查询
  26. }
  27. protected function execute(Input $input, Output $output)
  28. {
  29. $this->model = new AuthRule();
  30. $adminPath = dirname(__DIR__) . DS;
  31. //控制器名
  32. $controller = $input->getOption('controller') ?: '';
  33. if (!$controller) {
  34. throw new Exception("please input controller name");
  35. }
  36. $force = $input->getOption('force');
  37. //是否为删除模式
  38. $delete = $input->getOption('delete');
  39. //是否控制器完全匹配
  40. $equal = $input->getOption('equal');
  41. if ($delete) {
  42. if (in_array('all-controller', $controller)) {
  43. throw new Exception("could not delete all menu");
  44. }
  45. $ids = [];
  46. $list = $this->model->where(function ($query) use ($controller, $equal) {
  47. foreach ($controller as $index => $item) {
  48. if ($equal) {
  49. $query->whereOr('name', 'eq', $item);
  50. } else {
  51. $query->whereOr('name', 'like', strtolower($item) . "%");
  52. }
  53. }
  54. })->select();
  55. foreach ($list as $k => $v) {
  56. $output->warning($v->name);
  57. $ids[] = $v->id;
  58. }
  59. if (!$ids) {
  60. throw new Exception("There is no menu to delete");
  61. }
  62. if (!$force) {
  63. $output->info("Are you sure you want to delete all those menu? Type 'yes' to continue: ");
  64. $line = fgets(STDIN);
  65. if (trim($line) != 'yes') {
  66. throw new Exception("Operation is aborted!");
  67. }
  68. }
  69. AuthRule::destroy($ids);
  70. Cache::rm("__menu__");
  71. $output->info("Delete Successed");
  72. return;
  73. }
  74. if (!in_array('all-controller', $controller)) {
  75. foreach ($controller as $index => $item) {
  76. $controllerArr = explode('/', $item);
  77. end($controllerArr);
  78. $key = key($controllerArr);
  79. $controllerArr[$key] = ucfirst($controllerArr[$key]);
  80. $adminPath = dirname(__DIR__) . DS . 'controller' . DS . implode(DS, $controllerArr) . '.php';
  81. if (!is_file($adminPath)) {
  82. $output->error("controller not found");
  83. return;
  84. }
  85. $this->importRule($item);
  86. }
  87. } else {
  88. $authRuleList = AuthRule::select();
  89. //生成权限规则备份文件
  90. file_put_contents(RUNTIME_PATH . 'authrule.json', json_encode(collection($authRuleList)->toArray()));
  91. $this->model->where('id', '>', 0)->delete();
  92. $controllerDir = $adminPath . 'controller' . DS;
  93. // 扫描新的节点信息并导入
  94. $treelist = $this->import($this->scandir($controllerDir));
  95. }
  96. Cache::rm("__menu__");
  97. $output->info("Build Successed!");
  98. }
  99. /**
  100. * 递归扫描文件夹
  101. * @param string $dir
  102. * @return array
  103. */
  104. public function scandir($dir)
  105. {
  106. $result = [];
  107. $cdir = scandir($dir);
  108. foreach ($cdir as $value) {
  109. if (!in_array($value, array(".", ".."))) {
  110. if (is_dir($dir . DS . $value)) {
  111. $result[$value] = $this->scandir($dir . DS . $value);
  112. } else {
  113. $result[] = $value;
  114. }
  115. }
  116. }
  117. return $result;
  118. }
  119. /**
  120. * 导入规则节点
  121. * @param array $dirarr
  122. * @param array $parentdir
  123. * @return array
  124. */
  125. public function import($dirarr, $parentdir = [])
  126. {
  127. $menuarr = [];
  128. foreach ($dirarr as $k => $v) {
  129. if (is_array($v)) {
  130. //当前是文件夹
  131. $nowparentdir = array_merge($parentdir, [$k]);
  132. $this->import($v, $nowparentdir);
  133. } else {
  134. //只匹配PHP文件
  135. if (!preg_match('/^(\w+)\.php$/', $v, $matchone)) {
  136. continue;
  137. }
  138. //导入文件
  139. $controller = ($parentdir ? implode('/', $parentdir) . '/' : '') . $matchone[1];
  140. $this->importRule($controller);
  141. }
  142. }
  143. return $menuarr;
  144. }
  145. protected function importRule($controller)
  146. {
  147. $controller = str_replace('\\', '/', $controller);
  148. $controllerArr = explode('/', $controller);
  149. end($controllerArr);
  150. $key = key($controllerArr);
  151. $controllerArr[$key] = ucfirst($controllerArr[$key]);
  152. $classSuffix = Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
  153. $className = "\\app\\admin\\controller\\" . implode("\\", $controllerArr) . $classSuffix;
  154. $pathArr = $controllerArr;
  155. array_unshift($pathArr, '', 'application', 'admin', 'controller');
  156. $classFile = ROOT_PATH . implode(DS, $pathArr) . $classSuffix . ".php";
  157. $classContent = file_get_contents($classFile);
  158. $uniqueName = uniqid("FastAdmin") . $classSuffix;
  159. $classContent = str_replace("class " . $controllerArr[$key] . $classSuffix . " ", 'class ' . $uniqueName . ' ', $classContent);
  160. $classContent = preg_replace("/namespace\s(.*);/", 'namespace ' . __NAMESPACE__ . ";", $classContent);
  161. //临时的类文件
  162. $tempClassFile = __DIR__ . DS . $uniqueName . ".php";
  163. file_put_contents($tempClassFile, $classContent);
  164. $className = "\\app\\admin\\command\\" . $uniqueName;
  165. //反射机制调用类的注释和方法名
  166. $reflector = new ReflectionClass($className);
  167. if (isset($tempClassFile)) {
  168. //删除临时文件
  169. @unlink($tempClassFile);
  170. }
  171. //只匹配公共的方法
  172. $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
  173. $classComment = $reflector->getDocComment();
  174. //判断是否有启用软删除
  175. $softDeleteMethods = ['destroy', 'restore', 'recyclebin'];
  176. $withSofeDelete = false;
  177. preg_match_all("/\\\$this\->model\s*=\s*model\('(\w+)'\);/", $classContent, $matches);
  178. if (isset($matches[1]) && isset($matches[1][0]) && $matches[1][0]) {
  179. \think\Request::instance()->module('admin');
  180. $model = model($matches[1][0]);
  181. if (in_array('trashed', get_class_methods($model))) {
  182. $withSofeDelete = true;
  183. }
  184. }
  185. //忽略的类
  186. if (stripos($classComment, "@internal") !== false) {
  187. return;
  188. }
  189. preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
  190. $controllerIcon = 'fa fa-circle-o';
  191. $controllerRemark = '';
  192. //判断注释中是否设置了icon值
  193. if (isset($annotations[1])) {
  194. foreach ($annotations[1] as $tag) {
  195. if (stripos($tag, '@icon') !== false) {
  196. $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
  197. }
  198. if (stripos($tag, '@remark') !== false) {
  199. $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
  200. }
  201. }
  202. }
  203. //过滤掉其它字符
  204. $controllerTitle = trim(preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $classComment));
  205. //导入中文语言包
  206. \think\Lang::load(dirname(__DIR__) . DS . 'lang/zh-cn.php');
  207. //先导入菜单的数据
  208. $pid = 0;
  209. foreach ($controllerArr as $k => $v) {
  210. $key = $k + 1;
  211. //驼峰转下划线
  212. $controllerNameArr = array_slice($controllerArr, 0, $key);
  213. foreach ($controllerNameArr as &$val) {
  214. $val = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $val), "_"));
  215. }
  216. unset($val);
  217. $name = implode('/', $controllerNameArr);
  218. $title = (!isset($controllerArr[$key]) ? $controllerTitle : '');
  219. $icon = (!isset($controllerArr[$key]) ? $controllerIcon : 'fa fa-list');
  220. $remark = (!isset($controllerArr[$key]) ? $controllerRemark : '');
  221. $title = $title ? $title : $v;
  222. $rulemodel = $this->model->get(['name' => $name]);
  223. if (!$rulemodel) {
  224. $this->model
  225. ->data(['pid' => $pid, 'name' => $name, 'title' => $title, 'icon' => $icon, 'remark' => $remark, 'ismenu' => 1, 'status' => 'normal'])
  226. ->isUpdate(false)
  227. ->save();
  228. $pid = $this->model->id;
  229. } else {
  230. $pid = $rulemodel->id;
  231. }
  232. }
  233. $ruleArr = [];
  234. foreach ($methods as $m => $n) {
  235. //过滤特殊的类
  236. if (substr($n->name, 0, 2) == '__' || $n->name == '_initialize') {
  237. continue;
  238. }
  239. //未启用软删除时过滤相关方法
  240. if (!$withSofeDelete && in_array($n->name, $softDeleteMethods)) {
  241. continue;
  242. }
  243. //只匹配符合的方法
  244. if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo)) {
  245. unset($methods[$m]);
  246. continue;
  247. }
  248. $comment = $reflector->getMethod($n->name)->getDocComment();
  249. //忽略的方法
  250. if (stripos($comment, "@internal") !== false) {
  251. continue;
  252. }
  253. //过滤掉其它字符
  254. $comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $comment);
  255. $title = $comment ? $comment : ucfirst($n->name);
  256. //获取主键,作为AuthRule更新依据
  257. $id = $this->getAuthRulePK($name . "/" . strtolower($n->name));
  258. $ruleArr[] = array('id' => $id, 'pid' => $pid, 'name' => $name . "/" . strtolower($n->name), 'icon' => 'fa fa-circle-o', 'title' => $title, 'ismenu' => 0, 'status' => 'normal');
  259. }
  260. $this->model->isUpdate(false)->saveAll($ruleArr);
  261. }
  262. //获取主键
  263. protected function getAuthRulePK($name)
  264. {
  265. if (!empty($name)) {
  266. $id = $this->model
  267. ->where('name', $name)
  268. ->value('id');
  269. return $id ? $id : null;
  270. }
  271. }
  272. }