src/Event/RequestIdListener.php line 26
<?php
declare(strict_types=1);
namespace App\Event;
use MarfaTech\Component\IdGenerator\Generator\IdGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestIdListener implements EventSubscriberInterface
{
public function __construct(
private readonly IdGenerator $idGenerator,
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 100],
];
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
if (!$request->attributes->has('requestId')) {
$requestId = $this->idGenerator->generateUniqueId();
$request->attributes->set('requestId', $requestId);
}
}
}