This commit is contained in:
Yuri Kuznetsov
2020-07-07 18:20:17 +03:00
parent 13e24501e5
commit 627fa8f89e
+56 -62
View File
@@ -97,8 +97,62 @@ class Application
*/
public function run()
{
$this->initRoutes();
$this->getSlim()->run();
$crudList = array_keys($this->getConfig()->get('crud'));
$slim = $this->getSlim();
$slim->addRoutingMiddleware();
foreach ($this->getRouteList() as $route) {
$method = strtolower($route['method']);
if (!in_array($method, $crudList) && $method !== 'options') {
$GLOBALS['log']->error(
'Route: Method ['.$method.'] does not exist. Please check your route ['.$route['route'].']'
);
continue;
}
$currentRoute = $slim->$method(
$route['route'],
function (Request $request, Response $response, array $args) use ($route, $slim) {
$requestWrapped = new RequestWrapper($request, $slim->getBasePath());
$responseWrapped = new ResponseWrapper($response);
try {
$authRequired = true;
$conditions = $route['conditions'] ?? [];
if (($conditions['auth'] ?? true) === false) {
$authRequired = false;
}
$auth = $this->createAuth($requestWrapped);
$apiAuth = new ApiAuth($auth, $authRequired);
$apiAuth->process($requestWrapped, $responseWrapped);
if (!$apiAuth->isResolved()) {
return $responseWrapped->getResponse();
}
if ($apiAuth->isResolvedUseNoAuth()) {
$this->setupSystemUser();
}
$this->processRoute($route, $requestWrapped, $responseWrapped, $args);
} catch (\Throwable $exception) {
(new ApiErrorOutput($requestWrapped))->process(
$responseWrapped, $exception, false, $route, $args
);
}
return $responseWrapped->getResponse();
}
);
}
$slim->addErrorMiddleware(false, true, true);
$slim->run();
}
/**
@@ -337,66 +391,6 @@ class Application
return $routes->getAll();
}
protected function initRoutes()
{
$crudList = array_keys($this->getConfig()->get('crud'));
$slim = $this->getSlim();
$slim->addRoutingMiddleware();
foreach ($this->getRouteList() as $route) {
$method = strtolower($route['method']);
if (!in_array($method, $crudList) && $method !== 'options') {
$GLOBALS['log']->error(
'Route: Method ['.$method.'] does not exist. Please check your route ['.$route['route'].']'
);
continue;
}
$currentRoute = $slim->
$method(
$route['route'],
function (Request $request, Response $response, array $args) use ($route, $slim) {
$requestWrapped = new RequestWrapper($request, $slim->getBasePath());
$responseWrapped = new ResponseWrapper($response);
try {
$authRequired = true;
$conditions = $route['conditions'] ?? [];
if (($conditions['auth'] ?? true) === false) {
$authRequired = false;
}
$auth = $this->createAuth($requestWrapped);
$apiAuth = new ApiAuth($auth, $authRequired);
$apiAuth->process($requestWrapped, $responseWrapped);
if (!$apiAuth->isResolved()) {
return $responseWrapped->getResponse();
}
if ($apiAuth->isResolvedUseNoAuth()) {
$this->setupSystemUser();
}
$this->processRoute($route, $requestWrapped, $responseWrapped, $args);
} catch (\Throwable $exception) {
(new ApiErrorOutput($requestWrapped))->process(
$responseWrapped, $exception, false, $route, $args
);
}
return $responseWrapped->getResponse();
}
);
}
$slim->addErrorMiddleware(false, true, true);
}
protected function processRoute(array $route, RequestWrapper $request, ResponseWrapper $response, array $args)
{
$response->setHeader('Content-Type', 'application/json');