Skip to content

Commit 4c071f0

Browse files
committed
Ported master’s route injection behavior to 0.33.x by registering the route resource after wildcard resolution
1 parent decfa49 commit 4c071f0

2 files changed

Lines changed: 122 additions & 14 deletions

File tree

src/Http.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,17 @@ private function runInternal(Request $request, Response $response): static
849849
$this->matchedRoute = $route;
850850
$groups = ($route instanceof Route) ? $route->getGroups() : [];
851851

852+
if (null === $route && null !== self::$wildcardRoute) {
853+
$route = self::$wildcardRoute;
854+
$this->route = $route;
855+
$path = \parse_url($request->getURI(), PHP_URL_PATH);
856+
$route->path($path);
857+
}
858+
859+
self::setResource('route', function () use ($route, $request) {
860+
return $route ?? new Route($request->getMethod(), $request->getURI());
861+
});
862+
852863
if (self::REQUEST_METHOD_HEAD == $method) {
853864
$method = self::REQUEST_METHOD_GET;
854865
$response->disablePayload();
@@ -891,13 +902,6 @@ private function runInternal(Request $request, Response $response): static
891902
return $this;
892903
}
893904

894-
if (null === $route && null !== self::$wildcardRoute) {
895-
$route = self::$wildcardRoute;
896-
$this->route = $route;
897-
$path = \parse_url($request->getURI(), PHP_URL_PATH);
898-
$route->path($path);
899-
}
900-
901905
if (null !== $route) {
902906
return $this->execute($route, $request, $response);
903907
} elseif (self::REQUEST_METHOD_OPTIONS == $method) {

tests/HttpTest.php

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public function testCanGetEnvironmentVariable(): void
8080

8181
public function testCanGetResources(): void
8282
{
83-
Http::setResource('rand', fn () => rand());
84-
Http::setResource('first', fn ($second) => "first-{$second}", ['second']);
85-
Http::setResource('second', fn () => 'second');
83+
Http::setResource('rand', fn() => rand());
84+
Http::setResource('first', fn($second) => "first-{$second}", ['second']);
85+
Http::setResource('second', fn() => 'second');
8686

8787
$second = $this->app->getResource('second');
8888
$first = $this->app->getResource('first');
@@ -117,8 +117,8 @@ public function testCanGetResources(): void
117117

118118
public function testCanGetDefaultValueWithFunction(): void
119119
{
120-
Http::setResource('first', fn ($second) => "first-{$second}", ['second']);
121-
Http::setResource('second', fn () => 'second');
120+
Http::setResource('first', fn($second) => "first-{$second}", ['second']);
121+
Http::setResource('second', fn() => 'second');
122122

123123
$second = $this->app->getResource('second');
124124
$first = $this->app->getResource('first');
@@ -163,7 +163,7 @@ public function testCanGetDefaultValueWithFunction(): void
163163

164164
public function testCanExecuteRoute(): void
165165
{
166-
Http::setResource('rand', fn () => rand());
166+
Http::setResource('rand', fn() => rand());
167167
$resource = $this->app->getResource('rand');
168168

169169
$this->app
@@ -447,6 +447,110 @@ public function testCanSetRoute()
447447
$this->assertEquals($this->app->getRoute(), $route);
448448
}
449449

450+
public function testRouteInjectionAvailableInLifecycle(): void
451+
{
452+
$_SERVER['REQUEST_METHOD'] = 'GET';
453+
$_SERVER['REQUEST_URI'] = '/route-inject';
454+
455+
$initRoutePath = null;
456+
$initRouteMethod = null;
457+
$actionRoutePath = null;
458+
$actionRouteMethod = null;
459+
460+
Http::init()
461+
->inject('route')
462+
->action(function (Route $route) use (&$initRoutePath, &$initRouteMethod) {
463+
$initRoutePath = $route->getPath();
464+
$initRouteMethod = $route->getMethod();
465+
});
466+
467+
Http::get('/route-inject')
468+
->inject('route')
469+
->action(function (Route $route) use (&$actionRoutePath, &$actionRouteMethod) {
470+
$actionRoutePath = $route->getPath();
471+
$actionRouteMethod = $route->getMethod();
472+
});
473+
474+
$this->app->run(new Request(), new Response());
475+
476+
$this->assertSame('/route-inject', $initRoutePath);
477+
$this->assertSame('GET', $initRouteMethod);
478+
$this->assertSame('/route-inject', $actionRoutePath);
479+
$this->assertSame('GET', $actionRouteMethod);
480+
}
481+
482+
public function testRouteInjectionAvailableForOptions(): void
483+
{
484+
$_SERVER['REQUEST_METHOD'] = 'OPTIONS';
485+
$_SERVER['REQUEST_URI'] = '/options-route';
486+
487+
$optionsRoutePath = null;
488+
$optionsRouteMethod = null;
489+
490+
Http::options()
491+
->inject('route')
492+
->action(function (Route $route) use (&$optionsRoutePath, &$optionsRouteMethod) {
493+
$optionsRoutePath = $route->getPath();
494+
$optionsRouteMethod = $route->getMethod();
495+
});
496+
497+
$this->app->run(new Request(), new Response());
498+
499+
$this->assertSame('/options-route', $optionsRoutePath);
500+
$this->assertSame('OPTIONS', $optionsRouteMethod);
501+
}
502+
503+
public function testRouteInjectionAvailableInShutdown(): void
504+
{
505+
$_SERVER['REQUEST_METHOD'] = 'GET';
506+
$_SERVER['REQUEST_URI'] = '/route-inject-shutdown';
507+
508+
$shutdownRoutePath = null;
509+
$shutdownRouteMethod = null;
510+
511+
Http::shutdown()
512+
->inject('route')
513+
->action(function (Route $route) use (&$shutdownRoutePath, &$shutdownRouteMethod) {
514+
$shutdownRoutePath = $route->getPath();
515+
$shutdownRouteMethod = $route->getMethod();
516+
});
517+
518+
Http::get('/route-inject-shutdown')
519+
->action(function () {
520+
});
521+
522+
$this->app->run(new Request(), new Response());
523+
524+
$this->assertSame('/route-inject-shutdown', $shutdownRoutePath);
525+
$this->assertSame('GET', $shutdownRouteMethod);
526+
}
527+
528+
public function testRouteInjectionAvailableInError(): void
529+
{
530+
$_SERVER['REQUEST_METHOD'] = 'GET';
531+
$_SERVER['REQUEST_URI'] = '/route-inject-error';
532+
533+
$errorRoutePath = null;
534+
$errorRouteMethod = null;
535+
536+
Http::error()
537+
->inject('route')
538+
->action(function (Route $route) use (&$errorRoutePath, &$errorRouteMethod) {
539+
$errorRoutePath = $route->getPath();
540+
$errorRouteMethod = $route->getMethod();
541+
});
542+
543+
Http::get('/route-inject-error')
544+
->action(function () {
545+
throw new Exception('Error');
546+
});
547+
548+
$this->app->run(new Request(), new Response());
549+
550+
$this->assertSame('/route-inject-error', $errorRoutePath);
551+
$this->assertSame('GET', $errorRouteMethod);
552+
}
553+
450554
public function providerRouteMatching(): array
451555
{
452556
return [
@@ -650,7 +754,7 @@ public function testWildcardRoute(): void
650754
->inject('response')
651755
->action(function (Request $request, Response $response) {
652756
$route = $this->app->getRoute();
653-
Http::setResource('myRoute', fn () => $route);
757+
Http::setResource('myRoute', fn() => $route);
654758

655759
if ($request->getURI() === '/init_response') {
656760
$response->send('THIS IS RESPONSE FROM INIT!');

0 commit comments

Comments
 (0)