@@ -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 );
0 commit comments