/
ServiceProvider.php
116 lines (93 loc) · 3.8 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
<?php
namespace Sentry\Laravel\Tracing;
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\View\Engines\EngineResolver;
use Illuminate\View\Factory as ViewFactory;
use InvalidArgumentException;
use Sentry\Laravel\BaseServiceProvider;
use Sentry\Serializer\RepresentationSerializer;
class ServiceProvider extends BaseServiceProvider
{
public const DEFAULT_INTEGRATIONS = [
Integrations\LighthouseIntegration::class,
];
public function boot(): void
{
if ($this->hasDsnSet() && $this->couldHavePerformanceTracingEnabled()) {
$tracingConfig = $this->getUserConfig()['tracing'] ?? [];
$this->bindEvents($tracingConfig);
$this->bindViewEngine($tracingConfig);
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));
});
$this->app->booted(function () {
$this->app->make(Middleware::class)->setBootedTimestamp();
});
}
private function bindEvents(array $tracingConfig): void
{
$handler = new EventHandler(
$this->app,
$this->app->make(BacktraceHelper::class),
$tracingConfig
);
$handler->subscribe();
if ($this->app->bound('queue')) {
$handler->subscribeQueueEvents(
$this->app->make('queue')
);
}
}
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);
}
}