Skip to content

Commit 694d807

Browse files
loks0nclaude
andcommitted
Remove Http::getRoute / setRoute / get-set MatchedPath
Route and matched path are now pure context values — read them via \$http->getResource('route') / 'matchedPath', or inject them into a hook/action. Internal call sites that wrote them go through the context container directly (or via setContext()). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 23c23c4 commit 694d807

2 files changed

Lines changed: 23 additions & 50 deletions

File tree

src/Http/Http.php

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -457,38 +457,6 @@ public static function getRoutes(): array
457457
return Router::getRoutes();
458458
}
459459

460-
/**
461-
* Get the current route
462-
*/
463-
public function getRoute(): ?Route
464-
{
465-
$container = $this->server->getContainer();
466-
467-
return $container->has('route') ? $container->get('route') : null;
468-
}
469-
470-
private function setRoute(?Route $route): void
471-
{
472-
$this->server->getContainer()->set('route', fn() => $route);
473-
}
474-
475-
/**
476-
* Read the prepared-path string the current request matched under.
477-
* Falls back to '' so callers (e.g. tests invoking execute() directly)
478-
* keep getting the legacy "first registered path-params" behavior.
479-
*/
480-
private function getMatchedPath(): string
481-
{
482-
$container = $this->server->getContainer();
483-
484-
return $container->has('matchedPath') ? $container->get('matchedPath') : '';
485-
}
486-
487-
private function setMatchedPath(string $path): void
488-
{
489-
$this->server->getContainer()->set('matchedPath', fn() => $path);
490-
}
491-
492460
/**
493461
* Add Route
494462
*
@@ -601,8 +569,10 @@ public function start(): void
601569
*/
602570
public function match(Request $request, bool $fresh = true): ?Route
603571
{
604-
if (!$fresh) {
605-
$cached = $this->getRoute();
572+
$context = $this->server->getContainer();
573+
574+
if (!$fresh && $context->has('route')) {
575+
$cached = $context->get('route');
606576
if (null !== $cached) {
607577
return $cached;
608578
}
@@ -615,14 +585,14 @@ public function match(Request $request, bool $fresh = true): ?Route
615585

616586
$matched = Router::match($method, $url);
617587
if (null === $matched) {
618-
$this->setRoute(null);
619-
$this->setMatchedPath('');
588+
$context->set('route', fn() => null);
589+
$context->set('matchedPath', fn() => '');
620590
return null;
621591
}
622592

623593
[$route, $matchedPath] = $matched;
624-
$this->setRoute($route);
625-
$this->setMatchedPath($matchedPath);
594+
$context->set('route', fn() => $route);
595+
$context->set('matchedPath', fn() => $matchedPath);
626596

627597
return $route;
628598
}
@@ -635,7 +605,9 @@ public function execute(Route $route, Request $request, Response $response): sta
635605
$arguments = [];
636606
$groups = $route->getGroups();
637607

638-
$preparedPath = Router::preparePath($this->getMatchedPath());
608+
$context = $this->server->getContainer();
609+
$matchedPath = $context->has('matchedPath') ? $context->get('matchedPath') : '';
610+
$preparedPath = Router::preparePath($matchedPath);
639611
$pathValues = $route->getPathValues($request, $preparedPath[0]);
640612

641613
try {
@@ -766,10 +738,12 @@ public function run(Request $request, Response $response): static
766738
$result = $this->runInternal($request, $response);
767739

768740
$requestDuration = microtime(true) - $start;
741+
$context = $this->server->getContainer();
742+
$route = $context->has('route') ? $context->get('route') : null;
769743
$attributes = [
770744
'url.scheme' => $request->getProtocol(),
771745
'http.request.method' => $request->getMethod(),
772-
'http.route' => $this->getRoute()?->getPath(),
746+
'http.route' => $route?->getPath(),
773747
'http.response.status_code' => $response->getStatusCode(),
774748
];
775749
$this->requestDuration->record($requestDuration, $attributes);
@@ -883,8 +857,7 @@ private function runInternal(Request $request, Response $response): static
883857
$path = \is_string($path) ? ($path === '' ? '/' : $path) : '/';
884858
$route->path($path);
885859

886-
$this->setRoute($route);
887-
$this->setContext('route', fn() => $route, []);
860+
$this->setContext('route', fn() => $route);
888861
}
889862
if (null !== $route) {
890863
return $this->execute($route, $request, $response);

tests/HttpTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public function testCanMatchRoute(string $method, string $path, ?string $url = n
413413
$_SERVER['REQUEST_URI'] = $url;
414414

415415
$this->assertSame($expected, $this->http->match(new Request()));
416-
$this->assertSame($expected, $this->http->getRoute());
416+
$this->assertSame($expected, $this->http->getResource("route"));
417417
}
418418

419419
public function testNoMismatchRoute(): void
@@ -442,7 +442,7 @@ public function testNoMismatchRoute(): void
442442
$route = $this->http->match(new Request(), fresh: true);
443443

444444
$this->assertNull($route);
445-
$this->assertNull($this->http->getRoute());
445+
$this->assertNull($this->http->getResource("route"));
446446
}
447447
}
448448

@@ -457,20 +457,20 @@ public function testCanMatchFreshRoute(): void
457457
$_SERVER['REQUEST_URI'] = '/path1';
458458
$matched = $this->http->match(new Request());
459459
$this->assertSame($route1, $matched);
460-
$this->assertSame($route1, $this->http->getRoute());
460+
$this->assertSame($route1, $this->http->getResource("route"));
461461

462462
// Second request match returns cached route
463463
$_SERVER['REQUEST_METHOD'] = 'HEAD';
464464
$_SERVER['REQUEST_URI'] = '/path2';
465465
$request2 = new Request();
466466
$matched = $this->http->match($request2, fresh: false);
467467
$this->assertSame($route1, $matched);
468-
$this->assertSame($route1, $this->http->getRoute());
468+
$this->assertSame($route1, $this->http->getResource("route"));
469469

470470
// Fresh match returns new route
471471
$matched = $this->http->match($request2, fresh: true);
472472
$this->assertSame($route2, $matched);
473-
$this->assertSame($route2, $this->http->getRoute());
473+
$this->assertSame($route2, $this->http->getResource("route"));
474474
} catch (\Exception $e) {
475475
$this->fail($e->getMessage());
476476
}
@@ -484,7 +484,7 @@ public function testCanMatchRootRouteWhenUriHasNoPath(): void
484484
$_SERVER['REQUEST_URI'] = 'https://example.com?x=1';
485485

486486
$this->assertSame($route, $this->http->match(new Request()));
487-
$this->assertSame($route, $this->http->getRoute());
487+
$this->assertSame($route, $this->http->getResource("route"));
488488
}
489489

490490
public function testCanRunRequest(): void
@@ -523,8 +523,8 @@ public function testWildcardRoute(): void
523523
$_SERVER['REQUEST_URI'] = '/unknown_path';
524524

525525
Http::init()
526-
->action(function () {
527-
$route = $this->http->getRoute();
526+
->inject('route')
527+
->action(function ($route) {
528528
$this->container->set('myRoute', fn() => $route);
529529
});
530530

0 commit comments

Comments
 (0)