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 <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
namespace think\console\command; |
12
|
|
|
|
13
|
|
|
use think\console\Command; |
14
|
|
|
use think\console\Input; |
15
|
|
|
use think\console\input\Argument; |
16
|
|
|
use think\console\input\Option; |
17
|
|
|
use think\console\Output; |
18
|
|
|
use think\console\Table; |
19
|
|
|
use think\event\RouteLoaded; |
20
|
|
|
|
21
|
|
|
class RouteList extends Command |
22
|
|
|
{ |
23
|
|
|
protected $sortBy = [ |
24
|
|
|
'rule' => 0, |
25
|
|
|
'route' => 1, |
26
|
|
|
'method' => 2, |
27
|
|
|
'name' => 3, |
28
|
|
|
'domain' => 4, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected function configure() |
32
|
|
|
{ |
33
|
|
|
$this->setName('route:list') |
34
|
|
|
->addArgument('dir', Argument::OPTIONAL, 'dir name .') |
35
|
|
|
->addArgument('style', Argument::OPTIONAL, "the style of the table.", 'default') |
36
|
|
|
->addOption('sort', 's', Option::VALUE_OPTIONAL, 'order by rule name.', 0) |
37
|
|
|
->addOption('more', 'm', Option::VALUE_NONE, 'show route options.') |
38
|
|
|
->setDescription('show route list.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function execute(Input $input, Output $output) |
42
|
|
|
{ |
43
|
|
|
$dir = $input->getArgument('dir') ?: ''; |
44
|
|
|
|
45
|
|
|
$filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . ($dir ? $dir . DIRECTORY_SEPARATOR : '') . 'route_list.php'; |
|
|
|
|
46
|
|
|
|
47
|
|
|
if (is_file($filename)) { |
48
|
|
|
unlink($filename); |
49
|
|
|
} elseif (!is_dir(dirname($filename))) { |
50
|
|
|
mkdir(dirname($filename), 0755); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$content = $this->getRouteList($dir); |
|
|
|
|
54
|
|
|
file_put_contents($filename, 'Route List' . PHP_EOL . $content); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getRouteList(string $dir = null): string |
58
|
|
|
{ |
59
|
|
|
$this->app->route->setTestMode(true); |
60
|
|
|
$this->app->route->clear(); |
61
|
|
|
|
62
|
|
|
if ($dir) { |
63
|
|
|
$path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
64
|
|
|
} else { |
65
|
|
|
$path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$files = is_dir($path) ? scandir($path) : []; |
69
|
|
|
|
70
|
|
|
foreach ($files as $file) { |
71
|
|
|
if (strpos($file, '.php')) { |
72
|
|
|
include $path . $file; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//触发路由载入完成事件 |
77
|
|
|
$this->app->event->trigger(RouteLoaded::class); |
78
|
|
|
|
79
|
|
|
$table = new Table(); |
80
|
|
|
|
81
|
|
|
if ($this->input->hasOption('more')) { |
82
|
|
|
$header = ['Rule', 'Route', 'Method', 'Name', 'Domain', 'Option', 'Pattern']; |
83
|
|
|
} else { |
84
|
|
|
$header = ['Rule', 'Route', 'Method', 'Name']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$table->setHeader($header); |
88
|
|
|
|
89
|
|
|
$routeList = $this->app->route->getRuleList(); |
90
|
|
|
$rows = []; |
91
|
|
|
|
92
|
|
|
foreach ($routeList as $item) { |
93
|
|
|
$item['route'] = $item['route'] instanceof \Closure ? '<Closure>' : $item['route']; |
94
|
|
|
$row = [$item['rule'], $item['route'], $item['method'], $item['name']]; |
95
|
|
|
|
96
|
|
|
if ($this->input->hasOption('more')) { |
97
|
|
|
array_push($row, $item['domain'], json_encode($item['option']), json_encode($item['pattern'])); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$rows[] = $row; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->input->getOption('sort')) { |
104
|
|
|
$sort = strtolower($this->input->getOption('sort')); |
|
|
|
|
105
|
|
|
|
106
|
|
|
if (isset($this->sortBy[$sort])) { |
107
|
|
|
$sort = $this->sortBy[$sort]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
uasort($rows, function ($a, $b) use ($sort) { |
111
|
|
|
$itemA = $a[$sort] ?? null; |
112
|
|
|
$itemB = $b[$sort] ?? null; |
113
|
|
|
|
114
|
|
|
return strcasecmp($itemA, $itemB); |
|
|
|
|
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$table->setRows($rows); |
119
|
|
|
|
120
|
|
|
if ($this->input->getArgument('style')) { |
121
|
|
|
$style = $this->input->getArgument('style'); |
122
|
|
|
$table->setStyle($style); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->table($table); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|