Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Move REST route recognition after routing resolves #130

Open
wants to merge 3 commits into
base: 4.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/bundle/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
alongosz marked this conversation as resolved.
Show resolved Hide resolved
['addAttributeForIbexaRestRoute', 30],
],
];
}

Expand All @@ -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
*
Expand Down
12 changes: 11 additions & 1 deletion tests/bundle/EventListener/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/bundle/EventListener/RequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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();
}
}
Expand Down
Loading