1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector\Web; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Message; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorTrait; |
11
|
|
|
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; |
12
|
|
|
use Yiisoft\Yii\Debug\Collector\TimelineCollector; |
13
|
|
|
use Yiisoft\Yii\Http\Event\AfterRequest; |
14
|
|
|
use Yiisoft\Yii\Http\Event\BeforeRequest; |
15
|
|
|
|
16
|
|
|
use function is_object; |
17
|
|
|
|
18
|
|
|
final class RequestCollector implements SummaryCollectorInterface |
19
|
|
|
{ |
20
|
|
|
use CollectorTrait; |
21
|
|
|
|
22
|
|
|
private string $requestUrl = ''; |
23
|
|
|
private string $requestPath = ''; |
24
|
|
|
private string $requestQuery = ''; |
25
|
|
|
private string $requestMethod = ''; |
26
|
|
|
private bool $requestIsAjax = false; |
27
|
|
|
private ?string $userIp = null; |
28
|
|
|
private int $responseStatusCode = 200; |
29
|
|
|
private ?ServerRequestInterface $request = null; |
30
|
|
|
private ?ResponseInterface $response = null; |
31
|
|
|
|
32
|
|
|
public function __construct(private TimelineCollector $timelineCollector) |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getCollected(): array |
37
|
|
|
{ |
38
|
|
|
if (!$this->isActive()) { |
39
|
|
|
return []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$requestRaw = null; |
43
|
|
|
if ($this->request instanceof ServerRequestInterface) { |
44
|
|
|
$requestRaw = Message::toString($this->request); |
45
|
|
|
Message::rewindBody($this->request); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$responseRaw = null; |
49
|
|
|
if ($this->response instanceof ResponseInterface) { |
50
|
|
|
$responseRaw = Message::toString($this->response); |
51
|
|
|
Message::rewindBody($this->response); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return [ |
55
|
|
|
'requestUrl' => $this->requestUrl, |
56
|
|
|
'requestPath' => $this->requestPath, |
57
|
|
|
'requestQuery' => $this->requestQuery, |
58
|
|
|
'requestMethod' => $this->requestMethod, |
59
|
|
|
'requestIsAjax' => $this->requestIsAjax, |
60
|
|
|
'userIp' => $this->userIp, |
61
|
|
|
'responseStatusCode' => $this->responseStatusCode, |
62
|
|
|
'request' => $this->request, |
63
|
|
|
'requestRaw' => $requestRaw, |
64
|
|
|
'response' => $this->response, |
65
|
|
|
'responseRaw' => $responseRaw, |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function collect(object $event): void |
70
|
|
|
{ |
71
|
|
|
if (!is_object($event) || !$this->isActive()) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($event instanceof BeforeRequest) { |
76
|
|
|
$request = $event->getRequest(); |
77
|
|
|
|
78
|
|
|
$this->request = $request; |
79
|
|
|
$this->requestUrl = (string) $request->getUri(); |
80
|
|
|
$this->requestPath = $request->getUri()->getPath(); |
81
|
|
|
$this->requestQuery = $request->getUri()->getQuery(); |
82
|
|
|
$this->requestMethod = $request->getMethod(); |
83
|
|
|
$this->requestIsAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest'; |
84
|
|
|
$this->userIp = $request->getServerParams()['REMOTE_ADDR'] ?? null; |
85
|
|
|
} |
86
|
|
|
if ($event instanceof AfterRequest) { |
87
|
|
|
$response = $event->getResponse(); |
88
|
|
|
|
89
|
|
|
$this->response = $response; |
90
|
|
|
$this->responseStatusCode = $response !== null ? $response->getStatusCode() : 500; |
91
|
|
|
} |
92
|
|
|
$this->timelineCollector->collect($this, spl_object_id($event)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getSummary(): array |
96
|
|
|
{ |
97
|
|
|
if (!$this->isActive()) { |
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
return [ |
101
|
|
|
'request' => [ |
102
|
|
|
'url' => $this->requestUrl, |
103
|
|
|
'path' => $this->requestPath, |
104
|
|
|
'query' => $this->requestQuery, |
105
|
|
|
'method' => $this->requestMethod, |
106
|
|
|
'isAjax' => $this->requestIsAjax, |
107
|
|
|
'userIp' => $this->userIp, |
108
|
|
|
], |
109
|
|
|
'response' => [ |
110
|
|
|
'statusCode' => $this->responseStatusCode, |
111
|
|
|
], |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function reset(): void |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->request = null; |
118
|
|
|
$this->response = null; |
119
|
|
|
$this->requestUrl = ''; |
120
|
|
|
$this->requestMethod = ''; |
121
|
|
|
$this->requestIsAjax = false; |
122
|
|
|
$this->userIp = null; |
123
|
|
|
$this->responseStatusCode = 200; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.