src/EventSubscriber/NotificationSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\NotificationService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Twig\Environment;
  9. class NotificationSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private Environment $twig,
  13.         private NotificationService $notificationService,
  14.         private TokenStorageInterface $tokenStorage,
  15.     ) {
  16.     }
  17.     
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             KernelEvents::CONTROLLER => 'onKernelController',
  22.         ];
  23.     }
  24.     public function onKernelController(ControllerEvent $event): void
  25.     {
  26.         
  27.         if ($event->getRequest()->isXmlHttpRequest()) {
  28.             return;
  29.         }
  30.         $user $this->tokenStorage->getToken()?->getUser();
  31.         if ($user) {
  32.             $this->twig->addGlobal('notification_service'$this->notificationService);
  33.         }
  34.     }
  35. }