diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index f38d50fb15..724c156f89 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -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');