123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- // +----------------------------------------------------------------------
- // | Yzncms [ 御宅男工作室 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018 http://yzncms.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | fastadmin: https://www.fastadmin.net/
- // +----------------------------------------------------------------------
-
- // +----------------------------------------------------------------------
- // | 菜单主程序
- // +----------------------------------------------------------------------
-
- namespace app\admin\command;
-
- use app\admin\model\AuthRule;
- use ReflectionClass;
- use ReflectionMethod;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use think\Exception;
- use think\facade\Cache;
- use think\facade\Config;
- use think\Loader;
-
- class Menu extends Command
- {
- protected $model = null;
-
- protected function configure()
- {
- $this
- ->setName('menu')
- ->addOption('controller', 'c', Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, 'controller name,use \'all-controller\' when build all menu', null)
- ->addOption('delete', 'd', Option::VALUE_OPTIONAL, 'delete the specified menu', '')
- ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force delete menu,without tips', null)
- ->addOption('equal', 'e', Option::VALUE_OPTIONAL, 'the controller must be equal', null)
- ->setDescription('Build auth menu from controller');
- //要执行的controller必须一样,不适用模糊查询
- }
-
- protected function execute(Input $input, Output $output)
- {
- $this->model = new AuthRule();
- $adminPath = dirname(__DIR__) . DS;
- //控制器名
- $controller = $input->getOption('controller') ?: '';
- if (!$controller) {
- throw new Exception("please input controller name");
- }
- $force = $input->getOption('force');
- //是否为删除模式
- $delete = $input->getOption('delete');
- //是否控制器完全匹配
- $equal = $input->getOption('equal');
-
- if ($delete) {
- $ids = [];
- $list = $this->model->where(function ($query) use ($controller, $equal) {
- foreach ($controller as $index => $item) {
- if (stripos($item, '_') !== false) {
- $item = Loader::parseName($item, 1);
- }
- if (stripos($item, '/') !== false) {
- $controllerArr = explode('/', $item);
- end($controllerArr);
- $key = key($controllerArr);
- $controllerArr[$key] = Loader::parseName($controllerArr[$key]);
- } else {
- $controllerArr = [Loader::parseName($item)];
- }
- $item = str_replace('_', '\_', implode('/', $controllerArr));
- if ($equal) {
- $query->whereOr('name', 'eq', $item);
- } else {
- $query->whereOr('name', 'like', strtolower($item) . "%");
- }
- }
- })->select();
- foreach ($list as $k => $v) {
- $output->warning($v->name);
- $ids[] = $v->id;
- }
- if (!$ids) {
- throw new Exception("There is no menu to delete");
- }
- if (!$force) {
- $question = $output->confirm($input, "Are you sure you want to delete all those menu? Type 'yes' to continue: ", false);
- if (!$question) {
- throw new Exception("Operation is aborted!");
- }
- }
- AuthRule::destroy($ids);
-
- Cache::rm("__menu__");
- $output->info("Delete Successed");
- return;
- }
-
- foreach ($controller as $index => $item) {
- if (stripos($item, '_') !== false) {
- $item = Loader::parseName($item, 1);
- }
- if (stripos($item, '/') !== false) {
- $controllerArr = explode('/', $item);
- end($controllerArr);
- $key = key($controllerArr);
- $controllerArr[$key] = ucfirst($controllerArr[$key]);
- } else {
- $controllerArr = [ucfirst($item)];
- }
- $adminPath = dirname(__DIR__) . DS . 'controller' . DS . implode(DS, $controllerArr) . '.php';
- if (!is_file($adminPath)) {
- $output->error("controller not found");
- return;
- }
- $this->importRule($item);
- }
-
- Cache::rm("__menu__");
- $output->info("Build Successed!");
- }
-
- protected function importRule($controller)
- {
- $controller = str_replace('\\', '/', $controller);
- if (stripos($controller, '/') !== false) {
- $controllerArr = explode('/', $controller);
- end($controllerArr);
- $key = key($controllerArr);
- $controllerArr[$key] = ucfirst($controllerArr[$key]);
- } else {
- $key = 0;
- $controllerArr = [ucfirst($controller)];
- }
- $classSuffix = Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
- $className = "\\app\\admin\\controller\\" . implode("\\", $controllerArr) . $classSuffix;
-
- $pathArr = $controllerArr;
- array_unshift($pathArr, '', 'application', 'admin', 'controller');
- $classFile = ROOT_PATH . implode(DS, $pathArr) . $classSuffix . ".php";
- $classContent = file_get_contents($classFile);
- $uniqueName = uniqid("YznCMS") . $classSuffix;
- $classContent = str_replace("class " . $controllerArr[$key] . $classSuffix . " ", 'class ' . $uniqueName . ' ', $classContent);
- $classContent = preg_replace("/namespace\s(.*);/", 'namespace ' . __NAMESPACE__ . ";", $classContent);
-
- //临时的类文件
- $tempClassFile = __DIR__ . DS . $uniqueName . ".php";
- file_put_contents($tempClassFile, $classContent);
-
- $className = "\\app\\admin\\command\\" . $uniqueName;
-
- //删除临时文件
- register_shutdown_function(function () use ($tempClassFile) {
- if ($tempClassFile) {
- //删除临时文件
- @unlink($tempClassFile);
- }
- });
-
- //反射机制调用类的注释和方法名
- $reflector = new ReflectionClass($className);
-
- //只匹配公共的方法
- $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
-
- $classComment = $reflector->getDocComment();
- //判断是否有启用软删除
- $softDeleteMethods = ['destroy', 'restore', 'recyclebin'];
- $withSofeDelete = false;
- $modelRegexArr = ["/\\\$this\->model\s*=\s*model\(['|\"](\w+)['|\"]\);/", "/\\\$this\->model\s*=\s*new\s+([a-zA-Z\\\]+);/"];
- $modelRegex = preg_match($modelRegexArr[0], $classContent) ? $modelRegexArr[0] : $modelRegexArr[1];
-
- preg_match_all($modelRegex, $classContent, $matches);
- if (isset($matches[1]) && isset($matches[1][0]) && $matches[1][0]) {
- \think\facade\Request::instance()->module('admin');
- $model = model($matches[1][0]);
- if (in_array('trashed', get_class_methods($model))) {
- $withSofeDelete = true;
- }
- }
- //忽略的类
- if (stripos($classComment, "@internal") !== false) {
- return;
- }
- preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
- $controllerIcon = 'iconfont icon-other';
- $controllerRemark = '';
- //判断注释中是否设置了icon值
- if (isset($annotations[1])) {
- foreach ($annotations[1] as $tag) {
- if (stripos($tag, '@icon') !== false) {
- $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
- }
- if (stripos($tag, '@remark') !== false) {
- $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
- }
- }
- }
- //过滤掉其它字符
- $controllerTitle = trim(preg_replace(['/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'], '', $classComment));
-
- //先导入菜单的数据
- $pid = 0;
- foreach ($controllerArr as $k => $v) {
- $key = $k + 1;
- //驼峰转下划线
- $controllerNameArr = array_slice($controllerArr, 0, $key);
- foreach ($controllerNameArr as &$val) {
- $val = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $val), "_"));
- }
- unset($val);
- $name = implode('.', $controllerNameArr);
- $title = (!isset($controllerArr[$key]) ? $controllerTitle : '');
- $icon = (!isset($controllerArr[$key]) ? $controllerIcon : 'iconfont icon-other');
- $remark = (!isset($controllerArr[$key]) ? $controllerRemark : '');
- $title = $title ? $title : $v;
- $rulemodel = $this->model->get(['name' => $name]);
- if (!$rulemodel) {
- $this->model
- ->data(['parentid' => $pid, 'name' => $name, 'title' => $title, 'icon' => $icon, 'remark' => $remark, 'ismenu' => 1, 'status' => 1])
- ->isUpdate(false)
- ->save();
- $pid = $this->model->id;
- } else {
- $pid = $rulemodel->id;
- }
- }
- $ruleArr = [];
- foreach ($methods as $m => $n) {
- //过滤特殊的类
- if (in_array($n->name, ['initialize', '_empty', 'registerMiddleware']) || substr($n->name, 0, 2) == '__') {
- continue;
- }
- //未启用软删除时过滤相关方法
- if (!$withSofeDelete && in_array($n->name, $softDeleteMethods)) {
- continue;
- }
- //只匹配符合的方法
- if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo)) {
- unset($methods[$m]);
- continue;
- }
- $comment = $reflector->getMethod($n->name)->getDocComment();
- //忽略的方法
- if (stripos($comment, "@internal") !== false) {
- continue;
- }
- //过滤掉其它字符
- $comment = preg_replace(['/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'], '', $comment);
-
- $title = $comment ? $comment : ucfirst($n->name);
-
- //获取主键,作为AuthRule更新依据
- $id = $this->getAuthRulePK($name . "/" . strtolower($n->name));
-
- $ruleArr[] = ['id' => $id, 'parentid' => $pid, 'name' => $name . "/" . strtolower($n->name), 'icon' => 'iconfont icon-circle-line', 'title' => $title, 'ismenu' => 0, 'status' => 1];
- }
- $this->model->isUpdate(false)->saveAll($ruleArr);
- }
-
- //获取主键
- protected function getAuthRulePK($name)
- {
- if (!empty($name)) {
- $id = $this->model
- ->where('name', $name)
- ->value('id');
- return $id ? $id : null;
- }
- }
- }
|