This commit is contained in:
Yuri Kuznetsov
2020-07-02 17:12:58 +03:00
parent 784b0e8e40
commit 8a0d2c9b6a
11 changed files with 199 additions and 32 deletions
+38
View File
@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Controllers;
class ApiIndex {
public function getActionIndex()
{
return "EspoCRM REST API";
}
}
@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\Api;
namespace Espo\Core\Api;
use Espo\Core\Utils\Auth as AuthUtil;
@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\Api;
namespace Espo\Core\Api;
use Psr\Http\Message\{
ResponseInterface as Response,
@@ -62,15 +62,14 @@ class Output
public function render(Response $response, $data = null) : Response
{
if (is_array($data)) {
$dataArr = array_values($data);
$data = empty($dataArr[0]) ? false : $dataArr[0];
} else if ($data instanceof \StdClass) {
$data = json_encode($data);
if (is_string($data)) {
$response->getBody()->write($data);
} else {
if ($data) {
$response->setBody($data);
}
}
$response->getBody()->write($data);
return $response;
}
+48
View File
@@ -0,0 +1,48 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Api;
interface Request
{
/**
* Get a query parameter.
*/
public function get(string $name) : ?string;
/**
* Get a header value.
*/
public function getHeader(string $name) : ?string;
/**
* Get a request method.
*/
public function getMethod() : string;
}
@@ -27,17 +27,19 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\Api;
namespace Espo\Core\Api;
use Psr\Http\Message\{
ServerRequestInterface as Request,
ServerRequestInterface as Psr7Request,
};
class RequestWrapper
use Espo\Core\Api\Request as ApiRequest;
class RequestWrapper implements ApiRequest
{
protected $request;
public function __construct(Request $request)
public function __construct(Psr7Request $request)
{
$this->request = $request;
}
@@ -54,7 +56,7 @@ class RequestWrapper
return $this->request->getHeaderLine($name);
}
public function getMethod()
public function getMethod() : string
{
return $this->request->getMethod();
}
+52
View File
@@ -0,0 +1,52 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Api;
use Psr\Http\Message\{
ResponseInterface as Psr7Response,
};
interface Response
{
/**
* Set a status code.
*/
public function setStatus(int $code, ?string $reason);
/**
* Set a specific header.
*/
public function setHeader(string $name, string $value);
/**
* Get a result psr7 response.
*/
public function getResponse() : Psr7Response;
}
@@ -27,17 +27,19 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\Api;
namespace Espo\Core\Api;
use Psr\Http\Message\{
ResponseInterface as Response,
ResponseInterface as Psr7Response,
};
class ResponseWrapper
use Espo\Core\Api\Response as ApiResponse;
class ResponseWrapper implements ApiResponse
{
protected $response;
public function __construct(Response $response)
public function __construct(Psr7Response $response)
{
$this->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;
}
+18 -9
View File
@@ -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;
+17 -3
View File
@@ -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);
+4 -1
View File
@@ -2,7 +2,10 @@
{
"route": "/",
"method": "get",
"params": "\"EspoCRM REST API\""
"params": {
"controller": "ApiIndex",
"action": "index"
}
},
{
"route": "/App/user",