refactoring and route duplicate check

This commit is contained in:
Yuri Kuznetsov
2020-09-11 22:59:11 +03:00
parent df471cf5a0
commit 8a7a2071b6
3 changed files with 48 additions and 65 deletions
@@ -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();
}
}
@@ -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()
+38 -21
View File
@@ -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;
}
}