1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pageon\SlackWebhookMonolog\Slack\Attachment; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use Pageon\SlackWebhookMonolog\General\Url; |
8
|
|
|
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface; |
9
|
|
|
use Pageon\SlackWebhookMonolog\Slack\StringFormat; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* An attachment containing basic info about the error. |
13
|
|
|
* |
14
|
|
|
* @author Jelmer Prins <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @since 0.4.0 |
17
|
|
|
*/ |
18
|
|
|
class TraceAttachment extends Attachment |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Extra error data. |
22
|
|
|
* |
23
|
|
|
* @var ErrorInterface |
24
|
|
|
*/ |
25
|
|
|
private $error; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var StringFormat |
29
|
|
|
*/ |
30
|
|
|
private $formatter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* TraceAttachment constructor. |
34
|
|
|
* |
35
|
|
|
* @param ErrorInterface $error |
36
|
|
|
* @param StringFormat $formatter |
37
|
|
|
*/ |
38
|
6 |
|
public function __construct(ErrorInterface $error, StringFormat $formatter) |
39
|
|
|
{ |
40
|
6 |
|
$this->error = $error; |
41
|
6 |
|
$this->formatter = $formatter; |
42
|
|
|
|
43
|
6 |
|
$this->setTitle(new Title('Trace')); |
44
|
6 |
|
$this->setText( |
45
|
6 |
|
$formatter->arrayToNumberedList( |
46
|
6 |
|
array_map( |
47
|
|
|
function ($traceItem) { |
48
|
6 |
|
return $this->parseTraceItem($traceItem); |
49
|
6 |
|
}, |
50
|
6 |
|
$this->error->getTrace() |
51
|
6 |
|
) |
52
|
6 |
|
) |
53
|
6 |
|
); |
54
|
6 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Parse all the data into the markup of slack. |
58
|
|
|
* |
59
|
|
|
* @param array $traceItem |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
6 |
|
private function parseTraceItem(array $traceItem) |
64
|
|
|
{ |
65
|
6 |
|
$text = ''; |
66
|
|
|
$info = [ |
67
|
6 |
|
'file' => $this->getArrayValue($traceItem, 'file', 'unknown'), |
68
|
6 |
|
'function' => $this->getArrayValue($traceItem, 'function', 'unknown'), |
69
|
6 |
|
'line' => $this->getArrayValue($traceItem, 'line', 'unknown'), |
70
|
6 |
|
'class' => $this->getArrayValue($traceItem, 'class', 'unknown'), |
71
|
6 |
|
'type' => $this->getArrayValue($traceItem, 'type', 'unknown'), |
72
|
6 |
|
'arguments' => $this->parseArguments($traceItem), |
73
|
6 |
|
]; |
74
|
6 |
|
$text .= $this->formatter->arrayToKeyValueList($info); |
75
|
|
|
|
76
|
6 |
|
return "\n" . $this->formatter->indent($text); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $array |
81
|
|
|
* @param string $key |
82
|
|
|
* @param mixed|null $fallback |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
6 |
|
private function getArrayValue(array $array, $key, $fallback = null) |
87
|
|
|
{ |
88
|
6 |
|
return isset($array[$key]) ? $array[$key] : $fallback; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $traceItem |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
6 |
|
private function parseArguments($traceItem) |
96
|
|
|
{ |
97
|
6 |
|
return "\n" . $this->formatter->arrayToKeyValueList( |
98
|
6 |
|
array_map( |
99
|
|
|
function ($item) { |
100
|
6 |
|
return print_r($item, true); |
101
|
6 |
|
}, |
102
|
6 |
|
array_filter( |
103
|
6 |
|
isset($traceItem['args']) ? $traceItem['args'] : [], |
104
|
6 |
|
function ($item) { |
105
|
6 |
|
return !empty($item); |
106
|
|
|
} |
107
|
6 |
|
) |
108
|
6 |
|
) |
109
|
6 |
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|