forked from getsentry/sentry-symfony
/
SentryExtension.php
170 lines (148 loc) · 6.42 KB
/
SentryExtension.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
<?php
namespace Sentry\SentryBundle\DependencyInjection;
use Sentry\ClientBuilderInterface;
use Sentry\Options;
use Sentry\SentryBundle\Command\SentryTestCommand;
use Sentry\SentryBundle\ErrorTypesParser;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\SentryBundle\EventListener\ErrorListener;
use Sentry\SentryBundle\EventListener\RequestListener;
use Sentry\SentryBundle\EventListener\SubRequestListener;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Kernel;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class SentryExtension extends Extension
{
/**
* {@inheritDoc}
*
* @throws InvalidConfigurationException
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$processedConfiguration = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$this->passConfigurationToOptions($container, $processedConfiguration);
$container->getDefinition(ClientBuilderInterface::class)
->setConfigurator([ClientBuilderConfigurator::class, 'configure']);
foreach ($processedConfiguration['listener_priorities'] as $key => $priority) {
$container->setParameter('sentry.listener_priorities.' . $key, $priority);
}
$this->tagConsoleErrorListener($container);
$this->setLegacyVisibilities($container);
}
private function passConfigurationToOptions(ContainerBuilder $container, array $processedConfiguration): void
{
$options = $container->getDefinition(Options::class);
$options->addArgument(['dsn' => $processedConfiguration['dsn']]);
$processedOptions = $processedConfiguration['options'];
$mappableOptions = [
'attach_stacktrace',
'capture_silenced_errors',
'context_lines',
'default_integrations',
'enable_compression',
'environment',
'excluded_exceptions',
'http_proxy',
'logger',
'max_breadcrumbs',
'max_value_length',
'prefixes',
'project_root',
'release',
'sample_rate',
'send_attempts',
'send_default_pii',
'server_name',
'tags',
];
foreach ($mappableOptions as $optionName) {
if (\array_key_exists($optionName, $processedOptions)) {
$setterMethod = 'set' . str_replace('_', '', ucwords($optionName, '_'));
$options->addMethodCall($setterMethod, [$processedOptions[$optionName]]);
}
}
if (\array_key_exists('in_app_exclude', $processedOptions)) {
$options->addMethodCall('setInAppExcludedPaths', [$processedOptions['in_app_exclude']]);
}
if (\array_key_exists('error_types', $processedOptions)) {
$parsedValue = (new ErrorTypesParser($processedOptions['error_types']))->parse();
$options->addMethodCall('setErrorTypes', [$parsedValue]);
}
if (\array_key_exists('before_send', $processedOptions)) {
$this->mapCallableValue($options, 'setBeforeSendCallback', $processedOptions['before_send']);
}
if (\array_key_exists('before_breadcrumb', $processedOptions)) {
$this->mapCallableValue($options, 'setBeforeBreadcrumbCallback', $processedOptions['before_breadcrumb']);
}
if (\array_key_exists('integrations', $processedOptions)) {
$integrations = [];
foreach ($processedOptions['integrations'] as $integrationName) {
$integrations[] = new Reference(substr($integrationName, 1));
}
$options->addMethodCall('setIntegrations', [$integrations]);
}
}
/**
* @param Definition $options
* @param string $method
* @param callable|string $optionValue
*/
private function mapCallableValue(Definition $options, string $method, $optionValue): void
{
if (is_string($optionValue) && 0 === strpos($optionValue, '@')) {
$beforeSend = new Reference(substr($optionValue, 1));
} else {
$beforeSend = $optionValue;
}
$options->addMethodCall($method, [$beforeSend]);
}
/**
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
*/
private function tagConsoleErrorListener(ContainerBuilder $container): void
{
$listener = $container->getDefinition(ErrorListener::class);
if (class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')) {
$tagAttributes = [
'event' => ConsoleEvents::ERROR,
'method' => 'onConsoleError',
'priority' => '%sentry.listener_priorities.console_error%',
];
} else {
$tagAttributes = [
'event' => ConsoleEvents::EXCEPTION,
'method' => 'onConsoleException',
'priority' => '%sentry.listener_priorities.console_error%',
];
}
$listener->addTag('kernel.event_listener', $tagAttributes);
}
/**
* BC layer for symfony < 3.3, listeners and commands must be public
*/
private function setLegacyVisibilities(ContainerBuilder $container): void
{
if (Kernel::VERSION_ID < 30300) {
$container->getDefinition(SentryTestCommand::class)->setPublic(true);
$container->getDefinition(ConsoleListener::class)->setPublic(true);
$container->getDefinition(ErrorListener::class)->setPublic(true);
$container->getDefinition(RequestListener::class)->setPublic(true);
$container->getDefinition(SubRequestListener::class)->setPublic(true);
}
}
}