src/EventListener/UserSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  5. use Doctrine\DBAL\Connection;
  6. use Twig\Environment;
  7. class UserSubscriber
  8. {
  9.     private $connection;
  10.     private $twig;
  11.     public function __construct(Connection $connectionEnvironment $twig)
  12.     {
  13.         $this->connection $connection;
  14.         $this->twig $twig;
  15.     }
  16.     public function onKernelController(ControllerEvent $event): void
  17.     {
  18.         $request $event->getRequest();
  19.         $session $request->getSession();
  20.         $user null;
  21.         if ($session->has('hash')) {
  22.             $hash $session->get('hash');
  23.             $user $this->connection->fetchAssociative('SELECT * FROM users WHERE hash = ?', [$hash]);
  24.         }
  25.         $this->twig->addGlobal('user'$user);
  26.     }
  27. }