custom/plugins/SwagB2bPlatform/SwagB2bPlatform/Subscriber/CookieSubscriber.php line 25

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SwagB2bPlatform\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\Cookie;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use function array_keys;
  8. use function mb_strpos;
  9. use function sprintf;
  10. class CookieSubscriber implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             KernelEvents::RESPONSE => 'overrideCsrfCookies',
  16.         ];
  17.     }
  18.     public function overrideCsrfCookies(ResponseEvent $event): void
  19.     {
  20.         $cookies $event->getRequest()->cookies;
  21.         $csrf $cookies->get('csrf');
  22.         if (!$csrf) {
  23.             return;
  24.         }
  25.         $names array_keys($csrf);
  26.         foreach ($names as $name) {
  27.             if (mb_strpos($name'frontend.b2b') === false) {
  28.                 continue;
  29.             }
  30.             $cookie = new Cookie(sprintf('csrf[%s]'$name));
  31.             $event->getResponse()->headers->setCookie($cookie);
  32.         }
  33.     }
  34. }