cs fix, allow 201 http code

This commit is contained in:
yuri
2019-03-13 16:38:34 +02:00
parent 9c0a53728b
commit 45f1b4c427
8 changed files with 23 additions and 60 deletions
+6 -4
View File
@@ -99,7 +99,7 @@ class Application
exit;
}
public function runEntryPoint($entryPoint, $data = array(), $final = false)
public function runEntryPoint($entryPoint, $data = [], $final = false)
{
if (empty($entryPoint)) {
throw new \Error();
@@ -262,7 +262,7 @@ class Application
}
$routeOptions = call_user_func($route->getCallable());
$routeKeys = is_array($routeOptions) ? array_keys($routeOptions) : array();
$routeKeys = is_array($routeOptions) ? array_keys($routeOptions) : [];
if (!in_array('controller', $routeKeys, true)) {
return $container->get('output')->render($routeOptions);
@@ -328,7 +328,7 @@ class Application
continue;
}
$currentRoute = $this->getSlim()->$method($route['route'], function() use ($route) { //todo change "use" for php 5.4
$currentRoute = $this->getSlim()->$method($route['route'], function() use ($route) {
return $route['params'];
});
@@ -360,7 +360,9 @@ class Application
return $_GET['portalId'];
}
if (!empty($_COOKIE['auth-token'])) {
$token = $this->getContainer()->get('entityManager')->getRepository('AuthToken')->where(array('token' => $_COOKIE['auth-token']))->findOne();
$token =
$this->getContainer()->get('entityManager')
->getRepository('AuthToken')->where(['token' => $_COOKIE['auth-token']])->findOne();
if ($token && $token->get('portalId')) {
return $token->get('portalId');
@@ -25,14 +25,11 @@
*
* 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\Exceptions;
class BadRequest extends \Exception
{
protected $code = 400;
}
@@ -25,13 +25,11 @@
*
* 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\Exceptions;
class Conflict extends \Exception
{
protected $code = 409;
}
+1 -3
View File
@@ -25,7 +25,7 @@
*
* 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\Exceptions;
@@ -33,5 +33,3 @@ class Error extends InternalServerError
{
}
@@ -25,7 +25,7 @@
*
* 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\Exceptions;
@@ -34,5 +34,3 @@ class Forbidden extends \Exception
protected $code = 403;
}
@@ -25,12 +25,11 @@
*
* 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\Exceptions;
class InternalServerError extends \Exception
{
protected $code = 500;
}
@@ -25,14 +25,11 @@
*
* 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\Exceptions;
class NotFound extends \Exception
{
protected $code = 404;
}
+11 -37
View File
@@ -33,7 +33,7 @@ class Output
{
private $slim;
protected $errorDesc = [
protected $errorDescriptions = [
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
@@ -43,7 +43,7 @@ class Output
];
protected $allowedStatusCodeList = [
200, 400, 401, 403, 404, 409, 500
200, 201, 400, 401, 403, 404, 409, 500
];
protected $ignorePrintXStatusReasonExceptionClassNameList = [
@@ -60,11 +60,6 @@ class Output
return $this->slim;
}
/**
* Output the result
*
* @param mixed $data - JSON
*/
public function render($data = null)
{
if (is_array($data)) {
@@ -76,7 +71,7 @@ class Output
echo $data;
}
public function processError($message = 'Error', $code = 500, $isPrint = false, $exception = null)
public function processError(string $message = 'Error', int $code = 500, bool $toPrint = false, $exception = null)
{
$currentRoute = $this->getSlim()->router()->getCurrentRoute();
@@ -86,24 +81,16 @@ class Output
$GLOBALS['log']->error('API ['.$this->getSlim()->request()->getMethod().']:'.$currentRoute->getPattern().', Params:'.print_r($currentRoute->getParams(), true).', InputData: '.$inputData.' - '.$message);
}
$this->displayError($message, $code, $isPrint, $exception);
$this->displayError($message, $code, $toPrint, $exception);
}
/**
* Output the error and stop app execution
*
* @param string $text
* @param int $statusCode
*
* @return void
*/
public function displayError($text, $statusCode = 500, $isPrint = false, $exception = null)
public function displayError(string $text, int $statusCode = 500, bool $toPrint = false, $exception = null)
{
$GLOBALS['log']->error('Display Error: '.$text.', Code: '.$statusCode.' URL: '.$_SERVER['REQUEST_URI']);
ob_clean();
if (!empty( $this->slim)) {
if (!empty($this->slim)) {
$toPrintXStatusReason = true;
if ($exception && in_array(get_class($exception), $this->ignorePrintXStatusReasonExceptionClassNameList)) {
$toPrintXStatusReason = false;
@@ -118,8 +105,8 @@ class Output
$this->getSlim()->response()->headers->set('X-Status-Reason', $text);
}
if ($isPrint) {
$status = $this->getCodeDesc($statusCode);
if ($toPrint) {
$status = $this->getCodeDescription($statusCode);
$status = isset($status) ? $statusCode.' '.$status : 'HTTP '.$statusCode;
$this->getSlim()->printError($text, $status);
}
@@ -131,28 +118,15 @@ class Output
}
}
/**
* Get status code desription
*
* @param int $statusCode
* @return string | null
*/
protected function getCodeDesc($statusCode)
protected function getCodeDescription($statusCode)
{
if (isset($this->errorDesc[$statusCode])) {
return $this->errorDesc[$statusCode];
if (isset($this->errorDescriptions[$statusCode])) {
return $this->errorDescriptions[$statusCode];
}
return null;
}
/**
* Clear passwords for inputData
*
* @param string $inputData
*
* @return string
*/
protected function clearPasswords($inputData)
{
return preg_replace('/"(.*?password.*?)":".*?"/i', '"$1":"*****"', $inputData);