src/EventListener/DeferredCookieListener.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Request\DeferredCookieInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class DeferredCookieListener implements EventSubscriberInterface
  10. {
  11.     protected DeferredCookieInterface $cookieManager;
  12.     public function __construct(DeferredCookieInterface $cookieManager)
  13.     {
  14.         $this->cookieManager $cookieManager;
  15.     }
  16.     public function onKernelResponse(ResponseEvent $event)
  17.     {
  18.         $headers $event->getResponse()->headers;
  19.         /** @var Cookie $cookie */
  20.         foreach ($this->cookieManager->getDeferred() as $cookie)
  21.             $headers->setCookie($cookie);
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             KernelEvents::RESPONSE => ['onKernelResponse'0],
  27.         ];
  28.     }
  29. }