1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| <?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class UnuseRoute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:UnuseRoute';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Unuse Route List';
const DOMAIN = 'Domain';
const METHOD = 'Method';
const URI = 'URI';
const NAME = 'Name';
const ACTION = 'Action';
const MIDDLEWARE = 'Middleware';
const CONTROLLER_EXCLUDE_METHOD = [
'__construct',
'__call',
'middleware',
'getMiddleware',
'callAction',
'authorize',
'authorizeForUser',
'authorizeResource',
'dispatchNow',
'dispatchSync',
'validateWith',
'validate',
'validateWithBag',
];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
exec('php artisan route:list', $result);
$result = array_slice($result, 3, count($result) - 4);
// Domain, Method, URI, Name, Action, Middleware
$routes = [];
foreach ($result as $value) {
$route = explode('| ', $value);
$route = array_map('trim', $route);
$routes[] = [
self::DOMAIN => $route[1],
self::METHOD => $route[2],
self::URI => $route[3],
self::NAME => $route[4],
self::ACTION => $route[5],
self::MIDDLEWARE => trim(str_replace('|', '', $route[6])),
];
}
// Closure を除外
$routes = array_filter($routes, function ($value) {
if (($value[self::ACTION] ?? null) == 'Closure') {
return false;
}
return true;
});
// Controllerファイル群取得
$files = $this->getFileList(app_path('Http/Controllers'));
$actions = array_column($routes, self::ACTION);
$controllers = [];
foreach ($actions as $action) {
$controller = explode('@', $action);
if (is_null($controller[1] ?? null)) {
continue;
}
$controllers[$controller[0]][] = $controller[1];
}
$controllerFiles = array_map(
function ($value) {
// ファイルパスから base_path を削除
$value = str_replace(base_path() . '/', '', $value);
// 拡張子を削除
$filepath = pathinfo($value);
$value = $filepath['dirname'] . '/' . $filepath['filename'];
// ファイルパスを namespace に修正
$value = str_replace('app/', 'App/', $value);
$value = str_replace('/', '\\', $value);
return $value;
},
$files
);
// Controllerに無いが定義されている(vendor系など
$this->info('App Controller not defined');
foreach (array_diff(array_keys($controllers), $controllerFiles) as $value) {
$this->comment($value);
}
// 使用されていないControllerファイル
$this->info(PHP_EOL.'Unuse Route Contorller Files');
foreach (array_diff($controllerFiles, array_keys($controllers)) as $value) {
$this->comment($value);
}
$this->info(PHP_EOL.'Unuse Route Controller Methods');
$usedControllers = array_intersect($controllerFiles, array_keys($controllers));
foreach ($usedControllers as $value) {
$controller = app()->make($value);
// 該当のControllerのメソッドを取得
$methods = get_class_methods($controller);
// Controllerの特定のメソッドを除外
$methods = array_diff($methods, self::CONTROLLER_EXCLUDE_METHOD);
// route で定義されているメソッドを除外
$methods = array_diff($methods, $controllers[$value]);
if (count($methods) > 0) {
$this->info($value);
$this->comment(implode(',', $methods));
}
}
return 0;
}
private function getFileList($dir)
{
$iterator = new RecursiveDirectoryIterator($dir);
$iterator = new RecursiveIteratorIterator($iterator);
$list = array();
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$list[] = $fileinfo->getPathname();
}
}
return $list;
}
}
|