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
4 changes: 4 additions & 0 deletions packages/router/src/MatchRouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function __construct(

public function __invoke(Request $request, HttpMiddlewareCallable $next): Response
{
// In long-running contexts, a previous request may have left a matched route behind.
// It must be cleared before matching, since we only rebind it on a successful match.
$this->container->unregister(MatchedRoute::class);
Comment thread
aidan-casey marked this conversation as resolved.

$matchedRoute = $this->routeMatcher->match($request);

if (! $matchedRoute instanceof MatchedRoute && $request->method === Method::HEAD && $request instanceof GenericRequest) {
Expand Down
18 changes: 18 additions & 0 deletions packages/router/src/RouterReset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tempest\Router;

use Tempest\Container\Container;
use Tempest\Container\Resettable;

final readonly class RouterReset implements Resettable
{
public function __construct(
private Container $container,
) {}

public function reset(): void
{
$this->container->unregister(MatchedRoute::class);
}
}
24 changes: 24 additions & 0 deletions tests/Integration/Router/MatchRouteMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Router;

use PHPUnit\Framework\Attributes\Test;
use Tempest\Router\MatchedRoute;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

final class MatchRouteMiddlewareTest extends FrameworkIntegrationTestCase
{
#[Test]
public function unmatched_request_does_not_leave_the_previous_matched_route_bound(): void
{
$this->http->get('/repeated/a')->assertOk();

$this->assertTrue($this->container->has(MatchedRoute::class));

$this->http->get('/does-not-exist')->assertNotFound();

$this->assertFalse($this->container->has(MatchedRoute::class));
}
}
24 changes: 24 additions & 0 deletions tests/Integration/Router/RouterResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Router;

use PHPUnit\Framework\Attributes\Test;
use Tempest\Router\MatchedRoute;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

final class RouterResetTest extends FrameworkIntegrationTestCase
{
#[Test]
public function reset_clears_the_matched_route(): void
{
$this->http->get('/repeated/a')->assertOk();

$this->assertTrue($this->container->has(MatchedRoute::class));

$this->container->reset();

$this->assertFalse($this->container->has(MatchedRoute::class));
}
}
Loading