src/EventSubscriber/Api/RedirectCookieSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Api;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. /**
  7.  * Subscriber to remove sf_redirect cookies on certain HTTP responses.
  8.  *
  9.  * @package API
  10.  * @internal
  11.  */
  12. class RedirectCookieSubscriber extends AbstractSubscriber
  13. {
  14.     /**
  15.      * Returns an array of event names this subscriber wants to listen to.
  16.      *
  17.      * The array keys are event names and the value can be:
  18.      *
  19.      *  * The method name to call (priority defaults to 0)
  20.      *  * An array composed of the method name to call and the priority
  21.      *  * An array of arrays composed of the method names to call and respective
  22.      *    priorities, or 0 if unset
  23.      *
  24.      * For instance:
  25.      *
  26.      *  * ['eventName' => 'methodName']
  27.      *  * ['eventName' => ['methodName', $priority]]
  28.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  29.      *
  30.      * The code must not depend on runtime state as it will only be called at compile time.
  31.      * All logic depending on runtime state must be put into the individual methods handling the events.
  32.      *
  33.      * @noinspection PhpUnused
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             /**
  39.              * Must be registered after (i.e. with a lower priority than) {@see ProfilerListener::onKernelResponse())
  40.              * listener (currently -100 by bin/console debug:event)
  41.              */
  42.             KernelEvents::RESPONSE => [
  43.                 ['onKernelResponse', -101]
  44.             ]
  45.         ];
  46.     }
  47.     /**
  48.      * Remove sf_redirect cookie on certain HTTP responses.
  49.      *
  50.      * @param ResponseEvent $event Response event.
  51.      * @return void
  52.      * @noinspection PhpUnused
  53.      */
  54.     public function onKernelResponse(ResponseEvent $event): void
  55.     {
  56.         $request $event->getRequest();
  57.         $response $event->getResponse();
  58.         // only handle API routes, create/redirect requests and HTTP 201/307 status codes
  59.         if (str_starts_with($request->getRequestUri(), '/api/') && (
  60.                 (
  61.                     str_ends_with($request->attributes->get('_route'), 'create') &&
  62.                     $response->getStatusCode() === Response::HTTP_CREATED
  63.                 ) ||
  64.                 (
  65.                     str_ends_with($request->attributes->get('_route'), 'default') &&
  66.                     $response->getStatusCode() === Response::HTTP_TEMPORARY_REDIRECT
  67.                 )
  68.             )) {
  69.             $response->headers->removeCookie('sf_redirect');
  70.         }
  71.     }
  72. }