Atualizar 'src/BBRouter/RouteCollection.php'

This commit is contained in:
ahwelp
2019-03-07 04:42:50 +00:00
parent 5b974f1224
commit c6cdba991e
+90 -71
View File
@@ -2,65 +2,61 @@
namespace BBRouter; namespace BBRouter;
class RouteCollection{ 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 $_default_middlewares = Array();
private $_middleware_set = Array();
private static $_verbs_whitelist = Array('*', 'GET','POST','PUT','PATCH','DELETE', 'CLI'); private static $_verbs_whitelist = Array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
private static $_verbs_web = Array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
private static $_verbs_web = Array('GET','POST','PUT','PATCH','DELETE');
//SINGLETON============================================== //SINGLETON==============================================
private function __construct(){ } private function __construct() {
}
private static function newObj(){ private static function newObj() {
if (!isset(self::$_route_collection)) { if (!isset(self::$_route_collection)) {
self::$_route_collection = new RouteCollection(); self::$_route_collection = new RouteCollection();
if(php_sapi_name() == "cli"){ if (php_sapi_name() == "cli") {
self::$_route_collection->_uri = isset( $_SERVER['argv'][1] ) ? '/'. $_SERVER['argv'][1] : '/'; self::$_route_collection->_uri = isset($_SERVER['argv'][1]) ? '/' . $_SERVER['argv'][1] : '/';
}else{ } else {
self::$_route_collection->_uri = isset( $_SERVER['REQUEST_URI'] ) ? explode('&', $_SERVER['REQUEST_URI'])[0] : '/'; self::$_route_collection->_uri = isset($_SERVER['REQUEST_URI']) ? explode('&', $_SERVER['REQUEST_URI'])[0] : '/';
} }
self::$_route_collection->define_verb(); self::$_route_collection->define_verb();
} }
return self::$_route_collection; return self::$_route_collection;
} }
public static function getInstance(){ /**
*
*/
public static function getInstance() {
if (!isset(self::$_route_collection)) { if (!isset(self::$_route_collection)) {
return self::newObj(); return self::newObj();
} }
return self::$_route_collection; return self::$_route_collection;
} }
//HELPERS================================================ //HELPERS================================================
/* /*
* Find route files with names under pathname * Find route files with names under pathname
*/ */
public function crawl($basepath = __DIR__, $filenames = Array('Routes.php', 'routes.php') ){ public function crawl($basepath = __DIR__, $filenames = Array('Routes.php', 'routes.php')) {
$instance = self::getInstance(); $instance = self::getInstance();
$rdi = new \RecursiveDirectoryIterator($basepath); $rdi = new \RecursiveDirectoryIterator($basepath);
foreach(new \RecursiveIteratorIterator($rdi) as $file){ foreach (new \RecursiveIteratorIterator($rdi) as $file) {
foreach ($filenames as $filename) { foreach ($filenames as $filename) {
if( strpos($file, $filename) ){ if (strpos($file, $filename)) {
$instance->_loaded_files[] = $file; $instance->_loaded_files[] = $file;
} }
} }
@@ -68,46 +64,48 @@ class RouteCollection{
return $instance; return $instance;
} }
/* /**
*
* Load the files with the routes * Load the files with the routes
*/ */
function load_routes(){ function load_routes() {
$instance = self::getInstance(); $instance = self::getInstance();
foreach ($instance->_loaded_files as $loaded_file){ foreach ($instance->_loaded_files as $loaded_file) {
include $loaded_file; include $loaded_file;
} }
return $instance; return $instance;
} }
/* /**
* GET | POST | PUT | PATCH | DELETE | CLI
* *
* GET | POST | PUT | PATCH | DELETE | CLI
*
*/ */
private function define_verb(){ private function define_verb() {
$verb = ''; $verb = '';
if(isset($_POST['_method'])){ if (isset($_POST['_method'])) {
$verb = strtoupper( $_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 = strtoupper( $_SERVER['REQUEST_METHOD'] ); $verb = strtoupper($_SERVER['REQUEST_METHOD']);
} }
if(in_array($verb, self::$_verbs_whitelist)){ if (in_array($verb, self::$_verbs_whitelist)) {
$this->_verb = $verb; $this->_verb = $verb;
}else{ } else {
echo 'Invalid Verb'; echo 'Invalid Verb';
} }
} }
/* /**
* *
*/ */
private function sort_routes(){ private function sort_routes() {
usort($this->_routes, function($a, $b){ usort($this->_routes, function($a, $b) {
if ($a->_weight == $b->_weight) { if ($a->_weight == $b->_weight) {
return 0; return 0;
} }
@@ -115,19 +113,19 @@ class RouteCollection{
}); });
} }
/* /**
* Dispatch routes *
*/ */
function submit(){ function submit() {
global $ROUTE; global $ROUTE;
self::$_route_collection->sort_routes(); self::$_route_collection->sort_routes();
$one_hit = false; $one_hit = false;
foreach (self::$_route_collection->_routes as $route){ foreach (self::$_route_collection->_routes as $route) {
if(!in_array( self::$_route_collection->_verb, $route->_verb) && !in_array( '*', $route->_verb) ){ if (!in_array(self::$_route_collection->_verb, $route->_verb) && !in_array('*', $route->_verb)) {
//No it is not //No it is not
continue; continue;
} }
@@ -135,7 +133,7 @@ class RouteCollection{
//Request match a route //Request match a route
$match = $route->match(self::$_route_collection->_uri); $match = $route->match(self::$_route_collection->_uri);
if($match){ if ($match) {
//Yup. Execute the route //Yup. Execute the route
$ROUTE = $route; $ROUTE = $route;
@@ -144,23 +142,23 @@ class RouteCollection{
$route->execute(); $route->execute();
if(!$route->_ignore){ if (!$route->_ignore) {
$one_hit = true; $one_hit = true;
} }
if($route->_http_error){ if ($route->_http_error) {
$this->http_error($route->_http_error); $this->http_error($route->_http_error);
return; return;
} }
//If Executed, interrupt route chain? //If Executed, interrupt route chain?
if($route->_block){ if ($route->_block) {
return; return;
} }
} }
} }
if(!$one_hit){ if (!$one_hit) {
$info = new \stdClass(); $info = new \stdClass();
$info->code = 404; $info->code = 404;
$info->message = 'Not Found'; $info->message = 'Not Found';
@@ -170,7 +168,10 @@ class RouteCollection{
//DEFINITORS============================================= //DEFINITORS=============================================
static function get($uri, $callback, $weight = 0){ /**
*
*/
static function get($uri, $callback, $weight = 0) {
$route = new Route(); $route = new Route();
$route->_verb[] = 'GET'; $route->_verb[] = 'GET';
@@ -184,7 +185,10 @@ class RouteCollection{
return $route; return $route;
} }
static function post($uri, $callback, $weight = 0){ /**
*
*/
static function post($uri, $callback, $weight = 0) {
$route = new Route(); $route = new Route();
$route->_verb[] = 'POST'; $route->_verb[] = 'POST';
@@ -198,7 +202,10 @@ class RouteCollection{
return $route; return $route;
} }
static function put($uri, $callback, $weight = 0){ /**
*
*/
static function put($uri, $callback, $weight = 0) {
$route = new Route(); $route = new Route();
$route->_verb[] = 'PUT'; $route->_verb[] = 'PUT';
@@ -212,7 +219,10 @@ class RouteCollection{
return $route; return $route;
} }
static function patch($uri, $callback, $weight = 0){ /**
*
*/
static function patch($uri, $callback, $weight = 0) {
$route = new Route(); $route = new Route();
$route->_verb[] = 'PATCH'; $route->_verb[] = 'PATCH';
@@ -226,7 +236,10 @@ class RouteCollection{
return $route; return $route;
} }
static function delete($uri, $callback, $weight = 0){ /**
*
*/
static function delete($uri, $callback, $weight = 0) {
$route = new Route(); $route = new Route();
$route->_verb[] = 'DELETE'; $route->_verb[] = 'DELETE';
@@ -240,22 +253,22 @@ class RouteCollection{
return $route; return $route;
} }
static function resource($uri){ static function resource($uri) {
throw new Exception('Not implemented'); throw new Exception('Not implemented');
} }
/* /**
* *
*/ */
static function add($verb, $uri, $callback, $weight = 0){ static function add($verb, $uri, $callback, $weight = 0) {
$route = new Route(); $route = new Route();
if(is_array( $verb ) ){ if (is_array($verb)) {
$route->_verb = array_map('strtoupper', $verb); $route->_verb = array_map('strtoupper', $verb);
}else if( strtoupper($verb) == 'WEB' ){ } else if (strtoupper($verb) == 'WEB') {
$route->_verb[] = self::_verbs_web; $route->_verb[] = self::_verbs_web;
}else{ } else {
$route->_verb[] = strtoupper( $verb ); $route->_verb[] = strtoupper($verb);
} }
$route->_uri = $uri; $route->_uri = $uri;
@@ -268,37 +281,43 @@ class RouteCollection{
return $route; return $route;
} }
/* /**
* *
*/ */
function addRoute(Route $route){ function addRoute(Route $route) {
$this->_routes[] = $route; $this->_routes[] = $route;
} }
/* /**
* *
*/ */
static function addMiddleware($name = '', $function){ static function addDefaultMiddleware($name = '', $function) {
self::getInstance()->_default_middlewares[$name] = $function; self::getInstance()->_default_middlewares[$name] = $function;
} }
/* /**
* *
*/ */
static function on_http_error($function){ static function registerMiddleware($name = '', $function) {
self::getInstance()->_middleware_set[$name] = $function;
}
/**
*
*/
static function on_http_error($function) {
$instance = self::getInstance(); $instance = self::getInstance();
$instance->_errors[] = $function; $instance->_errors[] = $function;
} }
/* /**
* *
*/ */
private function http_error(\stdClass $info){ private function http_error(\stdClass $info) {
$info = (array) $info; $info = (array) $info;
foreach ($this->_errors as $error) { foreach ($this->_errors as $error) {
call_user_func_array($error, $info); call_user_func_array($error, $info);
} }
} }
} }