Route.php 5.8 KB

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