forked from intersvyaz/yii-sentry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SentryComponent.php
213 lines (183 loc) · 4.89 KB
/
SentryComponent.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Skillshare\YiiSentry;
use CApplicationComponent;
use CAssetManager;
use CClientScript;
use CException;
use CJavaScript;
use CWebApplication;
use CWebUser;
use IApplicationComponent;
use Sentry\ClientBuilder;
use Sentry\ClientInterface;
use Sentry\ErrorHandler;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Yii;
class SentryComponent extends CApplicationComponent
{
/**
* @var string Sentry DSN.
* @see https://github.com/getsentry/raven-php#configuration
*/
public string $dsn = '';
/**
* @var mixed[] Raven_Client options.
* @see https://github.com/getsentry/raven-php#configuration
*/
public array $options = [];
/**
* Publish, register and configure Raven-JS.
* @see https://raven-js.readthedocs.org/
*/
public bool $useRavenJs = false;
/**
* Raven-JS configuration options.
* @var mixed[]
* @see https://raven-js.readthedocs.org/en/latest/config/index.html#optional-settings
*/
public array $ravenJsOptions = [];
/**
* Raven-JS plugins.
* @var mixed[]
* @see https://raven-js.readthedocs.org/en/latest/plugins/index.html
*/
public array $ravenJsPlugins = [];
/**
* Initialize Raven_ErrorHandler.
*/
public bool $useRavenErrorHandler = false;
/**
* @var ClientInterface|null instance.
*/
protected ?ClientInterface $raven = null;
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
if ($this->useRavenJs) {
$this->registerRavenJs();
}
if ($this->useRavenErrorHandler) {
$this->registerRavenErrorHandler();
}
}
/**
* Get Raven_Client instance.
*/
public function getRaven(): ClientInterface
{
if (!isset($this->raven)) {
$this->registerRaven();
}
/** @var ClientInterface */
return $this->raven;
}
/**
* Register and configure Raven js.
* @param mixed[]|null $options If null, then will be used $this->ravenJsOptions.
* @param mixed[]|null $plugins If null, then will be used $this->ravenJsPlugins.
* @param mixed[]|null $context If null, then will be used $this->getUserContext().
* @throws CException
*/
public function registerRavenJs(?array $options = null, ?array $plugins = null, ?array $context = null): bool
{
/** @var CAssetManager|null $assetManager */
$assetManager = $this->getComponent('assetManager');
/** @var CClientScript|null $clientScript */
$clientScript = $this->getComponent('clientScript');
if (!$assetManager || !$clientScript) {
return false;
}
$jsOptions = $options ?? $this->ravenJsOptions;
$jsPlugins = $plugins ?? $this->ravenJsPlugins;
$jsContext = $context ?? $this->getUserContext();
$assetUrl = $assetManager->publish(__DIR__ . '/assets');
$clientScript
->registerScriptFile($assetUrl . '/raven.min.js', CClientScript::POS_HEAD)
->registerScript(
'raven-js1',
'Raven.config(\'' . $this->getJsDsn() . '\', ' . CJavaScript::encode($jsOptions) . ').install()',
CClientScript::POS_BEGIN
);
if ($jsContext) {
$clientScript->registerScript(
'raven-js2',
'Raven.setUserContext(' . CJavaScript::encode($jsContext) . ');',
CClientScript::POS_BEGIN
);
}
foreach ($jsPlugins as $plugin) {
$clientScript->registerScriptFile($assetUrl . '/plugins/' . $plugin . '.js', CClientScript::POS_HEAD);
}
return true;
}
/**
* Initialize raven client.
*/
protected function registerRaven(): void
{
$this->raven = ClientBuilder::create($this->options)->getClient();
SentrySdk::getCurrentHub()->bindClient($this->raven);
$userContext = $this->getUserContext();
SentrySdk::getCurrentHub()->configureScope(function (Scope $scope) use ($userContext) {
if ($userContext) {
$scope->setUser($userContext);
}
});
}
/**
* Return Dsn without security token.
*/
protected function getJsDsn(): string
{
return (string) preg_replace('#:\w+@#', '@', $this->dsn);
}
/**
* Get get context (id, name).
* @return array{id: mixed, name: string}|null
*/
protected function getUserContext(): ?array
{
/** @var CWebUser|null $user */
$user = $this->getComponent('user');
if ($user && (!property_exists($user, 'isGuest') || !$user->isGuest)) {
return array(
'id' => $user->getId(),
'name' => strtoupper($user->getName()),
);
}
return null;
}
/**
* Get Yii component if exists and available.
*/
protected function getComponent(string $component): ?IApplicationComponent
{
$app = Yii::app();
if (!$app instanceof CWebApplication) {
return null;
}
/** @var IApplicationComponent|null $instance */
$instance = $app->getComponent($component);
if ($instance !== null) {
return $instance;
}
return null;
}
/**
* Register Raven Error Handlers for exceptions and errors.
*/
protected function registerRavenErrorHandler(): bool
{
if (!isset($this->raven)) {
$this->registerRaven();
}
ErrorHandler::registerOnceExceptionHandler();
ErrorHandler::registerOnceErrorHandler();
ErrorHandler::registerOnceFatalErrorHandler();
return true;
}
}