updateMenu.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use Swoole\Coroutine\MySQL\Exception;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\input\Argument;
  8. use think\console\input\Option;
  9. use think\console\Output;
  10. use think\event\RouteLoaded;
  11. use think\facade\Route;
  12. use app\common\repositories\system\auth\MenuRepository;
  13. class updateMenu extends Command
  14. {
  15. protected function configure()
  16. {
  17. // 指令配置
  18. $this->setName('menu')
  19. ->setDescription('the menu command');
  20. }
  21. /**
  22. * @Author:Qinii
  23. * @Date: 2020/5/15
  24. * @param Input $input
  25. * @param Output $output
  26. * @return int|void|null
  27. */
  28. protected function execute(Input $input, Output $output)
  29. {
  30. $output->writeln('开始执行');
  31. $output->writeln('---------------->');
  32. $count = $this->routeList();
  33. $output->writeln('<----------------');
  34. $output->writeln('执行成功,合计'.$count.'条');
  35. }
  36. /**
  37. * @Author:Qinii
  38. * @Date: 2020/5/15
  39. * @param string|null $dir
  40. * @return mixed
  41. */
  42. public function routeList(string $dir = null)
  43. {
  44. $this->app->route->setTestMode(true);
  45. $this->app->route->clear();
  46. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  47. include $path . 'admin.php';
  48. include $path . 'merchant.php';
  49. // $files = is_dir($path) ? scandir($path) : [];
  50. // foreach ($files as $file) {
  51. // if (strpos($file, '.php')) {
  52. // include $path . $file;
  53. // }
  54. // }
  55. //触发路由载入完成事件
  56. $this->app->event->trigger(RouteLoaded::class);
  57. $routeList = $this->app->route->getRuleList();
  58. $rows = [];
  59. $name = [];
  60. foreach ($routeList as $k => $item) {
  61. if (!(strpos($item['name'], '/') !== false) && !(strpos($item['name'], '@') !== false) && is_string($item['route'])) {
  62. $rule = Route::getRule($item['rule']);
  63. $group = ($rule[$item['route']])->getParent();
  64. $groupRoute = $this->getGroupTree($group);
  65. $groupName = !empty($group->getName()) ? $group->getName() : 'route';
  66. if (in_array($item['name'], $name))
  67. throw new Exception( "< " . $item['name'] . ' >路由名重复');
  68. $name[] = $item['name'];
  69. $rows[$groupRoute][$groupName][] = $item['name'];
  70. }
  71. }
  72. return app()->make(MenuRepository::class)->commandCreate($rows);
  73. }
  74. /**
  75. * @Author:Qinii
  76. * @Date: 2020/5/15
  77. * @param $group
  78. * @return mixed
  79. */
  80. protected function getGroupTree($group)
  81. {
  82. $name = $group->getName();
  83. if (in_array($name, ['sys','mer'])) {
  84. return $name;
  85. } else {
  86. return $this->getGroupTree($group->getParent());
  87. }
  88. }
  89. }