-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSentryComponent.php
208 lines (181 loc) · 4.54 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
<?php
namespace Intersvyaz\YiiSentry;
use CApplicationComponent;
use CAssetManager;
use CClientScript;
use CException;
use CJavaScript;
use CWebApplication;
use CWebUser;
use IApplicationComponent;
use Raven_Client;
use Raven_ErrorHandler;
use Yii;
class SentryComponent extends CApplicationComponent
{
/**
* @var string Sentry DSN.
* @see https://github.com/getsentry/raven-php#configuration
*/
public $dsn;
/**
* @var array Raven_Client options.
* @see https://github.com/getsentry/raven-php#configuration
*/
public $options = array();
/**
* Publish, register and configure Raven-JS.
* @see https://raven-js.readthedocs.org/
* @var bool
*/
public $useRavenJs = false;
/**
* Raven-JS configuration options.
* @var array
* @see https://raven-js.readthedocs.org/en/latest/config/index.html#optional-settings
*/
public $ravenJsOptions = array();
/**
* Raven-JS plugins.
* @var array
* @see https://raven-js.readthedocs.org/en/latest/plugins/index.html
*/
public $ravenJsPlugins = array();
/**
* Initialize Raven_ErrorHandler.
* @var bool
*/
public $useRavenErrorHandler = false;
/**
* @var Raven_Client instance.
*/
protected $raven;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->useRavenJs) {
$this->registerRavenJs();
}
if ($this->useRavenErrorHandler) {
$this->registerRavenErrorHandler();
}
}
/**
* Get Raven_Client instance.
* @return Raven_Client
*/
public function getRaven()
{
if (!isset($this->raven)) {
$this->registerRaven();
}
return $this->raven;
}
/**
* Register and configure Raven js.
* @param array|null $options If null, then will be used $this->ravenJsOptions.
* @param array|null $plugins If null, then will be used $this->ravenJsPlugins.
* @param array|null $context If null, then will be used $this->getUserContext().
* @return bool
* @throws CException
*/
public function registerRavenJs(array $options = null, array $plugins = null, array $context = null)
{
/** @var CAssetManager $assetManager */
$assetManager = $this->getComponent('assetManager');
/** @var CClientScript $clientScript */
$clientScript = $this->getComponent('clientScript');
if (!$assetManager || !$clientScript) {
return false;
}
$jsOptions = $options !== null ? $options : $this->ravenJsOptions;
$jsPlugins = $plugins !== null ? $plugins : $this->ravenJsPlugins;
$jsContext = $context !== null ? $context : $this->getUserContext();
$assetUrl = $assetManager->publish(__DIR__ . '/../../bower-asset/raven-js/dist');
$clientScript
->registerScriptFile($assetUrl . '/raven.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);
}
}
/**
* Initialize raven client.
*/
protected function registerRaven()
{
$this->raven = new Raven_Client($this->dsn, $this->options);
if ($userContext = $this->getUserContext()) {
$this->raven->user_context($userContext);
}
}
/**
* Return Dsn without security token.
* @return string
*/
protected function getJsDsn()
{
return preg_replace('#:\w+@#', '@', $this->dsn);
}
/**
* Get get context (id, name).
* @return array|null
*/
protected function getUserContext()
{
/** @var CWebUser $user */
$user = $this->getComponent('user');
if ($user && !$user->isGuest) {
return array(
'id' => $user->getId(),
'name' => strtoupper($user->getName()),
);
}
return null;
}
/**
* Get Yii component if exists and available.
* @param string $component
* @return IApplicationComponent|null
*/
protected function getComponent($component)
{
if (!Yii::app() instanceof CWebApplication) {
return null;
}
if ($instance = Yii::app()->getComponent($component)) {
return $instance;
}
return null;
}
/**
* Register Raven Error Handlers for exceptions and errors.
* @return bool
*/
protected function registerRavenErrorHandler()
{
$raven = $this->getRaven();
if ($raven) {
$handler = new Raven_ErrorHandler($raven);
$handler->registerExceptionHandler();
$handler->registerErrorHandler();
$handler->registerShutdownFunction();
return true;
}
return false;
}
}