<?php
namespace App\EventSubscriber;
use App\Form\Website\Type\Authentication\ConnectionType;
use App\Form\Website\Type\Authentication\ForgotPasswordType;
use App\Form\Website\Type\Authentication\SubscriptionType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
/**
* Subscriber pour rendre les formulaires d'authentification disponibles globalement dans Twig
*/
class AuthenticationFormsSubscriber implements EventSubscriberInterface
{
private FormFactoryInterface $formFactory;
private Environment $twig;
public function __construct(FormFactoryInterface $formFactory, Environment $twig)
{
$this->formFactory = $formFactory;
$this->twig = $twig;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
public function onKernelController(ControllerEvent $event): void
{
// Ne s'exécuter que dans le contexte website (pas admin)
$request = $event->getRequest();
if (!$request || strpos($request->getPathInfo(), '/admin') === 0) {
return;
}
// Créer les formulaires d'authentification
$connectionForm = $this->formFactory->create(ConnectionType::class);
$forgotPasswordForm = $this->formFactory->create(ForgotPasswordType::class);
$subscriptionForm = $this->formFactory->create(SubscriptionType::class);
// Les rendre disponibles globalement dans Twig
$this->twig->addGlobal('connectionForm', $connectionForm->createView());
$this->twig->addGlobal('forgotPasswordForm', $forgotPasswordForm->createView());
$this->twig->addGlobal('subscriptionForm', $subscriptionForm->createView());
}
}