Route.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace KarmaFW\Routing;
  3. class Route
  4. {
  5. private $name = null;
  6. private $methods = [];
  7. private $called_method = null;
  8. private $called_url = null;
  9. private $match_url = '';
  10. private $match_type = 'exact';
  11. private $regex_params = [];
  12. private $before_callback = null;
  13. private $callback = null;
  14. private $prefix = '';
  15. private $prefix_match_type = null;
  16. private $get_prefix = null;
  17. private $matched_prefix = '';
  18. private $matched_params = [];
  19. private $middlewares = [];
  20. private $continue = false;
  21. public function __construct()
  22. {
  23. }
  24. // Set route method (can be called multiple times for differents methods)
  25. public function setMethod($method = null)
  26. {
  27. if (! is_null($method)) {
  28. $this->methods[] = $method;
  29. }
  30. }
  31. public function getMethods()
  32. {
  33. return implode(',', $this->methods);
  34. }
  35. // Set route match url
  36. public function setMatchUrl($match_url)
  37. {
  38. $this->match_url = $match_url;
  39. }
  40. // Set route called method
  41. public function setCalledMethod($called_method)
  42. {
  43. $this->called_method = $called_method;
  44. }
  45. // Set route called url
  46. public function setCalledUrl($called_url)
  47. {
  48. $this->called_url = $called_url;
  49. }
  50. // Get route match url
  51. public function getMatchUrl()
  52. {
  53. //return $this->prefix . $this->match_url;
  54. return $this->match_url;
  55. }
  56. // Set route match type (exact, startsWith, endsWith, regex, regexStartsWith, regexEndsWith)
  57. public function setMatchType($match_type)
  58. {
  59. $this->match_type = $match_type;
  60. }
  61. // Set route regex params (WORKS WITH: regex, regexStartsWith, regexEndsWith)
  62. public function setRegexParams(array $regex_params)
  63. {
  64. $this->regex_params = $regex_params;
  65. }
  66. public function getRegexParams()
  67. {
  68. return $this->regex_params;
  69. }
  70. public function setMatchedParams(array $matched_params)
  71. {
  72. $this->matched_params = $matched_params;
  73. }
  74. public function getMatchedParams()
  75. {
  76. return $this->matched_params;
  77. }
  78. public function setContinue(bool $continue)
  79. {
  80. $this->continue = $continue;
  81. }
  82. public function getContinue()
  83. {
  84. return $this->continue;
  85. }
  86. public function getMatchedPrefix()
  87. {
  88. return $this->matched_prefix;
  89. }
  90. // Get route name
  91. public function getName()
  92. {
  93. return $this->name;
  94. }
  95. // Set route name
  96. public function setName($name)
  97. {
  98. $this->name = $name;
  99. }
  100. // Get middlewares
  101. public function getMiddlewares()
  102. {
  103. return $this->middlewares;
  104. }
  105. // Set middlewares
  106. public function setMiddlewares($middlewares)
  107. {
  108. $this->middlewares = $middlewares;
  109. }
  110. // Add middleware
  111. public function addMiddleware($middleware)
  112. {
  113. $this->middlewares[] = $middleware;
  114. }
  115. // Set route callback
  116. public function setCallback($callback)
  117. {
  118. $this->callback = $callback;
  119. }
  120. // Get route callback
  121. public function getCallback()
  122. {
  123. return $this->callback;
  124. }
  125. // Get route before_callback
  126. public function setBeforeCallback($before_callback)
  127. {
  128. $this->before_callback = $before_callback;
  129. }
  130. // Get route before_callback
  131. public function getBeforeCallback()
  132. {
  133. return $this->before_callback;
  134. }
  135. // Set route prefix
  136. public function setPrefix($prefix=null, $match_type=null, $get_prefix=null)
  137. {
  138. $this->prefix = $prefix;
  139. $this->prefix_match_type = $match_type;
  140. $this->get_prefix = $get_prefix;
  141. }
  142. // Get route prefix
  143. public function getPrefix()
  144. {
  145. return $this->prefix;
  146. }
  147. // Get route get_prefix
  148. public function getCallbackGetPrefix()
  149. {
  150. if (empty($this->get_prefix)) {
  151. return null;
  152. }
  153. $func = $this->get_prefix;
  154. if (! is_callable($func)) {
  155. //error500("get_prefix is not callable");
  156. return null;
  157. }
  158. //pre($func); exit;
  159. return $func();
  160. }
  161. // Check if route is matching the request_method and request_uri
  162. public function match($request_method, $request_uri)
  163. {
  164. if (empty($this->methods) || in_array($request_method, $this->methods)) {
  165. $request_uri_short = explode('?', $request_uri)[0];
  166. $matched_params = [];
  167. $prefix = $this->getPrefix();
  168. if ($prefix) {
  169. if ($this->prefix_match_type == 'regex') {
  170. $match_pattern = '#^(' . $prefix . ')#';
  171. if (preg_match($match_pattern, $request_uri_short, $regs)) {
  172. $this->matched_prefix = $regs[1];
  173. $matched_params['prefix'] = $this->matched_prefix;
  174. //pre($matched_prefix, 1, 'regex matched_prefix: ');
  175. }
  176. } else if ($this->prefix_match_type == 'array') {
  177. foreach ($prefix as $prefix_value) {
  178. if (strpos($request_uri_short, $prefix_value) === 0) {
  179. $this->matched_prefix = $prefix_value;
  180. $matched_params['prefix'] = $this->matched_prefix;
  181. //pre($matched_prefix, 1, 'array matched_prefix: ');
  182. }
  183. }
  184. } else {
  185. if (strpos($request_uri_short, $prefix) === 0) {
  186. $this->matched_prefix = $prefix;
  187. $matched_params['prefix'] = $this->matched_prefix;
  188. //pre($matched_prefix, 1, 'exact matched_prefix: ');
  189. }
  190. }
  191. }
  192. if (! empty($this->matched_prefix)) {
  193. $request_uri_short = substr($request_uri_short, strlen($this->matched_prefix));
  194. }
  195. $match_url = $this->getMatchUrl();
  196. // exact match
  197. if ($this->match_type == 'exact') {
  198. if ($request_uri_short === $match_url) {
  199. $this->setMatchedParams($matched_params);
  200. return true;
  201. }
  202. }
  203. // startsWith
  204. if ($this->match_type == 'startsWith') {
  205. if (substr($request_uri_short, 0, strlen($match_url)) === $match_url) {
  206. $this->setMatchedParams($matched_params);
  207. return true;
  208. }
  209. }
  210. // endsWith
  211. if ($this->match_type == 'endsWith') {
  212. if (substr($request_uri_short, -1 * strlen($match_url)) === $match_url) {
  213. $this->setMatchedParams($matched_params);
  214. return true;
  215. }
  216. }
  217. // regex / regexStartsWith / regexEndsWith
  218. if (in_array($this->match_type, ['regex', 'regexStartsWith', 'regexEndsWith'])) {
  219. $match_pattern = '#^' . $match_url . '$#';
  220. if ($this->match_type == 'regexStartsWith') {
  221. $match_pattern = '#^' . $match_url . '#';
  222. }
  223. if ($this->match_type == 'regexEndsWith') {
  224. $match_pattern = '#' . $match_url . '$#';
  225. }
  226. if (preg_match($match_pattern, $request_uri_short, $regs)) {
  227. $matched_uri = array_shift($regs); // $matched_uri == $request_uri_short
  228. $args = $regs;
  229. if (! empty($this->regex_params)) {
  230. $args = array_combine($this->regex_params, $args);
  231. }
  232. //pre($args, 1);
  233. $matched_params = array_merge($matched_params, $args);
  234. //pre($matched_params, 1);
  235. $this->setMatchedParams($matched_params);
  236. return true;
  237. }
  238. }
  239. }
  240. return null;
  241. }
  242. }