-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathIntegrationRegistry.php
More file actions
156 lines (124 loc) · 4.57 KB
/
IntegrationRegistry.php
File metadata and controls
156 lines (124 loc) · 4.57 KB
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
<?php
declare(strict_types=1);
namespace Sentry\Integration;
use Psr\Log\LoggerInterface;
use Sentry\Options;
/**
* @internal
*/
final class IntegrationRegistry
{
/**
* @var self|null The current instance
*/
private static $instance;
/**
* @var array<class-string<IntegrationInterface>, bool> The registered integrations
*/
private $integrations = [];
private function __construct()
{
}
/**
* Gets the current singleton instance or creates a new one if it didn't
* exists yet.
*/
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Setups the integrations according to the given options. For each integration
* the {@see IntegrationInterface::setupOnce()} method will be called only once
* during the application lifetime.
*
* @param Options $options The SDK client options
*
* @return array<class-string<IntegrationInterface>, IntegrationInterface>
*/
public function setupIntegrations(Options $options, LoggerInterface $logger): array
{
$integrations = [];
$installed = [];
foreach ($this->getIntegrationsToSetup($options) as $integration) {
$integrationName = \get_class($integration);
$integrations[$integrationName] = $integration;
if ($this->setupIntegration($integration, $options)) {
$installed[] = $integrationName;
}
}
if (\count($installed) > 0) {
$logger->debug(\sprintf('The "%s" integration(s) have been installed.', implode(', ', $installed)));
}
return $integrations;
}
private function setupIntegration(IntegrationInterface $integration, Options $options): bool
{
$integrationName = \get_class($integration);
if (isset($this->integrations[$integrationName])) {
return false;
}
if ($integration instanceof OptionAwareIntegrationInterface) {
$integration->setOptions($options);
}
$integration->setupOnce();
$this->integrations[$integrationName] = true;
return true;
}
/**
* @return IntegrationInterface[]
*/
private function getIntegrationsToSetup(Options $options): array
{
$integrations = [];
$defaultIntegrations = $this->getDefaultIntegrations($options);
$userIntegrations = $options->getIntegrations();
if (\is_array($userIntegrations)) {
$userIntegrationsClasses = array_map('get_class', $userIntegrations);
$pickedIntegrationsClasses = [];
foreach ($defaultIntegrations as $defaultIntegration) {
$integrationClassName = \get_class($defaultIntegration);
if (!\in_array($integrationClassName, $userIntegrationsClasses, true) && !isset($pickedIntegrationsClasses[$integrationClassName])) {
$integrations[] = $defaultIntegration;
$pickedIntegrationsClasses[$integrationClassName] = true;
}
}
foreach ($userIntegrations as $userIntegration) {
$integrationClassName = \get_class($userIntegration);
if (!isset($pickedIntegrationsClasses[$integrationClassName])) {
$integrations[] = $userIntegration;
$pickedIntegrationsClasses[$integrationClassName] = true;
}
}
} else {
$integrations = $userIntegrations($defaultIntegrations);
if (!\is_array($integrations)) {
throw new \UnexpectedValueException(\sprintf('Expected the callback set for the "integrations" option to return a list of integrations. Got: "%s".', \gettype($integrations)));
}
}
return $integrations;
}
/**
* @return IntegrationInterface[]
*/
private function getDefaultIntegrations(Options $options): array
{
if (!$options->hasDefaultIntegrations()) {
return [];
}
$integrations = [
new RequestIntegration(),
new TransactionIntegration(),
new FrameContextifierIntegration(),
new EnvironmentIntegration(),
new ModulesIntegration(),
];
if ($options->getDsn() !== null) {
array_unshift($integrations, new ExceptionListenerIntegration(), new ErrorListenerIntegration(), new FatalErrorListenerIntegration());
}
return $integrations;
}
}