Menu.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. $modelRegexArr = ["/\\\$this\->model\s*=\s*model\(['|\"](\w+)['|\"]\);/", "/\\\$this\->model\s*=\s*new\s+([a-zA-Z\\\]+);/"];
  178. $modelRegex = preg_match($modelRegexArr[0], $classContent) ? $modelRegexArr[0] : $modelRegexArr[1];
  179. preg_match_all($modelRegex, $classContent, $matches);
  180. if (isset($matches[1]) && isset($matches[1][0]) && $matches[1][0]) {
  181. \think\Request::instance()->module('admin');
  182. $model = model($matches[1][0]);
  183. if (in_array('trashed', get_class_methods($model))) {
  184. $withSofeDelete = true;
  185. }
  186. }
  187. //忽略的类
  188. if (stripos($classComment, "@internal") !== false) {
  189. return;
  190. }
  191. preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
  192. $controllerIcon = 'fa fa-circle-o';
  193. $controllerRemark = '';
  194. //判断注释中是否设置了icon值
  195. if (isset($annotations[1])) {
  196. foreach ($annotations[1] as $tag) {
  197. if (stripos($tag, '@icon') !== false) {
  198. $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
  199. }
  200. if (stripos($tag, '@remark') !== false) {
  201. $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
  202. }
  203. }
  204. }
  205. //过滤掉其它字符
  206. $controllerTitle = trim(preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $classComment));
  207. //导入中文语言包
  208. \think\Lang::load(dirname(__DIR__) . DS . 'lang/zh-cn.php');
  209. //先导入菜单的数据
  210. $pid = 0;
  211. foreach ($controllerArr as $k => $v) {
  212. $key = $k + 1;
  213. //驼峰转下划线
  214. $controllerNameArr = array_slice($controllerArr, 0, $key);
  215. foreach ($controllerNameArr as &$val) {
  216. $val = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $val), "_"));
  217. }
  218. unset($val);
  219. $name = implode('/', $controllerNameArr);
  220. $title = (!isset($controllerArr[$key]) ? $controllerTitle : '');
  221. $icon = (!isset($controllerArr[$key]) ? $controllerIcon : 'fa fa-list');
  222. $remark = (!isset($controllerArr[$key]) ? $controllerRemark : '');
  223. $title = $title ? $title : $v;
  224. $rulemodel = $this->model->get(['name' => $name]);
  225. if (!$rulemodel) {
  226. $this->model
  227. ->data(['pid' => $pid, 'name' => $name, 'title' => $title, 'icon' => $icon, 'remark' => $remark, 'ismenu' => 1, 'status' => 'normal'])
  228. ->isUpdate(false)
  229. ->save();
  230. $pid = $this->model->id;
  231. } else {
  232. $pid = $rulemodel->id;
  233. }
  234. }
  235. $ruleArr = [];
  236. foreach ($methods as $m => $n) {
  237. //过滤特殊的类
  238. if (substr($n->name, 0, 2) == '__' || $n->name == '_initialize') {
  239. continue;
  240. }
  241. //未启用软删除时过滤相关方法
  242. if (!$withSofeDelete && in_array($n->name, $softDeleteMethods)) {
  243. continue;
  244. }
  245. //只匹配符合的方法
  246. if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo)) {
  247. unset($methods[$m]);
  248. continue;
  249. }
  250. $comment = $reflector->getMethod($n->name)->getDocComment();
  251. //忽略的方法
  252. if (stripos($comment, "@internal") !== false) {
  253. continue;
  254. }
  255. //过滤掉其它字符
  256. $comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $comment);
  257. $title = $comment ? $comment : ucfirst($n->name);
  258. //获取主键,作为AuthRule更新依据
  259. $id = $this->getAuthRulePK($name . "/" . strtolower($n->name));
  260. $ruleArr[] = array('id' => $id, 'pid' => $pid, 'name' => $name . "/" . strtolower($n->name), 'icon' => 'fa fa-circle-o', 'title' => $title, 'ismenu' => 0, 'status' => 'normal');
  261. }
  262. $this->model->isUpdate(false)->saveAll($ruleArr);
  263. }
  264. //获取主键
  265. protected function getAuthRulePK($name)
  266. {
  267. if (!empty($name)) {
  268. $id = $this->model
  269. ->where('name', $name)
  270. ->value('id');
  271. return $id ? $id : null;
  272. }
  273. }
  274. }