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