|
@@ -11,6 +11,7 @@ use FastRoute\DataGenerator\GroupCountBased as DataGeneratorGroupCountBased;
|
11
|
11
|
use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
|
12
|
12
|
use FastRoute\RouteCollector;
|
13
|
13
|
use FastRoute\RouteParser\Std;
|
|
14
|
+use Luticate\Utils\Dbo\LuMiddlewareDbo;
|
14
|
15
|
use Luticate\Utils\Dbo\LuRouteDbo;
|
15
|
16
|
use Luticate\Utils\Middleware\LuAbstractMiddleware;
|
16
|
17
|
|
|
@@ -26,9 +27,9 @@ class LuRoute {
|
26
|
27
|
private $_dispatcher = null;
|
27
|
28
|
|
28
|
29
|
/**
|
29
|
|
- * @var string[]
|
|
30
|
+ * @var LuMiddlewareDbo[]
|
30
|
31
|
*/
|
31
|
|
- private $middleware = array('Luticate\Utils\ParametersMiddleware');
|
|
32
|
+ private $_middlewares = [];
|
32
|
33
|
|
33
|
34
|
/**
|
34
|
35
|
* @var LuRouteDbo[]
|
|
@@ -45,11 +46,19 @@ class LuRoute {
|
45
|
46
|
}
|
46
|
47
|
|
47
|
48
|
/**
|
48
|
|
- * @param $middleware string
|
|
49
|
+ * @param $middleware mixed
|
49
|
50
|
*/
|
50
|
|
- public function addMiddleware($middleware)
|
|
51
|
+ public function addMiddleware(mixed $middleware)
|
51
|
52
|
{
|
52
|
|
- $this->middleware[] = $middleware;
|
|
53
|
+ if (is_string($middleware)) {
|
|
54
|
+ $this->_middlewares[] = new LuMiddlewareDbo($middleware);
|
|
55
|
+ }
|
|
56
|
+ else if (is_array($middleware)) {
|
|
57
|
+ $this->_middlewares[] = LuMiddlewareDbo::jsonDeserialize($middleware);
|
|
58
|
+ }
|
|
59
|
+ else {
|
|
60
|
+ $this->_middlewares[] = $middleware;
|
|
61
|
+ }
|
53
|
62
|
}
|
54
|
63
|
|
55
|
64
|
public function setup()
|
|
@@ -70,16 +79,25 @@ class LuRoute {
|
70
|
79
|
public function execute(LuRouteDbo $route, $parameters)
|
71
|
80
|
{
|
72
|
81
|
$middlewares = [];
|
73
|
|
- foreach ($route->getMiddlewares() as $middlewareName) {
|
|
82
|
+ /**
|
|
83
|
+ * @var $middlewareDbo LuMiddlewareDbo
|
|
84
|
+ */
|
|
85
|
+ foreach (array_merge($this->_middlewares, $route->getMiddlewares()) as $middlewareDbo) {
|
|
86
|
+ $middlewareName = $middlewareDbo->getMiddleware();
|
74
|
87
|
/**
|
75
|
88
|
* @var $middleware LuAbstractMiddleware
|
76
|
89
|
*/
|
77
|
90
|
$middleware = new $middlewareName();
|
|
91
|
+ $parameters["_middleware"] = array_merge($middlewareDbo->getParameters(), $route->getParameters());
|
78
|
92
|
$parameters = $middleware->onBefore($route, $parameters);
|
79
|
|
- $middlewares[] = $middleware;
|
|
93
|
+ $middlewares[] = [
|
|
94
|
+ 'middleware' => $middleware,
|
|
95
|
+ 'dbo' => $middlewareDbo
|
|
96
|
+ ];
|
80
|
97
|
}
|
|
98
|
+ unset($parameters["_middleware"]);
|
81
|
99
|
|
82
|
|
- $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
|
|
100
|
+ $reflect = new \ReflectionMethod($route->getController(), $route->getAction());
|
83
|
101
|
$params = $reflect->getParameters();
|
84
|
102
|
$docs = new LuMethodDocParser($reflect->getDocComment());
|
85
|
103
|
$docs->parse();
|
|
@@ -130,11 +148,14 @@ class LuRoute {
|
130
|
148
|
}
|
131
|
149
|
}
|
132
|
150
|
|
133
|
|
- $controller = $route->getControllerClass();
|
|
151
|
+ $controller = $route->getController();
|
134
|
152
|
$controllerInstance = new $controller();
|
135
|
|
- $result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
|
|
153
|
+ $result = call_user_func_array(array($controllerInstance, $route->getAction()), $args);
|
136
|
154
|
|
137
|
|
- foreach ($middlewares as $middleware) {
|
|
155
|
+ foreach ($middlewares as $middlewareArray) {
|
|
156
|
+ $middleware = $middlewareArray['middleware'];
|
|
157
|
+ $middlewareDbo = $middlewareArray['dbo'];
|
|
158
|
+ $parameters["_middleware"] = array_merge($middlewareDbo->getParameters(), $route->getParameters());
|
138
|
159
|
$result = $middleware->onAfter($route, $parameters, $result);
|
139
|
160
|
}
|
140
|
161
|
|
|
@@ -178,62 +199,69 @@ class LuRoute {
|
178
|
199
|
$json = json_decode($value, true);
|
179
|
200
|
$typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
|
180
|
201
|
}
|
181
|
|
- catch (LuDboDeserializeException $e)
|
182
|
|
- {
|
183
|
|
- throw $e;
|
184
|
|
- }
|
185
|
|
- catch (LuDboConstraintException $e)
|
186
|
|
- {
|
187
|
|
- throw $e;
|
188
|
|
- }
|
189
|
202
|
catch (\Exception $e)
|
190
|
203
|
{
|
|
204
|
+ if ($e instanceof LuDboDeserializeException || $e instanceof LuDboConstraintException) {
|
|
205
|
+ throw $e;
|
|
206
|
+ }
|
191
|
207
|
throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
|
192
|
208
|
}
|
193
|
209
|
}
|
194
|
210
|
return $typedValue;
|
195
|
211
|
}
|
196
|
|
-
|
197
|
|
- public function addRoute(string $httpMethod, string $url, string $controller, string $method, $permissions = array(), $middlewares = array())
|
198
|
|
- {
|
199
|
|
- if (!is_array($permissions)) {
|
200
|
|
- $permissions = array($permissions);
|
201
|
|
- }
|
202
|
|
- if (!is_array($middlewares)) {
|
203
|
|
- $middlewares = array($middlewares);
|
204
|
|
- }
|
205
|
|
-
|
|
212
|
+
|
|
213
|
+ public function addRoute2(LuRouteDbo $route) {
|
|
214
|
+ $controller = $route->getController();
|
206
|
215
|
if (strpos($controller, "\\") === false) {
|
207
|
216
|
$controller = "App\\Controller\\" . $controller . "Controller";
|
208
|
217
|
}
|
|
218
|
+ $route->setController($controller);
|
|
219
|
+ $this->routes[] = $route;
|
|
220
|
+ }
|
209
|
221
|
|
|
222
|
+ public function addRoute(string $httpMethod, string $url, string $controller, string $method, $parameters = array(), $middlewares = array())
|
|
223
|
+ {
|
|
224
|
+ $middlewaresDbo = [];
|
|
225
|
+ foreach ($middlewares as $middleware) {
|
|
226
|
+ if (is_string($middleware)) {
|
|
227
|
+ $middlewaresDbo[] = new LuMiddlewareDbo($middleware);
|
|
228
|
+ }
|
|
229
|
+ else if (is_array($middleware)) {
|
|
230
|
+ $middlewaresDbo[] = LuMiddlewareDbo::jsonDeserialize($middleware);
|
|
231
|
+ }
|
|
232
|
+ else {
|
|
233
|
+ $middlewaresDbo[] = $middleware;
|
|
234
|
+ }
|
|
235
|
+ }
|
|
236
|
+
|
210
|
237
|
$route = new LuRouteDbo();
|
211
|
238
|
$route->setUrl($url);
|
212
|
|
- $route->setControllerClass($controller);
|
213
|
|
- $route->setControllerMethod($method);
|
214
|
|
- $route->setMiddlewares($middlewares);
|
215
|
|
- $route->setPermissions($permissions);
|
|
239
|
+ $route->setController($controller);
|
|
240
|
+ $route->setAction($method);
|
|
241
|
+ $route->setMiddlewares($middlewaresDbo);
|
|
242
|
+ $route->setParameters($parameters);
|
216
|
243
|
$route->setMethod($httpMethod);
|
217
|
|
- $this->routes[] = $route;
|
|
244
|
+
|
|
245
|
+ $this->addRoute2($route);
|
218
|
246
|
}
|
219
|
247
|
|
220
|
|
- public function get(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
|
|
248
|
+ public function get(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
|
221
|
249
|
{
|
222
|
|
- $this->addRoute("GET", $url, $controller, $method, $permissions, $middleware);
|
|
250
|
+ $this->addRoute("GET", $url, $controller, $method, $parameters, $middleware);
|
223
|
251
|
}
|
224
|
252
|
|
225
|
|
- public function post(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
|
|
253
|
+ public function post(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
|
226
|
254
|
{
|
227
|
|
- $this->addRoute("POST", $url, $controller, $method, $permissions, $middleware);
|
|
255
|
+ $this->addRoute("POST", $url, $controller, $method, $parameters, $middleware);
|
228
|
256
|
}
|
229
|
257
|
|
230
|
|
- public function put(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
|
|
258
|
+ public function put(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
|
231
|
259
|
{
|
232
|
|
- $this->addRoute("PUT", $url, $controller, $method, $permissions, $middleware);
|
|
260
|
+ $this->addRoute("PUT", $url, $controller, $method, $parameters, $middleware);
|
233
|
261
|
}
|
234
|
262
|
|
235
|
|
- public function delete(string $url, string $controller, string $method, $permissions = array(), $middleware = array())
|
|
263
|
+ public function delete(string $url, string $controller, string $method, $parameters = array(), $middleware = array())
|
236
|
264
|
{
|
237
|
|
- $this->addRoute("DELETE", $url, $controller, $method, $permissions, $middleware);
|
|
265
|
+ $this->addRoute("DELETE", $url, $controller, $method, $parameters, $middleware);
|
238
|
266
|
}
|
239
|
267
|
}
|