updateMenu.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if($this->routeList())
  31. $output->writeln('执行成功');
  32. }
  33. /**
  34. * @Author:Qinii
  35. * @Date: 2020/5/15
  36. * @param string|null $dir
  37. * @return mixed
  38. */
  39. public function routeList(string $dir = null)
  40. {
  41. $this->app->route->setTestMode(true);
  42. $this->app->route->clear();
  43. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  44. include $path . 'admin.php';
  45. include $path . 'merchant.php';
  46. // $files = is_dir($path) ? scandir($path) : [];
  47. // foreach ($files as $file) {
  48. // if (strpos($file, '.php')) {
  49. // include $path . $file;
  50. // }
  51. // }
  52. //触发路由载入完成事件
  53. $this->app->event->trigger(RouteLoaded::class);
  54. $routeList = $this->app->route->getRuleList();
  55. $rows = [];
  56. $name = [];
  57. foreach ($routeList as $k => $item) {
  58. if (!(strpos($item['name'], '/') !== false) && !(strpos($item['name'], '@') !== false) && is_string($item['route'])) {
  59. $rule = Route::getRule($item['rule']);
  60. $group = ($rule[$item['route']])->getParent();
  61. $groupRoute = $this->getGroupTree($group);
  62. $groupName = !empty($group->getName()) ? $group->getName() : 'route';
  63. if (in_array($item['name'], $name))
  64. throw new Exception( "< " . $item['name'] . ' >路由名重复');
  65. $name[] = $item['name'];
  66. $rows[$groupRoute][$groupName][] = $item['name'];
  67. }
  68. }
  69. app()->make(MenuRepository::class)->commandCreate($rows);
  70. }
  71. /**
  72. * @Author:Qinii
  73. * @Date: 2020/5/15
  74. * @param $group
  75. * @return mixed
  76. */
  77. protected function getGroupTree($group)
  78. {
  79. $name = $group->getName();
  80. if (in_array($name, ['sys','mer'])) {
  81. return $name;
  82. } else {
  83. return $this->getGroupTree($group->getParent());
  84. }
  85. }
  86. }