From 62da2245c1cef76cf2bbfe24dffb9ceee108ed2f Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 7 Jul 2020 18:38:14 +0300 Subject: [PATCH] dev --- api/v1/index.php | 4 +- api/v1/portal-access/index.php | 2 +- application/Espo/Core/Application.php | 69 +++++++++++++-------------- 3 files changed, 36 insertions(+), 39 deletions(-) diff --git a/api/v1/index.php b/api/v1/index.php index 136681dc0b..10f6f00dac 100644 --- a/api/v1/index.php +++ b/api/v1/index.php @@ -1,4 +1,4 @@ -run(); +$app->runApi(); diff --git a/api/v1/portal-access/index.php b/api/v1/portal-access/index.php index 3b2c9aead1..0cd1872434 100644 --- a/api/v1/portal-access/index.php +++ b/api/v1/portal-access/index.php @@ -36,4 +36,4 @@ if (!empty($_GET['portalId'])) { } $app = new \Espo\Core\Portal\Application($portalId); -$app->run(); +$app->runApi(); diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 724c156f89..adbb64edbf 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -95,33 +95,34 @@ class Application /** * Run REST API. */ - public function run() + public function runApi() { - $crudList = array_keys($this->getConfig()->get('crud')); - $slim = $this->getSlim(); - $slim->addRoutingMiddleware(); - foreach ($this->getRouteList() as $route) { - $method = strtolower($route['method']); + $crudList = array_keys($this->getConfig()->get('crud')); + + $routeList = $this->getInjectableFactory()->create(Route::class)->getAll(); + + foreach ($routeList as $item) { + $method = strtolower($item['method']); + $route = $item['route']; + if (!in_array($method, $crudList) && $method !== 'options') { - $GLOBALS['log']->error( - 'Route: Method ['.$method.'] does not exist. Please check your route ['.$route['route'].']' - ); + $GLOBALS['log']->error("Route: Method '{$method}' does not exist. Check the route '{$route}'."); continue; } - $currentRoute = $slim->$method( - $route['route'], - function (Request $request, Response $response, array $args) use ($route, $slim) { + $slim->$method( + $route, + function (Request $request, Response $response, array $args) use ($item, $slim) { $requestWrapped = new RequestWrapper($request, $slim->getBasePath()); $responseWrapped = new ResponseWrapper($response); try { $authRequired = true; - $conditions = $route['conditions'] ?? []; + $conditions = $item['conditions'] ?? []; if (($conditions['auth'] ?? true) === false) { $authRequired = false; } @@ -138,10 +139,10 @@ class Application $this->setupSystemUser(); } - $this->processRoute($route, $requestWrapped, $responseWrapped, $args); - } catch (\Throwable $exception) { + $this->processRoute($item, $requestWrapped, $responseWrapped, $args); + } catch (\Exception $exception) { (new ApiErrorOutput($requestWrapped))->process( - $responseWrapped, $exception, false, $route, $args + $responseWrapped, $exception, false, $item, $args ); } @@ -151,7 +152,6 @@ class Application } $slim->addErrorMiddleware(false, true, true); - $slim->run(); } @@ -219,7 +219,7 @@ class Application if ($contents) { $responseWrapped->writeBody($contents); } - } catch (\Throwable $e) { + } catch (\Exception $e) { (new ApiErrorOutput($requestWrapped))->process($responseWrapped, $e, true); } @@ -356,15 +356,6 @@ class Application return $this->container; } - protected function getSlim() - { - if (!$this->slim) { - $this->slim = SlimAppFactory::create(); - $this->slim->setBasePath(Route::detectBasePath()); - } - return $this->slim; - } - protected function getMetadata() { return $this->container->get('metadata'); @@ -375,22 +366,28 @@ class Application return $this->container->get('config'); } + protected function getInjectableFactory() + { + return $this->container->get('injectableFactory'); + } + + protected function getSlim() + { + if (!$this->slim) { + $this->slim = SlimAppFactory::create(); + $this->slim->setBasePath(Route::detectBasePath()); + } + return $this->slim; + } + protected function createAuth(RequestWrapper $request, bool $allowAnyAccess = false) : Auth { - $injectableFactory = $this->container->get('injectableFactory'); - - return $injectableFactory->createWith(Auth::class, [ + return $this->getInjectableFactory()->createWith(Auth::class, [ 'request' => $request, 'allowAnyAccess' => $allowAnyAccess, ]); } - protected function getRouteList() - { - $routes = new Route($this->getConfig(), $this->getMetadata(), $this->container->get('fileManager')); - return $routes->getAll(); - } - protected function processRoute(array $route, RequestWrapper $request, ResponseWrapper $response, array $args) { $response->setHeader('Content-Type', 'application/json');