@@ -40,12 +40,14 @@ public function getRegisteredRoutes(): array
4040 public function route (Request $ request ): array
4141 {
4242
43- // Check if route info is found
44- if (! ( $ requestRouteInfo = $ this -> routes [ $ request -> route ] ?? null ) ) {
43+ $ matchedRoute = $ this -> matchRequestRoute ( $ request -> route );
44+ if ($ matchedRoute === null ) {
4545 return ['Error ' , 'notFound ' ];
4646 }
4747
48- $ requestRouteInfo = $ this ->parseRouteInfo ($ requestRouteInfo , $ request ->method );
48+ $ requestRouteInfo = $ this ->parseRouteInfo (
49+ $ this ->routes [$ matchedRoute ], $ request ->method
50+ );
4951
5052 // Get the controller
5153 $ controller = $ requestRouteInfo ['controller ' ];
@@ -54,7 +56,73 @@ public function route(Request $request): array
5456 return [$ controller , $ action ];
5557 }
5658
57- private function parseRouteInfo (array $ routeInfo , string $ requestMethod ): ?array
59+ private function matchRequestRoute (string $ requestRoute ): ?string
60+ {
61+
62+ foreach ($ this ->routes as $ route => $ routeInfo ) {
63+ if ($ route === $ requestRoute ) {
64+ return $ route ;
65+ }
66+
67+ if ($ this ->routeIsRoutePattern ($ route )) {
68+
69+ $ routeParams = $ this ->getRoutePatternParameters ($ route );
70+ $ routePatternRegex = $ this ->buildRoutePatternRegex ($ route , $ routeParams );
71+
72+ if (preg_match ($ routePatternRegex , $ requestRoute )) {
73+ return $ route ;
74+ }
75+ }
76+ }
77+
78+ return null ;
79+ }
80+
81+ private function buildRoutePatternRegex (
82+ string $ routePattern ,
83+ array $ routeParams
84+ )
85+ {
86+ $ regexStr = implode ("/ " , array_map (function (string $ chunk ) use ($ routeParams ) {
87+ if (
88+ $ chunk [0 ] === ': ' &&
89+ ($ param = substr ($ chunk , 1 )) &&
90+ ($ paramInfo = $ routeParams [$ param ] ?? null )
91+ ) {
92+ $ paramType = $ paramInfo ['type ' ] ?? "string " ;
93+ return match ($ paramType ) {
94+ "string " => "\w+ " ,
95+ "number " => "\d+ " ,
96+ default => "\w+ "
97+ };
98+
99+ }
100+
101+ return $ chunk ;
102+ }, explode ("/ " , $ routePattern )));
103+
104+ return "@ $ regexStr@ " ;
105+ }
106+
107+ private function getRoutePatternParameters (
108+ string $ route
109+ )
110+ {
111+ return $ this ->routes [$ route ]['parameters ' ] ?? [];
112+ }
113+
114+ private function routeIsRoutePattern (
115+ string $ route
116+ )
117+ {
118+
119+ return ! is_null ($ this ->routes [$ route ]['parameters ' ] ?? null );
120+ }
121+
122+
123+ private function parseRouteInfo (
124+ array $ routeInfo ,
125+ string $ requestMethod ): ?array
58126 {
59127
60128 $ getRouteParams = function (string $ param ) use ($ routeInfo , $ requestMethod ) {
@@ -81,4 +149,4 @@ private function parseRouteInfo(array $routeInfo, string $requestMethod): ?array
81149
82150
83151
84- }
152+ }
0 commit comments