diff --git a/src/bundle/EventListener/RequestListener.php b/src/bundle/EventListener/RequestListener.php index 890486ef..797eeef6 100644 --- a/src/bundle/EventListener/RequestListener.php +++ b/src/bundle/EventListener/RequestListener.php @@ -42,7 +42,10 @@ public static function getSubscribedEvents(): array { return [ // 10001 is to ensure that REST requests are tagged before CorsListener is called - KernelEvents::REQUEST => ['onKernelRequest', 10001], + KernelEvents::REQUEST => [ + ['onKernelRequest', 10001], + ['addAttributeForIbexaRestRoute', 30], + ], ]; } @@ -57,6 +60,19 @@ public function onKernelRequest(RequestEvent $event): void ); } + public function addAttributeForIbexaRestRoute(RequestEvent $event): void + { + $path = $event->getRequest()->attributes->get('semanticPathinfo'); + if ($path === null) { + return; + } + + $event->getRequest()->attributes->set( + 'is_rest_request', + $this->uriParser->hasRestPrefix($path) + ); + } + /** * @param \Symfony\Component\HttpFoundation\Request $request * diff --git a/tests/bundle/EventListener/EventListenerTest.php b/tests/bundle/EventListener/EventListenerTest.php index c41cd34f..f2891ca3 100644 --- a/tests/bundle/EventListener/EventListenerTest.php +++ b/tests/bundle/EventListener/EventListenerTest.php @@ -49,8 +49,18 @@ public function testGetSubscribedEvents($expectedEventTypes) // Check that referenced methods exist foreach ($supportedEvents as $method) { + // Multiple events, or single event with priority + if (is_array($method)) { + $method = $method[0]; + } + + // Multiple events with priorities + if (is_array($method)) { + $method = $method[0]; + } + self::assertTrue( - method_exists($eventListener, is_array($method) ? $method[0] : $method) + method_exists($eventListener, $method) ); } } diff --git a/tests/bundle/EventListener/RequestListenerTest.php b/tests/bundle/EventListener/RequestListenerTest.php index 2b9ef6ba..2989c7d3 100644 --- a/tests/bundle/EventListener/RequestListenerTest.php +++ b/tests/bundle/EventListener/RequestListenerTest.php @@ -40,6 +40,7 @@ public static function getDataForTestOnKernelRequest(): array // REST requests [self::REST_ROUTE, true], ['/api/ibexa/v2/true', true], + ['/admin/api/ibexa/v2/true', true], ['/api/bundle-name/v2/true', true], ['/api/MyBundle12/v2/true', true], ['/api/ThisIs_Bundle123/v2/true', true], @@ -95,14 +96,21 @@ protected function getEventListener(): RequestListener protected function performFakeRequest(string $uri, int $type = HttpKernelInterface::MAIN_REQUEST): Request { + $request = Request::create($uri); $event = new RequestEvent( $this->createMock(HttpKernelInterface::class), - Request::create($uri), + $request, $type ); $this->getEventListener()->onKernelRequest($event); + if (str_starts_with($uri, '/admin')) { + $uri = substr($uri, strlen('/admin')); + } + $request->attributes->set('semanticPathinfo', $uri); + $this->getEventListener()->addAttributeForIbexaRestRoute($event); + return $event->getRequest(); } }