lib/netgen/content-browser/bundle/EventListener/SetIsApiRequestListener.php line 31

  1. <?php
  2. declare(strict_types=1);
  3. namespace Netgen\Bundle\ContentBrowserBundle\EventListener;
  4. use Netgen\ContentBrowser\Utils\BackwardsCompatibility\MainRequestEventTrait;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use function str_starts_with;
  8. final class SetIsApiRequestListener implements EventSubscriberInterface
  9. {
  10.     use MainRequestEventTrait;
  11.     public const API_FLAG_NAME 'ngcb_is_api_request';
  12.     private const API_ROUTE_PREFIX 'ngcb_api_';
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [KernelEvents::REQUEST => ['onKernelRequest'30]];
  16.     }
  17.     /**
  18.      * Sets the self::API_FLAG_NAME flag if this is a REST API request.
  19.      *
  20.      * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
  21.      */
  22.     public function onKernelRequest($event): void
  23.     {
  24.         if (!$this->isMainRequest($event)) {
  25.             return;
  26.         }
  27.         $request $event->getRequest();
  28.         $currentRoute $request->attributes->get('_route''');
  29.         if (!str_starts_with($currentRouteself::API_ROUTE_PREFIX)) {
  30.             return;
  31.         }
  32.         $request->attributes->set(self::API_FLAG_NAMEtrue);
  33.     }
  34. }