diff --git a/application/Espo/Controllers/ApiIndex.php b/application/Espo/Controllers/ApiIndex.php new file mode 100644 index 0000000000..308cd49e2b --- /dev/null +++ b/application/Espo/Controllers/ApiIndex.php @@ -0,0 +1,38 @@ +getBody()->write($data); + } else { + if ($data) { + $response->setBody($data); + } } - $response->getBody()->write($data); - return $response; } diff --git a/application/Espo/Core/Api/Request.php b/application/Espo/Core/Api/Request.php new file mode 100644 index 0000000000..89e0bac6da --- /dev/null +++ b/application/Espo/Core/Api/Request.php @@ -0,0 +1,48 @@ +request = $request; } @@ -54,7 +56,7 @@ class RequestWrapper return $this->request->getHeaderLine($name); } - public function getMethod() + public function getMethod() : string { return $this->request->getMethod(); } diff --git a/application/Espo/Core/Api/Response.php b/application/Espo/Core/Api/Response.php new file mode 100644 index 0000000000..4ec684c90b --- /dev/null +++ b/application/Espo/Core/Api/Response.php @@ -0,0 +1,52 @@ +response = $response; } @@ -52,7 +54,7 @@ class ResponseWrapper $this->response = $this->response->withHeader($name, $value); } - public function getResponse() : Response + public function getResponse() : Psr7Response { return $this->response; } diff --git a/application/Espo/Core/Utils/Api/Slim.php b/application/Espo/Core/Api/Slim.php similarity index 100% rename from application/Espo/Core/Utils/Api/Slim.php rename to application/Espo/Core/Api/Slim.php diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index fb4090a751..cf45d94009 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -38,10 +38,10 @@ use Espo\Core\{ EntryPointManager, CronManager, Utils\Auth, - Utils\Api\Auth as ApiAuth, - Utils\Api\Output as ApiOutput, - Utils\Api\RequestWrapper, - Utils\Api\ResponseWrapper, + Api\Auth as ApiAuth, + Api\Output as ApiOutput, + Api\RequestWrapper, + Api\ResponseWrapper, Utils\Route, Utils\Autoload, Portal\Application as PortalApplication, @@ -420,7 +420,6 @@ class Application $conditions = $route['conditions'] ?? []; if (($conditions['auth'] ?? true) === false) { $authRequired = false; - } $auth = $this->createAuth($request); @@ -454,16 +453,26 @@ class Application $paramKeys = array_keys($route['params']); + $setKeyList = []; + foreach ($paramKeys as $key) { + $value = $route['params'][$key]; + $paramName = $key; - if ($key[0] === ':') { - $paramName = substr($key, 1); - $params[$paramName] = $args[$paramName]; + if ($value[0] === ':') { + $realKey = substr($value, 1); + $params[$paramName] = $args[$realKey]; + $setKeyList[] = $realKey; } else { - $params[$paramName] = $route['params'][$key]; + $params[$paramName] = $value; } } + foreach ($args as $key => $value) { + if (in_array($key, $setKeyList)) continue; + $params[$key] = $value; + } + $controllerName = $params['controller'] ?? $args['controller'] ?? null; $actionName = $params['action'] ?? $args['action'] ?? null; diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index 39ab0e6bd8..209c6e9ab0 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -37,6 +37,8 @@ use Espo\Core\{ Utils\ClassFinder, Utils\Json, Utils\Util, + Api\Request, + Api\Response, }; class ControllerManager @@ -54,8 +56,14 @@ class ControllerManager $this->controllersHash = (object) []; } - public function process(string $controllerName, string $actionName, $params, $data, $request, $response = null) - { + public function process( + string $controllerName, + string $actionName, + array $params, + $data, + Request $request, + Response $response + ) { $controller = $this->getController($controllerName); return $this->processRequest($controller, $controllerName, $actionName, $params, $data, $request, $response); } @@ -95,7 +103,13 @@ class ControllerManager } protected function processRequest( - object $controller, string $controllerName, string $actionName, $params, $data, $request, $response = null + object $controller, + string $controllerName, + string $actionName, + array $params, + $data, + Request $request, + Response $response ) { if ($data && stristr($request->getContentType(), 'application/json')) { $data = json_decode($data); diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index 66759c8c81..38722f5eba 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -2,7 +2,10 @@ { "route": "/", "method": "get", - "params": "\"EspoCRM REST API\"" + "params": { + "controller": "ApiIndex", + "action": "index" + } }, { "route": "/App/user",