|
@@ -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)
|