| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace Routes;
- /*
- *
- */
- class Route {
- /*
- * HTTP VERB
- */
- public $_verb = Array();
- /*
- * Execution Source
- * HTTP | Cli | SOAP |
- */
- public $_source;
- /*
- * URI String
- */
- public $_uri;
- /*
- * Routes dispatch priority
- */
- public $_weight = 0;
- /*
- * URI Segments
- */
- public $_uri_segments = Array();
- /*
- * Extracted params
- */
- public $_segments = Array();
- /*
- * Prepared params
- */
- public $_params = Array();
- /*
- * Execute before dispatch route
- */
- public $_before = Array();
- /*
- * Route callback
- */
- public $_callback = Array();
- /*
- * Execute after route dispatched
- */
- public $_after = Array();
- /*
- * Prepared RegEx
- */
- public $_regex = '';
- /*
- * Can execute other route after this one?
- */
- public $_block = false;
- /*
- * Is there a problem witch one?
- */
- public $_http_error = false;
- /*
- * This rout should be counted?
- */
- public $_ignore = false;
- /*
- * Middlewares to remove from the list
- */
- public $_middlewares_to_ignore = Array();
- /*
- * From Klein
- * HTTP status List
- */
- protected static $http_messages = array(
- // Informational 1xx
- 100 => 'Continue',
- 101 => 'Switching Protocols',
- // Successful 2xx
- 200 => 'OK',
- 201 => 'Created',
- 202 => 'Accepted',
- 203 => 'Non-Authoritative Information',
- 204 => 'No Content',
- 205 => 'Reset Content',
- 206 => 'Partial Content',
- // Redirection 3xx
- 300 => 'Multiple Choices',
- 301 => 'Moved Permanently',
- 302 => 'Found',
- 303 => 'See Other',
- 304 => 'Not Modified',
- 305 => 'Use Proxy',
- 306 => '(Unused)',
- 307 => 'Temporary Redirect',
- // Client Error 4xx
- 400 => 'Bad Request',
- 401 => 'Unauthorized',
- 402 => 'Payment Required',
- 403 => 'Forbidden',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 406 => 'Not Acceptable',
- 407 => 'Proxy Authentication Required',
- 408 => 'Request Timeout',
- 409 => 'Conflict',
- 410 => 'Gone',
- 411 => 'Length Required',
- 412 => 'Precondition Failed',
- 413 => 'Request Entity Too Large',
- 414 => 'Request-URI Too Long',
- 415 => 'Unsupported Media Type',
- 416 => 'Requested Range Not Satisfiable',
- 417 => 'Expectation Failed',
- // Server Error 5xx
- 500 => 'Internal Server Error',
- 501 => 'Not Implemented',
- 502 => 'Bad Gateway',
- 503 => 'Service Unavailable',
- 504 => 'Gateway Timeout',
- 505 => 'HTTP Version Not Supported',
- );
- //=======================================================
- function execute() {
- foreach ($this->_before as $key => $before) {
- if (!in_array($key, $this->_middlewares_to_ignore) && !$this->_ignore) {
- call_user_func($before);
- }
- }
- foreach ($this->_callback as $callback) {
- if (is_string($callback)) {
- $segments = explode('@', $callback);
- $class = new $segments[0]();
- $r = new \ReflectionMethod($segments[0], $segments[1]);
- if (sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL) {
- $element;
- $element = $r->getParameters()[0]->getClass()->name;
- $element = new $element;
- $element->load($this->_params['id']);
- $this->_params['id'] = $element;
- }
- $class->{$segments[1]}(...array_values($this->_params));
- } else {
- $r = new \ReflectionFunction($callback);
- if(sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL){
- $element;
- $element = $r->getParameters()[0]->getClass()->name;
- $element = new $element;
- $element->load($this->_params['id']);
- $this->_params['id'] = $element;
- }
- call_user_func_array($callback, $this->_params);
- }
- }
- foreach ($this->_after as $after) {
- call_user_func($after);
- }
- }
- function prepare() {
- $this->_segments = explode('/', $this->_uri);
- foreach ($this->_segments as $segment) {
- if (strpos($segment, 'i:') > -1) {
- $this->_regex .= "\/[0-9]+";
- } else if (strpos($segment, 'h:') > -1) {
- $this->_regex .= "\/[A-z]+";
- } else if (strpos($segment, ':') > -1) {
- $this->_regex .= "\/[A-z0-9]+";
- } else {
- $this->_regex .= "\/" . $segment;
- }
- }
- $this->_regex = ltrim($this->_regex, '\/');
- $this->_regex = "/\/" . $this->_regex . "$/";
- }
- function create_indexes() {
- foreach ($this->_segments as $key => $segment) {
- if (preg_match('/\[.*?\]$/', $segment)) {
- $segment = trim(trim($segment, '['), ']');
- $param = explode(':', $segment);
- $this->_params[$param[1]] = $this->_uri_segments[$key];
- }
- }
- }
- function set_uri($uri) {
- $this->_uri = $uri;
- return $this;
- }
- function get_segments() {
- return $this->_segments;
- }
- function do_block() {
- $this->_block = true;
- return $this;
- }
- function not_block() {
- $this->_block = false;
- return $this;
- }
- function do_ignore() {
- $this->_ignore = true;
- return $this;
- }
- function not_ignore() {
- $this->_ignore = false;
- return $this;
- }
- function middleware_ignore($name = '') {
- $this->_middlewares_to_ignore[] = $name;
- return $this;
- }
- function middleware_add($name = '') {
- $this->_before[$name] = $function;
- }
- function middleware_append($name = '', $function) {
- $this->_before[$name] = $function;
- }
- function set_weight($weigth = 0) {
- $this->_weight = $weigth;
- return $this;
- }
- function set_http_error($code = 400, $message = null) {
- if ($message == null) {
- $message = self::$http_messages[$code];
- }
- $info = new \stdClass();
- $info->code = $code;
- $info->message = $message;
- $this->_http_error = $info;
- return $this;
- }
- function match($uri) {
- //Wildcards allways match
- if (in_array('*', $this->_verb) || $this->_uri == '*') {
- return true;
- }
- if (preg_match($this->_regex, $uri)) {
- $this->_uri_segments = explode('/', $uri);
- $this->create_indexes();
- return true;
- }
- return false;
- }
- }
|