88use Mrfoo \PHPRouter \Exceptions \MethodNotSupportedException ;
99use Exception ;
1010use Mrfoo \PHPRouter \Core \HashTable ;
11+ use Mrfoo \PHPRouter \Exceptions \NotFoundException ;
1112
1213class Router
1314{
1415 public static HashTable $ routeList ;
1516 public static ?HashTable $ tmpGroup ;
1617 public static bool $ isTrackingGroup = false ;
18+ public static Router $ instance ;
1719
1820 public static function get ($ uri , $ handler )
1921 {
@@ -55,6 +57,7 @@ public static function init()
5557 if (!isset (self ::$ routeList )) {
5658 self ::$ routeList = new HashTable ();
5759 self ::$ tmpGroup = new HashTable ();
60+ self ::$ instance = new Router ();
5861 }
5962 }
6063
@@ -69,18 +72,10 @@ public static function run()
6972 $ route ->handle ();
7073 $ route ->postHandleMiddlewares ();
7174 } else {
72- // shoud be moved to another method ie: performTests($route);
73- if ($ route && $ route ->getMethod () != $ user_method ) {
74- try {
75- throw new MethodNotSupportedException ($ user_method , [$ route ->getMethod ()]);
76- } catch (Exception $ e ) {
77- print ($ e ->getMessage ());
78- return false ;
79- }
80- } else {
81- // TODO: NotFoundException
82- header ("HTTP/1.0 404 Not Found " );
83- echo "404 Not Found " ;
75+ try {
76+ self ::$ instance ->performTests ($ route , $ user_uri , $ user_method );
77+ } catch (Exception $ e ) {
78+ print ($ e ->getMessage ());
8479 return false ;
8580 }
8681 }
@@ -111,6 +106,17 @@ public static function buildRoute($uri, $handler, $method)
111106 return $ route ;
112107 }
113108
109+ private function performTests ($ route , $ uri , $ user_method )
110+ {
111+ // shoud be moved to another method ie: performTests($route);
112+ if ($ route == null ) {
113+ return throw new NotFoundException ($ uri );
114+ }
115+ if ($ route ->getMethod () != $ user_method ) {
116+ return throw new MethodNotSupportedException ($ user_method , [$ route ->getMethod ()]);
117+ }
118+ }
119+
114120 public static function group (array $ options , $ callback ): void
115121 {
116122 self ::$ isTrackingGroup = true ;
@@ -172,12 +178,12 @@ public static function generateURL($routeName, ...$params): string
172178 $ parameters = $ route ->getURI ()->getParameters ();
173179
174180 if (count ($ params ) == count ($ parameters )) {
175-
181+
176182 // construct the URL string
177183 foreach ($ parameters as $ key => $ p ) {
178184 $ parameters [$ key ] = $ params [array_search ($ key , array_keys ($ parameters ))];
179185 }
180-
186+
181187 $ base = getBaseUrl ();
182188 $ uri = [];
183189 $ nextParam = 0 ;
@@ -187,16 +193,14 @@ public static function generateURL($routeName, ...$params): string
187193 $ uri [$ i ] = $ parameters [array_keys ($ parameters )[$ nextParam ]];
188194 $ nextParam ++;
189195 } else {
190- $ uri [$ i ] = $ seg ;
196+ $ uri [$ i ] = $ seg ;
191197 }
192198 }
193199
194200 return $ base . implode ("/ " , $ uri );
195-
196201 } else {
197202 return die ("Parameters provided for route ' $ routeName' do not match parameters count " );
198203 }
199-
200204 } else {
201205 return die ("No route with name ' $ routeName' " );
202206 }
0 commit comments