Skip to content

Commit 607abe2

Browse files
committed
Revert "Wildcard is a Hook, not a Route"
This reverts commit e89d58f.
1 parent e89d58f commit 607abe2

4 files changed

Lines changed: 31 additions & 54 deletions

File tree

src/Http/Http.php

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,16 @@ public static function delete(string $url): Route
223223
}
224224

225225
/**
226-
* Register a method-agnostic fallback handler used when no route matches.
226+
* Wildcard
227227
*
228-
* Use this for catch-all behavior: rendering a custom 404, proxying
229-
* unmatched paths, serving a SPA, etc. The returned {@see Hook} accepts
230-
* the usual `param()`, `inject()`, `action()` configuration but has no
231-
* method or path of its own.
228+
* Add Wildcard route
232229
*/
233-
public static function wildcard(): Hook
230+
public static function wildcard(): Route
234231
{
235-
$hook = new Hook();
236-
$hook->groups(['*']);
237-
Router::setWildcard($hook);
232+
$route = new Route('', '');
233+
Router::setWildcard($route);
238234

239-
return $hook;
235+
return $route;
240236
}
241237

242238
/**
@@ -549,31 +545,30 @@ public function execute(Request $request, Response $response): static
549545
$match = $this->match($request);
550546

551547
if (self::REQUEST_METHOD_OPTIONS === $method) {
552-
$matchedRoute = $match?->route instanceof Route ? $match->route : null;
553-
$groups = $matchedRoute?->getGroups() ?? [];
548+
$groups = $match?->route->getGroups() ?? [];
554549

555550
try {
556551
foreach ($groups as $group) {
557552
foreach (self::$options as $option) { // Group options hooks
558553
/** @var Hook $option */
559554
if (\in_array($group, $option->getGroups())) {
560-
\call_user_func_array($option->getAction(), $this->getArguments($option, [], $request->getParams(), $matchedRoute));
555+
\call_user_func_array($option->getAction(), $this->getArguments($option, [], $request->getParams(), $match->route));
561556
}
562557
}
563558
}
564559

565560
foreach (self::$options as $option) { // Global options hooks
566561
/** @var Hook $option */
567562
if (\in_array('*', $option->getGroups())) {
568-
\call_user_func_array($option->getAction(), $this->getArguments($option, [], $request->getParams(), $matchedRoute));
563+
\call_user_func_array($option->getAction(), $this->getArguments($option, [], $request->getParams(), $match?->route));
569564
}
570565
}
571566
} catch (\Throwable $e) {
572567
foreach (self::$errors as $error) { // Global error hooks
573568
/** @var Hook $error */
574569
if (\in_array('*', $error->getGroups())) {
575570
$this->context()->set('error', fn() => $e, []);
576-
\call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams(), $matchedRoute));
571+
\call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams(), $match?->route));
577572
}
578573
}
579574
}
@@ -595,14 +590,12 @@ public function execute(Request $request, Response $response): static
595590
$route = $match->route;
596591
$arguments = [];
597592
$groups = $route->getGroups();
598-
$injectedRoute = $route instanceof Route ? $route : null;
599-
$runGlobalHooks = !($route instanceof Route) || $route->getHook();
600593

601594
try {
602-
if ($runGlobalHooks) {
595+
if ($route->getHook()) {
603596
foreach (self::$init as $hook) { // Global init hooks
604597
if (\in_array('*', $hook->getGroups())) {
605-
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $injectedRoute);
598+
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $route);
606599
\call_user_func_array($hook->getAction(), $arguments);
607600
}
608601
}
@@ -611,30 +604,30 @@ public function execute(Request $request, Response $response): static
611604
foreach ($groups as $group) {
612605
foreach (self::$init as $hook) { // Group init hooks
613606
if (\in_array($group, $hook->getGroups())) {
614-
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $injectedRoute);
607+
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $route);
615608
\call_user_func_array($hook->getAction(), $arguments);
616609
}
617610
}
618611
}
619612

620613
if (!$response->isSent()) {
621-
$arguments = $this->getArguments($route, $match->params, $request->getParams(), $injectedRoute);
614+
$arguments = $this->getArguments($route, $match->params, $request->getParams(), $route);
622615
\call_user_func_array($route->getAction(), $arguments);
623616
}
624617

625618
foreach ($groups as $group) {
626619
foreach (self::$shutdown as $hook) { // Group shutdown hooks
627620
if (\in_array($group, $hook->getGroups())) {
628-
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $injectedRoute);
621+
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $route);
629622
\call_user_func_array($hook->getAction(), $arguments);
630623
}
631624
}
632625
}
633626

