/
ServiceProvider.php
142 lines (114 loc) · 4.96 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
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
<?php
namespace Sentry\Laravel\Tracing;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Http\Kernel as HttpKernelInterface;
use Illuminate\Contracts\View\Engine;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Contracts\CallableDispatcher;
use Illuminate\Routing\Contracts\ControllerDispatcher;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory as ViewFactory;
use InvalidArgumentException;
use Sentry\Laravel\BaseServiceProvider;
use Sentry\Laravel\Tracing\Routing\TracingCallableDispatcherTracing;
use Sentry\Laravel\Tracing\Routing\TracingControllerDispatcherTracing;
use Sentry\Serializer\RepresentationSerializer;
class ServiceProvider extends BaseServiceProvider
{
public const DEFAULT_INTEGRATIONS = [
Integrations\LighthouseIntegration::class,
];
public function boot(): void
{
// If there is no DSN set we register nothing since it's impossible for us to send traces without a DSN set
if (!$this->hasDsnSet()) {
return;
}
$this->app->booted(function () {
$this->app->make(Middleware::class)->setBootedTimestamp();
});
$tracingConfig = $this->getUserConfig()['tracing'] ?? [];
$this->bindEvents($tracingConfig);
$this->bindViewEngine($tracingConfig);
$this->decorateRoutingDispatchers();
if ($this->app->bound(HttpKernelInterface::class)) {
/** @var \Illuminate\Foundation\Http\Kernel $httpKernel */
$httpKernel = $this->app->make(HttpKernelInterface::class);
if ($httpKernel instanceof HttpKernel) {
$httpKernel->prependMiddleware(Middleware::class);
}
}
}
public function register(): void
{
$this->app->singleton(Middleware::class);
$this->app->singleton(BacktraceHelper::class, function () {
/** @var \Sentry\State\Hub $sentry */
$sentry = $this->app->make(self::$abstract);
$options = $sentry->getClient()->getOptions();
return new BacktraceHelper($options, new RepresentationSerializer($options));
});
}
private function bindEvents(array $tracingConfig): void
{
$handler = new EventHandler(
$tracingConfig,
$this->app->make(BacktraceHelper::class)
);
try {
/** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
$dispatcher = $this->app->make(Dispatcher::class);
$handler->subscribe($dispatcher);
if ($this->app->bound('queue')) {
$handler->subscribeQueueEvents($dispatcher, $this->app->make('queue'));
}
} catch (BindingResolutionException $e) {
// If we cannot resolve the event dispatcher we also cannot listen to events
}
}
private function bindViewEngine($tracingConfig): void
{
if (($tracingConfig['views'] ?? true) !== true) {
return;
}
$viewEngineWrapper = function (EngineResolver $engineResolver): void {
foreach (['file', 'php', 'blade'] as $engineName) {
try {
$realEngine = $engineResolver->resolve($engineName);
$engineResolver->register($engineName, function () use ($realEngine) {
return $this->wrapViewEngine($realEngine);
});
} catch (InvalidArgumentException $e) {
// The `file` engine was introduced in Laravel 5.4. On lower Laravel versions
// resolving that driver will throw an `InvalidArgumentException`. We can
// ignore this exception because we can't wrap drivers that don't exist
}
}
};
if ($this->app->resolved('view.engine.resolver')) {
$viewEngineWrapper($this->app->make('view.engine.resolver'));
} else {
$this->app->afterResolving('view.engine.resolver', $viewEngineWrapper);
}
}
private function wrapViewEngine(Engine $realEngine): Engine
{
/** @var ViewFactory $viewFactory */
$viewFactory = $this->app->make('view');
$viewFactory->composer('*', static function (View $view) use ($viewFactory): void {
$viewFactory->share(ViewEngineDecorator::SHARED_KEY, $view->name());
});
return new ViewEngineDecorator($realEngine, $viewFactory);
}
private function decorateRoutingDispatchers(): void
{
$this->app->extend(CallableDispatcher::class, static function (CallableDispatcher $dispatcher) {
return new TracingCallableDispatcherTracing($dispatcher);
});
$this->app->extend(ControllerDispatcher::class, static function (ControllerDispatcher $dispatcher) {
return new TracingControllerDispatcherTracing($dispatcher);
});
}
}