Menu.php 13 KB

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