634-
if ($runGlobalHooks) {
635-
foreach (self::$shutdown as $hook) { // Global shutdown hooks
627+
if ($route->getHook()) {
628+
foreach (self::$shutdown as $hook) { // Group shutdown hooks
636629
if (\in_array('*', $hook->getGroups())) {
637-
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $injectedRoute);
630+
$arguments = $this->getArguments($hook, $match->params, $request->getParams(), $route);
638631
\call_user_func_array($hook->getAction(), $arguments);
639632
}
640633
}
@@ -646,7 +639,7 @@ public function execute(Request $request, Response $response): static
646639
foreach (self::$errors as $error) { // Group error hooks
647640
if (\in_array($group, $error->getGroups())) {
648641
try {
649-
$arguments = $this->getArguments($error, $match->params, $request->getParams(), $injectedRoute);
642+
$arguments = $this->getArguments($error, $match->params, $request->getParams(), $route);
650643
\call_user_func_array($error->getAction(), $arguments);
651644
} catch (\Throwable $e) {
652645
throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e);
@@ -658,7 +651,7 @@ public function execute(Request $request, Response $response): static
658651
foreach (self::$errors as $error) { // Global error hooks
659652
if (\in_array('*', $error->getGroups())) {
660653
try {
661-
$arguments = $this->getArguments($error, $match->params, $request->getParams(), $injectedRoute);
654+
$arguments = $this->getArguments($error, $match->params, $request->getParams(), $route);
662655
\call_user_func_array($error->getAction(), $arguments);
663656
} catch (\Throwable $e) {
664657
throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e);
@@ -770,10 +763,7 @@ public function run(Request $request, Response $response): static
770763
'http.request.method' => $request->getMethod(),
771764
// OTel semantics: http.route is the matched route template, or
772765
// unset when no template applies (wildcard / no match).
773-
'http.route' => (function () use ($request) {
774-
$matched = $this->match($request)?->route;
775-
return $matched instanceof Route ? $matched->getPath() : null;
776-
})(),
766+
'http.route' => ($this->match($request)?->route->getPath() ?: null),
777767
'http.response.status_code' => $response->getStatusCode(),
778768
];
779769
$this->requestDuration->record($requestDuration, $attributes);

src/Http/RouteMatch.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44

55
namespace Utopia\Http;
66

7-
use Utopia\Servers\Hook;
8-
97
/**
108
* The result of matching a request against the registered routes.
119
*/
1210
final readonly class RouteMatch
1311
{
1412
public function __construct(
15-
/**
16-
* The handler that will run for this request — usually a {@see Route},
17-
* or the wildcard fallback (a {@see Hook}) registered via
18-
* {@see Http::wildcard()} when no route matched.
19-
*
20-
* Use `instanceof Route` to access route-only fields like
21-
* `getMethod()` / `getPath()`.
22-
*/
23-
public Hook $route,
13+
public Route $route,
2414
/**
2515
* Path params parsed from the request URL.
2616
*

src/Http/Router.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Utopia\Http;
44

55
use Exception;
6-
use Utopia\Servers\Hook;
76

87
class Router
98
{
@@ -16,9 +15,9 @@ class Router
1615
protected static bool $allowOverride = false;
1716

1817
/**
19-
* Method-agnostic fallback handler used when no route matches.
18+
* Method-agnostic wildcard route used when no method-specific route matches.
2019
*/
21-
protected static ?Hook $wildcard = null;
20+
protected static ?Route $wildcard = null;
2221

2322
/**
2423
* @var array<string,Route[]>
@@ -112,11 +111,11 @@ public static function addRouteAlias(string $path, Route $route): void
112111
}
113112

114113
/**
115-
* Register a method-agnostic fallback handler used when no route matches.
114+
* Register a method-agnostic catch-all route, used when nothing else matches.
116115
*/
117-
public static function setWildcard(?Hook $hook): void
116+
public static function setWildcard(?Route $route): void
118117
{
119-
self::$wildcard = $hook;
118+
self::$wildcard = $route;
120119
}
121120

122121
/**

tests/HttpTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,10 @@ public function testWildcardRoute(): void
760760
->inject('myRoute')
761761
->inject('response')
762762
->action(function (mixed $myRoute, $response) {
763-
// Wildcard matches don't carry a Route — they're a method-
764-
// agnostic fallback. inject('route') is null here.
765-
if ($myRoute === null) {
766-
$response->send('NO ROUTE');
763+
if ($myRoute == null) {
764+
$response->send('ROUTE IS NULL!');
767765
} else {
768-
$response->send('UNEXPECTED ROUTE');
766+
$response->send('HELLO');
769767
}
770768
});
771769

@@ -774,7 +772,7 @@ public function testWildcardRoute(): void
774772
$result = ob_get_contents();
775773
ob_end_clean();
776774

777-
$this->assertSame('NO ROUTE', $result);
775+
$this->assertSame('HELLO', $result);
778776

779777
ob_start();
780778
$req = new Request();

0 commit comments

Comments
 (0)