Route.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace Routes;
  3. /*
  4. *
  5. */
  6. class Route {
  7. /*
  8. * HTTP VERB
  9. */
  10. public $_verb = Array();
  11. /*
  12. * Execution Source
  13. * HTTP | Cli | SOAP |
  14. */
  15. public $_source;
  16. /*
  17. * URI String
  18. */
  19. public $_uri;
  20. /*
  21. * Routes dispatch priority
  22. */
  23. public $_weight = 0;
  24. /*
  25. * URI Segments
  26. */
  27. public $_uri_segments = Array();
  28. /*
  29. * Extracted params
  30. */
  31. public $_segments = Array();
  32. /*
  33. * Prepared params
  34. */
  35. public $_params = Array();
  36. /*
  37. * Execute before dispatch route
  38. */
  39. public $_before = Array();
  40. /*
  41. * Route callback
  42. */
  43. public $_callback = Array();
  44. /*
  45. * Execute after route dispatched
  46. */
  47. public $_after = Array();
  48. /*
  49. * Prepared RegEx
  50. */
  51. public $_regex = '';
  52. /*
  53. * Can execute other route after this one?
  54. */
  55. public $_block = false;
  56. /*
  57. * Is there a problem witch one?
  58. */
  59. public $_http_error = false;
  60. /*
  61. * This rout should be counted?
  62. */
  63. public $_ignore = false;
  64. /*
  65. * Middlewares to remove from the list
  66. */
  67. public $_middlewares_to_ignore = Array();
  68. /*
  69. * From Klein
  70. * HTTP status List
  71. */
  72. protected static $http_messages = array(
  73. // Informational 1xx
  74. 100 => 'Continue',
  75. 101 => 'Switching Protocols',
  76. // Successful 2xx
  77. 200 => 'OK',
  78. 201 => 'Created',
  79. 202 => 'Accepted',
  80. 203 => 'Non-Authoritative Information',
  81. 204 => 'No Content',
  82. 205 => 'Reset Content',
  83. 206 => 'Partial Content',
  84. // Redirection 3xx
  85. 300 => 'Multiple Choices',
  86. 301 => 'Moved Permanently',
  87. 302 => 'Found',
  88. 303 => 'See Other',
  89. 304 => 'Not Modified',
  90. 305 => 'Use Proxy',
  91. 306 => '(Unused)',
  92. 307 => 'Temporary Redirect',
  93. // Client Error 4xx
  94. 400 => 'Bad Request',
  95. 401 => 'Unauthorized',
  96. 402 => 'Payment Required',
  97. 403 => 'Forbidden',
  98. 404 => 'Not Found',
  99. 405 => 'Method Not Allowed',
  100. 406 => 'Not Acceptable',
  101. 407 => 'Proxy Authentication Required',
  102. 408 => 'Request Timeout',
  103. 409 => 'Conflict',
  104. 410 => 'Gone',
  105. 411 => 'Length Required',
  106. 412 => 'Precondition Failed',
  107. 413 => 'Request Entity Too Large',
  108. 414 => 'Request-URI Too Long',
  109. 415 => 'Unsupported Media Type',
  110. 416 => 'Requested Range Not Satisfiable',
  111. 417 => 'Expectation Failed',
  112. // Server Error 5xx
  113. 500 => 'Internal Server Error',
  114. 501 => 'Not Implemented',
  115. 502 => 'Bad Gateway',
  116. 503 => 'Service Unavailable',
  117. 504 => 'Gateway Timeout',
  118. 505 => 'HTTP Version Not Supported',
  119. );
  120. //=======================================================
  121. function execute() {
  122. foreach ($this->_before as $key => $before) {
  123. if (!in_array($key, $this->_middlewares_to_ignore) && !$this->_ignore) {
  124. call_user_func($before);
  125. }
  126. }
  127. foreach ($this->_callback as $callback) {
  128. if (is_string($callback)) {
  129. $segments = explode('@', $callback);
  130. $class = new $segments[0]();
  131. $r = new \ReflectionMethod($segments[0], $segments[1]);
  132. if (sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL) {
  133. $element;
  134. $element = $r->getParameters()[0]->getClass()->name;
  135. $element = new $element;
  136. $element->load($this->_params['id']);
  137. $this->_params['id'] = $element;
  138. }
  139. $class->{$segments[1]}(...array_values($this->_params));
  140. } else {
  141. $r = new \ReflectionFunction($callback);
  142. if(sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL){
  143. $element;
  144. $element = $r->getParameters()[0]->getClass()->name;
  145. $element = new $element;
  146. $element->load($this->_params['id']);
  147. $this->_params['id'] = $element;
  148. }
  149. call_user_func_array($callback, $this->_params);
  150. }
  151. }
  152. foreach ($this->_after as $after) {
  153. call_user_func($after);
  154. }
  155. }
  156. function prepare() {
  157. $this->_segments = explode('/', $this->_uri);
  158. foreach ($this->_segments as $segment) {
  159. if (strpos($segment, 'i:') > -1) {
  160. $this->_regex .= "\/[0-9]+";
  161. } else if (strpos($segment, 'h:') > -1) {
  162. $this->_regex .= "\/[A-z]+";
  163. } else if (strpos($segment, ':') > -1) {
  164. $this->_regex .= "\/[A-z0-9]+";
  165. } else {
  166. $this->_regex .= "\/" . $segment;
  167. }
  168. }
  169. $this->_regex = ltrim($this->_regex, '\/');
  170. $this->_regex = "/\/" . $this->_regex . "$/";
  171. }
  172. function create_indexes() {
  173. foreach ($this->_segments as $key => $segment) {
  174. if (preg_match('/\[.*?\]$/', $segment)) {
  175. $segment = trim(trim($segment, '['), ']');
  176. $param = explode(':', $segment);
  177. $this->_params[$param[1]] = $this->_uri_segments[$key];
  178. }
  179. }
  180. }
  181. function set_uri($uri) {
  182. $this->_uri = $uri;
  183. return $this;
  184. }
  185. function get_segments() {
  186. return $this->_segments;
  187. }
  188. function do_block() {
  189. $this->_block = true;
  190. return $this;
  191. }
  192. function not_block() {
  193. $this->_block = false;
  194. return $this;
  195. }
  196. function do_ignore() {
  197. $this->_ignore = true;
  198. return $this;
  199. }
  200. function not_ignore() {
  201. $this->_ignore = false;
  202. return $this;
  203. }
  204. function middleware_ignore($name = '') {
  205. $this->_middlewares_to_ignore[] = $name;
  206. return $this;
  207. }
  208. function middleware_add($name = '') {
  209. $this->_before[$name] = $function;
  210. }
  211. function middleware_append($name = '', $function) {
  212. $this->_before[$name] = $function;
  213. }
  214. function set_weight($weigth = 0) {
  215. $this->_weight = $weigth;
  216. return $this;
  217. }
  218. function set_http_error($code = 400, $message = null) {
  219. if ($message == null) {
  220. $message = self::$http_messages[$code];
  221. }
  222. $info = new \stdClass();
  223. $info->code = $code;
  224. $info->message = $message;
  225. $this->_http_error = $info;
  226. return $this;
  227. }
  228. function match($uri) {
  229. //Wildcards allways match
  230. if (in_array('*', $this->_verb) || $this->_uri == '*') {
  231. return true;
  232. }
  233. if (preg_match($this->_regex, $uri)) {
  234. $this->_uri_segments = explode('/', $uri);
  235. $this->create_indexes();
  236. return true;
  237. }
  238. return false;
  239. }
  240. }