瀏覽代碼

begin middleware execution; return business result

develop
Robin Thoni 8 年之前
父節點
當前提交
9c2ac6581f
共有 2 個檔案被更改,包括 20 行新增3 行删除
  1. 18
    2
      src/Utils/Controller/LuRoute.php
  2. 2
    1
      src/Utils/Middleware/LuAbstractMiddleware.php

+ 18
- 2
src/Utils/Controller/LuRoute.php 查看文件

@@ -3,6 +3,7 @@
3 3
 namespace Luticate\Utils\Controller;
4 4
 
5 5
 use FastRoute\Dispatcher;
6
+use Luticate\Utils\Business\LuBusiness;
6 7
 use Luticate\Utils\Business\LuBusinessException;
7 8
 use Luticate\Utils\Business\LuDocParser;
8 9
 use Luticate\Utils\Dbo\LuDboConstraintException;
@@ -12,6 +13,7 @@ use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
12 13
 use FastRoute\RouteCollector;
13 14
 use FastRoute\RouteParser\Std;
14 15
 use Luticate\Utils\Dbo\LuRouteDbo;
16
+use Luticate\Utils\LuAbstractMiddleware;
15 17
 
16 18
 class LuRoute {
17 19
 
@@ -72,7 +74,7 @@ class LuRoute {
72 74
             $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function() use($route)
73 75
             {
74 76
                 $router = static::getInstance();
75
-                $router->execute($route);
77
+                return $router->execute($route);
76 78
             });
77 79
         }
78 80
 
@@ -81,6 +83,16 @@ class LuRoute {
81 83
 
82 84
     public function execute(LuRouteDbo $route)
83 85
     {
86
+        $middlewares = [];
87
+        foreach ($route->getMiddlewares() as $middlewareName) {
88
+            /**
89
+             * @var $middleware LuAbstractMiddleware
90
+             */
91
+            $middleware = new $middlewareName();
92
+            $middleware->onBefore($route);
93
+            $middlewares[] = $middleware;
94
+        }
95
+
84 96
         $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
85 97
         $params = $reflect->getParameters();
86 98
         $doc = new LuDocParser($reflect->getDocComment());
@@ -121,7 +133,11 @@ class LuRoute {
121 133
         $controllerInstance = new $controller();
122 134
         $result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
123 135
 
124
-        return LuOutputFormatter::formatSuccess($result);
136
+        foreach ($middlewares as $middleware) {
137
+            $middleware->onAfter($route, $result);
138
+        }
139
+
140
+        return $result;
125 141
     }
126 142
 
127 143
     public function dispatch(string $httpMethod, string $url)

+ 2
- 1
src/Utils/Middleware/LuAbstractMiddleware.php 查看文件

@@ -8,6 +8,7 @@
8 8
 
9 9
 namespace Luticate\Utils;
10 10
 
11
+use Luticate\Utils\Dbo\LuRouteDbo;
11 12
 
12 13
 abstract class LuAbstractMiddleware
13 14
 {
@@ -16,7 +17,7 @@ abstract class LuAbstractMiddleware
16 17
         
17 18
     }
18 19
     
19
-    public function onAfter(LuRouteDbo $route, $output, ...$params)
20
+    public function onAfter(LuRouteDbo $route, $result, ...$params)
20 21
     {
21 22
         
22 23
     }

Loading…
取消
儲存