1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Sulao\YiiHoneybadger; |
||
6 | |||
7 | use Closure; |
||
8 | use Honeybadger\Honeybadger; |
||
9 | use Throwable; |
||
10 | use yii\base\Component as YiiComponent; |
||
11 | use yii\helpers\ArrayHelper; |
||
12 | |||
13 | class Component extends YiiComponent |
||
14 | { |
||
15 | public $config = []; |
||
16 | |||
17 | /** |
||
18 | * Get Honeybadger instance. |
||
19 | * |
||
20 | * @return Honeybadger |
||
21 | */ |
||
22 | 1 | protected function getHoneybadger(): Honeybadger |
|
23 | { |
||
24 | 1 | static $honeybadger; |
|
25 | |||
26 | 1 | if ($honeybadger) { |
|
27 | 1 | return $honeybadger; |
|
28 | } |
||
29 | |||
30 | 1 | $config = $this->config; |
|
31 | |||
32 | // Globally disable error handlers, record the errors via the logger |
||
33 | 1 | $config['handlers'] = [ |
|
34 | 1 | 'exception' => false, |
|
35 | 1 | 'error' => false, |
|
36 | 1 | 'shutdown' => false, |
|
37 | 1 | ]; |
|
38 | |||
39 | 1 | $context = ArrayHelper::remove($config, 'context'); |
|
40 | |||
41 | 1 | $honeybadger = Honeybadger::new($config); |
|
42 | |||
43 | 1 | if (!empty($context) && is_array($context)) { |
|
44 | 1 | foreach ($context as $key => $value) { |
|
45 | 1 | if ($value instanceof Closure) { |
|
46 | 1 | $context[$key] = call_user_func($value); |
|
47 | } |
||
48 | } |
||
49 | 1 | $honeybadger->context($context); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
50 | } |
||
51 | |||
52 | 1 | return $honeybadger; |
|
0 ignored issues
–
show
|
|||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Send an exception to Honeybadger. |
||
57 | * |
||
58 | * @param Throwable $throwable |
||
59 | * @param array $additionalParams |
||
60 | * @return array |
||
61 | */ |
||
62 | 1 | public function notify(Throwable $throwable, array $additionalParams = []) |
|
63 | { |
||
64 | 1 | return $this->getHoneybadger()->notify($throwable, null, $additionalParams); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Send a custom notification to Honeybadger. |
||
69 | * |
||
70 | * @param array $payload |
||
71 | * @return array |
||
72 | */ |
||
73 | 1 | public function customNotification(array $payload) |
|
74 | { |
||
75 | 1 | return $this->getHoneybadger()->customNotification($payload); |
|
76 | } |
||
77 | } |
||
78 |