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