-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceProvider.php
71 lines (59 loc) · 1.94 KB
/
ServiceProvider.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
<?php
namespace Butler\Audit;
use Butler\Audit\Bus\Dispatcher as BusDispatcher;
use Butler\Audit\Facades\Auditor;
use Illuminate\Bus\Dispatcher as BaseBusDispatcher;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
class ServiceProvider extends BaseServiceProvider
{
public function register()
{
$this->addPendingRequestMacro();
$this->addDefaultInitiatorResolver();
$this->extendBusDispatcher();
}
public function boot()
{
$this->publishes([__DIR__ . '/../config/butler.php' => config_path('butler.php')], 'config');
$this->listenForJobProcessedEvent();
}
private function addPendingRequestMacro(): void
{
PendingRequest::macro(
'withCorrelation',
fn () => $this->withHeaders(Auditor::httpHeaders())
);
}
private function addDefaultInitiatorResolver(): void
{
if (config('butler.audit.default_initiator_resolver') === false) {
return;
}
$resolver = $this->app->runningInConsole()
? fn () => ['console', ['hostname' => gethostname()]]
: fn () => [request()->ip(), ['userAgent' => request()->userAgent()]];
Auditor::initiatorResolver($resolver);
}
private function extendBusDispatcher()
{
if (config('butler.audit.extend_bus_dispatcher') === false) {
return;
}
$this->app->extend(
BaseBusDispatcher::class,
fn ($dispatcher, $app) => new BusDispatcher($app, $dispatcher)
);
}
public function listenForJobProcessedEvent()
{
if ($this->app->runningInConsole()) {
Queue::after(function (JobProcessed $event) {
Auditor::correlationId(null);
Auditor::correlationTrail(null);
});
}
}
}