Atualizar 'src/BBRouter/RouteCollection.php'

This commit is contained in:
ahwelp
2019-03-07 04:42:50 +00:00
parent 5b974f1224
commit c6cdba991e
+43 -24
View File
@@ -5,27 +5,21 @@ 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)) {
@@ -40,6 +34,9 @@ class RouteCollection{
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();
@@ -47,7 +44,6 @@ class RouteCollection{
return self::$_route_collection; return self::$_route_collection;
} }
//HELPERS================================================ //HELPERS================================================
/* /*
@@ -68,7 +64,8 @@ class RouteCollection{
return $instance; return $instance;
} }
/* /**
*
* Load the files with the routes * Load the files with the routes
*/ */
function load_routes() { function load_routes() {
@@ -80,7 +77,8 @@ class RouteCollection{
return $instance; return $instance;
} }
/* /**
*
* GET | POST | PUT | PATCH | DELETE | CLI * GET | POST | PUT | PATCH | DELETE | CLI
* *
*/ */
@@ -103,7 +101,7 @@ class RouteCollection{
} }
} }
/* /**
* *
*/ */
private function sort_routes() { private function sort_routes() {
@@ -115,8 +113,8 @@ class RouteCollection{
}); });
} }
/* /**
* Dispatch routes *
*/ */
function submit() { function submit() {
global $ROUTE; global $ROUTE;
@@ -170,6 +168,9 @@ 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,6 +185,9 @@ 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,6 +202,9 @@ 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,6 +219,9 @@ 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,6 +236,9 @@ 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';
@@ -244,7 +257,7 @@ class RouteCollection{
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) {
@@ -268,21 +281,28 @@ 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 registerMiddleware($name = '', $function) {
self::getInstance()->_middleware_set[$name] = $function;
}
/**
* *
*/ */
static function on_http_error($function) { static function on_http_error($function) {
@@ -290,7 +310,7 @@ class RouteCollection{
$instance->_errors[] = $function; $instance->_errors[] = $function;
} }
/* /**
* *
*/ */
private function http_error(\stdClass $info) { private function http_error(\stdClass $info) {
@@ -298,7 +318,6 @@ class RouteCollection{
foreach ($this->_errors as $error) { foreach ($this->_errors as $error) {
call_user_func_array($error, $info); call_user_func_array($error, $info);
} }
}
} }
}