|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class PrettyPrintRoutes extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
protected $signature = 'pp:routes'; |
|
12
|
|
|
|
|
13
|
|
|
protected $description = 'Pretty print routes'; |
|
14
|
|
|
|
|
15
|
|
|
public function handle() |
|
16
|
|
|
{ |
|
17
|
|
|
$calls = config('microscope.write.routes', []); |
|
18
|
|
|
|
|
19
|
|
|
foreach ($calls as $call) { |
|
20
|
|
|
foreach ($call['args'] as $val) { |
|
21
|
|
|
\is_array($val) && ($val = \implode('@', $val)); |
|
22
|
|
|
$val = \trim($val ?? ''); |
|
23
|
|
|
|
|
24
|
|
|
$route = $this->deduceRoute($val); |
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Illuminate\Routing\Route $route |
|
27
|
|
|
*/ |
|
28
|
|
|
if ($route) { |
|
29
|
|
|
($call['function'] == 'microscope_write_route') && $this->writeIt($route, $call['file']); |
|
30
|
|
|
($call['function'] == 'microscope_pretty_print_route') && $this->printIt($route); |
|
31
|
|
|
} else { |
|
32
|
|
|
$this->info('Route name not found.'); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function writeIt($route, $filename) |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
|
|
$middlewares = $this->getMiddlewares($route); |
|
42
|
|
|
|
|
43
|
|
|
$methods = $route->methods(); |
|
44
|
|
|
($methods == ['GET', 'HEAD']) && $methods = ['GET']; |
|
45
|
|
|
|
|
46
|
|
|
$action = $this->getAction($route->getActionName()); |
|
47
|
|
|
|
|
48
|
|
|
if (\count($methods) == 1) { |
|
49
|
|
|
$definition = PHP_EOL.$this->getMovableRoute($route, $methods, $action, $middlewares); |
|
50
|
|
|
|
|
51
|
|
|
file_put_contents($filename, $definition, FILE_APPEND); |
|
52
|
|
|
} |
|
53
|
|
|
} catch (Exception $e) { |
|
54
|
|
|
$this->handleRouteProblem($e); |
|
55
|
|
|
|
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function deduceRoute($value) |
|
61
|
|
|
{ |
|
62
|
|
|
if (Str::containsAll($value, ['@', '\\'])) { |
|
63
|
|
|
$route = app('routes')->getByAction($value); |
|
64
|
|
|
} else { |
|
65
|
|
|
$route = app('routes')->getByName($value); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $route; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function printIt($route) |
|
72
|
|
|
{ |
|
73
|
|
|
try { |
|
74
|
|
|
$middlewares = $this->getMiddlewares($route); |
|
75
|
|
|
$this->prettyPrintInConsole($route, $middlewares); |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
$this->handleRouteProblem($e); |
|
78
|
|
|
|
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function getMovableRoute($route, $methods, $action, $middlewareSection) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($action == '\Illuminate\Routing\ViewController::class') { |
|
86
|
|
|
$method = 'view'; |
|
87
|
|
|
$uriAction = "('/".$route->uri()."', '".$route->defaults['view']."')"; |
|
88
|
|
|
$defaults = ''; |
|
89
|
|
|
} else { |
|
90
|
|
|
$method = strtolower($methods[0]); |
|
91
|
|
|
$uriAction = "('/".$route->uri()."', ".$action.')'; |
|
92
|
|
|
$defaults = $this->getDefaults($route->defaults); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$nameSection = ($route->getName() ? ("->name('".$route->getName()."')") : ''); |
|
96
|
|
|
|
|
97
|
|
|
return 'Route::'.$method.$uriAction.PHP_EOL.$middlewareSection.$nameSection.$defaults.';'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getAction($action) |
|
101
|
|
|
{ |
|
102
|
|
|
$action = Str::start($action, '\\'); |
|
103
|
|
|
if (! Str::contains($action, ['@'])) { |
|
104
|
|
|
return $action.'::class'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$action = \explode('@', $action); |
|
108
|
|
|
|
|
109
|
|
|
return '['.$action[0].'::class'.", '".$action[1]."']"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function getMiddlewares($route) |
|
113
|
|
|
{ |
|
114
|
|
|
$middlewares = $route->gatherMiddleware(); |
|
115
|
|
|
$middlewares && $middlewares = "'".\implode("', '", $route->gatherMiddleware())."'"; |
|
116
|
|
|
|
|
117
|
|
|
return $middlewares ? '->middleware(['.$middlewares.'])' : ''; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function handleRouteProblem($e) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->info('The route has some problem.'); |
|
123
|
|
|
$this->info($e->getMessage()); |
|
124
|
|
|
$this->info($e->getFile()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function prettyPrintInConsole($route, $middlewares) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->getOutput()->writeln('---------------------------------------------------'); |
|
130
|
|
|
$this->info(' name: '.($route->getName() ? ($route->getName()) : '')); |
|
131
|
|
|
$this->info(' uri: '.\implode(', ', $route->methods())." '/".$route->uri()."' "); |
|
132
|
|
|
$this->info(' middlewares: '.$middlewares); |
|
133
|
|
|
$this->info(' action: '.$route->getActionName()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private function getDefaults($values) |
|
137
|
|
|
{ |
|
138
|
|
|
$defaults = ''; |
|
139
|
|
|
foreach ($values as $key => $val) { |
|
140
|
|
|
$defaults .= "\n".'->defaults('.var_export($key, true).', '.var_export($val, true).')'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $defaults; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|