1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
6
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Yiisoft\Yii\Http\Event\AfterRequest; |
11
|
|
|
use Yiisoft\Yii\Http\Event\BeforeRequest; |
12
|
|
|
|
13
|
|
|
use function is_object; |
14
|
|
|
|
15
|
|
|
final class RequestCollector implements CollectorInterface, IndexCollectorInterface |
16
|
|
|
{ |
17
|
|
|
use CollectorTrait; |
18
|
|
|
|
19
|
|
|
private string $requestUrl = ''; |
20
|
|
|
private string $requestMethod = ''; |
21
|
|
|
private bool $requestIsAjax = false; |
22
|
|
|
private ?string $userIp = null; |
23
|
|
|
private int $responseStatusCode = 200; |
24
|
|
|
private ?ServerRequestInterface $request = null; |
25
|
|
|
private ?ResponseInterface $response = null; |
26
|
|
|
|
27
|
1 |
|
#[ArrayShape([ |
28
|
|
|
'requestUrl' => 'string', |
29
|
|
|
'requestMethod' => 'string', |
30
|
|
|
'requestIsAjax' => 'bool', |
31
|
|
|
'userIp' => 'null|string', |
32
|
|
|
'responseStatusCode' => 'int', |
33
|
|
|
'request' => "null|\Psr\Http\Message\ServerRequestInterface", |
34
|
|
|
'response' => "null|\Psr\Http\Message\ResponseInterface", |
35
|
|
|
])] |
36
|
|
|
public function getCollected(): array |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
1 |
|
'requestUrl' => $this->requestUrl, |
40
|
1 |
|
'requestMethod' => $this->requestMethod, |
41
|
1 |
|
'requestIsAjax' => $this->requestIsAjax, |
42
|
1 |
|
'userIp' => $this->userIp, |
43
|
1 |
|
'responseStatusCode' => $this->responseStatusCode, |
44
|
1 |
|
'request' => $this->request, |
45
|
1 |
|
'response' => $this->response, |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function collect(object $event): void |
50
|
|
|
{ |
51
|
1 |
|
if (!is_object($event) || !$this->isActive()) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
if ($event instanceof BeforeRequest) { |
56
|
1 |
|
$this->request = $event->getRequest(); |
57
|
1 |
|
$this->requestUrl = (string)$event->getRequest()->getUri(); |
58
|
1 |
|
$this->requestMethod = $event->getRequest()->getMethod(); |
59
|
1 |
|
$this->requestIsAjax = strtolower( |
60
|
1 |
|
$event->getRequest()->getHeaderLine('X-Requested-With') ?? '' |
61
|
|
|
) === 'xmlhttprequest'; |
62
|
1 |
|
$this->userIp = $event->getRequest()->getServerParams()['REMOTE_ADDR'] ?? null; |
63
|
|
|
} |
64
|
1 |
|
if ($event instanceof AfterRequest) { |
65
|
1 |
|
$this->response = $event->getResponse(); |
66
|
1 |
|
$this->responseStatusCode = $event->getResponse() !== null ? $event->getResponse()->getStatusCode() : 500; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
#[ArrayShape([ |
71
|
|
|
'requestUrl' => 'string', |
72
|
|
|
'requestMethod' => 'string', |
73
|
|
|
'requestIsAjax' => 'bool', |
74
|
|
|
'userIp' => 'null|string', |
75
|
|
|
'responseStatusCode' => 'int', |
76
|
|
|
])] |
77
|
|
|
public function getIndexData(): array |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
1 |
|
'requestUrl' => $this->requestUrl, |
81
|
1 |
|
'requestMethod' => $this->requestMethod, |
82
|
1 |
|
'requestIsAjax' => $this->requestIsAjax, |
83
|
1 |
|
'userIp' => $this->userIp, |
84
|
1 |
|
'responseStatusCode' => $this->responseStatusCode, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
private function reset(): void |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
$this->request = null; |
91
|
1 |
|
$this->response = null; |
92
|
1 |
|
$this->requestUrl = ''; |
93
|
1 |
|
$this->requestMethod = ''; |
94
|
1 |
|
$this->requestIsAjax = false; |
95
|
1 |
|
$this->userIp = null; |
96
|
1 |
|
$this->responseStatusCode = 200; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.