|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/EventSubscriber/RequestLogSubscriber.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\EventSubscriber; |
|
10
|
|
|
|
|
11
|
|
|
use App\Security\ApiKeyUser; |
|
12
|
|
|
use App\Security\SecurityUser; |
|
13
|
|
|
use App\Security\UserTypeIdentification; |
|
14
|
|
|
use App\Utils\RequestLogger; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Event\TerminateEvent; |
|
19
|
|
|
use Throwable; |
|
20
|
|
|
use function array_filter; |
|
21
|
|
|
use function array_values; |
|
22
|
|
|
use function in_array; |
|
23
|
|
|
use function str_contains; |
|
24
|
|
|
use function substr; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class RequestLogSubscriber |
|
28
|
|
|
* |
|
29
|
|
|
* @package App\EventSubscriber |
|
30
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
31
|
|
|
* |
|
32
|
|
|
* @property array<int, string> $ignoredRoutes |
|
33
|
|
|
*/ |
|
34
|
|
|
class RequestLogSubscriber implements EventSubscriberInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* RequestLogSubscriber constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array<int, string> $ignoredRoutes |
|
40
|
|
|
*/ |
|
41
|
607 |
|
public function __construct( |
|
42
|
|
|
private readonly RequestLogger $requestLogger, |
|
43
|
|
|
private readonly UserTypeIdentification $userService, |
|
44
|
|
|
#[Autowire('%env(key:REQUEST_LOG_IGNORED_ROUTES:json:file:APPLICATION_CONFIG)%')] |
|
45
|
|
|
private readonly array $ignoredRoutes, |
|
46
|
|
|
) { |
|
47
|
607 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public static function getSubscribedEvents(): array |
|
53
|
|
|
{ |
|
54
|
1 |
|
return [ |
|
55
|
1 |
|
TerminateEvent::class => [ |
|
56
|
1 |
|
'onTerminateEvent', |
|
57
|
1 |
|
15, |
|
58
|
1 |
|
], |
|
59
|
1 |
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Subscriber method to log every request / response. |
|
64
|
|
|
* |
|
65
|
|
|
* @throws Throwable |
|
66
|
|
|
*/ |
|
67
|
607 |
|
public function onTerminateEvent(TerminateEvent $event): void |
|
68
|
|
|
{ |
|
69
|
607 |
|
$request = $event->getRequest(); |
|
70
|
607 |
|
$path = $request->getPathInfo(); |
|
71
|
|
|
|
|
72
|
607 |
|
$filter = static fn (string $route): bool => |
|
73
|
545 |
|
str_contains($route, '/*') && str_contains($path, substr($route, 0, -2)); |
|
74
|
|
|
|
|
75
|
|
|
// We don't want to log OPTIONS requests, /_profiler* -path, ignored routes and wildcard ignored routes |
|
76
|
607 |
|
if ($request->getRealMethod() === Request::METHOD_OPTIONS |
|
77
|
558 |
|
|| str_contains($path, '/_profiler') |
|
78
|
558 |
|
|| in_array($path, $this->ignoredRoutes, true) |
|
79
|
607 |
|
|| array_values(array_filter($this->ignoredRoutes, $filter)) !== [] |
|
80
|
|
|
) { |
|
81
|
62 |
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
545 |
|
$this->process($event); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Method to process current request event. |
|
89
|
|
|
* |
|
90
|
|
|
* @throws Throwable |
|
91
|
|
|
*/ |
|
92
|
545 |
|
private function process(TerminateEvent $event): void |
|
93
|
|
|
{ |
|
94
|
545 |
|
$request = $event->getRequest(); |
|
95
|
|
|
|
|
96
|
|
|
// Set needed data to logger and handle actual log |
|
97
|
545 |
|
$this->requestLogger->setRequest($request); |
|
98
|
545 |
|
$this->requestLogger->setResponse($event->getResponse()); |
|
99
|
|
|
|
|
100
|
545 |
|
$identify = $this->userService->getIdentity(); |
|
101
|
|
|
|
|
102
|
545 |
|
if ($identify instanceof SecurityUser) { |
|
103
|
257 |
|
$this->requestLogger->setUserId($identify->getUserIdentifier()); |
|
104
|
290 |
|
} elseif ($identify instanceof ApiKeyUser) { |
|
105
|
6 |
|
$this->requestLogger->setApiKeyId($identify->getApiKeyIdentifier()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
545 |
|
$this->requestLogger->setMainRequest($event->isMainRequest()); |
|
109
|
545 |
|
$this->requestLogger->handle(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|