<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use Gedmo\Loggable\LoggableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class DoctrineExtensionsSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly LoggableListener $loggableListener,
private readonly Security $security
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $requestEvent): void
{
if (!$requestEvent->isMainRequest()) {
return;
}
if ($this->security->getUser()) {
$this->loggableListener->setUsername($this->security->getUser()->getUsername());
}
}
}