From 33f5b7462b3a32a761ecbfae10561f30f42eb17b Mon Sep 17 00:00:00 2001 From: ahwelp Date: Tue, 5 Apr 2022 20:05:58 +0000 Subject: [PATCH] =?UTF-8?q?Mudan=C3=A7as?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Routes/Route.php | 12 +++++--- src/Routes/RouteCollection.php | 51 +++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 26 deletions(-) mode change 100755 => 100644 src/Routes/Route.php mode change 100755 => 100644 src/Routes/RouteCollection.php diff --git a/src/Routes/Route.php b/src/Routes/Route.php old mode 100755 new mode 100644 index b2ac205..7696271 --- a/src/Routes/Route.php +++ b/src/Routes/Route.php @@ -144,6 +144,8 @@ class Route { $before = explode('@', $before); $class = new $before[0](); $class->{$before[1]}(); + }else if(is_array($before)){ + call_user_func($before['callback'], $before['params']); }else{ call_user_func($before); } @@ -176,7 +178,7 @@ class Route { if(isset($this->_params['id'])){ $element->load($this->_params['id']); } - $this->_params['id'] = $element; + $this->_params['id'] = $element; } call_user_func_array($callback, $this->_params); } @@ -195,8 +197,10 @@ class Route { $this->_regex .= "\/[0-9]+"; } else if (strpos($segment, 'h:') > -1) { $this->_regex .= "\/[A-z]+"; - } else if (strpos($segment, ':') > -1) { + } else if (strpos($segment, 'd:') > -1) { $this->_regex .= "\/[A-z0-9]+"; + } else if (strpos($segment, 'r:') > -1) { + $this->_regex .= "\/[ -~]+"; } else { $this->_regex .= "\/" . $segment; } @@ -249,8 +253,8 @@ class Route { return $this; } - function middlewareAdd($name = '') { - $this->_before[$name] = $function; + function middlewareAdd($name = '', $params) { + $this->_before[$name] = $params; return $this; } diff --git a/src/Routes/RouteCollection.php b/src/Routes/RouteCollection.php old mode 100755 new mode 100644 index a90654a..8057755 --- a/src/Routes/RouteCollection.php +++ b/src/Routes/RouteCollection.php @@ -5,7 +5,7 @@ namespace Routes; class RouteCollection { private static $_routeCollection; - + public $_uri = '/'; public $_routes = Array(); private $_verb = ''; @@ -19,12 +19,12 @@ class RouteCollection { public $_groupIn = false; public $_groupBase = ''; public $_groupList = []; - - + + //SINGLETON============================================== private function __construct() { - + } private static function newObj() { @@ -65,7 +65,8 @@ class RouteCollection { foreach (new \RecursiveIteratorIterator($rdi) as $file) { foreach ($filenames as $filename) { - if (strpos($file, $filename)) { + // if (strpos($file, $filename) && $file[0] != '.') { + if (strpos($file, $filename) && !strpos($file, '.swp') ) { $instance->_loadedFiles[] = $file; } } @@ -90,7 +91,7 @@ class RouteCollection { /** * * GET | POST | PUT | PATCH | DELETE | CLI - * + * */ private function defineVerb() { @@ -148,7 +149,13 @@ class RouteCollection { $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(); @@ -175,7 +182,7 @@ class RouteCollection { self::$_routeCollection->httpError($info); } } - + /** * * @ctag RouteCollection::group('base',function(){}) @@ -190,7 +197,7 @@ class RouteCollection { $collection->_groupIn = false; return new RouteGroup($collection->_groupList); } - + //DEFINITORS============================================= /** @@ -206,15 +213,15 @@ class RouteCollection { * @ctag RouteCollection::cli('/url',function(){}) */ 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(){}) */ 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(){}) */ 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(){}) */ 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(){}) */ 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(){}) - */ + */ static function resource($uri) { throw new Exception('Not implemented'); } @@ -257,7 +264,7 @@ class RouteCollection { */ static function add($verb, $uri, $callback, $weight = 0) { $route = new Route(); - + if(self::getInstance()->_groupIn){ $uri = self::getInstance()->_groupBase . $uri; self::getInstance()->_groupList[] = &$route; @@ -294,6 +301,7 @@ class RouteCollection { */ static function addDefaultMiddleware($name = '', $function) { self::getInstance()->_defaultMiddlewares[$name] = $function; + return self::getInstance(); } /** @@ -301,16 +309,15 @@ class RouteCollection { * @ctag RouteCollection::addDefaultMiddleware('name',function(){}) */ static function registerMiddleware($name = '', $function) { - self::getInstance()->_middlewareSet[$name] = $function; + return self::getInstance()->_middlewareSet[$name] = $function; } /** * * @ctag RouteCollection::onHttpError(function(){}) */ - static function onHttpError($function) { + static function onHttpError($code, $function) { $instance = self::getInstance(); - $instance->_errors[] = $function; } /**