Mudanças

This commit is contained in:
ahwelp
2022-04-05 20:05:58 +00:00
parent 47a08f5d49
commit 33f5b7462b
2 changed files with 37 additions and 26 deletions
Executable → Regular
+8 -4
View File
@@ -144,6 +144,8 @@ class Route {
$before = explode('@', $before); $before = explode('@', $before);
$class = new $before[0](); $class = new $before[0]();
$class->{$before[1]}(); $class->{$before[1]}();
}else if(is_array($before)){
call_user_func($before['callback'], $before['params']);
}else{ }else{
call_user_func($before); call_user_func($before);
} }
@@ -176,7 +178,7 @@ class Route {
if(isset($this->_params['id'])){ if(isset($this->_params['id'])){
$element->load($this->_params['id']); $element->load($this->_params['id']);
} }
$this->_params['id'] = $element; $this->_params['id'] = $element;
} }
call_user_func_array($callback, $this->_params); call_user_func_array($callback, $this->_params);
} }
@@ -195,8 +197,10 @@ class Route {
$this->_regex .= "\/[0-9]+"; $this->_regex .= "\/[0-9]+";
} else if (strpos($segment, 'h:') > -1) { } else if (strpos($segment, 'h:') > -1) {
$this->_regex .= "\/[A-z]+"; $this->_regex .= "\/[A-z]+";
} else if (strpos($segment, ':') > -1) { } else if (strpos($segment, 'd:') > -1) {
$this->_regex .= "\/[A-z0-9]+"; $this->_regex .= "\/[A-z0-9]+";
} else if (strpos($segment, 'r:') > -1) {
$this->_regex .= "\/[ -~]+";
} else { } else {
$this->_regex .= "\/" . $segment; $this->_regex .= "\/" . $segment;
} }
@@ -249,8 +253,8 @@ class Route {
return $this; return $this;
} }
function middlewareAdd($name = '') { function middlewareAdd($name = '', $params) {
$this->_before[$name] = $function; $this->_before[$name] = $params;
return $this; return $this;
} }
Executable → Regular
+29 -22
View File
@@ -5,7 +5,7 @@ namespace Routes;
class RouteCollection { class RouteCollection {
private static $_routeCollection; private static $_routeCollection;
public $_uri = '/'; public $_uri = '/';
public $_routes = Array(); public $_routes = Array();
private $_verb = ''; private $_verb = '';
@@ -19,12 +19,12 @@ class RouteCollection {
public $_groupIn = false; public $_groupIn = false;
public $_groupBase = ''; public $_groupBase = '';
public $_groupList = []; public $_groupList = [];
//SINGLETON============================================== //SINGLETON==============================================
private function __construct() { private function __construct() {
} }
private static function newObj() { private static function newObj() {
@@ -65,7 +65,8 @@ 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) && $file[0] != '.') {
if (strpos($file, $filename) && !strpos($file, '.swp') ) {
$instance->_loadedFiles[] = $file; $instance->_loadedFiles[] = $file;
} }
} }
@@ -90,7 +91,7 @@ class RouteCollection {
/** /**
* *
* GET | POST | PUT | PATCH | DELETE | CLI * GET | POST | PUT | PATCH | DELETE | CLI
* *
*/ */
private function defineVerb() { private function defineVerb() {
@@ -148,7 +149,13 @@ class RouteCollection {
$ROUTE = $route; $ROUTE = $route;
$route->_before = $this->_defaultMiddlewares; foreach ($route->_before as $key => $requestedMiddleware){
$route->_before[$key] = Array(
'callback' => $this->_middlewareSet[$key],
'params' => $requestedMiddleware);
}
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
$route->execute(); $route->execute();
@@ -175,7 +182,7 @@ class RouteCollection {
self::$_routeCollection->httpError($info); self::$_routeCollection->httpError($info);
} }
} }
/** /**
* *
* @ctag RouteCollection::group('base',function(){}) * @ctag RouteCollection::group('base',function(){})
@@ -190,7 +197,7 @@ class RouteCollection {
$collection->_groupIn = false; $collection->_groupIn = false;
return new RouteGroup($collection->_groupList); return new RouteGroup($collection->_groupList);
} }
//DEFINITORS============================================= //DEFINITORS=============================================
/** /**
@@ -206,15 +213,15 @@ 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);
} }
/** /**
* *
* @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);
} }
/** /**
@@ -222,7 +229,7 @@ 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);
} }
/** /**
@@ -230,7 +237,7 @@ 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);
} }
/** /**
@@ -239,13 +246,13 @@ class RouteCollection {
* @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);
} }
/** /**
* *
* @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');
} }
@@ -257,7 +264,7 @@ 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){ if(self::getInstance()->_groupIn){
$uri = self::getInstance()->_groupBase . $uri; $uri = self::getInstance()->_groupBase . $uri;
self::getInstance()->_groupList[] = &$route; self::getInstance()->_groupList[] = &$route;
@@ -294,6 +301,7 @@ class RouteCollection {
*/ */
static function addDefaultMiddleware($name = '', $function) { static function addDefaultMiddleware($name = '', $function) {
self::getInstance()->_defaultMiddlewares[$name] = $function; self::getInstance()->_defaultMiddlewares[$name] = $function;
return self::getInstance();
} }
/** /**
@@ -301,16 +309,15 @@ class RouteCollection {
* @ctag RouteCollection::addDefaultMiddleware('name',function(){}) * @ctag RouteCollection::addDefaultMiddleware('name',function(){})
*/ */
static function registerMiddleware($name = '', $function) { static function registerMiddleware($name = '', $function) {
self::getInstance()->_middlewareSet[$name] = $function; return self::getInstance()->_middlewareSet[$name] = $function;
} }
/** /**
* *
* @ctag RouteCollection::onHttpError(function(){}) * @ctag RouteCollection::onHttpError(function(){})
*/ */
static function onHttpError($function) { static function onHttpError($code, $function) {
$instance = self::getInstance(); $instance = self::getInstance();
$instance->_errors[] = $function;
} }
/** /**