|
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('app', Argument::OPTIONAL, 'app 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
|
|
|
$app = $input->getArgument('app'); |
|
44
|
|
|
|
|
45
|
|
|
if ($app) { |
|
46
|
|
|
$filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'route_list.php'; |
|
|
|
|
|
|
47
|
|
|
} else { |
|
48
|
|
|
$filename = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'route_list.php'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (is_file($filename)) { |
|
52
|
|
|
unlink($filename); |
|
53
|
|
|
} elseif (!is_dir(dirname($filename))) { |
|
54
|
|
|
mkdir(dirname($filename), 0755); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$content = $this->getRouteList($app); |
|
58
|
|
|
file_put_contents($filename, 'Route List' . PHP_EOL . $content); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function getRouteList(string $app = null): string |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$this->app->route->setTestMode(true); |
|
64
|
|
|
$this->app->route->clear(); |
|
65
|
|
|
|
|
66
|
|
|
if ($app && $this->isMultiApp()) { |
|
67
|
|
|
$file = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $app . '.php'; |
|
68
|
|
|
if (is_file($file)) { |
|
69
|
|
|
include $file; |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
$path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR; |
|
73
|
|
|
$files = is_dir($path) ? scandir($path) : []; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($files as $file) { |
|
76
|
|
|
if (strpos($file, '.php')) { |
|
77
|
|
|
if ($this->isMultiApp()) { |
|
78
|
|
|
$this->app->route->setAppName(pathinfo($file, PATHINFO_FILENAME)); |
|
79
|
|
|
} |
|
80
|
|
|
include $path . $file; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//触发路由载入完成事件 |
|
86
|
|
|
$this->app->event->trigger(RouteLoaded::class); |
|
87
|
|
|
|
|
88
|
|
|
$table = new Table(); |
|
89
|
|
|
|
|
90
|
|
|
if ($this->input->hasOption('more')) { |
|
91
|
|
|
$header = ['Rule', 'Route', 'Method', 'Name', 'Domain', 'Option', 'Pattern']; |
|
92
|
|
|
} else { |
|
93
|
|
|
$header = ['Rule', 'Route', 'Method', 'Name']; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$table->setHeader($header); |
|
97
|
|
|
|
|
98
|
|
|
$routeList = $this->app->route->getRuleList(); |
|
99
|
|
|
$rows = []; |
|
100
|
|
|
|
|
101
|
|
|
foreach ($routeList as $item) { |
|
102
|
|
|
$item['route'] = $item['route'] instanceof \Closure ? '<Closure>' : $item['route']; |
|
103
|
|
|
|
|
104
|
|
|
if ($this->input->hasOption('more')) { |
|
105
|
|
|
$item = [$item['rule'], $item['route'], $item['method'], $item['name'], $item['domain'], json_encode($item['option']), json_encode($item['pattern'])]; |
|
106
|
|
|
} else { |
|
107
|
|
|
$item = [$item['rule'], $item['route'], $item['method'], $item['name']]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$rows[] = $item; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($this->input->getOption('sort')) { |
|
114
|
|
|
$sort = strtolower($this->input->getOption('sort')); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
if (isset($this->sortBy[$sort])) { |
|
117
|
|
|
$sort = $this->sortBy[$sort]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
uasort($rows, function ($a, $b) use ($sort) { |
|
|
|
|
|
|
121
|
|
|
$itemA = $a[$sort] ?? null; |
|
122
|
|
|
$itemB = $b[$sort] ?? null; |
|
123
|
|
|
|
|
124
|
|
|
return strcasecmp($itemA, $itemB); |
|
125
|
|
|
}); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$table->setRows($rows); |
|
129
|
|
|
|
|
130
|
|
|
if ($this->input->getArgument('style')) { |
|
131
|
|
|
$style = $this->input->getArgument('style'); |
|
132
|
|
|
$table->setStyle($style); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this->table($table); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|