Parser.php
1.7 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
<?php
namespace Lackoxygen\ExceptionPush;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Lackoxygen\ExceptionPush\Attribute\Context;
class Parser
{
protected \Throwable $throw;
protected Context $context;
protected Request $request;
public function __construct()
{
$this->context = new Context();
$this->request = app(Request::class);
}
/**
* @return array
*/
protected function simpleTrace(): array
{
return array_map(function ($line) {
return $this->realpath($line);
}, array_slice(explode(PHP_EOL, $this->throw->getTraceAsString()), 0, 4));
}
/**
* @param string $line
*
* @return string
*/
protected function realpath(string $line): string
{
return Str::replace(app()->basePath(), '', $line);
}
/**
* @param \Throwable $e
*
* @return void
*/
public function extract(\Throwable $e): void
{
$this->throw = $e;
$this->context->setException(get_class($this->throw));
$this->context->setMethod($this->request->getMethod());
$this->context->setPath($this->request->path());
$this->context->setCode((string)$this->throw->getCode());
$this->context->setFile($this->realpath($this->throw->getFile()));
$this->context->setLine($this->throw->getLine());
$this->context->setMessage($this->throw->getMessage());
$this->context->setTrace($this->simpleTrace());
$this->context->setInput($this->request->post());
$this->context->setIp($this->request->ip());
}
/**
* @return Context
*/
public function context(): Context
{
return $this->context;
}
}