ExceptionPush.php
1.8 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
<?php
namespace Lackoxygen\ExceptionPush;
use Illuminate\Support\Arr;
class ExceptionPush
{
/**
* @var Parser
*/
protected Parser $parser;
/**
* @var array
*/
protected array $agents;
public function __construct()
{
$this->parser = new Parser;
$this->agents = $this->getAgents();
}
/**
* @return array
*/
protected function getAgents(): array
{
$agentOpts = (array) static::config('agents');
$agents = [];
foreach ($agentOpts as $agentName => $opts) {
$agent = new $agentName;
if (!is_array($opts)) {
continue;
}
foreach ($opts as $key => $value) {
echo "{$key} => $value\n";
$agent->{$key} = $value;
}
$agents[] = $agent;
}
return $agents;
}
/**
* @param $key
* @param $default
*
* @return array|\ArrayAccess|mixed
*/
public static function config($key = null, $default = null)
{
$config = \config('exception.push');
return Arr::get($config, $key, $default);
}
/**
* @return void
*/
public function boot(\Throwable $e)
{
$this->parser->extract($e);
$this->dispatch($this->format());
}
protected function format(): array
{
$formatter = Formatter::callback();
$body = $formatter($this->parser->context());
if (!is_array($body)) {
throw new \RuntimeException('Custom function must return array format');
}
return $body;
}
/**
* @param array $body
*
* @return void
*/
protected function dispatch(array $body)
{
$dispatcher = Dispatcher::callback();
$dispatcher($this->agents, $body);
}
}