Browse Source

begin middleware execution; return business result

develop
Robin Thoni 8 years ago
parent
commit
9c2ac6581f
2 changed files with 20 additions and 3 deletions
  1. 18
    2
      src/Utils/Controller/LuRoute.php
  2. 2
    1
      src/Utils/Middleware/LuAbstractMiddleware.php

+ 18
- 2
src/Utils/Controller/LuRoute.php View File

3
 namespace Luticate\Utils\Controller;
3
 namespace Luticate\Utils\Controller;
4
 
4
 
5
 use FastRoute\Dispatcher;
5
 use FastRoute\Dispatcher;
6
+use Luticate\Utils\Business\LuBusiness;
6
 use Luticate\Utils\Business\LuBusinessException;
7
 use Luticate\Utils\Business\LuBusinessException;
7
 use Luticate\Utils\Business\LuDocParser;
8
 use Luticate\Utils\Business\LuDocParser;
8
 use Luticate\Utils\Dbo\LuDboConstraintException;
9
 use Luticate\Utils\Dbo\LuDboConstraintException;
12
 use FastRoute\RouteCollector;
13
 use FastRoute\RouteCollector;
13
 use FastRoute\RouteParser\Std;
14
 use FastRoute\RouteParser\Std;
14
 use Luticate\Utils\Dbo\LuRouteDbo;
15
 use Luticate\Utils\Dbo\LuRouteDbo;
16
+use Luticate\Utils\LuAbstractMiddleware;
15
 
17
 
16
 class LuRoute {
18
 class LuRoute {
17
 
19
 
72
             $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function() use($route)
74
             $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function() use($route)
73
             {
75
             {
74
                 $router = static::getInstance();
76
                 $router = static::getInstance();
75
-                $router->execute($route);
77
+                return $router->execute($route);
76
             });
78
             });
77
         }
79
         }
78
 
80
 
81
 
83
 
82
     public function execute(LuRouteDbo $route)
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
         $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
96
         $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
85
         $params = $reflect->getParameters();
97
         $params = $reflect->getParameters();
86
         $doc = new LuDocParser($reflect->getDocComment());
98
         $doc = new LuDocParser($reflect->getDocComment());
121
         $controllerInstance = new $controller();
133
         $controllerInstance = new $controller();
122
         $result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
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
     public function dispatch(string $httpMethod, string $url)
143
     public function dispatch(string $httpMethod, string $url)

+ 2
- 1
src/Utils/Middleware/LuAbstractMiddleware.php View File

8
 
8
 
9
 namespace Luticate\Utils;
9
 namespace Luticate\Utils;
10
 
10
 
11
+use Luticate\Utils\Dbo\LuRouteDbo;
11
 
12
 
12
 abstract class LuAbstractMiddleware
13
 abstract class LuAbstractMiddleware
13
 {
14
 {
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…
Cancel
Save