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