/
Configuration.php
182 lines (163 loc) · 6.23 KB
/
Configuration.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
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Sentry\SentryBundle\DependencyInjection;
use Jean85\PrettyVersions;
use Sentry\Options;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\HttpKernel\Kernel;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('sentry');
/** @var ArrayNodeDefinition $rootNode */
$rootNode = \method_exists(TreeBuilder::class, 'getRootNode')
? $treeBuilder->getRootNode()
: $treeBuilder->root('sentry');
// Basic Sentry configuration
$rootNode->children()
->scalarNode('dsn')
->defaultNull()
->beforeNormalization()
->ifString()
->then($this->getTrimClosure());
// Options array (to be passed to Sentry\Options constructor) -- please keep alphabetical order!
$optionsNode = $rootNode->children()
->arrayNode('options')
->addDefaultsIfNotSet();
$defaultValues = new Options();
$optionsChildNodes = $optionsNode->children();
$optionsChildNodes->booleanNode('attach_stacktrace');
$optionsChildNodes->variableNode('before_breadcrumb')
->validate()
->ifTrue($this->isNotAValidCallback())
->thenInvalid('Expecting callable or service reference, got %s');
$optionsChildNodes->variableNode('before_send')
->validate()
->ifTrue($this->isNotAValidCallback())
->thenInvalid('Expecting callable or service reference, got %s');
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
$optionsChildNodes->booleanNode('capture_silenced_errors');
}
if ($this->classSerializersAreSupported()) {
$optionsChildNodes->arrayNode('class_serializers')
->defaultValue([])
->prototype('scalar');
}
$optionsChildNodes->integerNode('context_lines')
->min(0)
->max(99);
$optionsChildNodes->booleanNode('default_integrations');
$optionsChildNodes->booleanNode('enable_compression');
$optionsChildNodes->scalarNode('environment')
->defaultValue('%kernel.environment%')
->cannotBeEmpty();
$optionsChildNodes->scalarNode('error_types');
$optionsChildNodes->arrayNode('in_app_exclude')
->defaultValue([
'%kernel.cache_dir%',
$this->getProjectRoot() . '/vendor',
])
->prototype('scalar');
$optionsChildNodes->arrayNode('excluded_exceptions')
->defaultValue($defaultValues->getExcludedExceptions())
->prototype('scalar');
$optionsChildNodes->scalarNode('http_proxy');
$optionsChildNodes->arrayNode('integrations')
->prototype('scalar')
->validate()
->ifTrue(function ($value): bool {
if (! is_string($value) && '' != $value) {
return true;
}
return '@' !== $value[0];
})
->thenInvalid('Expecting service reference, got "%s"');
$optionsChildNodes->scalarNode('logger');
$optionsChildNodes->integerNode('max_breadcrumbs')
->min(1);
$optionsChildNodes->integerNode('max_value_length')
->min(1);
$optionsChildNodes->arrayNode('prefixes')
->defaultValue($defaultValues->getPrefixes())
->prototype('scalar');
$optionsChildNodes->scalarNode('project_root')
->defaultValue($this->getProjectRoot());
$optionsChildNodes->scalarNode('release');
$optionsChildNodes->floatNode('sample_rate')
->min(0.0)
->max(1.0);
$optionsChildNodes->integerNode('send_attempts')
->min(1);
$optionsChildNodes->booleanNode('send_default_pii');
$optionsChildNodes->scalarNode('server_name');
$optionsChildNodes->arrayNode('tags')
->normalizeKeys(false)
->prototype('scalar');
// Bundle-specific configuration
$listenerPriorities = $rootNode->children()
->arrayNode('listener_priorities')
->addDefaultsIfNotSet()
->children();
$listenerPriorities->scalarNode('request')
->defaultValue(1);
$listenerPriorities->scalarNode('sub_request')
->defaultValue(1);
$listenerPriorities->scalarNode('console')
->defaultValue(1);
$listenerPriorities->scalarNode('request_error')
->defaultValue(128);
$listenerPriorities->scalarNode('console_error')
->defaultValue(128);
return $treeBuilder;
}
private function getTrimClosure(): \Closure
{
return function ($str): ?string {
$value = trim($str);
if ($value === '') {
return null;
}
return $value;
};
}
private function getProjectRoot(): string
{
if (method_exists(Kernel::class, 'getProjectDir')) {
return '%kernel.project_dir%';
}
return '%kernel.root_dir%/..';
}
private function isNotAValidCallback(): \Closure
{
return function ($value): bool {
if (is_callable($value)) {
return false;
}
if (is_string($value) && 0 === strpos($value, '@')) {
return false;
}
return true;
};
}
private function classSerializersAreSupported(): bool
{
try {
new Options(['class_serializers' => []]);
return true;
} catch (\Throwable $throwable) {
return false;
}
}
}