-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCatcher.php
More file actions
167 lines (145 loc) · 3.44 KB
/
Catcher.php
File metadata and controls
167 lines (145 loc) · 3.44 KB
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
<?php
declare(strict_types=1);
namespace Hawk;
use Hawk\Addons\Environment;
use Hawk\Addons\Headers;
use Throwable;
/**
* Hawk PHP Catcher SDK
*
* @copyright CodeX
*
* @see https://hawk.so/docs#add-server-handler
*/
final class Catcher
{
/**
* Catcher SDK private instance. Created once
*
* @var Catcher
*/
private static $instance;
/**
* SDK handler: contains methods that catch errors and exceptions
*
* @var Handler
*/
private $handler;
/**
* Static method to initialize Catcher
*
* @param array $options
*
* @return Catcher
*/
public static function init(array $options): Catcher
{
if (!self::$instance) {
self::$instance = new self($options);
}
return self::$instance;
}
/**
* Returns initialized instance or throws an exception if it is not created yet
*
* @return Catcher
*
* @throws \Exception
*/
public static function get(): Catcher
{
if (self::$instance === null) {
throw new \Exception('Catcher is not initialized');
}
return self::$instance;
}
/**
* @param array $user
*
* @return $this
*/
public function setUser(array $user): self
{
$this->handler->setUser($user);
return $this;
}
/**
* @param array $context
*
* @return $this
*/
public function setContext(array $context): self
{
$this->handler->setContext($context);
return $this;
}
/**
* @param string $message
* @param array $context
*
* @example
* \Hawk\Catcher::get()
* ->sendMessage('my special message', [
* ... // context
* ])
*/
public function sendMessage(string $message, array $context = []): void
{
$this->handler->sendEvent([
'title' => $message,
'context' => $context
]);
}
/**
* @param Throwable $throwable
* @param array $context
*
* @throws Throwable
*
* @example
* \Hawk\Catcher::get()
* ->sendException($exception, [
* ... // context
* ])
*/
public function sendException(Throwable $throwable, array $context = [])
{
$this->handler->handleException($throwable, $context);
}
/**
* @example
* \Hawk\Catcher::get()
* ->sendEvent([
* ... // payload
* ])
*
* @param array $payload
*/
public function sendEvent(array $payload): void
{
$this->handler->sendEvent($payload);
}
/**
* @param array $options
*/
private function __construct(array $options)
{
$options = new Options($options);
/**
* Init stacktrace frames builder and inject serializer
*/
$serializer = new Serializer();
$stacktraceBuilder = new StacktraceFrameBuilder($serializer);
/**
* Prepare Event payload builder
*/
$builder = new EventPayloadBuilder($stacktraceBuilder);
$builder->registerAddon(new Headers());
$builder->registerAddon(new Environment());
$transport = Transport::init($options);
$this->handler = new Handler($options, $transport, $builder);
$this->handler->registerErrorHandler();
$this->handler->registerExceptionHandler();
$this->handler->registerFatalHandler();
}
}