-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathPulseServiceProvider.php
245 lines (217 loc) · 9.08 KB
/
PulseServiceProvider.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Laravel\Pulse;
use Composer\InstalledVersions;
use Illuminate\Auth\Events\Logout;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Queue\Events\Looping;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Factory as ViewFactory;
use Laravel\Pulse\Contracts\Ingest;
use Laravel\Pulse\Contracts\ResolvesUsers;
use Laravel\Pulse\Contracts\Storage;
use Laravel\Pulse\Ingests\NullIngest;
use Laravel\Pulse\Ingests\RedisIngest;
use Laravel\Pulse\Ingests\StorageIngest;
use Laravel\Pulse\Storage\DatabaseStorage;
use Livewire\LivewireManager;
use RuntimeException;
/**
* @internal
*/
class PulseServiceProvider extends ServiceProvider
{
/**
* Register any package services.
*/
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/pulse.php', 'pulse'
);
$this->app->singleton(Pulse::class);
$this->app->bind(Storage::class, DatabaseStorage::class);
$this->app->singletonIf(ResolvesUsers::class, Users::class);
$this->registerIngest();
}
/**
* Register the ingest implementation.
*/
protected function registerIngest(): void
{
$this->app->bind(Ingest::class, fn (Application $app) => match ($app->make('config')->get('pulse.ingest.driver')) {
'storage' => $app->make(StorageIngest::class),
'redis' => $app->make(RedisIngest::class),
null, 'null' => $app->make(NullIngest::class),
default => throw new RuntimeException("Unknown ingest driver [{$app->make('config')->get('pulse.ingest.driver')}]."),
});
}
/**
* Bootstrap any package services.
*/
public function boot(): void
{
if ($this->app->make('config')->get('pulse.enabled')) {
$this->app->make(Pulse::class)->register($this->app->make('config')->get('pulse.recorders'));
$this->listenForEvents();
} else {
$this->app->make(Pulse::class)->stopRecording();
}
$this->registerAuthorization();
$this->registerRoutes();
$this->registerComponents();
$this->registerResources();
$this->registerPublishing();
$this->registerCommands();
}
/**
* Register the package authorization.
*/
protected function registerAuthorization(): void
{
$this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) {
$gate->define('viewPulse', fn ($user = null) => $app->environment('local'));
});
}
/**
* Register the package routes.
*/
protected function registerRoutes(): void
{
$this->callAfterResolving('router', function (Router $router, Application $app) {
if ($app->make(Pulse::class)->registersRoutes()) {
$router->group([
'domain' => $app->make('config')->get('pulse.domain', null),
'prefix' => $app->make('config')->get('pulse.path'),
'middleware' => $app->make('config')->get('pulse.middleware'),
], function (Router $router) {
$router->get('/', function (Pulse $pulse, ViewFactory $view) {
return $view->make('pulse::dashboard');
})->name('pulse');
});
}
});
}
/**
* Listen for the events that are relevant to the package.
*/
protected function listenForEvents(): void
{
$this->app->booted(function () {
$this->callAfterResolving(Dispatcher::class, function (Dispatcher $event, Application $app) {
$event->listen(function (Logout $event) use ($app) {
if ($event->user === null) {
return;
}
$pulse = $app->make(Pulse::class);
$pulse->rescue(fn () => $pulse->rememberUser($event->user));
});
$event->listen([
Looping::class,
WorkerStopping::class,
], function () use ($app) {
$app->make(Pulse::class)->ingest();
});
});
$this->callAfterResolving(HttpKernel::class, function (HttpKernel $kernel, Application $app) {
$kernel->whenRequestLifecycleIsLongerThan(-1, function () use ($app) { // @phpstan-ignore method.notFound
$app->make(Pulse::class)->ingest();
});
});
$this->callAfterResolving(ConsoleKernel::class, function (ConsoleKernel $kernel, Application $app) {
$kernel->whenCommandLifecycleIsLongerThan(-1, function () use ($app) { // @phpstan-ignore method.notFound
$app->make(Pulse::class)->ingest();
});
});
});
$this->callAfterResolving(Dispatcher::class, function (Dispatcher $event, Application $app) {
$event->listen([
\Laravel\Octane\Events\RequestReceived::class, // @phpstan-ignore class.notFound
\Laravel\Octane\Events\TaskReceived::class, // @phpstan-ignore class.notFound
\Laravel\Octane\Events\TickReceived::class, // @phpstan-ignore class.notFound
], function ($event) {
if ($event->sandbox->resolved(Pulse::class)) {
$event->sandbox->make(Pulse::class)->setContainer($event->sandbox);
}
});
});
}
/**
* Register the package's components.
*/
protected function registerComponents(): void
{
$this->callAfterResolving('blade.compiler', function (BladeCompiler $blade) {
$blade->anonymousComponentPath(__DIR__.'/../resources/views/components', 'pulse');
});
$this->callAfterResolving('livewire', function (LivewireManager $livewire, Application $app) {
$middleware = collect($app->make('config')->get('pulse.middleware')) // @phpstan-ignore argument.templateType, argument.templateType
->map(fn ($middleware) => is_string($middleware)
? Str::before($middleware, ':')
: $middleware)
->all();
$livewire->addPersistentMiddleware($middleware);
$livewire->component('pulse.cache', Livewire\Cache::class);
$livewire->component('pulse.usage', Livewire\Usage::class);
$livewire->component('pulse.queues', Livewire\Queues::class);
$livewire->component('pulse.servers', Livewire\Servers::class);
$livewire->component('pulse.slow-jobs', Livewire\SlowJobs::class);
$livewire->component('pulse.exceptions', Livewire\Exceptions::class);
$livewire->component('pulse.slow-requests', Livewire\SlowRequests::class);
$livewire->component('pulse.slow-queries', Livewire\SlowQueries::class);
$livewire->component('pulse.period-selector', Livewire\PeriodSelector::class);
$livewire->component('pulse.slow-outgoing-requests', Livewire\SlowOutgoingRequests::class);
});
}
/**
* Register the package's resources.
*/
protected function registerResources(): void
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'pulse');
}
/**
* Register the package's publishable resources.
*/
protected function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/pulse.php' => config_path('pulse.php'),
], ['pulse', 'pulse-config']);
$this->publishes([
__DIR__.'/../resources/views/dashboard.blade.php' => resource_path('views/vendor/pulse/dashboard.blade.php'),
], ['pulse', 'pulse-dashboard']);
$method = method_exists($this, 'publishesMigrations') ? 'publishesMigrations' : 'publishes';
$this->{$method}([
__DIR__.'/../database/migrations' => database_path('migrations'),
], ['pulse', 'pulse-migrations']);
}
}
/**
* Register the package's commands.
*/
protected function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Commands\WorkCommand::class,
Commands\CheckCommand::class,
Commands\RestartCommand::class,
Commands\ClearCommand::class,
]);
AboutCommand::add('Pulse', fn () => [
'Version' => InstalledVersions::getPrettyVersion('laravel/pulse'),
'Enabled' => AboutCommand::format(config('pulse.enabled'), console: fn ($value) => $value ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF'),
]);
}
}
}