1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\FlareClient\Context; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
9
|
|
|
|
10
|
|
|
class RequestContext implements ContextInterface |
11
|
|
|
{ |
12
|
|
|
/** @var \Symfony\Component\HttpFoundation\Request|null */ |
13
|
|
|
protected $request; |
14
|
|
|
|
15
|
|
|
public function __construct(Request $request = null) |
16
|
|
|
{ |
17
|
|
|
$this->request = $request ?? Request::createFromGlobals(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getRequest(): array |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
'url' => $this->request->getUri(), |
24
|
|
|
'ip' => $this->request->getClientIp(), |
25
|
|
|
'method' => $this->request->getMethod(), |
26
|
|
|
'useragent' => $this->request->headers->get('User-Agent'), |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function getFiles(): array |
31
|
|
|
{ |
32
|
|
|
if (is_null($this->request->files)) { |
33
|
|
|
return []; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->mapFiles($this->request->files->all()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function mapFiles(array $files) |
40
|
|
|
{ |
41
|
|
|
return array_map(function ($file) { |
42
|
|
|
if (is_array($file)) { |
43
|
|
|
return $this->mapFiles($file); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (! $file instanceof UploadedFile) { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
'pathname' => $file->getPathname(), |
52
|
|
|
'size' => $file->getSize(), |
53
|
|
|
'mimeType' => $file->getMimeType() |
54
|
|
|
]; |
55
|
|
|
}, $files); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getSession(): array |
59
|
|
|
{ |
60
|
|
|
$session = $this->request->getSession(); |
61
|
|
|
|
62
|
|
|
return $session ? $this->getValidSessionData($session) : []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param SessionInterface $session |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function getValidSessionData($session): array |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
json_encode($session->all()); |
73
|
|
|
} catch (Throwable $e) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $session->all(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getCookies(): array |
81
|
|
|
{ |
82
|
|
|
return $this->request->cookies->all(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getHeaders(): array |
86
|
|
|
{ |
87
|
|
|
return $this->request->headers->all(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getRequestData(): array |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'queryString' => $this->request->query->all(), |
94
|
|
|
'body' => $this->request->request->all(), |
95
|
|
|
'files' => $this->getFiles(), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function toArray(): array |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
|
|
'request' => $this->getRequest(), |
103
|
|
|
'request_data' => $this->getRequestData(), |
104
|
|
|
'headers' => $this->getHeaders(), |
105
|
|
|
'cookies' => $this->getCookies(), |
106
|
|
|
'session' => $this->getSession(), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|