ExceptionCollector.php
1.98 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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/4/26
* Time: 下午12:26
*/
namespace Hood\Debug\Debugbar;
class ExceptionCollector implements InterfaceCollector
{
private $collector = array();
private $debugType = 'Exception';
public function __construct(\Exception $exception)
{
$errorList = $this->formatExceptionData($exception);
foreach ($exception->getTrace() as $key => $val) {
$_error = array(
'file' => empty($val['file']) ? '↑' : $val['file'],
'function' => $val['function'],
'line' => empty($val['line']) ? '' : $val['line'],
'class' => empty($val['class']) ? '' : $val['class'],
'type' => empty($val['type']) ? '' : $val['type']
);
if (!empty($val['args'])) {
$args = array();
foreach ($val['args'] as $argsKey => $argsVal) {
if (empty($argsVal)) {
continue;
}
$args[] = $argsVal;
}
$_error['args'] = $args;
}
$errorList['exception_list'][] = $_error;
}
$this->collector = $errorList;
}
public function formatExceptionData(\Exception $e)
{
$filePath = $e->getFile();
$lines = array();
if ($filePath && file_exists($filePath)) {
$lines = file($filePath);
$start = $e->getLine() - 4;
$lines = array_slice($lines, $start < 0 ? 0 : $start, 7, true);
}
return array(
'type' => get_class($e),
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $filePath,
'line' => $e->getLine(),
'surrounding_lines' => $lines
);
}
public function getDebugType()
{
return $this->debugType;
}
public function getCollector()
{
return $this->collector;
}
}