|
@@ -2,19 +2,20 @@
|
|
|
|
|
|
|
|
namespace Routes;
|
|
namespace Routes;
|
|
|
|
|
|
|
|
-class RouteCollection {
|
|
|
|
|
|
|
+class RouteCollection
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
private static $_routeCollection;
|
|
private static $_routeCollection;
|
|
|
|
|
|
|
|
- public $_uri = '/';
|
|
|
|
|
- public $_routes = Array();
|
|
|
|
|
|
|
+ public $_uri = '/';
|
|
|
|
|
+ public $_routes = array();
|
|
|
private $_verb = '';
|
|
private $_verb = '';
|
|
|
- private $_loadedFiles = Array();
|
|
|
|
|
- private $_errors = Array();
|
|
|
|
|
- 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');
|
|
|
|
|
|
|
+ private $_loadedFiles = array();
|
|
|
|
|
+ private $_errors = array();
|
|
|
|
|
+ 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 $_groupIn = false;
|
|
|
public $_groupBase = '';
|
|
public $_groupBase = '';
|
|
@@ -23,11 +24,13 @@ class RouteCollection {
|
|
|
|
|
|
|
|
//SINGLETON==============================================
|
|
//SINGLETON==============================================
|
|
|
|
|
|
|
|
- private function __construct() {
|
|
|
|
|
|
|
+ private function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static function newObj() {
|
|
|
|
|
|
|
+ private static function newObj()
|
|
|
|
|
+ {
|
|
|
if (!isset(self::$_routeCollection)) {
|
|
if (!isset(self::$_routeCollection)) {
|
|
|
self::$_routeCollection = new RouteCollection();
|
|
self::$_routeCollection = new RouteCollection();
|
|
|
if (php_sapi_name() == "cli") {
|
|
if (php_sapi_name() == "cli") {
|
|
@@ -44,7 +47,8 @@ class RouteCollection {
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- public static function getInstance() {
|
|
|
|
|
|
|
+ public static function getInstance()
|
|
|
|
|
+ {
|
|
|
if (!isset(self::$_routeCollection)) {
|
|
if (!isset(self::$_routeCollection)) {
|
|
|
return self::newObj();
|
|
return self::newObj();
|
|
|
}
|
|
}
|
|
@@ -58,7 +62,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag\t RouteCollection->crawl()
|
|
* @ctag\t RouteCollection->crawl()
|
|
|
*/
|
|
*/
|
|
|
- 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);
|
|
@@ -66,7 +71,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) && $file[0] != '.') {
|
|
// if (strpos($file, $filename) && $file[0] != '.') {
|
|
|
- if (strpos($file, $filename) && !strpos($file, '.swp') ) {
|
|
|
|
|
|
|
+ if (strpos($file, $filename) && !strpos($file, '.swp')) {
|
|
|
$instance->_loadedFiles[] = $file;
|
|
$instance->_loadedFiles[] = $file;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -79,7 +84,8 @@ class RouteCollection {
|
|
|
* Load the files with the routes
|
|
* Load the files with the routes
|
|
|
* @ctag\t RouteCollection->loadRoutes()
|
|
* @ctag\t RouteCollection->loadRoutes()
|
|
|
*/
|
|
*/
|
|
|
- function loadRoutes() {
|
|
|
|
|
|
|
+ function loadRoutes()
|
|
|
|
|
+ {
|
|
|
$instance = self::getInstance();
|
|
$instance = self::getInstance();
|
|
|
|
|
|
|
|
foreach ($instance->_loadedFiles as $loadedFile) {
|
|
foreach ($instance->_loadedFiles as $loadedFile) {
|
|
@@ -93,7 +99,8 @@ class RouteCollection {
|
|
|
* GET | POST | PUT | PATCH | DELETE | CLI
|
|
* GET | POST | PUT | PATCH | DELETE | CLI
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- private function defineVerb() {
|
|
|
|
|
|
|
+ private function defineVerb()
|
|
|
|
|
+ {
|
|
|
|
|
|
|
|
$verb = '';
|
|
$verb = '';
|
|
|
|
|
|
|
@@ -115,8 +122,9 @@ class RouteCollection {
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- private function sort_routes() {
|
|
|
|
|
- usort($this->_routes, function($a, $b) {
|
|
|
|
|
|
|
+ private function sort_routes()
|
|
|
|
|
+ {
|
|
|
|
|
+ usort($this->_routes, function ($a, $b) {
|
|
|
if ($a->_weight == $b->_weight) {
|
|
if ($a->_weight == $b->_weight) {
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
@@ -127,7 +135,8 @@ class RouteCollection {
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- function submit() {
|
|
|
|
|
|
|
+ function submit()
|
|
|
|
|
+ {
|
|
|
global $ROUTE;
|
|
global $ROUTE;
|
|
|
|
|
|
|
|
self::$_routeCollection->sort_routes();
|
|
self::$_routeCollection->sort_routes();
|
|
@@ -149,10 +158,11 @@ class RouteCollection {
|
|
|
|
|
|
|
|
$ROUTE = $route;
|
|
$ROUTE = $route;
|
|
|
|
|
|
|
|
- foreach ($route->_before as $key => $requestedMiddleware){
|
|
|
|
|
- $route->_before[$key] = Array(
|
|
|
|
|
|
|
+ foreach ($route->_before as $key => $requestedMiddleware) {
|
|
|
|
|
+ $route->_before[$key] = array(
|
|
|
'callback' => $this->_middlewareSet[$key],
|
|
'callback' => $this->_middlewareSet[$key],
|
|
|
- 'params' => $requestedMiddleware);
|
|
|
|
|
|
|
+ 'params' => $requestedMiddleware
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
|
|
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
|
|
@@ -187,7 +197,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::group('base',function(){})
|
|
* @ctag RouteCollection::group('base',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function group($base = '', $callback){
|
|
|
|
|
|
|
+ static function group($base = '', $callback)
|
|
|
|
|
+ {
|
|
|
$collection = self::getInstance();
|
|
$collection = self::getInstance();
|
|
|
$collection->_groupList = [];
|
|
$collection->_groupList = [];
|
|
|
$collection->_groupIn = true;
|
|
$collection->_groupIn = true;
|
|
@@ -204,7 +215,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::get('/url',function(){})
|
|
* @ctag RouteCollection::get('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function get($uri, $callback, $weight = 0) {
|
|
|
|
|
|
|
+ static function get($uri, $callback, $weight = 0)
|
|
|
|
|
+ {
|
|
|
return self::add('GET', $uri, $callback, $weight);
|
|
return self::add('GET', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -212,7 +224,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::cli('/url',function(){})
|
|
* @ctag RouteCollection::cli('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function cli($uri, $callback, $weight = 0) {
|
|
|
|
|
|
|
+ static function cli($uri, $callback, $weight = 0)
|
|
|
|
|
+ {
|
|
|
return self::add('CLI', $uri, $callback, $weight);
|
|
return self::add('CLI', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -220,7 +233,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::post('/url',function(){})
|
|
* @ctag RouteCollection::post('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function post($uri, $callback, $weight = 0) {
|
|
|
|
|
|
|
+ static function post($uri, $callback, $weight = 0)
|
|
|
|
|
+ {
|
|
|
return self::add('POST', $uri, $callback, $weight);
|
|
return self::add('POST', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -228,7 +242,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::put('/url',function(){})
|
|
* @ctag RouteCollection::put('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function put($uri, $callback, $weight = 0) {
|
|
|
|
|
|
|
+ static function put($uri, $callback, $weight = 0)
|
|
|
|
|
+ {
|
|
|
return self::add('PUT', $uri, $callback, $weight);
|
|
return self::add('PUT', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -236,7 +251,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::patch('/url',function(){})
|
|
* @ctag RouteCollection::patch('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function patch($uri, $callback, $weight = 0) {
|
|
|
|
|
|
|
+ static function patch($uri, $callback, $weight = 0)
|
|
|
|
|
+ {
|
|
|
return self::add('PATCH', $uri, $callback, $weight);
|
|
return self::add('PATCH', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -245,7 +261,8 @@ class RouteCollection {
|
|
|
* @ctag RouteCollection::delete('/url',Controller@Method)
|
|
* @ctag RouteCollection::delete('/url',Controller@Method)
|
|
|
* @ctag RouteCollection::delete('/url',function(){})
|
|
* @ctag RouteCollection::delete('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function delete($uri, $callback, $weight = 0) {
|
|
|
|
|
|
|
+ static function delete($uri, $callback, $weight = 0)
|
|
|
|
|
+ {
|
|
|
return self::add('DELETE', $uri, $callback, $weight);
|
|
return self::add('DELETE', $uri, $callback, $weight);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -253,7 +270,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::resource('/url',function(){})
|
|
* @ctag RouteCollection::resource('/url',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function resource($uri) {
|
|
|
|
|
|
|
+ static function resource($uri)
|
|
|
|
|
+ {
|
|
|
throw new Exception('Not implemented');
|
|
throw new Exception('Not implemented');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -262,10 +280,11 @@ class RouteCollection {
|
|
|
* @ctag RouteCollection::add('VERB','/url',function(){})
|
|
* @ctag RouteCollection::add('VERB','/url',function(){})
|
|
|
* @ctag RouteCollection::add('VERB','/url',Controller@Method)
|
|
* @ctag RouteCollection::add('VERB','/url',Controller@Method)
|
|
|
*/
|
|
*/
|
|
|
- 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){
|
|
|
|
|
|
|
+ if (self::getInstance()->_groupIn) {
|
|
|
$uri = self::getInstance()->_groupBase . $uri;
|
|
$uri = self::getInstance()->_groupBase . $uri;
|
|
|
self::getInstance()->_groupList[] = &$route;
|
|
self::getInstance()->_groupList[] = &$route;
|
|
|
}
|
|
}
|
|
@@ -291,7 +310,8 @@ class RouteCollection {
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- function addRoute(Route $route) {
|
|
|
|
|
|
|
+ function addRoute(Route $route)
|
|
|
|
|
+ {
|
|
|
$this->_routes[] = $route;
|
|
$this->_routes[] = $route;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -299,7 +319,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
|
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function addDefaultMiddleware($name = '', $function) {
|
|
|
|
|
|
|
+ static function addDefaultMiddleware($name = '', $function)
|
|
|
|
|
+ {
|
|
|
self::getInstance()->_defaultMiddlewares[$name] = $function;
|
|
self::getInstance()->_defaultMiddlewares[$name] = $function;
|
|
|
return self::getInstance();
|
|
return self::getInstance();
|
|
|
}
|
|
}
|
|
@@ -308,7 +329,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
|
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function registerMiddleware($name = '', $function) {
|
|
|
|
|
|
|
+ static function registerMiddleware($name = '', $function)
|
|
|
|
|
+ {
|
|
|
return self::getInstance()->_middlewareSet[$name] = $function;
|
|
return self::getInstance()->_middlewareSet[$name] = $function;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -316,7 +338,8 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::onHttpError(function(){})
|
|
* @ctag RouteCollection::onHttpError(function(){})
|
|
|
*/
|
|
*/
|
|
|
- static function onHttpError($code, $function) {
|
|
|
|
|
|
|
+ static function onHttpError($code, $function)
|
|
|
|
|
+ {
|
|
|
$instance = self::getInstance();
|
|
$instance = self::getInstance();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -324,11 +347,12 @@ class RouteCollection {
|
|
|
*
|
|
*
|
|
|
* @ctag RouteCollection::httpError($class)
|
|
* @ctag RouteCollection::httpError($class)
|
|
|
*/
|
|
*/
|
|
|
- private function httpError(\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);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+}
|