ahwelp 3 жил өмнө
parent
commit
33f5b7462b

+ 8 - 4
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;
     }
 

+ 29 - 22
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;
     }
 
     /**