From 8a7a2071b6d2521118faaefaac8c98f33c45d208 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 11 Sep 2020 22:59:11 +0300 Subject: [PATCH] refactoring and route duplicate check --- .../Espo/Core/ApplicationRunners/Api.php | 46 +++------------ .../Espo/Core/ApplicationRunners/Command.php | 8 +-- application/Espo/Core/Utils/Route.php | 59 ++++++++++++------- 3 files changed, 48 insertions(+), 65 deletions(-) diff --git a/application/Espo/Core/ApplicationRunners/Api.php b/application/Espo/Core/ApplicationRunners/Api.php index c24e57e4c3..f99a28ea41 100644 --- a/application/Espo/Core/ApplicationRunners/Api.php +++ b/application/Espo/Core/ApplicationRunners/Api.php @@ -60,22 +60,15 @@ use Throwable; */ class Api implements ApplicationRunner { - protected $allowedMethodList = [ - 'get', - 'post', - 'put', - 'patch', - 'delete', - 'options', - ]; - protected $injectableFactory; protected $applicationUser; + protected $routeUtil; - public function __construct(InjectableFactory $injectableFactory, ApplicationUser $applicationUser) + public function __construct(InjectableFactory $injectableFactory, ApplicationUser $applicationUser, Route $routeUtil) { $this->injectableFactory = $injectableFactory; $this->applicationUser = $applicationUser; + $this->routeUtil = $routeUtil; } public function run() @@ -86,14 +79,14 @@ class Api implements ApplicationRunner $slim->addRoutingMiddleware(); - $routeList = $this->getRouteList(); - $routeList = $this->filterRouteList($routeList); + $routeList = $this->routeUtil->getFullList(); foreach ($routeList as $item) { $this->addRoute($slim, $item); } $slim->addErrorMiddleware(false, true, true); + $slim->run(); } @@ -104,7 +97,8 @@ class Api implements ApplicationRunner $slim->$method( $route, - function (Psr7Request $request, Psr7Response $response, array $args) use ($item, $slim) { + function (Psr7Request $request, Psr7Response $response, array $args) use ($item, $slim) + { $routeParams = $this->getRouteParams($item, $args); $requestWrapped = new RequestWrapper($request, $slim->getBasePath(), $routeParams); @@ -206,30 +200,4 @@ class Api implements ApplicationRunner $responseWrapped->setStatus(500); } } - - protected function filterRouteList(array $routeList) : array - { - $routeList = array_filter($routeList, function ($item) { - $method = strtolower($item['method'] ?? ''); - $route = $item['route'] ?? null; - - if (!$route) { - return false; - } - - if (!in_array($method, $this->allowedMethodList)) { - $GLOBALS['log']->warning("Route: Method '{$method}' is not supported. Fix the route '{$route}'."); - - return false; - } - return true; - }); - - return $routeList; - } - - protected function getRouteList() : array - { - return $this->injectableFactory->create(Route::class)->getFullList(); - } } diff --git a/application/Espo/Core/ApplicationRunners/Command.php b/application/Espo/Core/ApplicationRunners/Command.php index ab9341d354..26bbf544e5 100644 --- a/application/Espo/Core/ApplicationRunners/Command.php +++ b/application/Espo/Core/ApplicationRunners/Command.php @@ -42,13 +42,11 @@ class Command implements ApplicationRunner use Cli; use SetupSystemUser; - protected $injectableFactory; + protected $commandManager; - public function __construct(InjectableFactory $injectableFactory) + public function __construct(ConsoleCommandManager $commandManager) { - $this->injectableFactory = $injectableFactory; - - $this->commandManager = $this->injectableFactory->create(ConsoleCommandManager::class); + $this->commandManager = $commandManager; } public function run() diff --git a/application/Espo/Core/Utils/Route.php b/application/Espo/Core/Utils/Route.php index 6f7e7fcb06..f94952af81 100644 --- a/application/Espo/Core/Utils/Route.php +++ b/application/Espo/Core/Utils/Route.php @@ -92,45 +92,47 @@ class Route */ protected function unify() : array { - $data = $this->getAddData([], $this->paths['customPath']); + $data = $this->addDataFromFile([], $this->paths['customPath']); $moduleData = []; foreach ($this->metadata->getModuleList() as $moduleName) { $modulePath = str_replace('{*}', $moduleName, $this->paths['modulePath']); - foreach ($this->getAddData([], $modulePath) as $row) { - $moduleData[$row['method'].$row['route']] = $row; + foreach ($this->addDataFromFile([], $modulePath) as $row) { + $key = $row['method'] . $row['route']; + + $moduleData[$key] = $row; } } $data = array_merge($data, array_values($moduleData)); - $data = $this->getAddData($data, $this->paths['corePath']); + $data = $this->addDataFromFile($data, $this->paths['corePath']); return $data; } - protected function getAddData(array $currentData, string $routeFile) : array + protected function addDataFromFile(array $currentData, string $routeFile) : array { - if (file_exists($routeFile)) { - $content = $this->fileManager->getContents($routeFile); - - $arrayContent = Json::getArrayData($content); - - if (empty($arrayContent)) { - $GLOBALS['log']->error('Route::unify() - Empty file or syntax error - ['.$routeFile.'].'); - - return $currentData; - } - - $currentData = $this->addToData($currentData, $arrayContent); + if (!file_exists($routeFile)) { + return $currentData; } - return $currentData; + $content = $this->fileManager->getContents($routeFile); + + $data = Json::getArrayData($content); + + if (empty($data)) { + $GLOBALS['log']->warning("Route: No data or syntax error in '{$routeFile}'."); + + return $currentData; + } + + return $this->appendRoutesToData($currentData, $data); } - protected function addToData(array $data, array $newData) : array + protected function appendRoutesToData(array $data, array $newData) : array { foreach ($newData as $route) { $route['route'] = $this->adjustPath($route['route']); @@ -141,6 +143,10 @@ class Route unset($route['conditions']); } + if (self::isRouteInList($route, $data)) { + continue; + } + $data[] = $route; } @@ -157,8 +163,8 @@ class Route // to fast route format $routePath = preg_replace('/\:([a-zA-Z0-9]+)/', '{${1}}', $routePath); - if (substr($routePath, 0, 1) != '/') { - return '/'.$routePath; + if (substr($routePath, 0, 1) !== '/') { + return '/' . $routePath; } return $routePath; @@ -181,4 +187,15 @@ class Route return ''; } + + static protected function isRouteInList(array $newRoute, array $routeList) : bool + { + foreach ($routeList as $route) { + if (Util::isEquals($route, $newRoute)) { + return true; + } + } + + return false; + } }