Skip to content
Merged
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
14 changes: 14 additions & 0 deletions src/Tempest/Router/src/GenericRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Tempest\View\View;

use function Tempest\map;
use function Tempest\Support\Regex\replace;
use function Tempest\Support\str;

/**
Expand Down Expand Up @@ -173,6 +174,19 @@ public function toUri(array|string $action, ...$params): string
return $uri->toString();
}

public function isCurrentUri(array|string $action, ...$params): bool
{
$matchedRoute = $this->container->get(MatchedRoute::class);
$candidateUri = $this->toUri($action, ...[...$matchedRoute->params, ...$params]);
$currentUri = $this->toUri([$matchedRoute->route->handler->getDeclaringClass(), $matchedRoute->route->handler->getName()]);

foreach ($matchedRoute->params as $key => $value) {
$currentUri = replace($currentUri, '/({' . preg_quote($key, '/') . '(?::.*?)?})/', $value);
}

return $currentUri === $candidateUri;
}

private function createResponse(Response|View $input): Response
{
if ($input instanceof View) {
Expand Down
8 changes: 8 additions & 0 deletions src/Tempest/Router/src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ interface Router
{
public function dispatch(Request|PsrRequest $request): Response;

/**
* Creates a valid URI to the given `$action`.
*/
public function toUri(array|string $action, ...$params): string;

/**
* Checks if the URI to the given `$action` would match the current route.
*/
public function isCurrentUri(array|string $action, ...$params): bool;

/**
* @template T of \Tempest\Router\HttpMiddleware
* @param class-string<T> $middlewareClass
Expand Down
20 changes: 20 additions & 0 deletions src/Tempest/Router/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,24 @@ function uri(array|string|MethodReflector $action, mixed ...$params): string
...$params,
);
}

/**
* Checks whether the given controller action matches the current URI.
*/
function is_current_uri(array|string|MethodReflector $action, mixed ...$params): bool
{
if ($action instanceof MethodReflector) {
$action = [
$action->getDeclaringClass()->getName(),
$action->getName(),
];
}

$router = get(Router::class);

return $router->isCurrentUri(
$action,
...$params,
);
}
}
38 changes: 38 additions & 0 deletions tests/Integration/Route/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Tempest\Database\Migrations\CreateMigrationsTable;
use Tempest\Http\Status;
use Tempest\Router\GenericRouter;
use Tempest\Router\MatchedRoute;
use Tempest\Router\Responses\Ok;
use Tempest\Router\Router;
use Tests\Tempest\Fixtures\Controllers\ControllerWithEnumBinding;
Expand Down Expand Up @@ -209,4 +210,41 @@ public function test_json_request(): void
$this->assertSame(Status::OK, $response->status);
$this->assertSame('foo', $response->body);
}

public function test_is_current_uri(): void
{
$router = $this->container->get(GenericRouter::class);

$this->http->get('/test')->assertOk();

$this->assertTrue($router->isCurrentUri([TestController::class, '__invoke']));
$this->assertFalse($router->isCurrentUri([TestController::class, 'withParams']));
$this->assertFalse($router->isCurrentUri([TestController::class, 'withParams'], id: 1));
$this->assertFalse($router->isCurrentUri([TestController::class, 'withParams'], id: 1, name: 'a'));
}

public function test_is_current_uri_with_constrained_parameters(): void
{
$router = $this->container->get(GenericRouter::class);

$this->http->get('/test/1/a')->assertOk();

$this->assertTrue($router->isCurrentUri([TestController::class, 'withCustomRegexParams']));
$this->assertTrue($router->isCurrentUri([TestController::class, 'withCustomRegexParams'], id: 1));
$this->assertTrue($router->isCurrentUri([TestController::class, 'withCustomRegexParams'], id: 1, name: 'a'));
$this->assertFalse($router->isCurrentUri([TestController::class, 'withCustomRegexParams'], id: 1, name: 'b'));
$this->assertFalse($router->isCurrentUri([TestController::class, 'withCustomRegexParams'], id: 0, name: 'a'));
$this->assertFalse($router->isCurrentUri([TestController::class, 'withCustomRegexParams'], id: 0, name: 'b'));
}

public function test_is_current_uri_with_enum(): void
{
$router = $this->container->get(GenericRouter::class);

$this->http->get('/with-enum/foo')->assertOk();

$this->assertTrue($router->isCurrentUri(ControllerWithEnumBinding::class));
$this->assertTrue($router->isCurrentUri(ControllerWithEnumBinding::class, input: EnumForController::FOO));
$this->assertFalse($router->isCurrentUri(ControllerWithEnumBinding::class, input: EnumForController::BAR));
}
}
Loading