Laravel injection v1 #5

This commit is contained in:
Artur Welp
2019-03-18 19:51:16 -03:00
parent aec4de71f7
commit 490f52f1cc
+66 -47
View File
@@ -1,14 +1,16 @@
<?php <?php
namespace BBRouter; namespace BBRouter;
/** /*
* ROTA *
*/ */
class Route{
class Route {
/* /*
* HTTP VERB * HTTP VERB
*/ */
public $_verb = Array(); public $_verb = Array();
/* /*
* Execution Source * Execution Source
@@ -88,7 +90,6 @@ class Route{
// Informational 1xx // Informational 1xx
100 => 'Continue', 100 => 'Continue',
101 => 'Switching Protocols', 101 => 'Switching Protocols',
// Successful 2xx // Successful 2xx
200 => 'OK', 200 => 'OK',
201 => 'Created', 201 => 'Created',
@@ -97,7 +98,6 @@ class Route{
204 => 'No Content', 204 => 'No Content',
205 => 'Reset Content', 205 => 'Reset Content',
206 => 'Partial Content', 206 => 'Partial Content',
// Redirection 3xx // Redirection 3xx
300 => 'Multiple Choices', 300 => 'Multiple Choices',
301 => 'Moved Permanently', 301 => 'Moved Permanently',
@@ -107,7 +107,6 @@ class Route{
305 => 'Use Proxy', 305 => 'Use Proxy',
306 => '(Unused)', 306 => '(Unused)',
307 => 'Temporary Redirect', 307 => 'Temporary Redirect',
// Client Error 4xx // Client Error 4xx
400 => 'Bad Request', 400 => 'Bad Request',
401 => 'Unauthorized', 401 => 'Unauthorized',
@@ -127,7 +126,6 @@ class Route{
415 => 'Unsupported Media Type', 415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable', 416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed', 417 => 'Expectation Failed',
// Server Error 5xx // Server Error 5xx
500 => 'Internal Server Error', 500 => 'Internal Server Error',
501 => 'Not Implemented', 501 => 'Not Implemented',
@@ -138,104 +136,123 @@ class Route{
); );
//======================================================= //=======================================================
function execute(){ function execute() {
foreach ($this->_before as $key => $before){ foreach ($this->_before as $key => $before) {
if(!in_array($key, $this->_middlewares_to_ignore) && !$this->_ignore ){ if (!in_array($key, $this->_middlewares_to_ignore) && !$this->_ignore) {
call_user_func($before); call_user_func($before);
} }
} }
foreach ($this->_callback as $callback){ foreach ($this->_callback as $callback) {
if(is_string($callback)){ if (is_string($callback)) {
$segments = explode('@', $callback); $segments = explode('@', $callback);
$class = new $segments[0](); $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)); $class->{$segments[1]}(...array_values($this->_params));
}else{ } 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); call_user_func_array($callback, $this->_params);
} }
} }
foreach ($this->_after as $after){ foreach ($this->_after as $after) {
call_user_func($after); call_user_func($after);
} }
} }
function prepare(){ function prepare() {
$this->_segments = explode('/', $this->_uri); $this->_segments = explode('/', $this->_uri);
foreach ($this->_segments as $segment){ foreach ($this->_segments as $segment) {
if( strpos($segment, 'i:') > -1 ){ if (strpos($segment, 'i:') > -1) {
$this->_regex .= "\/[0-9]+"; $this->_regex .= "\/[0-9]+";
}else if( strpos($segment, 'h:') > -1 ){ } else if (strpos($segment, 'h:') > -1) {
$this->_regex .= "\/[A-z]+"; $this->_regex .= "\/[A-z]+";
}else if( strpos($segment, ':') > -1 ){ } else if (strpos($segment, ':') > -1) {
$this->_regex .= "\/[A-z0-9]+"; $this->_regex .= "\/[A-z0-9]+";
}else{ } else {
$this->_regex .= "\/".$segment; $this->_regex .= "\/" . $segment;
} }
} }
$this->_regex = ltrim($this->_regex, '\/'); $this->_regex = ltrim($this->_regex, '\/');
$this->_regex = "/\/" . $this->_regex . "$/"; $this->_regex = "/\/" . $this->_regex . "$/";
} }
function create_indexes(){ function create_indexes() {
foreach ($this->_segments as $key => $segment){ foreach ($this->_segments as $key => $segment) {
if(preg_match('/\[.*?\]$/', $segment)){ if (preg_match('/\[.*?\]$/', $segment)) {
$segment = trim( trim( $segment, '[' ), ']'); $segment = trim(trim($segment, '['), ']');
$param = explode(':', $segment); $param = explode(':', $segment);
$this->_params[ $param[1] ] = $this->_uri_segments[$key]; $this->_params[$param[1]] = $this->_uri_segments[$key];
} }
} }
} }
function set_uri($uri){ function set_uri($uri) {
$this->_uri = $uri; $this->_uri = $uri;
return $this; return $this;
} }
function get_segments(){ function get_segments() {
return $this->_segments; return $this->_segments;
} }
function do_block(){ function do_block() {
$this->_block = true; $this->_block = true;
return $this; return $this;
} }
function not_block(){
function not_block() {
$this->_block = false; $this->_block = false;
return $this; return $this;
} }
function do_ignore(){ function do_ignore() {
$this->_ignore = true; $this->_ignore = true;
return $this; return $this;
} }
function not_ignore(){
function not_ignore() {
$this->_ignore = false; $this->_ignore = false;
return $this; return $this;
} }
function middleware_ignore($name = ''){ function middleware_ignore($name = '') {
$this->_middlewares_to_ignore[] = $name; $this->_middlewares_to_ignore[] = $name;
return $this; return $this;
} }
function middleware_add($name = ''){ function middleware_add($name = '') {
$this->_before[$name] = $function; $this->_before[$name] = $function;
} }
function middleware_append($name = '', $function){
$this->_before[$name] = $function;
}
function set_weight($weigth = 0){ function middleware_append($name = '', $function) {
$this->_before[$name] = $function;
}
function set_weight($weigth = 0) {
$this->_weight = $weigth; $this->_weight = $weigth;
return $this; return $this;
} }
function set_http_error($code = 400, $message = null){ function set_http_error($code = 400, $message = null) {
if($message == null){ if ($message == null) {
$message = self::$http_messages[$code]; $message = self::$http_messages[$code];
} }
@@ -247,13 +264,13 @@ class Route{
return $this; return $this;
} }
function match($uri){ function match($uri) {
//Wildcards allways match //Wildcards allways match
if(in_array('*', $this->_verb) || $this->_uri == '*'){ if (in_array('*', $this->_verb) || $this->_uri == '*') {
return true; return true;
} }
if( preg_match($this->_regex, $uri) ){ if (preg_match($this->_regex, $uri)) {
$this->_uri_segments = explode('/', $uri); $this->_uri_segments = explode('/', $uri);
$this->create_indexes(); $this->create_indexes();
return true; return true;
@@ -261,4 +278,6 @@ class Route{
return false; return false;
} }
} }