-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathLaravelMicroscopeServiceProvider.php
150 lines (120 loc) · 4.8 KB
/
LaravelMicroscopeServiceProvider.php
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
<?php
namespace Imanghafoori\LaravelMicroscope;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use ImanGhafoori\ComposerJson\ComposerJson as Composer;
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ConsolePrinterInstaller;
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
use Imanghafoori\LaravelMicroscope\Features\CheckEvents\Installer;
use Imanghafoori\LaravelMicroscope\Features\CheckUnusedBladeVars\UnusedVarsInstaller;
use Imanghafoori\LaravelMicroscope\Features\CheckView\Check\CheckView;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\LaravelMicroscope\Foundations\Path;
use Imanghafoori\LaravelMicroscope\ServiceProvider\CommandsRegistry;
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyBladeCompiler;
use Imanghafoori\LaravelMicroscope\SpyClasses\SpyGate;
use Imanghafoori\TokenAnalyzer\ImportsAnalyzer;
class LaravelMicroscopeServiceProvider extends ServiceProvider
{
use CommandsRegistry;
public function boot()
{
if (! $this->canRun()) {
return;
}
UnusedVarsInstaller::spyView();
$this->addCacheStore();
$this->loadViewsFrom(__DIR__.'/../templates', 'microscope_package');
Event::listen('microscope.start.command', function () {
! defined('microscope_start') && define('microscope_start', microtime(true));
});
$this->resetCountersOnFinish();
$this->registerCommands();
ErrorPrinter::$ignored = config('microscope.ignore');
$this->publishes([
__DIR__.'/../templates' => base_path('resources/views/vendor/microscope'),
], 'microscope');
ConsolePrinterInstaller::boot();
}
public function register()
{
if (! $this->canRun()) {
return;
}
$this->setBasePath();
$this->setLineSeparatorColor();
$this->registerCompiler();
$this->loadConfig();
app()->singleton(ErrorPrinter::class, function () {
return ErrorPrinter::singleton();
});
Features\CheckRoutes\Installer::spyRouter();
// We need to start spying before the boot process starts.
$command = $_SERVER['argv'][1] ?? '';
// We spy the router in order to have a list of route files.
$checkAll = Str::startsWith('check:all', $command);
($checkAll || Str::startsWith('check:eve', $command)) && Installer::spyEvents();
($checkAll || Str::startsWith('check:routes', $command)) && app('router')->spyRouteConflict();
Str::startsWith('check:action_comment', $command) && app('router')->spyRouteConflict();
($checkAll || Str::startsWith('check:gates', $command)) && SpyGate::start();
}
private function loadConfig()
{
$configPath = __DIR__.'/../config/config.php';
$this->mergeConfigFrom($configPath, 'microscope');
$this->publishes([$configPath => config_path('microscope.php')], 'config');
}
private function canRun()
{
if (! $this->app->runningInConsole()) {
return false;
}
if (! config('microscope.is_enabled', true)) {
return false;
}
if (windows_os()) {
return true;
}
return app()['env'] !== 'production';
}
private function registerCompiler()
{
$this->app->singleton('microscope.blade.compiler', function () {
return new SpyBladeCompiler($this->app['files'], $this->app['config']['view.compiled']);
});
}
private function resetCountersOnFinish()
{
Event::listen('microscope.finished.checks', function () {
CheckView::$checkedCallsCount = 0;
CheckView::$skippedCallsCount = 0;
});
Event::listen('microscope.finished.checks', function () {
ImportsAnalyzer::$checkedRefCount = 0;
Iterators\ChecksOnPsr4Classes::$checkedFilesCount = 0;
});
}
private function setLineSeparatorColor()
{
[$major] = explode('.', app()->version());
$color = (int) $major >= 9 ? 'gray' : 'blue';
config()->set('microscope.colors.line_separator', $color);
}
private function setBasePath()
{
ComposerJson::$composer = function () {
return Composer::make(base_path(), config('microscope.ignored_namespaces', []), config('microscope.additional_composer_paths', []));
};
PhpFinder::$basePath = base_path();
Path::setBasePath(base_path());
}
private function addCacheStore()
{
config()->set('cache.stores.|-microscope-|', [
'driver' => 'file',
'path' => storage_path('framework'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'microscope'),
]);
}
}