RouteList.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\command;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Argument;
  15. use think\console\input\Option;
  16. use think\console\Output;
  17. use think\console\Table;
  18. use think\Container;
  19. class RouteList extends Command
  20. {
  21. protected $sortBy = [
  22. 'rule' => 0,
  23. 'route' => 1,
  24. 'method' => 2,
  25. 'name' => 3,
  26. 'domain' => 4,
  27. ];
  28. protected function configure()
  29. {
  30. $this->setName('route:list')
  31. ->addArgument('style', Argument::OPTIONAL, "the style of the table.", 'default')
  32. ->addOption('sort', 's', Option::VALUE_OPTIONAL, 'order by rule name.', 0)
  33. ->addOption('more', 'm', Option::VALUE_NONE, 'show route options.')
  34. ->setDescription('show route list.');
  35. }
  36. protected function execute(Input $input, Output $output)
  37. {
  38. $filename = Container::get('app')->getRuntimePath() . 'route_list.php';
  39. if (is_file($filename)) {
  40. unlink($filename);
  41. }
  42. $content = $this->getRouteList();
  43. file_put_contents($filename, 'Route List' . PHP_EOL . $content);
  44. }
  45. protected function getRouteList()
  46. {
  47. Container::get('route')->setTestMode(true);
  48. // 路由检测
  49. $path = Container::get('app')->getRoutePath();
  50. $files = is_dir($path) ? scandir($path) : [];
  51. foreach ($files as $file) {
  52. if (strpos($file, '.php')) {
  53. $filename = $path . DIRECTORY_SEPARATOR . $file;
  54. // 导入路由配置
  55. $rules = include $filename;
  56. if (is_array($rules)) {
  57. Container::get('route')->import($rules);
  58. }
  59. }
  60. }
  61. if (Container::get('config')->get('route_annotation')) {
  62. $suffix = Container::get('config')->get('controller_suffix') || Container::get('config')->get('class_suffix');
  63. include Container::get('build')->buildRoute($suffix);
  64. }
  65. $table = new Table();
  66. if ($this->input->hasOption('more')) {
  67. $header = ['Rule', 'Route', 'Method', 'Name', 'Domain', 'Option', 'Pattern'];
  68. } else {
  69. $header = ['Rule', 'Route', 'Method', 'Name', 'Domain'];
  70. }
  71. $table->setHeader($header);
  72. $routeList = Container::get('route')->getRuleList();
  73. $rows = [];
  74. foreach ($routeList as $domain => $items) {
  75. foreach ($items as $item) {
  76. $item['route'] = $item['route'] instanceof \Closure ? '<Closure>' : $item['route'];
  77. if ($this->input->hasOption('more')) {
  78. $item = [$item['rule'], $item['route'], $item['method'], $item['name'], $domain, json_encode($item['option']), json_encode($item['pattern'])];
  79. } else {
  80. $item = [$item['rule'], $item['route'], $item['method'], $item['name'], $domain];
  81. }
  82. $rows[] = $item;
  83. }
  84. }
  85. if ($this->input->getOption('sort')) {
  86. $sort = $this->input->getOption('sort');
  87. if (isset($this->sortBy[$sort])) {
  88. $sort = $this->sortBy[$sort];
  89. }
  90. uasort($rows, function ($a, $b) use ($sort) {
  91. $itemA = isset($a[$sort]) ? $a[$sort] : null;
  92. $itemB = isset($b[$sort]) ? $b[$sort] : null;
  93. return strcasecmp($itemA, $itemB);
  94. });
  95. }
  96. $table->setRows($rows);
  97. if ($this->input->getArgument('style')) {
  98. $style = $this->input->getArgument('style');
  99. $table->setStyle($style);
  100. }
  101. return $this->table($table);
  102. }
  103. }