This commit is contained in:
Yuri Kuznetsov
2020-07-07 18:38:14 +03:00
parent 627fa8f89e
commit 62da2245c1
3 changed files with 36 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
<?php
<?php
/************************************************************************
* This file is part of EspoCRM.
*
@@ -30,4 +30,4 @@
require_once('../../bootstrap.php');
$app = new \Espo\Core\Application();
$app->run();
$app->runApi();

View File

@@ -36,4 +36,4 @@ if (!empty($_GET['portalId'])) {
}
$app = new \Espo\Core\Portal\Application($portalId);
$app->run();
$app->runApi();

View File

@@ -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');