Implementing middlewares 0.1

This commit is contained in:
ahwelp
2018-08-20 21:22:15 -03:00
parent 8e5a67740e
commit 0e02aa0ca3
2 changed files with 47 additions and 11 deletions
+18 -1
View File
@@ -76,6 +76,10 @@ class Route{
*/ */
public $_ignore = false; public $_ignore = false;
/*
* Middlewares to remove from the list
*/
public $_middlewares_to_ignore = Array();
/* /*
* From Klein * From Klein
* HTTP status List * HTTP status List
@@ -135,12 +139,16 @@ class Route{
//======================================================= //=======================================================
function execute(){ function execute(){
foreach ($this->_before as $before){ foreach ($this->_before as $key => $before){
if(!in_array($key, $this->_middlewares_to_ignore) ){
call_user_func($before); call_user_func($before);
} }
}
foreach ($this->_callback as $callback){ foreach ($this->_callback as $callback){
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);
} }
@@ -199,6 +207,15 @@ class Route{
$this->_ignore = false; $this->_ignore = false;
return $this; return $this;
} }
function middleware_ignore($name = ''){
$this->_middlewares_to_ignore[$name];
}
function middleware_add($name = '', $function){
$this->_before[$name] = $function;
}
function set_weight($weigth = 0){ function set_weight($weigth = 0){
$this->_weight = $weigth; $this->_weight = $weigth;
return $this; return $this;
+28 -9
View File
@@ -7,13 +7,21 @@ class RouteCollection{
private static $_route_collection; private static $_route_collection;
public $_uri = '/'; public $_uri = '/';
public $_routes = Array(); public $_routes = Array();
private $_verb = ''; private $_verb = '';
private $_loaded_files = Array(); private $_loaded_files = Array();
private $_errors = Array(); private $_errors = Array();
private $_default_middlewares = Array();
private static $_verbs_whitelist = Array('*', 'GET','POST','PUT','PATCH','DELETE', 'CLI');
private static $_verbs_web = Array('GET','POST','PUT','PATCH','DELETE');
//SINGLETON============================================== //SINGLETON==============================================
@@ -57,7 +65,6 @@ class RouteCollection{
} }
} }
} }
return $instance; return $instance;
} }
@@ -74,26 +81,25 @@ class RouteCollection{
} }
/* /*
* GET | POST | PUT | PATCH | DELETE * GET | POST | PUT | PATCH | DELETE | CLI
* *
*/ */
private function define_verb(){ private function define_verb(){
$verbs_whitelist = Array('*', 'GET','POST','PUT','PATCH','DELETE', 'CLI');
$verb = ''; $verb = '';
if(isset($_POST['_method'])){ if(isset($_POST['_method'])){
$verb = $_POST['_method']; $verb = strtoupper( $_POST['_method'] );
}else if(php_sapi_name() == "cli"){ }else if(php_sapi_name() == "cli"){
$verb = "CLI"; $verb = "CLI";
}else{ }else{
$verb = $_SERVER['REQUEST_METHOD']; $verb = strtoupper( $_SERVER['REQUEST_METHOD'] );
} }
if(in_array($verb, $verbs_whitelist)){ if(in_array($verb, $this->_verbs_whitelist)){
$this->_verb = $verb; $this->_verb = $verb;
}else{ }else{
echo 'Invalid HTTP Verb'; echo 'Invalid Verb';
} }
} }
@@ -133,6 +139,9 @@ class RouteCollection{
//Yup. Execute the route //Yup. Execute the route
$ROUTE = $route; $ROUTE = $route;
$route->_before($this->_default_middlewares);
$route->execute(); $route->execute();
if(!$route->_ignore){ if(!$route->_ignore){
@@ -242,10 +251,13 @@ class RouteCollection{
$route = new Route(); $route = new Route();
if(is_array( $verb ) ){ if(is_array( $verb ) ){
$route->_verb = $verb; $route->_verb = array_map('strtoupper', $verb);
}else if( strtoupper($verb) == 'WEB' ){
$route->_verb[] = self::_verbs_web;
}else{ }else{
$route->_verb[] = $verb; $route->_verb[] = strtoupper( $verb );
} }
$route->_uri = $uri; $route->_uri = $uri;
$route->_callback[] = $callback; $route->_callback[] = $callback;
$route->_weight = $weight; $route->_weight = $weight;
@@ -263,6 +275,13 @@ class RouteCollection{
$this->_routes[] = $route; $this->_routes[] = $route;
} }
function addMiddleware($name = '', $function){
$this->_default_middlewares[$name] = $function;
}
/*
*
*/
static function on_http_error($function){ static function on_http_error($function){
$instance = self::getInstance(); $instance = self::getInstance();
$instance->_errors[] = $function; $instance->_errors[] = $function;