1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope; |
4
|
|
|
|
5
|
|
|
use Faker\Generator as FakerGenerator; |
6
|
|
|
use Illuminate\Contracts\Auth\Access\Gate as GateContract; |
7
|
|
|
use Illuminate\Contracts\Queue\Factory as QueueFactoryContract; |
8
|
|
|
use Illuminate\Database\Eloquent\Factory as EloquentFactory; |
9
|
|
|
use Illuminate\Support\Facades\Event; |
10
|
|
|
use Illuminate\Support\Facades\Log; |
11
|
|
|
use Illuminate\Support\Facades\Route; |
12
|
|
|
use Illuminate\Support\ServiceProvider; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Illuminate\View\View; |
15
|
|
|
use Imanghafoori\LaravelMicroscope\Checks\CheckClassReferences; |
16
|
|
|
use Imanghafoori\LaravelMicroscope\Commands\CheckViews; |
17
|
|
|
use Imanghafoori\LaravelMicroscope\ErrorReporters\ConsolePrinterInstaller; |
18
|
|
|
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter; |
19
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyBladeCompiler; |
20
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyDispatcher; |
21
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyFactory; |
22
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyGate; |
23
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyRouter; |
24
|
|
|
use Imanghafoori\LaravelMicroscope\SpyClasses\ViewsData; |
25
|
|
|
|
26
|
|
|
class LaravelMicroscopeServiceProvider extends ServiceProvider |
27
|
|
|
{ |
28
|
|
|
private static $commandNames = [ |
29
|
|
|
Commands\CheckEvents::class, |
30
|
|
|
Commands\CheckGates::class, |
31
|
|
|
Commands\CheckRoutes::class, |
32
|
|
|
Commands\CheckViews::class, |
33
|
|
|
Commands\CheckPsr4::class, |
34
|
|
|
Commands\CheckImports::class, |
35
|
|
|
Commands\CheckAll::class, |
36
|
|
|
Commands\ClassifyStrings::class, |
37
|
|
|
Commands\CheckDD::class, |
38
|
|
|
Commands\CheckEarlyReturns::class, |
39
|
|
|
Commands\CheckCompact::class, |
40
|
|
|
Commands\CheckBladeQueries::class, |
41
|
|
|
Commands\CheckActionComments::class, |
42
|
|
|
Commands\CheckBadPractice::class, |
43
|
|
|
Commands\CheckExtractBladeIncludes::class, |
44
|
|
|
Commands\PrettyPrintRoutes::class, |
45
|
|
|
Commands\CheckExpansions::class, |
46
|
|
|
Commands\CheckDeadControllers::class, |
47
|
|
|
Commands\CheckGenericActionComments::class, |
48
|
|
|
Commands\CheckPsr12::class, |
49
|
|
|
Commands\CheckEndIf::class, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
public function boot() |
53
|
|
|
{ |
54
|
|
|
(app()['env'] !== 'production') && config('microscope.log_unused_view_vars', true) && $this->spyView(); |
55
|
|
|
|
56
|
|
|
if (! $this->canRun()) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
Event::listen('microscope.start.command', function () { |
61
|
|
|
! defined('microscope_start') && define('microscope_start', microtime(true)); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->resetCountersOnFinish(); |
65
|
|
|
|
66
|
|
|
$this->commands(self::$commandNames); |
67
|
|
|
|
68
|
|
|
$this->publishes([ |
69
|
|
|
__DIR__.'/../config/config.php' => config_path('microscope.php'), |
70
|
|
|
], 'config'); |
71
|
|
|
|
72
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'microscope'); |
73
|
|
|
|
74
|
|
|
ConsolePrinterInstaller::boot(); |
75
|
|
|
|
76
|
|
|
Event::listen('microscope.checking', function ($path, $command) { |
77
|
|
|
$command->line('Checking: '.$path); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function register() |
82
|
|
|
{ |
83
|
|
|
! defined('T_NAME_QUALIFIED') && define('T_NAME_QUALIFIED', 3030); |
84
|
|
|
! defined('T_NAME_FULLY_QUALIFIED') && define('T_NAME_FULLY_QUALIFIED', 3031); |
85
|
|
|
|
86
|
|
|
if (! $this->canRun()) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
$this->spyEvents(); |
90
|
|
|
|
91
|
|
|
$this->registerCompiler(); |
92
|
|
|
|
93
|
|
|
$this->loadConfig(); |
94
|
|
|
|
95
|
|
|
app()->singleton(ErrorPrinter::class); |
96
|
|
|
// also we should spy the factory paths. |
97
|
|
|
$this->spyRouter(); |
98
|
|
|
if (class_exists('Illuminate\Database\Eloquent\Factory')) { |
99
|
|
|
$this->spyFactory(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// We need to start spying before the boot process starts. |
103
|
|
|
$command = $_SERVER['argv'][1] ?? null; |
104
|
|
|
// We spy the router in order to have a list of route files. |
105
|
|
|
$checkAll = Str::startsWith('check:all', $command); |
106
|
|
|
($checkAll || Str::startsWith('check:routes', $command)) && app('router')->spyRouteConflict(); |
107
|
|
|
Str::startsWith('check:action_comment', $command) && app('router')->spyRouteConflict(); |
108
|
|
|
// ($checkAll || Str::startsWith('check:events', $command)) && $this->spyEvents(); |
109
|
|
|
($checkAll || Str::startsWith('check:gates', $command)) && $this->spyGates(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function spyRouter() |
113
|
|
|
{ |
114
|
|
|
$router = new SpyRouter(app('events'), app()); |
115
|
|
|
$this->app->singleton('router', function ($app) use ($router) { |
|
|
|
|
116
|
|
|
return $router; |
117
|
|
|
}); |
118
|
|
|
Route::swap($router); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function spyFactory() |
122
|
|
|
{ |
123
|
|
|
$this->app->singleton(EloquentFactory::class, function ($app) { |
124
|
|
|
return SpyFactory::construct( |
125
|
|
|
$app->make(FakerGenerator::class), $app->databasePath('factories') |
126
|
|
|
); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function spyGates() |
131
|
|
|
{ |
132
|
|
|
$this->app->singleton(GateContract::class, function ($app) { |
133
|
|
|
return new SpyGate($app, function () use ($app) { |
134
|
|
|
return call_user_func($app['auth']->userResolver()); |
135
|
|
|
}); |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function spyEvents() |
140
|
|
|
{ |
141
|
|
|
app()->booting(function () { |
142
|
|
|
$this->app->singleton('events', function ($app) { |
143
|
|
|
return (new SpyDispatcher($app))->setQueueResolver(function () use ($app) { |
144
|
|
|
return $app->make(QueueFactoryContract::class); |
145
|
|
|
}); |
146
|
|
|
}); |
147
|
|
|
Event::clearResolvedInstance('events'); |
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function spyView() |
152
|
|
|
{ |
153
|
|
|
app()->singleton('microscope.views', ViewsData::class); |
154
|
|
|
|
155
|
|
|
\View::creator('*', function (View $view) { |
156
|
|
|
resolve('microscope.views')->add($view); |
157
|
|
|
}); |
158
|
|
|
|
159
|
|
|
app()->terminating(function () { |
160
|
|
|
$spy = resolve('microscope.views'); |
161
|
|
|
if (! $spy->main || Str::startsWith($spy->main->getName(), ['errors::'])) { |
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$action = $this->getActionName(); |
166
|
|
|
|
167
|
|
|
$uselessVars = array_keys(array_diff_key($spy->getMainVars(), $spy->readTokenizedVars())); |
168
|
|
|
$viewName = $spy->main->getName(); |
169
|
|
|
|
170
|
|
|
$uselessVars && $this->logUnusedViewVars($viewName, $action, $uselessVars); |
|
|
|
|
171
|
|
|
}); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private function loadConfig() |
175
|
|
|
{ |
176
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'microscope'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function canRun() |
180
|
|
|
{ |
181
|
|
|
return $this->app->runningInConsole() && config('microscope.is_enabled', true) && ! $this->app->runningUnitTests() && app()['env'] !== 'production'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getActionName() |
185
|
|
|
{ |
186
|
|
|
$cRoute = \Route::getCurrentRoute(); |
187
|
|
|
|
188
|
|
|
return $cRoute ? $cRoute->getActionName() : ''; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function registerCompiler() |
192
|
|
|
{ |
193
|
|
|
$this->app->singleton('microscope.blade.compiler', function () { |
194
|
|
|
return new SpyBladeCompiler($this->app['files'], $this->app['config']['view.compiled']); |
195
|
|
|
}); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private function resetCountersOnFinish() |
199
|
|
|
{ |
200
|
|
|
Event::listen('microscope.finished.checks', function () { |
201
|
|
|
CheckViews::$checkedCallsNum = 0; |
202
|
|
|
CheckClassReferences::$refCount = 0; |
203
|
|
|
Psr4Classes::$checkedFilesNum = 0; |
204
|
|
|
}); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
private function logUnusedViewVars($viewName, string $action, array $uselessVars) |
208
|
|
|
{ |
209
|
|
|
Log::info('Laravel Microscope - The view file "'.$viewName.'"'); |
210
|
|
|
Log::info('At "'.$action.'" has some unused variables passed to it: '); |
211
|
|
|
Log::info($uselessVars); |
|
|
|
|
212
|
|
|
Log::info('If you do not see these variables passed in a controller, look in view composers.'); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.