|
@@ -3,7 +3,6 @@
|
3
|
3
|
namespace Luticate\Utils\Controller;
|
4
|
4
|
|
5
|
5
|
use FastRoute\Dispatcher;
|
6
|
|
-use Luticate\Utils\Business\LuBusiness;
|
7
|
6
|
use Luticate\Utils\Business\LuBusinessException;
|
8
|
7
|
use Luticate\Utils\Business\LuDocParser;
|
9
|
8
|
use Luticate\Utils\Dbo\LuDboConstraintException;
|
|
@@ -71,17 +70,17 @@ class LuRoute {
|
71
|
70
|
$routeCollector = new RouteCollector(new Std(), new DataGeneratorGroupCountBased());
|
72
|
71
|
|
73
|
72
|
foreach ($this->getRoutes() as $route) {
|
74
|
|
- $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function() use($route)
|
|
73
|
+ $routeCollector->addRoute($route->getMethod(), $route->getUrl(), function($parameters) use($route)
|
75
|
74
|
{
|
76
|
75
|
$router = static::getInstance();
|
77
|
|
- return $router->execute($route);
|
|
76
|
+ return $router->execute($route, $parameters);
|
78
|
77
|
});
|
79
|
78
|
}
|
80
|
79
|
|
81
|
80
|
$this->_dispatcher = new DispatcherGroupCountBased($routeCollector->getData());
|
82
|
81
|
}
|
83
|
82
|
|
84
|
|
- public function execute(LuRouteDbo $route)
|
|
83
|
+ public function execute(LuRouteDbo $route, $parameters)
|
85
|
84
|
{
|
86
|
85
|
$middlewares = [];
|
87
|
86
|
foreach ($route->getMiddlewares() as $middlewareName) {
|
|
@@ -89,7 +88,7 @@ class LuRoute {
|
89
|
88
|
* @var $middleware LuAbstractMiddleware
|
90
|
89
|
*/
|
91
|
90
|
$middleware = new $middlewareName();
|
92
|
|
- $middleware->onBefore($route);
|
|
91
|
+ $middleware->onBefore($route, $parameters);
|
93
|
92
|
$middlewares[] = $middleware;
|
94
|
93
|
}
|
95
|
94
|
|
|
@@ -104,8 +103,8 @@ class LuRoute {
|
104
|
103
|
try {
|
105
|
104
|
if ($param->isOptional()) {
|
106
|
105
|
$value = null;
|
107
|
|
- if (LuBusiness::hasParam([$param->getName()])) {
|
108
|
|
- $value = $this->getParam($param, LuBusiness::getParam($param->getName()));
|
|
106
|
+ if (array_key_exists($param->getName(), $parameters)) {
|
|
107
|
+ $value = $this->getParam($param, $parameters[$param->getName()]);
|
109
|
108
|
}
|
110
|
109
|
else {
|
111
|
110
|
$value = $param->getDefaultValue();
|
|
@@ -113,8 +112,14 @@ class LuRoute {
|
113
|
112
|
$args[$param->getName()] = $value;
|
114
|
113
|
}
|
115
|
114
|
else {
|
116
|
|
- $args[$param->getName()] = $this->getParam($param, LuBusiness::checkParam($param->getName()));
|
|
115
|
+ if (array_key_exists($param->getName(), $parameters)) {
|
|
116
|
+ $args[$param->getName()] = $this->getParam($param, $parameters[$param->getName()]);
|
|
117
|
+ }
|
|
118
|
+ else {
|
|
119
|
+ throw new LuBusinessException("Missing parameter '" . $param->getName() . "'", 400);
|
|
120
|
+ }
|
117
|
121
|
}
|
|
122
|
+
|
118
|
123
|
if (array_key_exists($param->getName(), $doc->getParams())) {
|
119
|
124
|
foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
|
120
|
125
|
call_user_func_array([$args[$param->getName()], $constraint->getMethod()],
|
|
@@ -125,7 +130,7 @@ class LuRoute {
|
125
|
130
|
catch (LuDboConstraintException $e)
|
126
|
131
|
{
|
127
|
132
|
$paramName = $param->getName();
|
128
|
|
- LuBusiness::badInput("Invalid value for '${paramName}': " . $e->getMessage());
|
|
133
|
+ throw new LuBusinessException("Invalid value for '${paramName}': " . $e->getMessage(), 400, $e);
|
129
|
134
|
}
|
130
|
135
|
}
|
131
|
136
|
|
|
@@ -134,13 +139,13 @@ class LuRoute {
|
134
|
139
|
$result = call_user_func_array(array($controllerInstance, $route->getControllerMethod()), $args);
|
135
|
140
|
|
136
|
141
|
foreach ($middlewares as $middleware) {
|
137
|
|
- $middleware->onAfter($route, $result);
|
|
142
|
+ $middleware->onAfter($route, $parameters, $result);
|
138
|
143
|
}
|
139
|
144
|
|
140
|
145
|
return $result;
|
141
|
146
|
}
|
142
|
147
|
|
143
|
|
- public function dispatch(string $httpMethod, string $url)
|
|
148
|
+ public function dispatch(string $httpMethod, string $url, $parameters)
|
144
|
149
|
{
|
145
|
150
|
$routeInfo = $this->_dispatcher->dispatch($httpMethod, $url);
|
146
|
151
|
|
|
@@ -152,8 +157,9 @@ class LuRoute {
|
152
|
157
|
}
|
153
|
158
|
else {
|
154
|
159
|
$handler = $routeInfo[1];
|
155
|
|
- $vars = $routeInfo[2];
|
156
|
|
- $handler($vars);
|
|
160
|
+ $urlVars = $routeInfo[2];
|
|
161
|
+ $parameters = array_merge($parameters, $urlVars);
|
|
162
|
+ $handler($parameters);
|
157
|
163
|
}
|
158
|
164
|
}
|
159
|
165
|
|
|
@@ -185,8 +191,7 @@ class LuRoute {
|
185
|
191
|
}
|
186
|
192
|
catch (\Exception $e)
|
187
|
193
|
{
|
188
|
|
- LuLog::log($e);
|
189
|
|
- LuBusiness::badInput("Unable to parse JSON value for '" . $param->getName() . "'");
|
|
194
|
+ throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
|
190
|
195
|
}
|
191
|
196
|
}
|
192
|
197
|
return $typedValue;
|