vendor/symfony/mercure/src/EventSubscriber/SetCookieSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Mercure Component project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Symfony\Component\Mercure\EventSubscriber;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. /**
  16. * Sets the cookies created by the Authorization helper class.
  17. *
  18. * @author Kévin Dunglas <kevin@dunglas.fr>
  19. */
  20. final class SetCookieSubscriber implements EventSubscriberInterface
  21. {
  22. public function onKernelResponse(ResponseEvent $event): void
  23. {
  24. $mainRequest = method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest(); /* @phpstan-ignore-line */
  25. if (
  26. !($mainRequest) ||
  27. null === $cookies = ($request = $event->getRequest())->attributes->get('_mercure_authorization_cookies')) {
  28. return;
  29. }
  30. $request->attributes->remove('_mercure_authorization_cookies');
  31. $response = $event->getResponse();
  32. foreach ($cookies as $cookie) {
  33. $response->headers->setCookie($cookie);
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function getSubscribedEvents(): array
  40. {
  41. return [KernelEvents::RESPONSE => 'onKernelResponse'];
  42. }
  43. }