Router.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace KarmaFW\Routing;
  3. class Router
  4. {
  5. private static $routes = [];
  6. // Add a route to the router
  7. public static function add($methods, $url_match, $callback=null, $type_match='exact', $regex_params=[])
  8. {
  9. $route = new Route();
  10. $route->setMatchUrl($url_match);
  11. $route->setCallback($callback);
  12. $route->setMatchType($type_match );
  13. $route->setRegexParams($regex_params);
  14. if (! is_array($methods)) {
  15. $methods = [$methods];
  16. }
  17. foreach ($methods as $method) {
  18. $route->setMethod($method);
  19. }
  20. self::$routes[] = $route;
  21. return $route;
  22. }
  23. // Allow whatever method (GET, POST, HEAD, OPTION, DELETE, PUT, ...)
  24. public static function all($url_match, $callback=null, $type_match='exact', $regex_params=[])
  25. {
  26. return self::Add(null, $url_match, $callback, $type_match, $regex_params);
  27. }
  28. // GET method
  29. public static function get($url_match, $callback=null, $type_match='exact', $regex_params=[])
  30. {
  31. return self::Add('GET', $url_match, $callback, $type_match, $regex_params);
  32. }
  33. // POST method
  34. public static function post($url_match, $callback=null, $type_match='exact', $regex_params=[])
  35. {
  36. return self::Add('POST', $url_match, $callback, $type_match, $regex_params);
  37. }
  38. // Lookup the first matching route then execute it
  39. public static function routeByUrl($request_method, $request_uri, $debug = false)
  40. {
  41. foreach (self::$routes as $route) {
  42. if ($debug) {
  43. pre($route);
  44. }
  45. $match_params = $route->match($request_method, $request_uri);
  46. if (! is_null($match_params)) {
  47. if ($debug) {
  48. echo " => MATCH !<br />" . PHP_EOL;
  49. }
  50. $callback = $route->getCallback();
  51. if (empty($callback)) {
  52. // Do nothing
  53. return false;
  54. } else if (is_callable($callback)) {
  55. self::routeRun($route, $callback, $request_method, $request_uri, $match_params);
  56. } else {
  57. // Error: callback not callable
  58. return null;
  59. }
  60. return $route;
  61. }
  62. }
  63. // No matching route
  64. return false;
  65. }
  66. public static function routeRun($route, $callback, $request_method, $request_uri, $match_params)
  67. {
  68. if (gettype($callback) == 'array') {
  69. $class = new $callback[0]($route, $request_method, $request_uri);
  70. call_user_func([$class, $callback[1]], $match_params);
  71. } else {
  72. $callback($route, $request_method, $request_uri);
  73. }
  74. return true;
  75. }
  76. // Search a route by its name
  77. public static function findRouteByName($expected_route_name, $debug = false)
  78. {
  79. if (empty($expected_route_name)) {
  80. return null;
  81. }
  82. foreach (self::$routes as $route) {
  83. $route_name = $route->getName();
  84. if (! empty($route_name) && $route_name == $expected_route_name) {
  85. return $route;
  86. }
  87. }
  88. return null;
  89. }
  90. }