|
@@ -4,17 +4,23 @@ namespace Routes;
|
|
|
|
|
|
|
|
class RouteCollection {
|
|
class RouteCollection {
|
|
|
|
|
|
|
|
- private static $_route_collection;
|
|
|
|
|
- public $_uri = '/';
|
|
|
|
|
- public $_routes = Array();
|
|
|
|
|
|
|
+ private static $_routeCollection;
|
|
|
|
|
+
|
|
|
|
|
+ public $_uri = '/';
|
|
|
|
|
+ public $_routes = Array();
|
|
|
private $_verb = '';
|
|
private $_verb = '';
|
|
|
- private $_loaded_files = Array();
|
|
|
|
|
|
|
+ private $_loadedFiles = Array();
|
|
|
private $_errors = Array();
|
|
private $_errors = Array();
|
|
|
- private $_default_middlewares = Array();
|
|
|
|
|
- private $_middleware_set = Array();
|
|
|
|
|
- private static $_verbs_whitelist = Array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
|
|
|
|
|
- private static $_verbs_web = Array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
|
|
|
|
|
-
|
|
|
|
|
|
|
+ private $_defaultMiddlewares = Array();
|
|
|
|
|
+ private $_middlewareSet = Array();
|
|
|
|
|
+ private static $_verbsWhitelist = Array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
|
|
|
|
|
+ private static $_verbsWeb = Array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
|
|
|
|
|
+
|
|
|
|
|
+ public $_groupIn = false;
|
|
|
|
|
+ public $_groupBase = '';
|
|
|
|
|
+ public $_groupList = [];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
//SINGLETON==============================================
|
|
//SINGLETON==============================================
|
|
|
|
|
|
|
|
private function __construct() {
|
|
private function __construct() {
|
|
@@ -22,26 +28,26 @@ class RouteCollection {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static function newObj() {
|
|
private static function newObj() {
|
|
|
- if (!isset(self::$_route_collection)) {
|
|
|
|
|
- self::$_route_collection = new RouteCollection();
|
|
|
|
|
|
|
+ if (!isset(self::$_routeCollection)) {
|
|
|
|
|
+ self::$_routeCollection = new RouteCollection();
|
|
|
if (php_sapi_name() == "cli") {
|
|
if (php_sapi_name() == "cli") {
|
|
|
- self::$_route_collection->_uri = isset($_SERVER['argv'][1]) ? '/' . $_SERVER['argv'][1] : '/';
|
|
|
|
|
|
|
+ self::$_routeCollection->_uri = isset($_SERVER['argv'][1]) ? '/' . $_SERVER['argv'][1] : '/';
|
|
|
} else {
|
|
} else {
|
|
|
- self::$_route_collection->_uri = isset($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI'])[0] : '/';
|
|
|
|
|
|
|
+ self::$_routeCollection->_uri = isset($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI'])[0] : '/';
|
|
|
}
|
|
}
|
|
|
- self::$_route_collection->define_verb();
|
|
|
|
|
|
|
+ self::$_routeCollection->defineVerb();
|
|
|
}
|
|
}
|
|
|
- return self::$_route_collection;
|
|
|
|
|
|
|
+ return self::$_routeCollection;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
public static function getInstance() {
|
|
public static function getInstance() {
|
|
|
- if (!isset(self::$_route_collection)) {
|
|
|
|
|
|
|
+ if (!isset(self::$_routeCollection)) {
|
|
|
return self::newObj();
|
|
return self::newObj();
|
|
|
}
|
|
}
|
|
|
- return self::$_route_collection;
|
|
|
|
|
|
|
+ return self::$_routeCollection;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//HELPERS================================================
|
|
//HELPERS================================================
|
|
@@ -57,7 +63,7 @@ class RouteCollection {
|
|
|
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->_loadedFiles[] = $file;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -68,11 +74,11 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* Load the files with the routes
|
|
* Load the files with the routes
|
|
|
*/
|
|
*/
|
|
|
- function load_routes() {
|
|
|
|
|
|
|
+ function loadRoutes() {
|
|
|
$instance = self::getInstance();
|
|
$instance = self::getInstance();
|
|
|
|
|
|
|
|
- foreach ($instance->_loaded_files as $loaded_file) {
|
|
|
|
|
- include $loaded_file;
|
|
|
|
|
|
|
+ foreach ($instance->_loadedFiles as $loadedFile) {
|
|
|
|
|
+ include $loadedFile;
|
|
|
}
|
|
}
|
|
|
return $instance;
|
|
return $instance;
|
|
|
}
|
|
}
|
|
@@ -82,7 +88,7 @@ class RouteCollection {
|
|
|
* GET | POST | PUT | PATCH | DELETE | CLI
|
|
* GET | POST | PUT | PATCH | DELETE | CLI
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- private function define_verb() {
|
|
|
|
|
|
|
+ private function defineVerb() {
|
|
|
|
|
|
|
|
$verb = '';
|
|
$verb = '';
|
|
|
|
|
|
|
@@ -94,7 +100,7 @@ class RouteCollection {
|
|
|
$verb = strtoupper($_SERVER['REQUEST_METHOD']);
|
|
$verb = strtoupper($_SERVER['REQUEST_METHOD']);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (in_array($verb, self::$_verbs_whitelist)) {
|
|
|
|
|
|
|
+ if (in_array($verb, self::$_verbsWhitelist)) {
|
|
|
$this->_verb = $verb;
|
|
$this->_verb = $verb;
|
|
|
} else {
|
|
} else {
|
|
|
echo 'Invalid Verb';
|
|
echo 'Invalid Verb';
|
|
@@ -119,26 +125,26 @@ class RouteCollection {
|
|
|
function submit() {
|
|
function submit() {
|
|
|
global $ROUTE;
|
|
global $ROUTE;
|
|
|
|
|
|
|
|
- self::$_route_collection->sort_routes();
|
|
|
|
|
|
|
+ self::$_routeCollection->sort_routes();
|
|
|
|
|
|
|
|
$one_hit = false;
|
|
$one_hit = false;
|
|
|
|
|
|
|
|
- foreach (self::$_route_collection->_routes as $route) {
|
|
|
|
|
|
|
+ foreach (self::$_routeCollection->_routes as $route) {
|
|
|
|
|
|
|
|
- if (!in_array(self::$_route_collection->_verb, $route->_verb) && !in_array('*', $route->_verb)) {
|
|
|
|
|
|
|
+ if (!in_array(self::$_routeCollection->_verb, $route->_verb) && !in_array('*', $route->_verb)) {
|
|
|
//No it is not
|
|
//No it is not
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Request match a route
|
|
//Request match a route
|
|
|
- $match = $route->match(self::$_route_collection->_uri);
|
|
|
|
|
|
|
+ $match = $route->match(self::$_routeCollection->_uri);
|
|
|
|
|
|
|
|
if ($match) {
|
|
if ($match) {
|
|
|
//Yup. Execute the route
|
|
//Yup. Execute the route
|
|
|
|
|
|
|
|
$ROUTE = $route;
|
|
$ROUTE = $route;
|
|
|
|
|
|
|
|
- $route->_before = $this->_default_middlewares;
|
|
|
|
|
|
|
+ $route->_before = $this->_defaultMiddlewares;
|
|
|
|
|
|
|
|
$route->execute();
|
|
$route->execute();
|
|
|
|
|
|
|
@@ -146,8 +152,8 @@ class RouteCollection {
|
|
|
$one_hit = true;
|
|
$one_hit = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if ($route->_http_error) {
|
|
|
|
|
- $this->http_error($route->_http_error);
|
|
|
|
|
|
|
+ if ($route->_httpError) {
|
|
|
|
|
+ $this->httpError($route->_httpError);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -162,112 +168,63 @@ class RouteCollection {
|
|
|
$info = new \stdClass();
|
|
$info = new \stdClass();
|
|
|
$info->code = 404;
|
|
$info->code = 404;
|
|
|
$info->message = 'Not Found';
|
|
$info->message = 'Not Found';
|
|
|
- self::$_route_collection->http_error($info);
|
|
|
|
|
|
|
+ self::$_routeCollection->httpError($info);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ static function group($base = '', $callback){
|
|
|
|
|
+ $collection = self::getInstance();
|
|
|
|
|
+ $collection->_groupList = [];
|
|
|
|
|
+ $collection->_groupIn = true;
|
|
|
|
|
+ $collection->_groupBase = $base;
|
|
|
|
|
+ call_user_func($callback);
|
|
|
|
|
+ $collection->_groupBase = '';
|
|
|
|
|
+ $collection->_groupIn = false;
|
|
|
|
|
+ return new RouteGroup($collection->_groupList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
//DEFINITORS=============================================
|
|
//DEFINITORS=============================================
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function get($uri, $callback, $weight = 0) {
|
|
static function get($uri, $callback, $weight = 0) {
|
|
|
- $route = new Route();
|
|
|
|
|
- $route->_verb[] = 'GET';
|
|
|
|
|
-
|
|
|
|
|
- $route->_uri = $uri;
|
|
|
|
|
- $route->_callback[] = $callback;
|
|
|
|
|
- $route->_weight = $weight;
|
|
|
|
|
- $route->prepare();
|
|
|
|
|
-
|
|
|
|
|
- self::getInstance()->_routes[] = $route;
|
|
|
|
|
-
|
|
|
|
|
- return $route;
|
|
|
|
|
|
|
+ return self::add('GET', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function cli($uri, $callback, $weight = 0) {
|
|
static function cli($uri, $callback, $weight = 0) {
|
|
|
- $route = new Route();
|
|
|
|
|
- $route->_verb[] = 'CLI';
|
|
|
|
|
-
|
|
|
|
|
- $route->_uri = $uri;
|
|
|
|
|
- $route->_callback[] = $callback;
|
|
|
|
|
- $route->_weight = $weight;
|
|
|
|
|
- $route->prepare();
|
|
|
|
|
-
|
|
|
|
|
- self::getInstance()->_routes[] = $route;
|
|
|
|
|
-
|
|
|
|
|
- return $route;
|
|
|
|
|
|
|
+ return self::add('CLI', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function post($uri, $callback, $weight = 0) {
|
|
static function post($uri, $callback, $weight = 0) {
|
|
|
- $route = new Route();
|
|
|
|
|
- $route->_verb[] = 'POST';
|
|
|
|
|
-
|
|
|
|
|
- $route->_uri = $uri;
|
|
|
|
|
- $route->_callback[] = $callback;
|
|
|
|
|
- $route->_weight = $weight;
|
|
|
|
|
- $route->prepare();
|
|
|
|
|
-
|
|
|
|
|
- self::getInstance()->_routes[] = $route;
|
|
|
|
|
-
|
|
|
|
|
- return $route;
|
|
|
|
|
|
|
+ return self::add('POST', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function put($uri, $callback, $weight = 0) {
|
|
static function put($uri, $callback, $weight = 0) {
|
|
|
- $route = new Route();
|
|
|
|
|
- $route->_verb[] = 'PUT';
|
|
|
|
|
-
|
|
|
|
|
- $route->_uri = $uri;
|
|
|
|
|
- $route->_callback[] = $callback;
|
|
|
|
|
- $route->_weight = $weight;
|
|
|
|
|
- $route->prepare();
|
|
|
|
|
-
|
|
|
|
|
- self::getInstance()->_routes[] = $route;
|
|
|
|
|
-
|
|
|
|
|
- return $route;
|
|
|
|
|
|
|
+ return self::add('PUT', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function patch($uri, $callback, $weight = 0) {
|
|
static function patch($uri, $callback, $weight = 0) {
|
|
|
- $route = new Route();
|
|
|
|
|
- $route->_verb[] = 'PATCH';
|
|
|
|
|
-
|
|
|
|
|
- $route->_uri = $uri;
|
|
|
|
|
- $route->_callback[] = $callback;
|
|
|
|
|
- $route->_weight = $weight;
|
|
|
|
|
- $route->prepare();
|
|
|
|
|
-
|
|
|
|
|
- self::getInstance()->_routes[] = $route;
|
|
|
|
|
-
|
|
|
|
|
- return $route;
|
|
|
|
|
|
|
+ return self::add('PATCH', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function delete($uri, $callback, $weight = 0) {
|
|
static function delete($uri, $callback, $weight = 0) {
|
|
|
- $route = new Route();
|
|
|
|
|
- $route->_verb[] = 'DELETE';
|
|
|
|
|
-
|
|
|
|
|
- $route->_uri = $uri;
|
|
|
|
|
- $route->_callback[] = $callback;
|
|
|
|
|
- $route->_weight = $weight;
|
|
|
|
|
- $route->prepare();
|
|
|
|
|
-
|
|
|
|
|
- self::getInstance()->_routes[] = $route;
|
|
|
|
|
-
|
|
|
|
|
- return $route;
|
|
|
|
|
|
|
+ return self::add('DELETE', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static function resource($uri) {
|
|
static function resource($uri) {
|
|
@@ -279,11 +236,16 @@ class RouteCollection {
|
|
|
*/
|
|
*/
|
|
|
static function add($verb, $uri, $callback, $weight = 0) {
|
|
static function add($verb, $uri, $callback, $weight = 0) {
|
|
|
$route = new Route();
|
|
$route = new Route();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if(self::getInstance()->_groupIn){
|
|
|
|
|
+ $uri = self::getInstance()->_groupBase . $uri;
|
|
|
|
|
+ self::getInstance()->_groupList[] = &$route;
|
|
|
|
|
+ }
|
|
|
|
|
+ $uri = rtrim($uri, '/');
|
|
|
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::_verbsWeb;
|
|
|
} else {
|
|
} else {
|
|
|
$route->_verb[] = strtoupper($verb);
|
|
$route->_verb[] = strtoupper($verb);
|
|
|
}
|
|
}
|
|
@@ -309,20 +271,20 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function addDefaultMiddleware($name = '', $function) {
|
|
static function addDefaultMiddleware($name = '', $function) {
|
|
|
- self::getInstance()->_default_middlewares[$name] = $function;
|
|
|
|
|
|
|
+ self::getInstance()->_defaultMiddlewares[$name] = $function;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
static function registerMiddleware($name = '', $function) {
|
|
static function registerMiddleware($name = '', $function) {
|
|
|
- self::getInstance()->_middleware_set[$name] = $function;
|
|
|
|
|
|
|
+ self::getInstance()->_middlewareSet[$name] = $function;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- static function on_http_error($function) {
|
|
|
|
|
|
|
+ static function onHttpError($function) {
|
|
|
$instance = self::getInstance();
|
|
$instance = self::getInstance();
|
|
|
$instance->_errors[] = $function;
|
|
$instance->_errors[] = $function;
|
|
|
}
|
|
}
|
|
@@ -330,7 +292,7 @@ class RouteCollection {
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- private function http_error(\stdClass $info) {
|
|
|
|
|
|
|
+ private function httpError(\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);
|