controller ob_clean and some cs fix

This commit is contained in:
Yuri Kuznetsov
2020-09-01 18:42:29 +03:00
parent 1129d1c145
commit a7ed16c72f
3 changed files with 55 additions and 21 deletions
+6 -1
View File
@@ -37,6 +37,8 @@ use Espo\Core\{
ControllerManager,
};
use StdClass;
/**
* Processes routes. Obtains a controller name, action, body from a request. Then passes them to controller manager.
*/
@@ -92,8 +94,11 @@ class RouteProcessor
if (!$actionName) {
$httpMethod = strtolower($requestMethod);
$crudList = $this->config->get('crud') ?? [];
$actionName = $crudList[$httpMethod] ?? null;
if (!$actionName) {
throw new Error("No action for method {$httpMethod}.");
}
@@ -119,7 +124,7 @@ class RouteProcessor
is_float($result) ||
is_array($result) ||
is_bool($result) ||
$result instanceof \StdClass
$result instanceof StdClass
) {
$responseContents = Json::encode($result);
}
@@ -135,8 +135,14 @@ class Api implements ApplicationRunner
}
$routeProcessor = $this->injectableFactory->create(RouteProcessor::class);
ob_start();
$routeProcessor->process($item['route'], $item['params'], $requestWrapped, $responseWrapped, $args);
} catch (Exception $exception) {
ob_clean();
}
catch (Exception $exception) {
(new ApiErrorOutput($requestWrapped))->process(
$responseWrapped, $exception, false, $item, $args
);
+42 -19
View File
@@ -29,17 +29,19 @@
namespace Espo\Core;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\{
InjectableFactory,
Utils\ClassFinder,
Utils\Util,
Api\Request,
Api\Response,
Exceptions\NotFound,
Exceptions\BadRequest,
};
use ReflectionClass;
use StdClass;
/**
* Creates controller instances and processes actions.
*/
@@ -89,26 +91,12 @@ class ControllerManager
);
}
$this->processContentTypeCheck($controller, $primaryActionMethodName, 1);
if (method_exists($controller, $beforeMethodName)) {
$controller->$beforeMethodName($params, $data, $request, $response);
}
$class = new \ReflectionClass($controller);
$method = $class->getMethod($primaryActionMethodName);
$args = $method->getParameters();
if (count($args) >= 2) {
if ($args[1]->hasType()) {
$dataClass = $args[1]->getClass();
if ($dataClass && strtolower($dataClass->getName()) === 'stdclass') {
if (!$data instanceof \StdClass) {
throw new BadRequest(
"{$controllerName} {$requestMethod} {$actionName}: Content-Type should be 'application/json'."
);
}
}
}
}
$result = $controller->$primaryActionMethodName($params, $data, $request, $response);
if (method_exists($controller, $afterMethodName)) {
@@ -118,6 +106,41 @@ class ControllerManager
return $result;
}
protected function processContentTypeCheck(object $controller, string $primaryActionMethodName, int $parameterIndex)
{
$class = new ReflectionClass($controller);
$method = $class->getMethod($primaryActionMethodName);
$args = $method->getParameters();
if (count($args) <= $parameterIndex) {
return;
}
$param = $args[$parameterIndex];
if (! $param->hasType()) {
return;
}
$dataClass = $param->getClass();
if (!$dataClass) {
return;
}
if (strtolower($dataClass->getName()) !== strtolower(StdClass::class)) {
return;
}
if (! $data instanceof StdClass) {
throw new BadRequest(
"{$controllerName} {$requestMethod} {$actionName}: Content-Type should be 'application/json'."
);
}
}
protected function getControllerClassName(string $name) : string
{
$className = $this->classFinder->find('Controllers', $name);